Skip to content

Commit

Permalink
Include admins when editors are mentioned.
Browse files Browse the repository at this point in the history
More specifically, now when you mention a role by name, members of that
role and all roles higher than it will be notified. In practice right
now that means when editors are mentioned, admins will be too. You
cannot (and never have been able to) @mention curators as that's
everyone on the site.
  • Loading branch information
acoffman committed Jan 18, 2019
1 parent a1eb717 commit 68730f5
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
10 changes: 8 additions & 2 deletions app/jobs/notify_mentioned.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ def perform(text, originating_user, event)
mentioned_users_result = Actions::ExtractMentions.new(text).perform
mentioned_roles_result = Actions::ExtractRoleMentions.new(text).perform

users_to_notify = mentioned_users_result.mentioned_users.to_a +
User.where(role: mentioned_roles_result.mentioned_role_values)
mentioned_by_name = mentioned_users_result.mentioned_users.to_a
mentioned_by_role = if mentioned_roles_result.mentioned_role_values.present?
User.where("users.role >= ?", mentioned_roles_result.mentioned_role_values.max).to_a
else
[]
end

users_to_notify = mentioned_by_name + mentioned_by_role

users_to_notify.uniq.each do |user|
Notification.create(
Expand Down
1 change: 1 addition & 0 deletions spec/fabricators/user_fabricator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name { sequence(:user_name) { |i| "User_#{i}" } }
email { sequence(:email) { |i| "Email ##{i}" } }
username { sequence(:username) { |i| "Username_#{i}" } }
role 'curator'
end

Fabricator(:authorization) do
Expand Down
56 changes: 56 additions & 0 deletions spec/jobs/notify_mentioned_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
require 'rails_helper'

describe 'notifying mentioned users' do

before :each do
@editor = Fabricate(:user, role: 'editor')
@admin = Fabricate(:user, role: 'admin')
@curator = Fabricate(:user)
@originating_user = Fabricate(:user)
@event = Event.create(action: 'test', originating_user: @originating_user, subject: Fabricate(:gene))
end

it 'should allow mentiions by username' do
text = "hey @#{@curator.username}"
NotifyMentioned.new.perform(text, @originating_user, @event)
expect(Notification.where(notified_user: @curator).size).to eq 1
expect(Notification.count).to eq 1
end

it 'should allow mentions by role' do
text = "hey @admin"
NotifyMentioned.new.perform(text, @originating_user, @event)
expect(Notification.where(notified_user: @admin).size).to eq 1
expect(Notification.count).to eq 1
end

it 'should not allow mentions by the role "curator"' do
text = "hey @curator"
NotifyMentioned.new.perform(text, @originating_user, @event)
expect(Notification.count).to eq 0
end

it 'should also allow plural role names to be mentioned' do
text = "hey @admins"
NotifyMentioned.new.perform(text, @originating_user, @event)
expect(Notification.where(notified_user: @admin).size).to eq 1
expect(Notification.count).to eq 1
end

it 'should include admins when editors are mentioned' do
text = "hey @editors"
NotifyMentioned.new.perform(text, @originating_user, @event)
expect(Notification.where(notified_user: @admin).size).to eq 1
expect(Notification.where(notified_user: @editor).size).to eq 1
expect(Notification.count).to eq 2
end


it 'should not send notifications to the user who generated the notification' do
text = "hey @editors"
NotifyMentioned.new.perform(text, @editor, @event)
expect(Notification.where(notified_user: @admin).size).to eq 1
expect(Notification.where(notified_user: @editor).size).to eq 0
expect(Notification.count).to eq 1
end
end

0 comments on commit 68730f5

Please sign in to comment.