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

Commit 0f4a1fc

Browse files
authored
FIX: Not send notifications when it should never notify assignment (#620)
* FIX: Not send notifications when it should never notify assignment - Added silenced_assignments table and migration to store silenced assignments - Added tests for silenced assignments * DEV: Add annotation to model * DEV: add empty line after early return * DEV: lint file
1 parent 215e1e3 commit 0f4a1fc

File tree

7 files changed

+52
-0
lines changed

7 files changed

+52
-0
lines changed

app/jobs/regular/assign_notification.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ module Jobs
44
class AssignNotification < ::Jobs::Base
55
def execute(args)
66
raise Discourse::InvalidParameters.new(:assignment_id) if args[:assignment_id].nil?
7+
return if SilencedAssignment.exists?(assignment_id: args[:assignment_id])
8+
79
Assignment.find(args[:assignment_id]).create_missing_notifications!
810
end
911
end

app/models/silenced_assignment.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# frozen_string_literal: true
2+
3+
class SilencedAssignment < ActiveRecord::Base
4+
belongs_to :assignment
5+
end
6+
7+
# == Schema Information
8+
#
9+
# Table name: silenced_assignments
10+
#
11+
# id :bigint not null, primary key
12+
# assignment_id :bigint not null
13+
# created_at :datetime not null
14+
# updated_at :datetime not null
15+
#
16+
# Indexes
17+
#
18+
# index_silenced_assignments_on_assignment_id (assignment_id) UNIQUE
19+
#
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
class CreateSilencedAssignments < ActiveRecord::Migration[7.2]
3+
def up
4+
create_table :silenced_assignments do |t|
5+
t.bigint :assignment_id, null: false
6+
t.timestamps
7+
end
8+
add_index :silenced_assignments, :assignment_id, unique: true
9+
end
10+
11+
def down
12+
drop_table :silenced_assignments
13+
end
14+
end

lib/assigner.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,10 @@ def assign(
322322

323323
first_post.publish_change_to_clients!(:revised, reload_topic: true)
324324
queue_notification(assignment) if should_notify
325+
326+
# This assignment should never be notified
327+
SilencedAssignment.create!(assignment_id: assignment.id) if !should_notify
328+
325329
publish_assignment(assignment, assign_to, note, status)
326330

327331
if assignment.assigned_to_user?

spec/jobs/regular/assign_notification_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@
2626
assignment.expects(:create_missing_notifications!)
2727
execute_job
2828
end
29+
30+
it "does not create notifications if assignment is silenced" do
31+
SilencedAssignment.stubs(:exists?).with(assignment_id: assignment_id).returns(true)
32+
assignment.expects(:create_missing_notifications!).never
33+
execute_job
34+
end
2935
end
3036
end
3137
end

spec/lib/assigner_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,10 @@ def assigned_to?(assignee)
794794
assigner.assign(group, should_notify: false)
795795
expect(topic.allowed_groups).to include(group)
796796
expect(Notification.count).to eq(0)
797+
expect(SilencedAssignment.count).to eq(1)
798+
799+
group.add(Fabricate(:user))
800+
expect(Notification.count).to eq(0) # no one is ever notified about this assignment
797801
end
798802

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

spec/requests/assign_controller_spec.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ def assign_user_to_post
262262
it "notifies the assignee when the topic is assigned to a group" do
263263
admins = Group[:admins]
264264
admins.messageable_level = Group::ALIAS_LEVELS[:everyone]
265+
admins.assignable_level = Group::ALIAS_LEVELS[:everyone]
265266
admins.save!
266267

267268
SiteSetting.invite_on_assign = true
@@ -290,6 +291,7 @@ def assign_user_to_post
290291
it "does not notify the assignee when the topic is assigned to a group if should_notify option is set to false" do
291292
admins = Group[:admins]
292293
admins.messageable_level = Group::ALIAS_LEVELS[:everyone]
294+
admins.assignable_level = Group::ALIAS_LEVELS[:everyone]
293295
admins.save!
294296

295297
SiteSetting.invite_on_assign = true
@@ -313,6 +315,7 @@ def assign_user_to_post
313315
should_notify: false,
314316
}
315317
expect(Notification.count).to eq(0)
318+
expect(SilencedAssignment.count).to eq(1)
316319
end
317320

318321
it "fails with a specific error message if the topic is not a PM and the assignee can not see it" do

0 commit comments

Comments
 (0)