11# Helpers related to comments.
22module 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.
0 commit comments