-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
170 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# frozen_string_literal: true | ||
|
||
module DiscourseAssign | ||
class CreateNotification | ||
def self.call(...) | ||
new(...).call | ||
end | ||
|
||
attr_reader :assignment, :user, :mark_as_read | ||
alias mark_as_read? mark_as_read | ||
|
||
delegate :topic, | ||
:post, | ||
:assigned_by_user, | ||
:assigned_to, | ||
:notifications, | ||
:created_at, | ||
:updated_at, | ||
:assigned_to_user?, | ||
:id, | ||
to: :assignment, | ||
private: true | ||
|
||
def initialize(assignment:, user:, mark_as_read:) | ||
@assignment = assignment | ||
@user = user | ||
@mark_as_read = mark_as_read | ||
end | ||
|
||
def call | ||
Assigner.publish_topic_tracking_state(topic, user.id) | ||
unless mark_as_read? | ||
PostAlerter.new(post).create_notification_alert( | ||
user: user, | ||
post: post, | ||
username: assigned_by_user.username, | ||
notification_type: Notification.types[:assigned], | ||
excerpt: | ||
I18n.t( | ||
excerpt_key, | ||
title: topic.title, | ||
group: assigned_to.name, | ||
locale: user.effective_locale, | ||
), | ||
) | ||
end | ||
notifications.create!( | ||
created_at: created_at, | ||
updated_at: updated_at, | ||
user: user, | ||
topic: topic, | ||
post_number: post.post_number, | ||
high_priority: true, | ||
read: mark_as_read?, | ||
data: { | ||
message: notification_message, | ||
display_username: display_username, | ||
topic_title: topic.title, | ||
assignment_id: id, | ||
}.to_json, | ||
) | ||
end | ||
|
||
private | ||
|
||
def excerpt_key | ||
return "discourse_assign.topic_assigned_excerpt" if assigned_to_user? | ||
"discourse_assign.topic_group_assigned_excerpt" | ||
end | ||
|
||
def notification_message | ||
return "discourse_assign.assign_notification" if assigned_to_user? | ||
"discourse_assign.assign_group_notification" | ||
end | ||
|
||
def display_username | ||
return assigned_by_user.username if assigned_to_user? | ||
assigned_to.name | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
RSpec.describe DiscourseAssign::CreateNotification do | ||
describe ".call" do | ||
subject(:create_notification) do | ||
described_class.call(assignment: assignment, user: user, mark_as_read: mark_as_read) | ||
end | ||
|
||
let(:assignment) { Fabricate(:topic_assignment, topic: post.topic) } | ||
let(:post) { Fabricate(:post) } | ||
let(:user) { assignment.assigned_to } | ||
let(:mark_as_read) { false } | ||
let(:alerter) { stub_everything("alerter").responds_like_instance_of(PostAlerter) } | ||
|
||
before { PostAlerter.stubs(:new).returns(alerter) } | ||
|
||
it "publishes topic tracking state" do | ||
Assigner.expects(:publish_topic_tracking_state).with(assignment.topic, user.id) | ||
create_notification | ||
end | ||
|
||
context "when `mark_as_read` is false" do | ||
let(:excerpt) do | ||
I18n.t( | ||
"discourse_assign.topic_assigned_excerpt", | ||
title: post.topic.title, | ||
group: user.name, | ||
locale: user.effective_locale, | ||
) | ||
end | ||
|
||
it "creates a notification alert" do | ||
alerter.expects(:create_notification_alert).with( | ||
user: user, | ||
post: post, | ||
username: assignment.assigned_by_user.username, | ||
notification_type: Notification.types[:assigned], | ||
excerpt: excerpt, | ||
) | ||
create_notification | ||
end | ||
end | ||
|
||
context "when `mark_as_read` is true" do | ||
let(:mark_as_read) { true } | ||
|
||
it "does not create a notification alert" do | ||
alerter.expects(:create_notification_alert).never | ||
create_notification | ||
end | ||
end | ||
|
||
it "creates a notification" do | ||
expect { create_notification }.to change { Notification.count }.by(1) | ||
expect(Notification.assigned.last).to have_attributes( | ||
created_at: assignment.created_at, | ||
updated_at: assignment.updated_at, | ||
user: user, | ||
topic: post.topic, | ||
post_number: post.post_number, | ||
high_priority: true, | ||
read: false, | ||
data_hash: { | ||
message: "discourse_assign.assign_notification", | ||
display_username: assignment.assigned_by_user.username, | ||
topic_title: post.topic.title, | ||
assignment_id: assignment.id, | ||
}, | ||
) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters