Skip to content

Commit

Permalink
Merge branch 'master' into notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
aapomm committed Nov 7, 2019
2 parents 9d57c7a + 9ea6553 commit d073385
Show file tree
Hide file tree
Showing 14 changed files with 123 additions and 44 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ v3.15 (* 2019)
- Enhancement when adding new nodes to copy node label data between the single
and multiple node forms.
- Add an smtp.yml config file to handle the SMTP configuration
- 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.
- 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.
Expand Down
20 changes: 20 additions & 0 deletions app/assets/javascripts/snowcrash/modules/mentions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
(function(window) {
window.Mentions = {
init: function(elements) {
tribute = new Tribute({
allowSpaces: function() { return false },
menuItemTemplate: function(item) {
return '<img src="' + item.original.avatar_url + '" width="24px" height="24px" > ' + item.string
},
noMatchTemplate: function() { return '' },
values: JSON.parse($('meta[name=mentionable-users]').attr('content'))
});

$('[data-behavior~=mentions-scroll]').scroll(function(){
tribute.hideMenu();
});

tribute.attach(elements);
}
}
})(window);
15 changes: 0 additions & 15 deletions app/assets/javascripts/snowcrash/modules/mentions.js.coffee

This file was deleted.

6 changes: 6 additions & 0 deletions app/assets/stylesheets/snowcrash/modules.scss
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@
img {
@include rounded(50%)
}

&.gravatar-inline {
margin: 0 0 -3px 0;
float: none;
white-space: nowrap;
}
}

table.table-ajax-inputs {
Expand Down
1 change: 1 addition & 0 deletions app/controllers/activities_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# users.

class ActivitiesController < AuthenticatedController
include Commented
include ProjectScoped

def poll
Expand Down
6 changes: 6 additions & 0 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class CommentsController < AuthenticatedController
include ActivityTracking
include Commented
include ProjectScoped

load_and_authorize_resource
Expand All @@ -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
Expand Down
36 changes: 34 additions & 2 deletions app/controllers/concerns/commented.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,47 @@
# 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

included do
helper_method :commentable, :comments
helper_method :commentable, :comments, :mentions_builder
end

def commentable
instance_variable_get("@#{controller_name.singularize}")
end

def comments
@comments ||= commentable.comments.includes(:user)
@comments ||= commentable&.comments&.includes(:user)
end

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

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))

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
24 changes: 0 additions & 24 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,4 @@
module ApplicationHelper # :nodoc:
def avatar_url(user, options = {})
# The arguments are a noop here for CE-Pro parity.
image_path('profile.jpg')
end

def avatar_image(user, options = {})
alt = options.fetch(:alt, "#{user.email}'s avatar")
fallback_image = options.fetch(:fallback_image, image_path('logo_small.png'))
include_name = options.fetch(:include_name, false)
size = options.fetch(:size, 73)
title = options.fetch(:title, user.email)

content_tag :span, class: 'gravatar' do
image_tag(
avatar_url(user, size: size),
alt: alt,
data: { fallback_image: fallback_image },
title: title,
height: size,
width: size
) + (include_name ? ' ' + user.email : '')
end
end

def markup(text)
return unless text.present?

Expand Down
44 changes: 44 additions & 0 deletions app/helpers/avatar_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
module AvatarHelper
def avatar_url(user, options = {})
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 = {})
alt = options.fetch(:alt, "#{user.email}'s avatar")
fallback_image = options.fetch(:fallback_image, image_path('logo_small.png'))
include_name = options.fetch(:include_name, false)
size = options.fetch(:size, 73)
title = options.fetch(:title, user.email)
klass = options.fetch(:class, 'gravatar')

content_tag :span, class: klass do
image_tag(
avatar_url(user, size: size),
alt: alt,
data: { fallback_image: fallback_image },
title: title,
height: size,
width: size,
style: "width: #{size}px; height: #{size}px"
) + (include_name ? ' ' + user.email : '')
end
end

def comment_avatars(comment)
# 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)
comment_avatars(simple_format(h(comment))).html_safe
end
end
1 change: 1 addition & 0 deletions app/presenters/activity_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ 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
)
Expand Down
1 change: 1 addition & 0 deletions app/presenters/notification_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ 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
)
Expand Down
2 changes: 1 addition & 1 deletion app/views/comments/_comment.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
</div>

<div class="content">
<%= simple_format(h(comment.content)) %>
<%= comment_formatter(comment.content) %>
</div>
</div>
<% end %>
2 changes: 1 addition & 1 deletion app/views/comments/update.js.erb
Original file line number Diff line number Diff line change
@@ -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();
2 changes: 1 addition & 1 deletion app/views/layouts/snowcrash.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
<%= render partial: 'nodes/modals/add_node', locals: { type: :branch } %>
</div>
</div>
<div class="main-content <%= content_for?(:sidebar) ? 'no-padding' : ''%>">
<div class="main-content <%= content_for?(:sidebar) ? 'no-padding' : ''%>" data-behavior='mentions-scroll'>
<noscript>
<div class="alert alert-error" style="margin: 2em">
<h4>Error</h4>
Expand Down

0 comments on commit d073385

Please sign in to comment.