Skip to content
This repository was archived by the owner on Jul 14, 2025. It is now read-only.

Commit 6101ecd

Browse files
FEATURE: Add should_notify option to Assigner#assign (#604)
* FEATURE: Add `should_notify` option to `Assigner#assign` This option let's you control whether the added user within a group assignment should be notified or not. * DEV: stree assign_controller file * Update app/controllers/discourse_assign/assign_controller.rb Co-authored-by: David Taylor <[email protected]> --------- Co-authored-by: David Taylor <[email protected]>
1 parent 964e44a commit 6101ecd

File tree

4 files changed

+115
-10
lines changed

4 files changed

+115
-10
lines changed

app/controllers/discourse_assign/assign_controller.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ def assign
4343
group_name = params.permit(:group_name)["group_name"]
4444
note = params.permit(:note)["note"].presence
4545
status = params.permit(:status)["status"].presence
46+
should_notify = params.permit(:should_notify)["should_notify"]
47+
should_notify = (should_notify.present? ? should_notify.to_s == "true" : true)
4648

4749
assign_to =
4850
(
@@ -58,7 +60,13 @@ def assign
5860
target = target_type.constantize.where(id: target_id).first
5961
raise Discourse::NotFound unless target
6062

61-
assign = Assigner.new(target, current_user).assign(assign_to, note: note, status: status)
63+
assign =
64+
Assigner.new(target, current_user).assign(
65+
assign_to,
66+
note: note,
67+
status: status,
68+
should_notify: should_notify,
69+
)
6270

6371
if assign[:success]
6472
render json: success_json

lib/assigner.rb

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ def forbidden_reasons(assign_to:, type:, note:, status:, allow_self_reassign:)
221221
end
222222
end
223223

224-
def update_details(assign_to, note, status, skip_small_action_post: false)
224+
def update_details(assign_to, note, status, skip_small_action_post: false, should_notify: true)
225225
case
226226
when note.present? && status.present? && @target.assignment.note != note &&
227227
@target.assignment.status != status
@@ -240,7 +240,7 @@ def update_details(assign_to, note, status, skip_small_action_post: false)
240240
end
241241

242242
@target.assignment.update!(note: note, status: status)
243-
queue_notification(@target.assignment)
243+
queue_notification(@target.assignment) if should_notify
244244
publish_assignment(@target.assignment, assign_to, note, status)
245245

246246
# email is skipped, for now
@@ -258,12 +258,17 @@ def assign(
258258
note: nil,
259259
skip_small_action_post: false,
260260
status: nil,
261-
allow_self_reassign: false
261+
allow_self_reassign: false,
262+
should_notify: true
262263
)
263264
assigned_to_type = assign_to.is_a?(User) ? "User" : "Group"
264265

265266
if topic.private_message? && SiteSetting.invite_on_assign
266-
assigned_to_type == "Group" ? invite_group(assign_to) : invite_user(assign_to)
267+
if assigned_to_type == "Group"
268+
invite_group(assign_to, should_notify)
269+
else
270+
invite_user(assign_to)
271+
end
267272
end
268273

269274
forbidden_reason =
@@ -277,7 +282,15 @@ def assign(
277282
return { success: false, reason: forbidden_reason } if forbidden_reason
278283

279284
if no_assignee_change?(assign_to) && details_change?(note, status)
280-
return update_details(assign_to, note, status, skip_small_action_post: skip_small_action_post)
285+
return(
286+
update_details(
287+
assign_to,
288+
note,
289+
status,
290+
skip_small_action_post: skip_small_action_post,
291+
should_notify: should_notify,
292+
)
293+
)
281294
end
282295

283296
action_code = {}
@@ -308,7 +321,7 @@ def assign(
308321
)
309322

310323
first_post.publish_change_to_clients!(:revised, reload_topic: true)
311-
queue_notification(assignment)
324+
queue_notification(assignment) if should_notify
312325
publish_assignment(assignment, assign_to, note, status)
313326

314327
if assignment.assigned_to_user?
@@ -452,7 +465,7 @@ def invite_user(user)
452465
topic.invite(@assigned_by, user.username)
453466
end
454467

455-
def invite_group(group)
468+
def invite_group(group, should_notify)
456469
return if topic.topic_allowed_groups.exists?(group_id: group.id)
457470
if topic
458471
.all_allowed_users
@@ -463,7 +476,7 @@ def invite_group(group)
463476
end
464477

465478
guardian.ensure_can_invite_group_to_private_message!(group, topic)
466-
topic.invite_group(@assigned_by, group)
479+
topic.invite_group(@assigned_by, group, should_notify: should_notify)
467480
end
468481

469482
def guardian

spec/lib/assigner_spec.rb

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,11 +465,17 @@ def assigned_to?(assignee)
465465
it "queues notification" do
466466
assigner.assign(moderator)
467467

468+
expect(job_enqueued?(job: :assign_notification)).to eq(true)
468469
expect_enqueued_with(job: :assign_notification) do
469470
assigner.assign(moderator, status: "Done")
470471
end
471472
end
472473

474+
it "does not queue notification if should_notify is set to false" do
475+
assigner.assign(moderator, status: "Done", should_notify: false)
476+
expect(job_enqueued?(job: :assign_notification)).to eq(false)
477+
end
478+
473479
it "publishes topic assignment with note" do
474480
assigner.assign(moderator)
475481

@@ -756,16 +762,38 @@ def assigned_to?(assignee)
756762
expect(topic.allowed_users).not_to include(user)
757763
end
758764

759-
it "invites group to the PM" do
765+
it "invites group to the PM and notifies users" do
760766
group =
761767
Fabricate(
762768
:group,
763769
assignable_level: Group::ALIAS_LEVELS[:only_admins],
764770
messageable_level: Group::ALIAS_LEVELS[:only_admins],
765771
)
766772
group.add(Fabricate(:user))
773+
774+
Notification.delete_all
775+
Jobs.run_immediately!
776+
767777
assigner.assign(group)
768778
expect(topic.allowed_groups).to include(group)
779+
expect(Notification.count).to be > 0
780+
end
781+
782+
it "invites group to the PM and does not notifies users if should_notify is false" do
783+
group =
784+
Fabricate(
785+
:group,
786+
assignable_level: Group::ALIAS_LEVELS[:only_admins],
787+
messageable_level: Group::ALIAS_LEVELS[:only_admins],
788+
)
789+
group.add(Fabricate(:user))
790+
791+
Notification.delete_all
792+
Jobs.run_immediately!
793+
794+
assigner.assign(group, should_notify: false)
795+
expect(topic.allowed_groups).to include(group)
796+
expect(Notification.count).to eq(0)
769797
end
770798

771799
it "doesn't invite group if all members have access to the PM already" do

spec/requests/assign_controller_spec.rb

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,62 @@ def assign_user_to_post
259259
)
260260
end
261261

262+
it "notifies the assignee when the topic is assigned to a group" do
263+
admins = Group[:admins]
264+
admins.messageable_level = Group::ALIAS_LEVELS[:everyone]
265+
admins.save!
266+
267+
SiteSetting.invite_on_assign = true
268+
pm = Fabricate(:private_message_post, user: admin).topic
269+
270+
another_user = Fabricate(:user)
271+
admins.add(another_user)
272+
admins
273+
.group_users
274+
.find_by(user_id: another_user.id)
275+
.update!(notification_level: NotificationLevels.all[:watching])
276+
277+
Notification.delete_all
278+
Jobs.run_immediately!
279+
280+
put "/assign/assign.json",
281+
params: {
282+
target_id: pm.id,
283+
target_type: "Topic",
284+
group_name: admins.name,
285+
}
286+
287+
expect(Notification.count).to be > 0
288+
end
289+
290+
it "does not notify the assignee when the topic is assigned to a group if should_notify option is set to false" do
291+
admins = Group[:admins]
292+
admins.messageable_level = Group::ALIAS_LEVELS[:everyone]
293+
admins.save!
294+
295+
SiteSetting.invite_on_assign = true
296+
pm = Fabricate(:private_message_post, user: admin).topic
297+
298+
another_user = Fabricate(:user)
299+
admins.add(another_user)
300+
admins
301+
.group_users
302+
.find_by(user_id: another_user.id)
303+
.update!(notification_level: NotificationLevels.all[:watching])
304+
305+
Notification.delete_all
306+
Jobs.run_immediately!
307+
308+
put "/assign/assign.json",
309+
params: {
310+
target_id: pm.id,
311+
target_type: "Topic",
312+
group_name: admins.name,
313+
should_notify: false,
314+
}
315+
expect(Notification.count).to eq(0)
316+
end
317+
262318
it "fails with a specific error message if the topic is not a PM and the assignee can not see it" do
263319
topic = Fabricate(:topic, category: Fabricate(:private_category, group: Fabricate(:group)))
264320
another_user = Fabricate(:user)

0 commit comments

Comments
 (0)