mirror of
https://github.com/redmine/redmine.git
synced 2025-11-18 03:00:52 +01:00
Allow users to be mentioned using @ in issues and wiki pages (#13919):
* the user must have add watchers permission on that object in order to mention other users * mentioned user will receive a notification email * only visible users who can view the object can be mentioned git-svn-id: http://svn.redmine.org/redmine/trunk@21435 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
@@ -28,7 +28,7 @@ class WatchersController < ApplicationController
|
|||||||
set_watcher(@watchables, User.current, false)
|
set_watcher(@watchables, User.current, false)
|
||||||
end
|
end
|
||||||
|
|
||||||
before_action :find_project, :authorize, :only => [:new, :create, :append, :destroy, :autocomplete_for_user]
|
before_action :find_project, :authorize, :only => [:new, :create, :append, :destroy, :autocomplete_for_user, :autocomplete_for_mention]
|
||||||
accept_api_auth :create, :destroy
|
accept_api_auth :create, :destroy
|
||||||
|
|
||||||
def new
|
def new
|
||||||
@@ -93,6 +93,11 @@ class WatchersController < ApplicationController
|
|||||||
render :layout => false
|
render :layout => false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def autocomplete_for_mention
|
||||||
|
users = users_for_mention
|
||||||
|
render :json => format_users_json(users)
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def find_project
|
def find_project
|
||||||
@@ -155,6 +160,42 @@ class WatchersController < ApplicationController
|
|||||||
users
|
users
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def users_for_mention
|
||||||
|
users = []
|
||||||
|
q = params[:q].to_s.strip
|
||||||
|
|
||||||
|
scope = nil
|
||||||
|
if params[:q].blank? && @project.present?
|
||||||
|
scope = @project.principals.assignable_watchers
|
||||||
|
else
|
||||||
|
scope = Principal.assignable_watchers.limit(10)
|
||||||
|
end
|
||||||
|
# Exclude Group principal for now
|
||||||
|
scope = scope.where(:type => ['User'])
|
||||||
|
|
||||||
|
users = scope.sorted.like(params[:q]).to_a
|
||||||
|
|
||||||
|
if @watchables && @watchables.size == 1
|
||||||
|
object = @watchables.first
|
||||||
|
if object.respond_to?(:visible?)
|
||||||
|
users.reject! {|user| user.is_a?(User) && !object.visible?(user)}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
users
|
||||||
|
end
|
||||||
|
|
||||||
|
def format_users_json(users)
|
||||||
|
users.map do |user|
|
||||||
|
{
|
||||||
|
'firstname' => user.firstname,
|
||||||
|
'lastname' => user.lastname,
|
||||||
|
'name' => user.name,
|
||||||
|
'login' => user.login
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def find_objects_from_params
|
def find_objects_from_params
|
||||||
klass =
|
klass =
|
||||||
begin
|
begin
|
||||||
|
|||||||
@@ -1819,19 +1819,20 @@ module ApplicationHelper
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def autocomplete_data_sources(project)
|
|
||||||
{
|
|
||||||
issues: auto_complete_issues_path(:project_id => project, :q => ''),
|
|
||||||
wiki_pages: auto_complete_wiki_pages_path(:project_id => project, :q => '')
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
def heads_for_auto_complete(project)
|
def heads_for_auto_complete(project)
|
||||||
data_sources = autocomplete_data_sources(project)
|
data_sources = autocomplete_data_sources(project)
|
||||||
javascript_tag(
|
javascript_tag(
|
||||||
"rm = window.rm || {};" \
|
"rm = window.rm || {};" \
|
||||||
"rm.AutoComplete = rm.AutoComplete || {};" \
|
"rm.AutoComplete = rm.AutoComplete || {};" \
|
||||||
"rm.AutoComplete.dataSources = '#{data_sources.to_json}';"
|
"rm.AutoComplete.dataSources = JSON.parse('#{data_sources.to_json}');"
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
def update_data_sources_for_auto_complete(data_sources)
|
||||||
|
javascript_tag(
|
||||||
|
"const currentDataSources = rm.AutoComplete.dataSources;" \
|
||||||
|
"const newDataSources = JSON.parse('#{data_sources.to_json}'); " \
|
||||||
|
"rm.AutoComplete.dataSources = Object.assign(currentDataSources, newDataSources);"
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -1866,4 +1867,11 @@ module ApplicationHelper
|
|||||||
name = identifier.gsub(%r{^"(.*)"$}, "\\1")
|
name = identifier.gsub(%r{^"(.*)"$}, "\\1")
|
||||||
return CGI.unescapeHTML(name)
|
return CGI.unescapeHTML(name)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def autocomplete_data_sources(project)
|
||||||
|
{
|
||||||
|
issues: auto_complete_issues_path(project_id: project, q: ''),
|
||||||
|
wiki_pages: auto_complete_wiki_pages_path(project_id: project, q: ''),
|
||||||
|
}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -54,6 +54,8 @@ class Issue < ActiveRecord::Base
|
|||||||
acts_as_activity_provider :scope => proc {preload(:project, :author, :tracker, :status)},
|
acts_as_activity_provider :scope => proc {preload(:project, :author, :tracker, :status)},
|
||||||
:author_key => :author_id
|
:author_key => :author_id
|
||||||
|
|
||||||
|
acts_as_mentionable :attributes => ['description']
|
||||||
|
|
||||||
DONE_RATIO_OPTIONS = %w(issue_field issue_status)
|
DONE_RATIO_OPTIONS = %w(issue_field issue_status)
|
||||||
|
|
||||||
attr_reader :transition_warning
|
attr_reader :transition_warning
|
||||||
|
|||||||
@@ -58,6 +58,7 @@ class Journal < ActiveRecord::Base
|
|||||||
" (#{JournalDetail.table_name}.prop_key = 'status_id' OR #{Journal.table_name}.notes <> '')").distinct
|
" (#{JournalDetail.table_name}.prop_key = 'status_id' OR #{Journal.table_name}.notes <> '')").distinct
|
||||||
end
|
end
|
||||||
)
|
)
|
||||||
|
acts_as_mentionable :attributes => ['notes']
|
||||||
before_create :split_private_notes
|
before_create :split_private_notes
|
||||||
after_create_commit :send_notification
|
after_create_commit :send_notification
|
||||||
|
|
||||||
@@ -172,10 +173,12 @@ class Journal < ActiveRecord::Base
|
|||||||
|
|
||||||
def notified_watchers
|
def notified_watchers
|
||||||
notified = journalized.notified_watchers
|
notified = journalized.notified_watchers
|
||||||
if private_notes?
|
select_journal_visible_user(notified)
|
||||||
notified = notified.select {|user| user.allowed_to?(:view_private_notes, journalized.project)}
|
end
|
||||||
end
|
|
||||||
notified
|
def notified_mentions
|
||||||
|
notified = super
|
||||||
|
select_journal_visible_user(notified)
|
||||||
end
|
end
|
||||||
|
|
||||||
def watcher_recipients
|
def watcher_recipients
|
||||||
@@ -337,4 +340,11 @@ class Journal < ActiveRecord::Base
|
|||||||
Mailer.deliver_issue_edit(self)
|
Mailer.deliver_issue_edit(self)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def select_journal_visible_user(notified)
|
||||||
|
if private_notes?
|
||||||
|
notified = notified.select {|user| user.allowed_to?(:view_private_notes, journalized.project)}
|
||||||
|
end
|
||||||
|
notified
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -94,7 +94,7 @@ class Mailer < ActionMailer::Base
|
|||||||
# Example:
|
# Example:
|
||||||
# Mailer.deliver_issue_add(issue)
|
# Mailer.deliver_issue_add(issue)
|
||||||
def self.deliver_issue_add(issue)
|
def self.deliver_issue_add(issue)
|
||||||
users = issue.notified_users | issue.notified_watchers
|
users = issue.notified_users | issue.notified_watchers | issue.notified_mentions
|
||||||
users.each do |user|
|
users.each do |user|
|
||||||
issue_add(user, issue).deliver_later
|
issue_add(user, issue).deliver_later
|
||||||
end
|
end
|
||||||
@@ -129,7 +129,7 @@ class Mailer < ActionMailer::Base
|
|||||||
# Example:
|
# Example:
|
||||||
# Mailer.deliver_issue_edit(journal)
|
# Mailer.deliver_issue_edit(journal)
|
||||||
def self.deliver_issue_edit(journal)
|
def self.deliver_issue_edit(journal)
|
||||||
users = journal.notified_users | journal.notified_watchers
|
users = journal.notified_users | journal.notified_watchers | journal.notified_mentions | journal.journalized.notified_mentions
|
||||||
users.select! do |user|
|
users.select! do |user|
|
||||||
journal.notes? || journal.visible_details(user).any?
|
journal.notes? || journal.visible_details(user).any?
|
||||||
end
|
end
|
||||||
@@ -306,7 +306,7 @@ class Mailer < ActionMailer::Base
|
|||||||
# Example:
|
# Example:
|
||||||
# Mailer.deliver_wiki_content_added(wiki_content)
|
# Mailer.deliver_wiki_content_added(wiki_content)
|
||||||
def self.deliver_wiki_content_added(wiki_content)
|
def self.deliver_wiki_content_added(wiki_content)
|
||||||
users = wiki_content.notified_users | wiki_content.page.wiki.notified_watchers
|
users = wiki_content.notified_users | wiki_content.page.wiki.notified_watchers | wiki_content.notified_mentions
|
||||||
users.each do |user|
|
users.each do |user|
|
||||||
wiki_content_added(user, wiki_content).deliver_later
|
wiki_content_added(user, wiki_content).deliver_later
|
||||||
end
|
end
|
||||||
@@ -343,6 +343,7 @@ class Mailer < ActionMailer::Base
|
|||||||
users = wiki_content.notified_users
|
users = wiki_content.notified_users
|
||||||
users |= wiki_content.page.notified_watchers
|
users |= wiki_content.page.notified_watchers
|
||||||
users |= wiki_content.page.wiki.notified_watchers
|
users |= wiki_content.page.wiki.notified_watchers
|
||||||
|
users |= wiki_content.notified_mentions
|
||||||
|
|
||||||
users.each do |user|
|
users.each do |user|
|
||||||
wiki_content_updated(user, wiki_content).deliver_later
|
wiki_content_updated(user, wiki_content).deliver_later
|
||||||
|
|||||||
@@ -24,6 +24,9 @@ class WikiContent < ActiveRecord::Base
|
|||||||
belongs_to :page, :class_name => 'WikiPage'
|
belongs_to :page, :class_name => 'WikiPage'
|
||||||
belongs_to :author, :class_name => 'User'
|
belongs_to :author, :class_name => 'User'
|
||||||
has_many :versions, :class_name => 'WikiContentVersion', :dependent => :delete_all
|
has_many :versions, :class_name => 'WikiContentVersion', :dependent => :delete_all
|
||||||
|
|
||||||
|
acts_as_mentionable :attributes => ['text']
|
||||||
|
|
||||||
validates_presence_of :text
|
validates_presence_of :text
|
||||||
validates_length_of :comments, :maximum => 1024, :allow_nil => true
|
validates_length_of :comments, :maximum => 1024, :allow_nil => true
|
||||||
|
|
||||||
|
|||||||
@@ -55,6 +55,11 @@
|
|||||||
<% heads_for_wiki_formatter %>
|
<% heads_for_wiki_formatter %>
|
||||||
<%= heads_for_auto_complete(@issue.project) %>
|
<%= heads_for_auto_complete(@issue.project) %>
|
||||||
|
|
||||||
|
<% if User.current.allowed_to?(:add_issue_watchers, @issue.project)%>
|
||||||
|
<%= update_data_sources_for_auto_complete({users: watchers_autocomplete_for_mention_path(project_id: @issue.project, q: '', object_type: 'issue',
|
||||||
|
object_id: @issue.id)}) %>
|
||||||
|
<% end %>
|
||||||
|
|
||||||
<%= javascript_tag do %>
|
<%= javascript_tag do %>
|
||||||
$(document).ready(function(){
|
$(document).ready(function(){
|
||||||
$("#issue_tracker_id, #issue_status_id").each(function(){
|
$("#issue_tracker_id, #issue_status_id").each(function(){
|
||||||
|
|||||||
@@ -64,6 +64,10 @@
|
|||||||
<%= link_to l(:button_cancel), wiki_page_edit_cancel_path(@page) %>
|
<%= link_to l(:button_cancel), wiki_page_edit_cancel_path(@page) %>
|
||||||
</p>
|
</p>
|
||||||
<%= wikitoolbar_for 'content_text', preview_project_wiki_page_path(:project_id => @project, :id => @page.title) %>
|
<%= wikitoolbar_for 'content_text', preview_project_wiki_page_path(:project_id => @project, :id => @page.title) %>
|
||||||
|
|
||||||
|
<% if User.current.allowed_to?(:add_wiki_page_watchers, @project)%>
|
||||||
|
<%= update_data_sources_for_auto_complete({users: watchers_autocomplete_for_mention_path(project_id: @project, q: '', object_type: 'wiki_page', object_id: @page.id)}) %>
|
||||||
|
<% end %>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
<% content_for :header_tags do %>
|
<% content_for :header_tags do %>
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ Rails.application.routes.draw do
|
|||||||
post 'boards/:board_id/topics/:id/edit', :to => 'messages#edit'
|
post 'boards/:board_id/topics/:id/edit', :to => 'messages#edit'
|
||||||
post 'boards/:board_id/topics/:id/destroy', :to => 'messages#destroy'
|
post 'boards/:board_id/topics/:id/destroy', :to => 'messages#destroy'
|
||||||
|
|
||||||
# Auto complate routes
|
# Auto complete routes
|
||||||
match '/issues/auto_complete', :to => 'auto_completes#issues', :via => :get, :as => 'auto_complete_issues'
|
match '/issues/auto_complete', :to => 'auto_completes#issues', :via => :get, :as => 'auto_complete_issues'
|
||||||
match '/wiki_pages/auto_complete', :to => 'auto_completes#wiki_pages', :via => :get, :as => 'auto_complete_wiki_pages'
|
match '/wiki_pages/auto_complete', :to => 'auto_completes#wiki_pages', :via => :get, :as => 'auto_complete_wiki_pages'
|
||||||
|
|
||||||
@@ -119,6 +119,7 @@ Rails.application.routes.draw do
|
|||||||
post 'watchers', :to => 'watchers#create'
|
post 'watchers', :to => 'watchers#create'
|
||||||
post 'watchers/append', :to => 'watchers#append'
|
post 'watchers/append', :to => 'watchers#append'
|
||||||
delete 'watchers', :to => 'watchers#destroy'
|
delete 'watchers', :to => 'watchers#destroy'
|
||||||
|
get 'watchers/autocomplete_for_mention', to: 'watchers#autocomplete_for_mention', via: [:get]
|
||||||
get 'watchers/autocomplete_for_user', :to => 'watchers#autocomplete_for_user'
|
get 'watchers/autocomplete_for_user', :to => 'watchers#autocomplete_for_user'
|
||||||
# Specific routes for issue watchers API
|
# Specific routes for issue watchers API
|
||||||
post 'issues/:object_id/watchers', :to => 'watchers#create', :object_type => 'issue'
|
post 'issues/:object_id/watchers', :to => 'watchers#create', :object_type => 'issue'
|
||||||
|
|||||||
110
lib/redmine/acts/mentionable.rb
Normal file
110
lib/redmine/acts/mentionable.rb
Normal file
@@ -0,0 +1,110 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
# Redmine - project management software
|
||||||
|
# Copyright (C) 2006-2022 Jean-Philippe Lang
|
||||||
|
#
|
||||||
|
# This program is free software; you can redistribute it and/or
|
||||||
|
# modify it under the terms of the GNU General Public License
|
||||||
|
# as published by the Free Software Foundation; either version 2
|
||||||
|
# of the License, or (at your option) any later version.
|
||||||
|
#
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License
|
||||||
|
# along with this program; if not, write to the Free Software
|
||||||
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
|
|
||||||
|
module Redmine
|
||||||
|
module Acts
|
||||||
|
module Mentionable
|
||||||
|
def self.included(base)
|
||||||
|
base.extend ClassMethods
|
||||||
|
end
|
||||||
|
|
||||||
|
module ClassMethods
|
||||||
|
def acts_as_mentionable(options = {})
|
||||||
|
class_attribute :mentionable_attributes
|
||||||
|
self.mentionable_attributes = options[:attributes]
|
||||||
|
|
||||||
|
attr_accessor :mentioned_users
|
||||||
|
|
||||||
|
send :include, Redmine::Acts::Mentionable::InstanceMethods
|
||||||
|
|
||||||
|
after_save :parse_mentions
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
module InstanceMethods
|
||||||
|
def self.included(base)
|
||||||
|
base.extend ClassMethods
|
||||||
|
end
|
||||||
|
|
||||||
|
def notified_mentions
|
||||||
|
notified = mentioned_users.to_a
|
||||||
|
notified.reject! {|user| user.mail.blank? || user.mail_notification == 'none'}
|
||||||
|
if respond_to?(:visible?)
|
||||||
|
notified.select! {|user| visible?(user)}
|
||||||
|
end
|
||||||
|
notified
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def parse_mentions
|
||||||
|
mentionable_attrs = self.mentionable_attributes
|
||||||
|
saved_mentionable_attrs = self.saved_changes.select{|a| mentionable_attrs.include?(a)}
|
||||||
|
|
||||||
|
saved_mentionable_attrs.each do |key, attr|
|
||||||
|
old_value, new_value = attr
|
||||||
|
get_mentioned_users(old_value, new_value)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def get_mentioned_users(old_content, new_content)
|
||||||
|
self.mentioned_users = []
|
||||||
|
|
||||||
|
previous_matches = scan_for_mentioned_users(old_content)
|
||||||
|
current_matches = scan_for_mentioned_users(new_content)
|
||||||
|
new_matches = (current_matches - previous_matches).flatten
|
||||||
|
|
||||||
|
if new_matches.any?
|
||||||
|
self.mentioned_users = User.visible.active.where(login: new_matches)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def scan_for_mentioned_users(content)
|
||||||
|
return [] if content.nil?
|
||||||
|
|
||||||
|
# remove quoted text
|
||||||
|
content = content.gsub(%r{\r\n(?:\>\s)+(.*?)\r\n}m, '')
|
||||||
|
|
||||||
|
text_formatting = Setting.text_formatting
|
||||||
|
# Remove text wrapped in pre tags based on text formatting
|
||||||
|
case text_formatting
|
||||||
|
when 'textile'
|
||||||
|
content = content.gsub(%r{<pre>(.*?)</pre>}m, '')
|
||||||
|
when 'markdown', 'common_mark'
|
||||||
|
content = content.gsub(%r{(~~~|```)(.*?)(~~~|```)}m, '')
|
||||||
|
end
|
||||||
|
|
||||||
|
users = content.scan(MENTION_PATTERN).flatten
|
||||||
|
end
|
||||||
|
|
||||||
|
MENTION_PATTERN = /
|
||||||
|
(?:^|\W) # beginning of string or non-word char
|
||||||
|
@((?>[a-z0-9][a-z0-9-]*)) # @username
|
||||||
|
(?!\/) # without a trailing slash
|
||||||
|
(?=
|
||||||
|
\.+[ \t\W]| # dots followed by space or non-word character
|
||||||
|
\.+$| # dots at end of line
|
||||||
|
[^0-9a-zA-Z_.]| # non-word character except dot
|
||||||
|
$ # end of line
|
||||||
|
)
|
||||||
|
/ix
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -21,6 +21,7 @@ module Redmine
|
|||||||
module Preparation
|
module Preparation
|
||||||
def self.prepare
|
def self.prepare
|
||||||
ActiveRecord::Base.include Redmine::Acts::Positioned
|
ActiveRecord::Base.include Redmine::Acts::Positioned
|
||||||
|
ActiveRecord::Base.include Redmine::Acts::Mentionable
|
||||||
ActiveRecord::Base.include Redmine::I18n
|
ActiveRecord::Base.include Redmine::I18n
|
||||||
|
|
||||||
Scm::Base.add "Subversion"
|
Scm::Base.add "Subversion"
|
||||||
@@ -71,9 +72,10 @@ module Redmine
|
|||||||
map.permission :view_private_notes, {}, :read => true, :require => :member
|
map.permission :view_private_notes, {}, :read => true, :require => :member
|
||||||
map.permission :set_notes_private, {}, :require => :member
|
map.permission :set_notes_private, {}, :require => :member
|
||||||
map.permission :delete_issues, {:issues => :destroy}, :require => :member
|
map.permission :delete_issues, {:issues => :destroy}, :require => :member
|
||||||
|
map.permission :mention_users, {}
|
||||||
# Watchers
|
# Watchers
|
||||||
map.permission :view_issue_watchers, {}, :read => true
|
map.permission :view_issue_watchers, {}, :read => true
|
||||||
map.permission :add_issue_watchers, {:watchers => [:new, :create, :append, :autocomplete_for_user]}
|
map.permission :add_issue_watchers, {:watchers => [:new, :create, :append, :autocomplete_for_user, :autocomplete_for_mention]}
|
||||||
map.permission :delete_issue_watchers, {:watchers => :destroy}
|
map.permission :delete_issue_watchers, {:watchers => :destroy}
|
||||||
map.permission :import_issues, {}
|
map.permission :import_issues, {}
|
||||||
# Issue categories
|
# Issue categories
|
||||||
@@ -123,7 +125,7 @@ module Redmine
|
|||||||
map.permission :delete_wiki_pages, {:wiki => [:destroy, :destroy_version]}, :require => :member
|
map.permission :delete_wiki_pages, {:wiki => [:destroy, :destroy_version]}, :require => :member
|
||||||
map.permission :delete_wiki_pages_attachments, {}
|
map.permission :delete_wiki_pages_attachments, {}
|
||||||
map.permission :view_wiki_page_watchers, {}, :read => true
|
map.permission :view_wiki_page_watchers, {}, :read => true
|
||||||
map.permission :add_wiki_page_watchers, {:watchers => [:new, :create, :autocomplete_for_user]}
|
map.permission :add_wiki_page_watchers, {:watchers => [:new, :create, :autocomplete_for_user, :autocomplete_for_mention]}
|
||||||
map.permission :delete_wiki_page_watchers, {:watchers => :destroy}
|
map.permission :delete_wiki_page_watchers, {:watchers => :destroy}
|
||||||
map.permission :protect_wiki_pages, {:wiki => :protect}, :require => :member
|
map.permission :protect_wiki_pages, {:wiki => :protect}, :require => :member
|
||||||
map.permission :manage_wiki, {:wikis => :destroy, :wiki => :rename}, :require => :member
|
map.permission :manage_wiki, {:wikis => :destroy, :wiki => :rename}, :require => :member
|
||||||
@@ -145,7 +147,7 @@ module Redmine
|
|||||||
map.permission :delete_messages, {:messages => :destroy}, :require => :member
|
map.permission :delete_messages, {:messages => :destroy}, :require => :member
|
||||||
map.permission :delete_own_messages, {:messages => :destroy}, :require => :loggedin
|
map.permission :delete_own_messages, {:messages => :destroy}, :require => :loggedin
|
||||||
map.permission :view_message_watchers, {}, :read => true
|
map.permission :view_message_watchers, {}, :read => true
|
||||||
map.permission :add_message_watchers, {:watchers => [:new, :create, :autocomplete_for_user]}
|
map.permission :add_message_watchers, {:watchers => [:new, :create, :autocomplete_for_user, :autocomplete_for_mention]}
|
||||||
map.permission :delete_message_watchers, {:watchers => :destroy}
|
map.permission :delete_message_watchers, {:watchers => :destroy}
|
||||||
map.permission :manage_boards, {:projects => :settings, :boards => [:new, :create, :edit, :update, :destroy]}, :require => :member
|
map.permission :manage_boards, {:projects => :settings, :boards => [:new, :create, :edit, :update, :destroy]}, :require => :member
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1127,9 +1127,13 @@ function inlineAutoComplete(element) {
|
|||||||
if (element.dataset.tribute === 'true') {return};
|
if (element.dataset.tribute === 'true') {return};
|
||||||
|
|
||||||
const getDataSource = function(entity) {
|
const getDataSource = function(entity) {
|
||||||
const dataSources = JSON.parse(rm.AutoComplete.dataSources);
|
const dataSources = rm.AutoComplete.dataSources;
|
||||||
|
|
||||||
return dataSources[entity];
|
if (dataSources[entity]) {
|
||||||
|
return dataSources[entity];
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const remoteSearch = function(url, cb) {
|
const remoteSearch = function(url, cb) {
|
||||||
@@ -1187,6 +1191,26 @@ function inlineAutoComplete(element) {
|
|||||||
menuItemTemplate: function (wikiPage) {
|
menuItemTemplate: function (wikiPage) {
|
||||||
return sanitizeHTML(wikiPage.original.label);
|
return sanitizeHTML(wikiPage.original.label);
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
trigger: '@',
|
||||||
|
lookup: function (user, mentionText) {
|
||||||
|
return user.name + user.firstname + user.lastname + user.login;
|
||||||
|
},
|
||||||
|
values: function (text, cb) {
|
||||||
|
const url = getDataSource('users');
|
||||||
|
if (url) {
|
||||||
|
remoteSearch(url + text, function (users) {
|
||||||
|
return cb(users);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
menuItemTemplate: function (user) {
|
||||||
|
return user.original.name;
|
||||||
|
},
|
||||||
|
selectTemplate: function (user) {
|
||||||
|
return '@' + user.original.login;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
noMatchTemplate: ""
|
noMatchTemplate: ""
|
||||||
|
|||||||
@@ -79,7 +79,7 @@ class AutoCompletesControllerTest < Redmine::ControllerTest
|
|||||||
assert_include "Bug #13", response.body
|
assert_include "Bug #13", response.body
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_auto_complete_with_scope_all_should_search_other_projects
|
def test_issues_with_scope_all_should_search_other_projects
|
||||||
get(
|
get(
|
||||||
:issues,
|
:issues,
|
||||||
:params => {
|
:params => {
|
||||||
@@ -92,13 +92,13 @@ class AutoCompletesControllerTest < Redmine::ControllerTest
|
|||||||
assert_include "Bug #13", response.body
|
assert_include "Bug #13", response.body
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_auto_complete_without_project_should_search_all_projects
|
def test_issues_without_project_should_search_all_projects
|
||||||
get(:issues, :params => {:q => '13'})
|
get(:issues, :params => {:q => '13'})
|
||||||
assert_response :success
|
assert_response :success
|
||||||
assert_include "Bug #13", response.body
|
assert_include "Bug #13", response.body
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_auto_complete_without_scope_all_should_not_search_other_projects
|
def test_issues_without_scope_all_should_not_search_other_projects
|
||||||
get(
|
get(
|
||||||
:issues,
|
:issues,
|
||||||
:params => {
|
:params => {
|
||||||
@@ -128,7 +128,7 @@ class AutoCompletesControllerTest < Redmine::ControllerTest
|
|||||||
assert_equal 'Bug #13: Subproject issue two', issue['label']
|
assert_equal 'Bug #13: Subproject issue two', issue['label']
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_auto_complete_with_status_o_should_return_open_issues_only
|
def test_issues_with_status_o_should_return_open_issues_only
|
||||||
get(
|
get(
|
||||||
:issues,
|
:issues,
|
||||||
:params => {
|
:params => {
|
||||||
@@ -142,7 +142,7 @@ class AutoCompletesControllerTest < Redmine::ControllerTest
|
|||||||
assert_not_include "closed", response.body
|
assert_not_include "closed", response.body
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_auto_complete_with_status_c_should_return_closed_issues_only
|
def test_issues_with_status_c_should_return_closed_issues_only
|
||||||
get(
|
get(
|
||||||
:issues,
|
:issues,
|
||||||
:params => {
|
:params => {
|
||||||
@@ -156,7 +156,7 @@ class AutoCompletesControllerTest < Redmine::ControllerTest
|
|||||||
assert_not_include "Issue due today", response.body
|
assert_not_include "Issue due today", response.body
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_auto_complete_with_issue_id_should_not_return_that_issue
|
def test_issues_with_issue_id_should_not_return_that_issue
|
||||||
get(
|
get(
|
||||||
:issues,
|
:issues,
|
||||||
:params => {
|
:params => {
|
||||||
@@ -182,7 +182,7 @@ class AutoCompletesControllerTest < Redmine::ControllerTest
|
|||||||
assert_include 'application/json', response.headers['Content-Type']
|
assert_include 'application/json', response.headers['Content-Type']
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_auto_complete_without_term_should_return_last_10_issues
|
def test_issue_without_term_should_return_last_10_issues
|
||||||
# There are 9 issues generated by fixtures
|
# There are 9 issues generated by fixtures
|
||||||
# and we need two more to test the 10 limit
|
# and we need two more to test the 10 limit
|
||||||
%w(1..2).each do
|
%w(1..2).each do
|
||||||
|
|||||||
@@ -236,4 +236,12 @@ class JournalTest < ActiveSupport::TestCase
|
|||||||
assert_equal "image#{i}.png", attachment.filename
|
assert_equal "image#{i}.png", attachment.filename
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_notified_mentions_should_not_include_users_who_cannot_view_private_notes
|
||||||
|
journal = Journal.generate!(journalized: Issue.find(2), user: User.find(1), private_notes: true, notes: 'Hello @dlopper, @jsmith and @admin.')
|
||||||
|
|
||||||
|
# User "dlopper" has "Developer" role on project "eCookbook"
|
||||||
|
# Role "Developer" does not have the "View private notes" permission
|
||||||
|
assert_equal [1, 2], journal.notified_mentions.map(&:id)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
146
test/unit/lib/redmine/acts/mentionable_test.rb
Normal file
146
test/unit/lib/redmine/acts/mentionable_test.rb
Normal file
@@ -0,0 +1,146 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
# Redmine - project management software
|
||||||
|
# Copyright (C) 2006-2022 Jean-Philippe Lang
|
||||||
|
#
|
||||||
|
# This program is free software; you can redistribute it and/or
|
||||||
|
# modify it under the terms of the GNU General Public License
|
||||||
|
# as published by the Free Software Foundation; either version 2
|
||||||
|
# of the License, or (at your option) any later version.
|
||||||
|
#
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License
|
||||||
|
# along with this program; if not, write to the Free Software
|
||||||
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
|
|
||||||
|
require File.expand_path('../../../../../test_helper', __FILE__)
|
||||||
|
|
||||||
|
class Redmine::Acts::MentionableTest < ActiveSupport::TestCase
|
||||||
|
fixtures :projects, :users, :email_addresses, :members, :member_roles, :roles,
|
||||||
|
:groups_users,
|
||||||
|
:trackers, :projects_trackers,
|
||||||
|
:enabled_modules,
|
||||||
|
:issue_statuses, :issue_categories, :issue_relations, :workflows,
|
||||||
|
:enumerations,
|
||||||
|
:issues
|
||||||
|
|
||||||
|
def test_mentioned_users_with_user_mention
|
||||||
|
issue = Issue.generate!(project_id: 1, description: '@dlopper')
|
||||||
|
|
||||||
|
assert_equal [User.find(3)], issue.mentioned_users
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_mentioned_users_with_multiple_mentions
|
||||||
|
issue = Issue.generate!(project_id: 1, description: 'Hello @dlopper, @jsmith.')
|
||||||
|
|
||||||
|
assert_equal [User.find(2), User.find(3)], issue.mentioned_users
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_mentioned_users_should_not_mention_same_user_multiple_times
|
||||||
|
issue = Issue.generate!(project_id: 1, description: '@dlopper @jsmith @dlopper')
|
||||||
|
|
||||||
|
assert_equal [User.find(2), User.find(3)], issue.mentioned_users
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_mentioned_users_should_include_only_active_users
|
||||||
|
# disable dlopper account
|
||||||
|
user = User.find(3)
|
||||||
|
user.status = User::STATUS_LOCKED
|
||||||
|
user.save
|
||||||
|
|
||||||
|
issue = Issue.generate!(project_id: 1, description: '@dlopper @jsmith')
|
||||||
|
|
||||||
|
assert_equal [User.find(2)], issue.mentioned_users
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_mentioned_users_should_include_only_visible_users
|
||||||
|
User.current = nil
|
||||||
|
Role.non_member.update! users_visibility: 'members_of_visible_projects'
|
||||||
|
Role.anonymous.update! users_visibility: 'members_of_visible_projects'
|
||||||
|
user = User.generate!
|
||||||
|
|
||||||
|
issue = Issue.generate!(project_id: 1, description: "@jsmith @#{user.login}")
|
||||||
|
|
||||||
|
assert_equal [User.find(2)], issue.mentioned_users
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_mentioned_users_should_not_include_mentioned_users_in_existing_content
|
||||||
|
issue = Issue.generate!(project_id: 1, description: 'Hello @dlopper')
|
||||||
|
|
||||||
|
assert issue.save
|
||||||
|
assert_equal [User.find(3)], issue.mentioned_users
|
||||||
|
|
||||||
|
issue.description = 'Hello @dlopper and @jsmith'
|
||||||
|
issue.save
|
||||||
|
|
||||||
|
assert_equal [User.find(2)], issue.mentioned_users
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_mentioned_users_should_not_include_users_wrapped_in_pre_tags_for_textile
|
||||||
|
description = <<~STR
|
||||||
|
<pre>
|
||||||
|
Hello @jsmith
|
||||||
|
</pre>
|
||||||
|
STR
|
||||||
|
|
||||||
|
with_settings text_formatting: 'textile' do
|
||||||
|
issue = Issue.generate!(project_id: 1, description: description)
|
||||||
|
|
||||||
|
assert_equal [], issue.mentioned_users
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_mentioned_users_should_not_include_users_wrapped_in_pre_tags_for_markdown
|
||||||
|
description = <<~STR
|
||||||
|
```
|
||||||
|
Hello @jsmith
|
||||||
|
```
|
||||||
|
STR
|
||||||
|
|
||||||
|
with_settings text_formatting: 'markdown' do
|
||||||
|
issue = Issue.generate!(project_id: 1, description: description)
|
||||||
|
|
||||||
|
assert_equal [], issue.mentioned_users
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_mentioned_users_should_not_include_users_wrapped_in_pre_tags_for_common_mark
|
||||||
|
description = <<~STR
|
||||||
|
```
|
||||||
|
Hello @jsmith
|
||||||
|
```
|
||||||
|
STR
|
||||||
|
|
||||||
|
with_settings text_formatting: 'common_mark' do
|
||||||
|
issue = Issue.generate!(project_id: 1, description: description)
|
||||||
|
|
||||||
|
assert_equal [], issue.mentioned_users
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_notified_mentions
|
||||||
|
issue = Issue.generate!(project_id: 1, description: 'Hello @dlopper, @jsmith.')
|
||||||
|
|
||||||
|
assert_equal [User.find(2), User.find(3)], issue.notified_mentions
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_notified_mentions_should_not_include_users_who_out_of_all_email
|
||||||
|
User.find(3).update!(mail_notification: :none)
|
||||||
|
issue = Issue.generate!(project_id: 1, description: "Hello @dlopper, @jsmith.")
|
||||||
|
|
||||||
|
assert_equal [User.find(2)], issue.notified_mentions
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_notified_mentions_should_not_include_users_who_cannot_view_the_object
|
||||||
|
user = User.find(3)
|
||||||
|
|
||||||
|
# User dlopper does not have access to project "Private child of eCookbook"
|
||||||
|
issue = Issue.generate!(project_id: 5, description: "Hello @dlopper, @jsmith.")
|
||||||
|
|
||||||
|
assert !issue.notified_mentions.include?(user)
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -464,6 +464,19 @@ class MailerTest < ActiveSupport::TestCase
|
|||||||
assert_not_include user.mail, recipients
|
assert_not_include user.mail, recipients
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_issue_add_should_notify_mentioned_users_in_issue_description
|
||||||
|
User.find(1).mail_notification = 'only_my_events'
|
||||||
|
|
||||||
|
issue = Issue.generate!(project_id: 1, description: 'Hello @dlopper and @admin.')
|
||||||
|
|
||||||
|
assert Mailer.deliver_issue_add(issue)
|
||||||
|
# @jsmith and @dlopper are members of the project
|
||||||
|
# admin is mentioned
|
||||||
|
# @dlopper won't receive duplicated notifications
|
||||||
|
assert_equal 3, ActionMailer::Base.deliveries.size
|
||||||
|
assert_include User.find(1).mail, recipients
|
||||||
|
end
|
||||||
|
|
||||||
def test_issue_add_should_include_enabled_fields
|
def test_issue_add_should_include_enabled_fields
|
||||||
issue = Issue.find(2)
|
issue = Issue.find(2)
|
||||||
assert Mailer.deliver_issue_add(issue)
|
assert Mailer.deliver_issue_add(issue)
|
||||||
@@ -608,6 +621,39 @@ class MailerTest < ActiveSupport::TestCase
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_issue_edit_should_notify_mentioned_users_in_issue_updated_description
|
||||||
|
User.find(1).mail_notification = 'only_my_events'
|
||||||
|
|
||||||
|
issue = Issue.find(3)
|
||||||
|
issue.init_journal(User.current)
|
||||||
|
issue.update(description: "Hello @admin")
|
||||||
|
journal = issue.journals.last
|
||||||
|
|
||||||
|
ActionMailer::Base.deliveries.clear
|
||||||
|
Mailer.deliver_issue_edit(journal)
|
||||||
|
|
||||||
|
# @jsmith and @dlopper are members of the project
|
||||||
|
# admin is mentioned in the updated description
|
||||||
|
# @dlopper won't receive duplicated notifications
|
||||||
|
assert_equal 3, ActionMailer::Base.deliveries.size
|
||||||
|
assert_include User.find(1).mail, recipients
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_issue_edit_should_notify_mentioned_users_in_notes
|
||||||
|
User.find(1).mail_notification = 'only_my_events'
|
||||||
|
|
||||||
|
journal = Journal.generate!(journalized: Issue.find(3), user: User.find(1), notes: 'Hello @admin.')
|
||||||
|
|
||||||
|
ActionMailer::Base.deliveries.clear
|
||||||
|
Mailer.deliver_issue_edit(journal)
|
||||||
|
|
||||||
|
# @jsmith and @dlopper are members of the project
|
||||||
|
# admin is mentioned in the notes
|
||||||
|
# @dlopper won't receive duplicated notifications
|
||||||
|
assert_equal 3, ActionMailer::Base.deliveries.size
|
||||||
|
assert_include User.find(1).mail, recipients
|
||||||
|
end
|
||||||
|
|
||||||
def test_issue_should_send_email_notification_with_suppress_empty_fields
|
def test_issue_should_send_email_notification_with_suppress_empty_fields
|
||||||
ActionMailer::Base.deliveries.clear
|
ActionMailer::Base.deliveries.clear
|
||||||
with_settings :notified_events => %w(issue_added) do
|
with_settings :notified_events => %w(issue_added) do
|
||||||
@@ -703,6 +749,20 @@ class MailerTest < ActiveSupport::TestCase
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_wiki_content_added_should_notify_mentioned_users_in_content
|
||||||
|
content = WikiContent.new(text: 'Hello @admin.', author_id: 1, page_id: 1)
|
||||||
|
content.save!
|
||||||
|
|
||||||
|
ActionMailer::Base.deliveries.clear
|
||||||
|
Mailer.deliver_wiki_content_added(content)
|
||||||
|
|
||||||
|
# @jsmith and @dlopper are members of the project
|
||||||
|
# admin is mentioned in the notes
|
||||||
|
# @dlopper won't receive duplicated notifications
|
||||||
|
assert_equal 3, ActionMailer::Base.deliveries.size
|
||||||
|
assert_include User.find(1).mail, recipients
|
||||||
|
end
|
||||||
|
|
||||||
def test_wiki_content_updated
|
def test_wiki_content_updated
|
||||||
content = WikiContent.find(1)
|
content = WikiContent.find(1)
|
||||||
assert Mailer.deliver_wiki_content_updated(content)
|
assert Mailer.deliver_wiki_content_updated(content)
|
||||||
@@ -713,6 +773,21 @@ class MailerTest < ActiveSupport::TestCase
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_wiki_content_updated_should_notify_mentioned_users_in_updated_content
|
||||||
|
content = WikiContent.find(1)
|
||||||
|
content.update(text: 'Hello @admin.')
|
||||||
|
content.save!
|
||||||
|
|
||||||
|
ActionMailer::Base.deliveries.clear
|
||||||
|
Mailer.deliver_wiki_content_updated(content)
|
||||||
|
|
||||||
|
# @jsmith and @dlopper are members of the project
|
||||||
|
# admin is mentioned in the notes
|
||||||
|
# @dlopper won't receive duplicated notifications
|
||||||
|
assert_equal 3, ActionMailer::Base.deliveries.size
|
||||||
|
assert_include User.find(1).mail, recipients
|
||||||
|
end
|
||||||
|
|
||||||
def test_register
|
def test_register
|
||||||
token = Token.find(1)
|
token = Token.find(1)
|
||||||
assert Mailer.deliver_register(token.user, token)
|
assert Mailer.deliver_register(token.user, token)
|
||||||
|
|||||||
Reference in New Issue
Block a user