Skip to content

Commit 07cb550

Browse files
committed
fixed other cases of comments not accounting for pingable state
1 parent a8d4a69 commit 07cb550

7 files changed

Lines changed: 44 additions & 36 deletions

File tree

app/controllers/comments_controller.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,7 @@ def post_follow
273273

274274
def pingable
275275
thread = params[:id] == '-1' ? CommentThread.new(post_id: params[:post]) : CommentThread.find(params[:id])
276-
ids = helpers.get_pingable(thread)
277-
users = User.where(id: ids)
276+
users = User.where(id: thread.pingable)
278277
render json: users.to_h { |u| [u.username, u.id] }
279278
end
280279

@@ -377,7 +376,7 @@ def check_if_target_post_locked
377376
# @param content [String] content to extract pings from
378377
# @return [Array<Integer>] list of pinged user ids
379378
def check_for_pings(thread, content)
380-
pingable = helpers.get_pingable(thread)
379+
pingable = thread.pingable
381380
matches = content.scan(/@#(\d+)/)
382381
matches.flatten.select { |m| pingable.include?(m.to_i) }.map(&:to_i)
383382
end

app/helpers/comments_helper.rb

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -149,34 +149,6 @@ def rate_limited_error_msg(user, post)
149149
I18n.t('comments.errors.rate_limited', count: comments_count)
150150
end
151151

152-
##
153-
# Get a list of user IDs who should be pingable in a specified comment thread. This combines the post author, answer
154-
# authors, recent history event authors, recent comment authors on the post (in any thread), and all thread followers.
155-
# @param thread [CommentThread]
156-
# @return [Array<Integer>]
157-
def get_pingable(thread)
158-
post = thread.post
159-
160-
# post author +
161-
# answer authors +
162-
# last 500 history event users +
163-
# last 500 comment authors +
164-
# all thread followers
165-
query = <<~END_SQL
166-
SELECT posts.user_id FROM posts WHERE posts.id = #{post.id}
167-
UNION DISTINCT
168-
SELECT DISTINCT posts.user_id FROM posts WHERE posts.parent_id = #{post.id}
169-
UNION DISTINCT
170-
SELECT DISTINCT ph.user_id FROM post_histories ph WHERE ph.post_id = #{post.id}
171-
UNION DISTINCT
172-
SELECT DISTINCT comments.user_id FROM comments WHERE comments.post_id = #{post.id}
173-
UNION DISTINCT
174-
SELECT DISTINCT tf.user_id FROM thread_followers tf WHERE tf.comment_thread_id = #{thread.id || '-1'}
175-
END_SQL
176-
177-
ActiveRecord::Base.connection.execute(query).to_a.flatten
178-
end
179-
180152
##
181153
# Is the specified user comment rate limited for the specified post?
182154
# @param user [User] The user to check.

app/models/comment.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ def content_length
3333
end
3434
end
3535

36+
def pings
37+
pingable = thread.pingable
38+
matches = content.scan(/@#(\d+)/)
39+
matches.flatten.select { |m| pingable.include?(m.to_i) }.map(&:to_i)
40+
end
41+
3642
private
3743

3844
def create_follower

app/models/comment_thread.rb

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ class CommentThread < ApplicationRecord
1313

1414
after_create :create_follower
1515

16+
def self.post_followed?(post, user)
17+
ThreadFollower.where(post: post, user: user).any?
18+
end
19+
1620
def read_only?
1721
locked? || archived? || deleted?
1822
end
@@ -26,8 +30,27 @@ def can_access?(user)
2630
post.can_access?(user)
2731
end
2832

29-
def self.post_followed?(post, user)
30-
ThreadFollower.where(post: post, user: user).any?
33+
# Gets a list of user IDs who should be pingable in the thread.
34+
# @return [Array<Integer>]
35+
def pingable
36+
# post author +
37+
# answer authors +
38+
# last 500 history event users +
39+
# last 500 comment authors +
40+
# all thread followers
41+
query = <<~END_SQL
42+
SELECT posts.user_id FROM posts WHERE posts.id = #{post.id}
43+
UNION DISTINCT
44+
SELECT DISTINCT posts.user_id FROM posts WHERE posts.parent_id = #{post.id}
45+
UNION DISTINCT
46+
SELECT DISTINCT ph.user_id FROM post_histories ph WHERE ph.post_id = #{post.id}
47+
UNION DISTINCT
48+
SELECT DISTINCT comments.user_id FROM comments WHERE comments.post_id = #{post.id}
49+
UNION DISTINCT
50+
SELECT DISTINCT tf.user_id FROM thread_followers tf WHERE tf.comment_thread_id = #{id || '-1'}
51+
END_SQL
52+
53+
ActiveRecord::Base.connection.execute(query).to_a.flatten
3154
end
3255

3356
private

app/views/comment_threads/_expanded.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<% max_shown_comments = 5 %>
1212
<% comment_id ||= defined?(comment_id) ? comment_id : nil %>
13-
<% pingable = get_pingable(thread) %>
13+
<% pingable = thread.pingable %>
1414
<% shown_comments_count = [max_shown_comments, thread.reply_count].min %>
1515

1616
<%

app/views/flags/_flag.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<%= render 'posts/type_agnostic', show_type_tag: true, show_category_tag: true, post: flag.post %>
1717
</div>
1818
<% elsif flag.post_type == 'Comment' %>
19-
<%= render 'comments/comment', comment: flag.post, with_post_link: true %>
19+
<%= render 'comments/comment', comment: flag.post, with_post_link: true, pingable: flag.post.comment_thread.pingable %>
2020
<% end %>
2121
<div class="widget--body">
2222
<p>

app/views/moderator/recent_comments.html.erb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,16 @@
99
communities; consider using activity logs on user profiles instead.
1010
</p>
1111

12+
<%
13+
thread_pingables = @comments.map(&:comment_thread).to_set.to_h do |thread|
14+
[thread, thread.pingable]
15+
end
16+
%>
17+
1218
<% @comments.each do |comment| %>
13-
<%= render 'comments/comment', comment: comment, with_post_link: true %>
19+
<%= render 'comments/comment', comment: comment,
20+
pingable: thread_pingables[comment.comment_thread],
21+
with_post_link: true %>
1422
<% end %>
1523

1624
<div class="has-padding-top-4">

0 commit comments

Comments
 (0)