From e7571b3b72b72963c5d066818e179c288c52212c Mon Sep 17 00:00:00 2001 From: Brian Pearce Date: Wed, 23 Oct 2019 17:29:54 +0200 Subject: [PATCH 01/22] Close mentions when scrolling --- app/assets/javascripts/snowcrash/modules/mentions.js.coffee | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/assets/javascripts/snowcrash/modules/mentions.js.coffee b/app/assets/javascripts/snowcrash/modules/mentions.js.coffee index 12e4b3716..4236b2404 100644 --- a/app/assets/javascripts/snowcrash/modules/mentions.js.coffee +++ b/app/assets/javascripts/snowcrash/modules/mentions.js.coffee @@ -12,4 +12,7 @@ class @Mentions values: JSON.parse($('meta[name=mentionable-users]').attr('content')) ) + $('.main-content').scroll ()=> + @tribute.hideMenu() + @tribute.attach(elements); From 90808fe19601a3cdc0e2d24bd3ec6aa258e54ed5 Mon Sep 17 00:00:00 2001 From: Brian Pearce Date: Wed, 23 Oct 2019 23:26:13 +0200 Subject: [PATCH 02/22] Initialize tribute everytime --- .../snowcrash/modules/mentions.js.coffee | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/app/assets/javascripts/snowcrash/modules/mentions.js.coffee b/app/assets/javascripts/snowcrash/modules/mentions.js.coffee index 4236b2404..84b6fd60f 100644 --- a/app/assets/javascripts/snowcrash/modules/mentions.js.coffee +++ b/app/assets/javascripts/snowcrash/modules/mentions.js.coffee @@ -1,18 +1,17 @@ class @Mentions @init: (elements) -> # initialize mentions (https://github.com/zurb/tribute) - unless @tribute? - @tribute = new Tribute( - allowSpaces: -> - false - menuItemTemplate: (item) -> - ' ' + item.string - noMatchTemplate: -> - '' - values: JSON.parse($('meta[name=mentionable-users]').attr('content')) - ) + tribute = new Tribute( + allowSpaces: -> + false + menuItemTemplate: (item) -> + ' ' + item.string + noMatchTemplate: -> + '' + values: JSON.parse($('meta[name=mentionable-users]').attr('content')) + ) - $('.main-content').scroll ()=> - @tribute.hideMenu() + $('.main-content').scroll ()=> + tribute.hideMenu() - @tribute.attach(elements); + tribute.attach(elements) From 2f9bbd85c10ccd22a30474e37d0f2c3c2d5d4835 Mon Sep 17 00:00:00 2001 From: Brian Pearce Date: Wed, 23 Oct 2019 23:59:02 +0200 Subject: [PATCH 03/22] Convert coffee scritp to javascript --- .../javascripts/snowcrash/modules/mentions.js | 18 ++++++++++++++++++ .../snowcrash/modules/mentions.js.coffee | 17 ----------------- 2 files changed, 18 insertions(+), 17 deletions(-) create mode 100644 app/assets/javascripts/snowcrash/modules/mentions.js delete mode 100644 app/assets/javascripts/snowcrash/modules/mentions.js.coffee diff --git a/app/assets/javascripts/snowcrash/modules/mentions.js b/app/assets/javascripts/snowcrash/modules/mentions.js new file mode 100644 index 000000000..b088fbda5 --- /dev/null +++ b/app/assets/javascripts/snowcrash/modules/mentions.js @@ -0,0 +1,18 @@ +window.Mentions = { + init: function(elements) { + tribute = new Tribute({ + allowSpaces: function() { return false }, + menuItemTemplate: function(item) { + return ' ' + item.string + }, + noMatchTemplate: function() { return '' }, + values: JSON.parse($('meta[name=mentionable-users]').attr('content')) + }); + + $('.main-content').scroll(function(){ + tribute.hideMenu(); + }); + + tribute.attach(elements); + } +} diff --git a/app/assets/javascripts/snowcrash/modules/mentions.js.coffee b/app/assets/javascripts/snowcrash/modules/mentions.js.coffee deleted file mode 100644 index 84b6fd60f..000000000 --- a/app/assets/javascripts/snowcrash/modules/mentions.js.coffee +++ /dev/null @@ -1,17 +0,0 @@ -class @Mentions - @init: (elements) -> - # initialize mentions (https://github.com/zurb/tribute) - tribute = new Tribute( - allowSpaces: -> - false - menuItemTemplate: (item) -> - ' ' + item.string - noMatchTemplate: -> - '' - values: JSON.parse($('meta[name=mentionable-users]').attr('content')) - ) - - $('.main-content').scroll ()=> - tribute.hideMenu() - - tribute.attach(elements) From f99802c346d9f3e5d39248d6f68295fa01590df4 Mon Sep 17 00:00:00 2001 From: Brian Pearce Date: Thu, 24 Oct 2019 10:07:15 +0200 Subject: [PATCH 04/22] Wrap the code in a closure to prevent leakage --- .../javascripts/snowcrash/modules/mentions.js | 32 ++++++++++--------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/app/assets/javascripts/snowcrash/modules/mentions.js b/app/assets/javascripts/snowcrash/modules/mentions.js index b088fbda5..83bcaf518 100644 --- a/app/assets/javascripts/snowcrash/modules/mentions.js +++ b/app/assets/javascripts/snowcrash/modules/mentions.js @@ -1,18 +1,20 @@ -window.Mentions = { - init: function(elements) { - tribute = new Tribute({ - allowSpaces: function() { return false }, - menuItemTemplate: function(item) { - return ' ' + item.string - }, - noMatchTemplate: function() { return '' }, - values: JSON.parse($('meta[name=mentionable-users]').attr('content')) - }); +(function(window) { + window.Mentions = { + init: function(elements) { + tribute = new Tribute({ + allowSpaces: function() { return false }, + menuItemTemplate: function(item) { + return ' ' + item.string + }, + noMatchTemplate: function() { return '' }, + values: JSON.parse($('meta[name=mentionable-users]').attr('content')) + }); - $('.main-content').scroll(function(){ - tribute.hideMenu(); - }); + $('.main-content').scroll(function(){ + tribute.hideMenu(); + }); - tribute.attach(elements); + tribute.attach(elements); + } } -} +})(window); From f2876a3db4cf5b32cf0173b69a439212b4714a3c Mon Sep 17 00:00:00 2001 From: Brian Pearce Date: Thu, 24 Oct 2019 10:28:56 +0200 Subject: [PATCH 05/22] Mention Mentions fix and enhancement in changelog --- CHANGELOG | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG b/CHANGELOG index fbd6219e9..56248df19 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -13,6 +13,10 @@ v3.15 (* 2019) - Fix page jump when issues list is collapsed. - Enhancement when adding new nodes to copy node label data between the single and multiple node forms. + - Enhance the mentions box in comments to close when it is open and the page is + scrolled. + - Fix bug that prevents the mentions dialog from appearing after navigating + through the app. v3.14 (August 2019) - Highlight code snippets. From 1c22c77d4f22978bce78f122ef71725b2f4c1a8d Mon Sep 17 00:00:00 2001 From: Brian Pearce Date: Thu, 24 Oct 2019 10:57:29 +0200 Subject: [PATCH 06/22] Use data attributes for JS binding --- app/assets/javascripts/snowcrash/modules/mentions.js | 2 +- app/views/layouts/snowcrash.html.erb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/snowcrash/modules/mentions.js b/app/assets/javascripts/snowcrash/modules/mentions.js index 83bcaf518..ba4e19c9a 100644 --- a/app/assets/javascripts/snowcrash/modules/mentions.js +++ b/app/assets/javascripts/snowcrash/modules/mentions.js @@ -10,7 +10,7 @@ values: JSON.parse($('meta[name=mentionable-users]').attr('content')) }); - $('.main-content').scroll(function(){ + $('[data-behavior=mentions-scroll]').scroll(function(){ tribute.hideMenu(); }); diff --git a/app/views/layouts/snowcrash.html.erb b/app/views/layouts/snowcrash.html.erb index d14c18f2a..a1fa6a5e2 100644 --- a/app/views/layouts/snowcrash.html.erb +++ b/app/views/layouts/snowcrash.html.erb @@ -60,7 +60,7 @@ <%= render partial: 'nodes/modals/add_node', locals: { type: :branch } %> -
+
<% end %> From 2d773c8d4f2a2f0ff8e24e47e1aef3d36d660e50 Mon Sep 17 00:00:00 2001 From: Brian Pearce Date: Wed, 23 Oct 2019 11:57:20 +0200 Subject: [PATCH 09/22] Optimize the avatar replacment This enhancement does a handful of things: - Capture emails better (but not strict) - Reduce queries to a single user query per comment - Perform the substitution in a single gsub for all email to avatar swaps - Scope findable users to uses of this project only --- app/helpers/avatar_helper.rb | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/app/helpers/avatar_helper.rb b/app/helpers/avatar_helper.rb index a4c991fa7..9ffeb3473 100644 --- a/app/helpers/avatar_helper.rb +++ b/app/helpers/avatar_helper.rb @@ -25,9 +25,15 @@ def avatar_image(user, options = {}) end def comment_avatars(comment) - comment.gsub(/@\w*@\w*\.\w*/) do |capture| - user = User.find_by(email: capture[1..-1]) - user ? avatar_image(user, size: 20, include_name: true) : capture + # Match any string that starts with an @ has another @ and ends with whitespace + emails = comment.scan(/@(\S*@\S*)\s/).flatten.uniq + users = current_project.authors.where(email: emails) + + replacement_rules = users.each_with_object({}) do |user, hash| + hash['@' + user.email] = avatar_image(user, size: 20, include_name: true) end + + matcher = /#{users.map { |user| '@' + user.email }.join('|')}/ + comment.gsub(matcher, replacement_rules) end end From 4c132d563f72462568d66496a6dc21e2a35692d4 Mon Sep 17 00:00:00 2001 From: Brian Pearce Date: Wed, 23 Oct 2019 12:01:34 +0200 Subject: [PATCH 10/22] Unify how comments are rendered to a single helper --- app/helpers/avatar_helper.rb | 4 ++++ app/views/comments/_comment.html.erb | 2 +- app/views/comments/update.js.erb | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/app/helpers/avatar_helper.rb b/app/helpers/avatar_helper.rb index 9ffeb3473..f3eb78a8b 100644 --- a/app/helpers/avatar_helper.rb +++ b/app/helpers/avatar_helper.rb @@ -36,4 +36,8 @@ def comment_avatars(comment) matcher = /#{users.map { |user| '@' + user.email }.join('|')}/ comment.gsub(matcher, replacement_rules) end + + def comment_formatter(comment) + simple_format(comment_avatars(h(comment))) + end end diff --git a/app/views/comments/_comment.html.erb b/app/views/comments/_comment.html.erb index 4c5376cbe..ad98e273a 100644 --- a/app/views/comments/_comment.html.erb +++ b/app/views/comments/_comment.html.erb @@ -29,7 +29,7 @@
- <%= simple_format(comment_avatars(h(comment.content))) %> + <%= comment_formatter(comment.content) %>
<% end %> diff --git a/app/views/comments/update.js.erb b/app/views/comments/update.js.erb index e2555fc2c..86867c1fe 100644 --- a/app/views/comments/update.js.erb +++ b/app/views/comments/update.js.erb @@ -1,3 +1,3 @@ $('#<%= dom_id(@comment) %> form').hide(); -$('#<%= dom_id(@comment) %> .content').html("<%= j(simple_format(h(@comment.content))) %>"); +$('#<%= dom_id(@comment) %> .content').html("<%= j(comment_formatter(@comment.content)) %>"); $('#<%= dom_id(@comment) %> .content').show(); From 4b319f544e10dd851ed4e4592398cb09715db95d Mon Sep 17 00:00:00 2001 From: Matt Budz Date: Wed, 23 Oct 2019 18:34:02 +0700 Subject: [PATCH 11/22] fix text wrapping / avatar float issue --- app/assets/stylesheets/snowcrash/modules.scss | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/assets/stylesheets/snowcrash/modules.scss b/app/assets/stylesheets/snowcrash/modules.scss index d40ea043c..a13594691 100644 --- a/app/assets/stylesheets/snowcrash/modules.scss +++ b/app/assets/stylesheets/snowcrash/modules.scss @@ -136,6 +136,10 @@ img { @include rounded(50%) } + + &.gravatar-inline { + margin-bottom: -3px; + } } table.table-ajax-inputs { From 8bc19968b0487ebe693c709019fd12955e0bd7f6 Mon Sep 17 00:00:00 2001 From: Matt Budz Date: Wed, 23 Oct 2019 18:53:48 +0700 Subject: [PATCH 12/22] add class declaration to helper --- app/helpers/avatar_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/helpers/avatar_helper.rb b/app/helpers/avatar_helper.rb index f3eb78a8b..8f58e87f1 100644 --- a/app/helpers/avatar_helper.rb +++ b/app/helpers/avatar_helper.rb @@ -30,7 +30,7 @@ def comment_avatars(comment) users = current_project.authors.where(email: emails) replacement_rules = users.each_with_object({}) do |user, hash| - hash['@' + user.email] = avatar_image(user, size: 20, include_name: true) + hash['@' + user.email] = avatar_image(user, size: 20, include_name: true, class: 'gravatar gravatar-inline') end matcher = /#{users.map { |user| '@' + user.email }.join('|')}/ From e99542c29b02d00770c086b2df56ba24517e203c Mon Sep 17 00:00:00 2001 From: Matt Budz Date: Wed, 23 Oct 2019 19:15:35 +0700 Subject: [PATCH 13/22] prevent user email breaking away from avatar --- app/assets/stylesheets/snowcrash/modules.scss | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/assets/stylesheets/snowcrash/modules.scss b/app/assets/stylesheets/snowcrash/modules.scss index a13594691..5bc8e6570 100644 --- a/app/assets/stylesheets/snowcrash/modules.scss +++ b/app/assets/stylesheets/snowcrash/modules.scss @@ -138,7 +138,9 @@ } &.gravatar-inline { - margin-bottom: -3px; + margin: 0 0 -3px 0; + float: none; + white-space: nowrap; } } From 180b97fefc220dc81045f09e6b76aab16230478a Mon Sep 17 00:00:00 2001 From: Brian Pearce Date: Wed, 23 Oct 2019 14:20:35 +0200 Subject: [PATCH 14/22] Prevent the stripping of addition avatar attrs The style, and fallback image properties were being removed by the `simple_format` method. So we'll sub the text for avatars after simple format has been called on all other comment data. Then we need to call html safe manually. Where as simple_format previously did this for us. --- app/helpers/avatar_helper.rb | 5 +++-- app/presenters/activity_presenter.rb | 3 ++- app/presenters/notification_presenter.rb | 3 ++- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/app/helpers/avatar_helper.rb b/app/helpers/avatar_helper.rb index 8f58e87f1..9c0c41bff 100644 --- a/app/helpers/avatar_helper.rb +++ b/app/helpers/avatar_helper.rb @@ -19,7 +19,8 @@ def avatar_image(user, options = {}) data: { fallback_image: fallback_image }, title: title, height: size, - width: size + width: size, + style: "width: #{size}px; height: #{size}px" ) + (include_name ? ' ' + user.email : '') end end @@ -38,6 +39,6 @@ def comment_avatars(comment) end def comment_formatter(comment) - simple_format(comment_avatars(h(comment))) + comment_avatars(simple_format(h(comment))).html_safe end end diff --git a/app/presenters/activity_presenter.rb b/app/presenters/activity_presenter.rb index 19de9bca9..2f5349c31 100644 --- a/app/presenters/activity_presenter.rb +++ b/app/presenters/activity_presenter.rb @@ -88,7 +88,8 @@ def avatar_image(size) class: 'gravatar', data: { fallback_image: image_path('logo_small.png') }, title: activity.user, - width: size + width: size, + style: "width: #{size}px; height: #{size}px" ) else h.image_tag 'logo_small.png', width: size, alt: 'This user has been deleted from the system' diff --git a/app/presenters/notification_presenter.rb b/app/presenters/notification_presenter.rb index fabf7a2ea..4cd0279bc 100644 --- a/app/presenters/notification_presenter.rb +++ b/app/presenters/notification_presenter.rb @@ -59,7 +59,8 @@ def avatar_image(size) class: 'gravatar', data: { fallback_image: image_path('logo_small.png') }, title: notification.actor.email, - width: size + width: size, + style: "width: #{size}px; height: #{size}px" ) else h.image_tag 'logo_small.png', width: size, alt: 'This user has been deleted from the system' From 75fe3021d9536ecb3fe06197ca47d40d6b4d793a Mon Sep 17 00:00:00 2001 From: Brian Pearce Date: Wed, 23 Oct 2019 14:32:14 +0200 Subject: [PATCH 15/22] update changelog for avatar mentions --- CHANGELOG | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG b/CHANGELOG index 56248df19..d264d6ec5 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -17,6 +17,8 @@ v3.15 (* 2019) scrolled. - Fix bug that prevents the mentions dialog from appearing after navigating through the app. + - Fixed elongated avatar images so they are round once again. + - Added avatar images to mentions in comments. v3.14 (August 2019) - Highlight code snippets. From 13da1379dbe6c5a969ce95caef06603c320b703b Mon Sep 17 00:00:00 2001 From: Brian Pearce Date: Fri, 25 Oct 2019 13:56:52 +0200 Subject: [PATCH 16/22] Build and memoize all user avatars for mentions --- app/controllers/comments_controller.rb | 6 ++++++ app/controllers/concerns/commented.rb | 24 +++++++++++++++++++++++- app/helpers/avatar_helper.rb | 11 +---------- 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index acf78f115..1b2d7f7b7 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -1,6 +1,7 @@ class CommentsController < AuthenticatedController include ActivityTracking include ProjectScoped + include Commented load_and_authorize_resource @@ -26,6 +27,11 @@ def destroy private + # This is cheeky code to make mentions work from concerns/commented + def comments + [@comment] + end + def comment_params params.require(:comment).permit(:content, :commentable_type, :commentable_id) end diff --git a/app/controllers/concerns/commented.rb b/app/controllers/concerns/commented.rb index d57adf2f5..ed59321c3 100644 --- a/app/controllers/concerns/commented.rb +++ b/app/controllers/concerns/commented.rb @@ -2,7 +2,8 @@ module Commented extend ActiveSupport::Concern included do - helper_method :commentable, :comments + helper_method :commentable, :comments, + :mentioned_users_replacement_rules, :mentioned_users_matcher end def commentable @@ -12,4 +13,25 @@ def commentable def comments @comments ||= commentable.comments.includes(:user) end + + def mentioned_users + @mentioned_users ||= begin + # Match any non whitespace that starts with an @ has another @ + emails = comments.inject([]) do |collection, comment| + (collection + comment.content.scan(/@(\S*@\S*)/)).flatten.uniq + end + + current_project.authors.where(email: emails) + end + end + + def mentioned_users_matcher + @mentioned_users_matcher ||= /#{mentioned_users.map { |user| '@' + user.email }.join('|')}/ + end + + def mentioned_users_replacement_rules + @mentioned_users_replacement_rules ||= mentioned_users.each_with_object({}) do |user, hash| + hash['@' + user.email] = view_context.avatar_image(user, size: 20, include_name: true, class: 'gravatar gravatar-inline') + end + end end diff --git a/app/helpers/avatar_helper.rb b/app/helpers/avatar_helper.rb index 9c0c41bff..cabeecdcf 100644 --- a/app/helpers/avatar_helper.rb +++ b/app/helpers/avatar_helper.rb @@ -26,16 +26,7 @@ def avatar_image(user, options = {}) end def comment_avatars(comment) - # Match any string that starts with an @ has another @ and ends with whitespace - emails = comment.scan(/@(\S*@\S*)\s/).flatten.uniq - users = current_project.authors.where(email: emails) - - replacement_rules = users.each_with_object({}) do |user, hash| - hash['@' + user.email] = avatar_image(user, size: 20, include_name: true, class: 'gravatar gravatar-inline') - end - - matcher = /#{users.map { |user| '@' + user.email }.join('|')}/ - comment.gsub(matcher, replacement_rules) + comment.gsub(mentioned_users_matcher, mentioned_users_replacement_rules) end def comment_formatter(comment) From 2235db9e709ac92c28d4d8eb20ecb63a512d1151 Mon Sep 17 00:00:00 2001 From: Brian Pearce Date: Fri, 25 Oct 2019 17:19:26 +0200 Subject: [PATCH 17/22] Re-organize the memoizing The previous implementation was okay when we rendered collections of comments on main pages but it didn't work for areas where a single comment was rended, such as during polling, or after a creation event. --- app/controllers/activities_controller.rb | 1 + app/controllers/concerns/commented.rb | 34 ++++++++++++------------ app/helpers/avatar_helper.rb | 6 ++++- 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/app/controllers/activities_controller.rb b/app/controllers/activities_controller.rb index 2837849d2..e5adf208b 100644 --- a/app/controllers/activities_controller.rb +++ b/app/controllers/activities_controller.rb @@ -3,6 +3,7 @@ class ActivitiesController < AuthenticatedController include ProjectScoped + include Commented def poll @this_poll = Time.now.to_i diff --git a/app/controllers/concerns/commented.rb b/app/controllers/concerns/commented.rb index ed59321c3..904e9b8a9 100644 --- a/app/controllers/concerns/commented.rb +++ b/app/controllers/concerns/commented.rb @@ -2,8 +2,7 @@ module Commented extend ActiveSupport::Concern included do - helper_method :commentable, :comments, - :mentioned_users_replacement_rules, :mentioned_users_matcher + helper_method :commentable, :comments, :mentions_builder end def commentable @@ -11,27 +10,28 @@ def commentable end def comments - @comments ||= commentable.comments.includes(:user) + @comments ||= commentable&.comments&.includes(:user) end - def mentioned_users - @mentioned_users ||= begin - # Match any non whitespace that starts with an @ has another @ - emails = comments.inject([]) do |collection, comment| - (collection + comment.content.scan(/@(\S*@\S*)/)).flatten.uniq - end - - current_project.authors.where(email: emails) + def mentioned_users(comments) + # Match any non whitespace that starts with an @ has another @ + emails = comments.inject([]) do |collection, comment| + (collection + comment.scan(/@(\S*@\S*)/)).flatten.uniq end - end - def mentioned_users_matcher - @mentioned_users_matcher ||= /#{mentioned_users.map { |user| '@' + user.email }.join('|')}/ + current_project.authors.where(email: emails) end - def mentioned_users_replacement_rules - @mentioned_users_replacement_rules ||= mentioned_users.each_with_object({}) do |user, hash| - hash['@' + user.email] = view_context.avatar_image(user, size: 20, include_name: true, class: 'gravatar gravatar-inline') + def mentions_builder(comments) + @mentions_builder ||= begin + users = mentioned_users(Array(comments)) + + matcher = /#{users.map { |user| '@' + user.email }.join('|')}/ + rules = users.each_with_object({}) do |user, hash| + hash['@' + user.email] = view_context.avatar_image(user, size: 20, include_name: true, class: 'gravatar gravatar-inline') + end + + [matcher, rules] end end end diff --git a/app/helpers/avatar_helper.rb b/app/helpers/avatar_helper.rb index cabeecdcf..10de78bc0 100644 --- a/app/helpers/avatar_helper.rb +++ b/app/helpers/avatar_helper.rb @@ -26,7 +26,11 @@ def avatar_image(user, options = {}) end def comment_avatars(comment) - comment.gsub(mentioned_users_matcher, mentioned_users_replacement_rules) + # Support both concers/commented Comments collection for most views + # as well as rendering 1-off's for polling and ajax requests. + collection = comments ? comments.map(&:content) : Array(comment) + matcher, rules = mentions_builder(collection) + comment.gsub(matcher, rules) end def comment_formatter(comment) From 1556c2021beafadff6e476b686521bec7e0c48ea Mon Sep 17 00:00:00 2001 From: Matt Budz Date: Tue, 29 Oct 2019 20:30:39 +0700 Subject: [PATCH 18/22] add gravatars to ce --- app/helpers/avatar_helper.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/app/helpers/avatar_helper.rb b/app/helpers/avatar_helper.rb index 10de78bc0..ab017aa04 100644 --- a/app/helpers/avatar_helper.rb +++ b/app/helpers/avatar_helper.rb @@ -1,7 +1,12 @@ module AvatarHelper def avatar_url(user, options = {}) - # The arguments are a noop here for CE-Pro parity. - image_path('profile.jpg') + if user.nil? + image_path('profile.png') + else + gravatar_id = Digest::MD5.hexdigest(user.email.downcase) + size = options.fetch(:size, 64).to_i + "https://secure.gravatar.com/avatar/#{gravatar_id}.png?r=PG&s=#{size}" + end end def avatar_image(user, options = {}) From 8147833c8c275bd087ff2d2ebd3cbb42014b5a6b Mon Sep 17 00:00:00 2001 From: Brian Pearce Date: Tue, 29 Oct 2019 15:15:22 +0100 Subject: [PATCH 19/22] Allow multiple behaviors --- app/assets/javascripts/snowcrash/modules/mentions.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/assets/javascripts/snowcrash/modules/mentions.js b/app/assets/javascripts/snowcrash/modules/mentions.js index ba4e19c9a..48d77bfca 100644 --- a/app/assets/javascripts/snowcrash/modules/mentions.js +++ b/app/assets/javascripts/snowcrash/modules/mentions.js @@ -10,7 +10,7 @@ values: JSON.parse($('meta[name=mentionable-users]').attr('content')) }); - $('[data-behavior=mentions-scroll]').scroll(function(){ + $('[data-behavior~=mentions-scroll]').scroll(function(){ tribute.hideMenu(); }); From 749330a692c3160afa795ac0961017aebaa73b92 Mon Sep 17 00:00:00 2001 From: Brian Pearce Date: Tue, 29 Oct 2019 15:17:50 +0100 Subject: [PATCH 20/22] Order elements by alpha --- app/controllers/activities_controller.rb | 2 +- app/controllers/comments_controller.rb | 2 +- app/presenters/activity_presenter.rb | 4 ++-- app/presenters/notification_presenter.rb | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/controllers/activities_controller.rb b/app/controllers/activities_controller.rb index e5adf208b..bfdfab437 100644 --- a/app/controllers/activities_controller.rb +++ b/app/controllers/activities_controller.rb @@ -2,8 +2,8 @@ # users. class ActivitiesController < AuthenticatedController - include ProjectScoped include Commented + include ProjectScoped def poll @this_poll = Time.now.to_i diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 1b2d7f7b7..9f842c668 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -1,7 +1,7 @@ class CommentsController < AuthenticatedController include ActivityTracking - include ProjectScoped include Commented + include ProjectScoped load_and_authorize_resource diff --git a/app/presenters/activity_presenter.rb b/app/presenters/activity_presenter.rb index 2f5349c31..a7ea51e09 100644 --- a/app/presenters/activity_presenter.rb +++ b/app/presenters/activity_presenter.rb @@ -87,9 +87,9 @@ def avatar_image(size) alt: activity.user, class: 'gravatar', data: { fallback_image: image_path('logo_small.png') }, + style: "width: #{size}px; height: #{size}px", title: activity.user, - width: size, - style: "width: #{size}px; height: #{size}px" + width: size ) else h.image_tag 'logo_small.png', width: size, alt: 'This user has been deleted from the system' diff --git a/app/presenters/notification_presenter.rb b/app/presenters/notification_presenter.rb index 4cd0279bc..cc275abf5 100644 --- a/app/presenters/notification_presenter.rb +++ b/app/presenters/notification_presenter.rb @@ -58,9 +58,9 @@ def avatar_image(size) alt: notification.actor.email, class: 'gravatar', data: { fallback_image: image_path('logo_small.png') }, + style: "width: #{size}px; height: #{size}px", title: notification.actor.email, - width: size, - style: "width: #{size}px; height: #{size}px" + width: size ) else h.image_tag 'logo_small.png', width: size, alt: 'This user has been deleted from the system' From 004e513d6a029d84ca63319fc5c2fd31fc379634 Mon Sep 17 00:00:00 2001 From: Brian Pearce Date: Tue, 29 Oct 2019 15:19:46 +0100 Subject: [PATCH 21/22] Update changelog for gravatars --- CHANGELOG | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG b/CHANGELOG index d264d6ec5..11ce4b004 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -19,6 +19,7 @@ v3.15 (* 2019) through the app. - Fixed elongated avatar images so they are round once again. - Added avatar images to mentions in comments. + - Load gravatars for users who's email has been setup with gravatar. v3.14 (August 2019) - Highlight code snippets. From c02d4d2e1031119e744e3612b4cd3071ebf11033 Mon Sep 17 00:00:00 2001 From: Brian Pearce Date: Tue, 29 Oct 2019 15:53:33 +0100 Subject: [PATCH 22/22] Describe the purpose of the Commented module --- app/controllers/concerns/commented.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/app/controllers/concerns/commented.rb b/app/controllers/concerns/commented.rb index 904e9b8a9..29bea271f 100644 --- a/app/controllers/concerns/commented.rb +++ b/app/controllers/concerns/commented.rb @@ -1,3 +1,9 @@ +# This module can be included anywhere comments are rendered. Any item that is +# commentable can use this module to help render comment collections in views. +# It will load comments and authors without n+1 as well as build avatars for all +# mentions within the collection of comments. Additionally it can be used where +# a single comment is rendered such as activity polling, or comment creation via +# ajax. module Commented extend ActiveSupport::Concern @@ -22,6 +28,10 @@ def mentioned_users(comments) current_project.authors.where(email: emails) end + # When called this will scan the provided comments for email addresses and + # search for users of the current project with that address. It then builds + # out a hash of avatars as a lookup and replacement for all comments in the + # thread. def mentions_builder(comments) @mentions_builder ||= begin users = mentioned_users(Array(comments))