From 0d5e1a8307e2792acc1179223e1d756a7c3a1d9c Mon Sep 17 00:00:00 2001
From: Marius BALTEANU <%= f.text_area :description, :cols => 60, :rows => 15, :class => 'wiki-edit',
:data => {
:auto_complete => true,
- :issues_url => auto_complete_issues_path(:project_id => @project, :q => '')
+ :issues_url => auto_complete_issues_path(:project_id => @project, :q => ''),
+ :users_url => auto_complete_users_path(:project_id => @project, :q => '')
}
%>
<%= render :partial => 'issue', :formats => [:html], :locals => { :issue => @issue, :user => @user, :issue_url => @issue_url } %>
diff --git a/app/views/mailer/issue_edit.html.erb b/app/views/mailer/issue_edit.html.erb
index b5bdd88ee..7b5b28fec 100644
--- a/app/views/mailer/issue_edit.html.erb
+++ b/app/views/mailer/issue_edit.html.erb
@@ -2,6 +2,9 @@
(<%= l(:field_private_notes) %>)
<% end %>
<%= l(:text_issue_updated, :id => link_to("##{@issue.id}", @issue_url), :author => h(@journal.user)).html_safe %>
+<% unless @reason.nil? %>
+ You have been mentioned in it.
+<% end %>
diff --git a/app/views/mailer/user_mentioned.html.erb b/app/views/mailer/user_mentioned.html.erb
new file mode 100644
index 000000000..e69de29bb
diff --git a/app/views/mailer/user_mentioned.text.erb b/app/views/mailer/user_mentioned.text.erb
new file mode 100644
index 000000000..e69de29bb
diff --git a/app/views/messages/_form.html.erb b/app/views/messages/_form.html.erb
index 7d3a9c59f..6a3026852 100644
--- a/app/views/messages/_form.html.erb
+++ b/app/views/messages/_form.html.erb
@@ -27,7 +27,8 @@
:accesskey => accesskey(:edit),
:data => {
:auto_complete => true,
- :issues_url => auto_complete_issues_path(:project_id => @project, :q => '')
+ :issues_url => auto_complete_issues_path(:project_id => @project, :q => ''),
+ :users_url => auto_complete_users_path(:project_id => @project, :q => '')
}
%>
<%= f.text_area :description, :required => true, :cols => 60, :rows => 15, :class => 'wiki-edit', :data => { :auto_complete => true, - :issues_url => auto_complete_issues_path(:project_id => @project, :q => '') + :issues_url => auto_complete_issues_path(:project_id => @project, :q => ''), + :users_url => auto_complete_users_path(:project_id => @project, :q => '') } %>
<%= render :partial => 'attachments/form', :locals => {:container => @news} %>
diff --git a/app/views/news/show.html.erb b/app/views/news/show.html.erb index 04ea54a0a..6d2ef7b32 100644 --- a/app/views/news/show.html.erb +++ b/app/views/news/show.html.erb @@ -56,7 +56,8 @@ <%= text_area 'comment', 'comments', :cols => 80, :rows => 15, :class => 'wiki-edit', :data => { :auto_complete => true, - :issues_url => auto_complete_issues_path(:project_id => @project, :q => '') + :issues_url => auto_complete_issues_path(:project_id => @project, :q => ''), + :users_url => auto_complete_users_path(:project_id => @project, :q => '') } %> <%= wikitoolbar_for 'comment_comments', preview_news_path(:project_id => @project, :id => @news) %> diff --git a/app/views/wiki/edit.html.erb b/app/views/wiki/edit.html.erb index 0e1c23cc4..d00a4015b 100644 --- a/app/views/wiki/edit.html.erb +++ b/app/views/wiki/edit.html.erb @@ -17,7 +17,8 @@ :class => 'wiki-edit', :data => { :auto_complete => true, - :issues_url => auto_complete_issues_path(:project_id => @project, :q => '') + :issues_url => auto_complete_issues_path(:project_id => @project, :q => ''), + :users_url => auto_complete_users_path(:project_id => @project, :q => '') } %> diff --git a/config/routes.rb b/config/routes.rb index 7e8cdeac9..cc7b5d5b4 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -45,6 +45,7 @@ Rails.application.routes.draw do # Misc issue routes. TODO: move into resources match '/issues/auto_complete', :to => 'auto_completes#issues', :via => :get, :as => 'auto_complete_issues' + match '/users/auto_complete', :to => 'auto_completes#users', :via => :get, :as => 'auto_complete_users' match '/issues/context_menu', :to => 'context_menus#issues', :as => 'issues_context_menu', :via => [:get, :post] match '/issues/changes', :to => 'journals#index', :as => 'issue_changes', :via => :get match '/issues/:id/quoted', :to => 'journals#new', :id => /\d+/, :via => :post, :as => 'quoted_issue' diff --git a/lib/redmine.rb b/lib/redmine.rb index beaebaec8..cbfe31d6d 100644 --- a/lib/redmine.rb +++ b/lib/redmine.rb @@ -31,6 +31,7 @@ rescue LoadError end require 'redmine/acts/positioned' +require 'redmine/acts/mentionable' require 'redmine/scm/base' require 'redmine/access_control' diff --git a/lib/redmine/acts/mentionable.rb b/lib/redmine/acts/mentionable.rb new file mode 100644 index 000000000..3c6c05c72 --- /dev/null +++ b/lib/redmine/acts/mentionable.rb @@ -0,0 +1,94 @@ +# frozen_string_literal: true + +# Redmine - project management software +# Copyright (C) 2006-2019 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'} + 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) + 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 + if text_formatting == 'textile' + content = content.gsub(%r{(.*?)}m, '') + elsif text_formatting == 'markdown' + content = content.gsub(%r{(~~~|```)(.*?)(~~~|```)}m, '') + end + + content.scan(/@([a-z0-9_\-@\.]*)/i) + end + end + end + end +end + +ActiveRecord::Base.send :include, Redmine::Acts::Mentionable diff --git a/public/javascripts/application.js b/public/javascripts/application.js index f33c9caf8..edb5c8b95 100644 --- a/public/javascripts/application.js +++ b/public/javascripts/application.js @@ -1049,21 +1049,42 @@ function inlineAutoComplete(element) { }; const tribute = new Tribute({ - trigger: '#', - values: function (text, cb) { - if (event.target.type === 'text' && $(element).attr('autocomplete') != 'off') { - $(element).attr('autocomplete', 'off'); - } - remoteSearch(issuesUrl + text, function (issues) { - return cb(issues); - }); - }, - lookup: 'label', - fillAttr: 'label', - requireLeadingSpace: true, - selectTemplate: function (issue) { - return '#' + issue.original.id; - } + collection: [ + { + trigger: '#', + values: function (text, cb) { + if (event.target.type === 'text' && $(element).attr('autocomplete') != 'off') { + $(element).attr('autocomplete', 'off'); + } + remoteSearch(issuesUrl + text, function (issues) { + return cb(issues); + }); + }, + lookup: 'label', + fillAttr: 'label', + requireLeadingSpace: true, + selectTemplate: function (issue) { + return '#' + issue.original.id; + }, + }, + { + trigger: '@', + lookup: function (user, mentionText) { + return user.name + user.firstname + user.lastname + user.login; + }, + values: function (text, cb) { + remoteSearch(usersUrl + text, function (users) { + return cb(users); + }); + }, + menuItemTemplate: function (user) { + return user.original.name; + }, + selectTemplate: function (user) { + return '@' + user.original.login; + } + } + ] }); tribute.attach(element); diff --git a/test/functional/auto_completes_controller_test.rb b/test/functional/auto_completes_controller_test.rb index bc1f2eba1..13a4dcd0a 100644 --- a/test/functional/auto_completes_controller_test.rb +++ b/test/functional/auto_completes_controller_test.rb @@ -78,7 +78,7 @@ class AutoCompletesControllerTest < Redmine::ControllerTest assert_include "Bug #13", response.body end - def test_auto_complete_with_scope_all_should_search_other_projects + def test_issues_with_scope_all_should_search_other_projects get( :issues, :params => { @@ -91,13 +91,13 @@ class AutoCompletesControllerTest < Redmine::ControllerTest assert_include "Bug #13", response.body 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'}) assert_response :success assert_include "Bug #13", response.body 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( :issues, :params => { @@ -127,7 +127,7 @@ class AutoCompletesControllerTest < Redmine::ControllerTest assert_equal 'Bug #13: Subproject issue two', issue['label'] 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( :issues, :params => { @@ -141,7 +141,7 @@ class AutoCompletesControllerTest < Redmine::ControllerTest assert_not_include "closed", response.body 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( :issues, :params => { @@ -155,7 +155,7 @@ class AutoCompletesControllerTest < Redmine::ControllerTest assert_not_include "Issue due today", response.body 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( :issues, :params => { @@ -181,7 +181,7 @@ class AutoCompletesControllerTest < Redmine::ControllerTest assert_include 'application/json', response.headers['Content-Type'] 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 # and we need two more to test the 10 limit %w(1..2).each do @@ -196,4 +196,47 @@ class AutoCompletesControllerTest < Redmine::ControllerTest assert_equal 10, json.count assert_equal Issue.last.id, json.first['id'].to_i end + + def test_users_should_accept_case_insensitive_term_param + get :users, :params => { + :q => 'JoHN' + } + assert_response :success + assert_include "John Smith", response.body + end + + def test_users_should_return_json + get :users, :params => { + :q => 'john' + } + assert_response :success + json = ActiveSupport::JSON.decode(response.body) + assert_kind_of Array, json + user = json.first + assert_kind_of Hash, user + assert_equal 'John', user['firstname'] + assert_equal 'Smith', user['lastname'] + assert_equal 'John Smith', user['name'] + assert_equal 'jsmith', user['login'] + end + + def test_users_with_project_and_without_term_should_return_project_users + get :users, :params => { + :project_id => 'onlinestore' + } + assert_response :success + json = ActiveSupport::JSON.decode(response.body) + assert_equal 2, json.count + assert_equal 'jsmith', json.first['login'] + assert_equal 'miscuser8', json.last['login'] + end + + def test_users_without_project_and_with_term_should_search_in_all_users + get :users, :params => { + :q => 'm' + } + assert_response :success + assert_include "Redmine Admin", response.body + assert_include "Dave Lopper", response.body + end end -- 2.22.0