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

Commit 1a5091e

Browse files
authored
FIX: pending_assigns_reminder reminder leaking total assigned topics count (#617)
Before this commit, the reminder was leaking the total assigned topics count in the title. In the post raw it was ensured that it was not being leaked but the title was still leaking the count. Also add specs and updated existing specs to ensure that the count is not leaked in the title.
1 parent e4b9ed6 commit 1a5091e

File tree

2 files changed

+48
-11
lines changed

2 files changed

+48
-11
lines changed

lib/pending_assigns_reminder.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,23 @@ def delete_previous_reminders(user)
5555
posts.find_each { |post| PostDestroyer.new(Discourse.system_user, post).destroy }
5656
end
5757

58+
def visible_topics(user)
59+
Topic.listable_topics.secured(Guardian.new(user)).or(Topic.private_messages_for_user(user))
60+
end
61+
5862
def assigned_count_for(user)
5963
assignments =
60-
Assignment.joins_with_topics.where(
61-
assigned_to_id: user.id,
62-
assigned_to_type: "User",
63-
active: true,
64-
)
64+
Assignment
65+
.joins_with_topics
66+
.where(assigned_to_id: user.id, assigned_to_type: "User", active: true)
67+
.merge(visible_topics(user))
6568
assignments =
6669
DiscoursePluginRegistry.apply_modifier(:assigned_count_for_user_query, assignments, user)
6770
assignments.count
6871
end
6972

7073
def assigned_topics(user, order:)
71-
secure =
72-
Topic.listable_topics.secured(Guardian.new(user)).or(Topic.private_messages_for_user(user))
74+
secure = visible_topics(user)
7375

7476
topics =
7577
Topic

spec/lib/pending_assigns_reminder_spec.rb

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def assert_reminder_not_created
6060

6161
expect(topic.topic_allowed_users.pluck(:user_id)).to contain_exactly(system.id, user.id)
6262

63-
expect(topic.title).to eq(I18n.t("pending_assigns_reminder.title", pending_assignments: 3))
63+
expect(topic.title).to eq(I18n.t("pending_assigns_reminder.title", pending_assignments: 2))
6464

6565
expect(post.raw).to include(@post1.topic.fancy_title)
6666
expect(post.raw).to include(@post2.topic.fancy_title)
@@ -119,6 +119,41 @@ def assert_reminder_not_created
119119
expect(reminders_count).to eq(2)
120120
end
121121

122+
it "doesn't leak assigned topics that were moved to PM" do
123+
# we already add a fail to assign when the assigned user cannot view the pm
124+
# so we don't need to test that here
125+
# but if we move a topic to a PM that the user can't see, we should not
126+
# include it in the reminder
127+
post = Fabricate(:post)
128+
Assigner.new(post.topic, user).assign(user)
129+
post.topic.update(archetype: Archetype.private_message, category: nil)
130+
reminder.remind(user)
131+
132+
post = Post.last
133+
topic = post.topic
134+
expect(topic.title).to eq(I18n.t("pending_assigns_reminder.title", pending_assignments: 2))
135+
expect(post.raw).to include(@post1.topic.fancy_title)
136+
expect(post.raw).to include(@post2.topic.fancy_title)
137+
138+
expect(post.raw).to_not include(post.topic.fancy_title)
139+
end
140+
141+
it "reminds about PMs" do
142+
pm = Fabricate(:private_message_topic, user: user)
143+
Fabricate(:post, topic: pm)
144+
145+
Assigner.new(pm, user).assign(user)
146+
reminder.remind(user)
147+
148+
post = Post.last
149+
topic = post.topic
150+
151+
expect(topic.title).to eq(I18n.t("pending_assigns_reminder.title", pending_assignments: 3))
152+
expect(post.raw).to include(@post1.topic.fancy_title)
153+
expect(post.raw).to include(@post2.topic.fancy_title)
154+
expect(post.raw).to include(pm.fancy_title)
155+
end
156+
122157
it "closed topics aren't included as active assigns" do
123158
SiteSetting.unassign_on_close = true
124159

@@ -130,7 +165,7 @@ def assert_reminder_not_created
130165
post = Post.last
131166
topic = post.topic
132167

133-
expect(topic.title).to eq(I18n.t("pending_assigns_reminder.title", pending_assignments: 4))
168+
expect(topic.title).to eq(I18n.t("pending_assigns_reminder.title", pending_assignments: 3))
134169

135170
@post5.topic.update_status("closed", true, Discourse.system_user)
136171
expect(@post5.topic.closed).to eq(true)
@@ -140,7 +175,7 @@ def assert_reminder_not_created
140175
post = Post.last
141176
topic = post.topic
142177

143-
expect(topic.title).to eq(I18n.t("pending_assigns_reminder.title", pending_assignments: 3))
178+
expect(topic.title).to eq(I18n.t("pending_assigns_reminder.title", pending_assignments: 2))
144179
end
145180

146181
context "with assigns_reminder_assigned_topics_query modifier" do
@@ -162,7 +197,7 @@ def assert_reminder_not_created
162197
context "with assigned_count_for_user_query modifier" do
163198
let(:modifier_block) { Proc.new { |query, user| query.where.not(assigned_to_id: user.id) } }
164199
it "updates the query correctly" do
165-
expect(reminder.send(:assigned_count_for, user)).to eq(3)
200+
expect(reminder.send(:assigned_count_for, user)).to eq(2)
166201

167202
plugin_instance = Plugin::Instance.new
168203
plugin_instance.register_modifier(:assigned_count_for_user_query, &modifier_block)

0 commit comments

Comments
 (0)