Skip to content

Commit 2580452

Browse files
committed
Merge branch 'develop' into MoshiKoi/1025/remove-special-case-notifying-author-of-threads
2 parents 5393736 + 89c9b18 commit 2580452

6 files changed

Lines changed: 61 additions & 20 deletions

File tree

app/controllers/comments_controller.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,7 @@ class CommentsController < ApplicationController
2121
def create_thread
2222
title = params[:title]
2323
unless title.present?
24-
title = if params[:body].length > 100
25-
"#{params[:body][0..100]}..."
26-
else
27-
params[:body]
28-
end
24+
title = helpers.generate_thread_title(params[:body])
2925
end
3026

3127
body = params[:body]

app/helpers/comments_helper.rb

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
# Helpers related to comments.
22
module CommentsHelper
3+
# Generates a comment thread title from its body
4+
# @param body [String] coment thread body
5+
# @return [String] generated title
6+
def generate_thread_title(body)
7+
body = strip_markdown(body)
8+
body = body.gsub(/^>.+?$/, '') # also remove leading blockquotes
9+
10+
if body.length > 100
11+
"#{body[0..100]}..."
12+
else
13+
body
14+
end
15+
end
16+
317
##
418
# Get a link to the specified comment, accounting for deleted comments.
519
# @param comment [Comment]
@@ -20,26 +34,47 @@ def comment_user_link(comment)
2034
user_link(comment.user, { host: comment.community.host })
2135
end
2236

37+
# Gets a list of pinged users for a given content
38+
# @param content [String] content to get pinged users from
39+
# @return [Hash{String => User}] list of pinged users
40+
def pinged_users(content)
41+
user_ids = content.scan(/@#(\d+)/).map { |g| g[0].to_i }
42+
User.where(id: user_ids).to_a.to_h { |u| [u.id, u] }
43+
end
44+
2345
##
24-
# Process a comment and convert ping-strings (i.e. @#1234) into links.
25-
# @param comment [String] The text of the comment to process.
26-
# @param pingable [Array<Integer>, nil] A list of user IDs that should be pingable in this comment. Any user IDs not
27-
# present in the list will be displayed as 'unpingable'.
46+
# Converts all ping-strings (i.e. @#1234) into links.
47+
# @param content [String] content to convert ping-strings for
48+
# @param pingable [Array<Integer>, nil] A list of user IDs. Any user ID not present will be displayed as 'unpingable'.
2849
# @return [ActiveSupport::SafeBuffer]
29-
def render_pings(comment, pingable: nil)
30-
comment.gsub(/@#\d+/) do |id|
31-
u = User.where(id: id[2..-1].to_i).first
32-
if u.nil?
33-
id
50+
def render_pings(content, pingable: nil)
51+
users = pinged_users(content)
52+
53+
content.gsub(/@#(\d+)/) do |ping|
54+
user = users[Regexp.last_match(1).to_i]
55+
if user.nil?
56+
ping
3457
else
35-
was_pung = pingable.present? && pingable.include?(u.id)
36-
classes = "ping #{u.id == current_user&.id ? 'me' : ''} #{was_pung ? '' : 'unpingable'}"
37-
user_link u, class: classes, dir: 'ltr',
58+
was_pung = pingable.present? && pingable.include?(user.id)
59+
classes = "ping #{user.same_as?(current_user) ? 'me' : ''} #{was_pung ? '' : 'unpingable'}"
60+
user_link user, class: classes, dir: 'ltr',
3861
title: was_pung ? '' : 'This user was not notified because they have not participated in this thread.'
3962
end
4063
end.html_safe
4164
end
4265

66+
# Converts all ping-strings (i.e. @#1234) in content into usernames for use in text-only contexts
67+
# @param content [String] content to convert ping-strings for
68+
# @return [String] processed content
69+
def render_pings_text(content)
70+
users = pinged_users(content)
71+
72+
content.gsub(/@#(\d+)/) do |ping|
73+
user = users[Regexp.last_match(1).to_i]
74+
user.nil? ? ping : "@#{rtl_safe_username(user)}"
75+
end
76+
end
77+
4378
##
4479
# Process comment text and convert helper links (like [help] and [flags]) into real links.
4580
# @param comment_text [String] The text of the comment to process.

app/models/concerns/identity.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module Identity
33

44
included do
55
# Is this record the same as a given other record?
6-
# @param other [ApplicationRecord] record to compare with
6+
# @param other [Class, nil] record to compare with
77
# @return [Boolean] check result
88
def same_as?(other)
99
instance_of?(other.class) && id == other.id

app/views/comment_threads/_collapsed.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@
2121
<% elsif thread.locked? %>
2222
<i class="fas fa-lock fa-fw" title="Locked thread" aria-label="Locked thread"></i>
2323
<% end %>
24-
<%= link_to thread.title, comment_thread_path(thread), class: 'js--comment-link', data: { thread: thread.id }, role: 'button' %>
24+
<%= link_to render_pings_text(thread.title), comment_thread_path(thread), class: 'js--comment-link', data: { thread: thread.id }, role: 'button' %>
2525
(<%= pluralize(thread.reply_count, 'comment') %>)
2626
</div>

app/views/comment_threads/_expanded.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
<% elsif thread.locked? %>
5252
<i class="fas fa-lock fa-fw" title="Locked. This thread has been locked and cannot be modified."></i>
5353
<% end %>
54-
<span class="js-thread-title"><%= thread.title %></span>
54+
<span class="js-thread-title"><%= render_pings_text(thread.title) %></span>
5555
</div>
5656
<% skipped_deleted = 0 %>
5757
<% comments = thread.comments %>

test/models/concerns/identity_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,14 @@ def initialize(id)
3636

3737
assert_not first.same_as?(second)
3838
end
39+
40+
test 'same_as? should not fail if the compared model is nil' do
41+
first = @klass1.new(42)
42+
43+
assert_nothing_raised do
44+
first.same_as?(nil)
45+
end
46+
47+
assert_not first.same_as?(nil)
48+
end
3949
end

0 commit comments

Comments
 (0)