Project

General

Profile

Feature #13919 » 0001-Demo-for-acts_as_mentionable.patch

Marius BĂLTEANU, 2020-04-05 21:17

View differences:

app/controllers/auto_completes_controller.rb
render :json => format_issues_json(issues)
end
def users
users = []
q = (params[:q] || params[:term]).to_s.strip
scope = nil
if params[:q].blank? && @project.present?
scope = @project.users
else
scope = User.all.limit(10)
end
users = scope.active.visible.sorted.like(params[:q]).to_a
render :json => format_users_json(users)
end
private
def find_project
......
}
}
end
def format_users_json(users)
users.map {|user| {
'firstname' => user.firstname,
'lastname' => user.lastname,
'name' => user.name,
'login' => user.login
}
}
end
end
app/models/issue.rb
acts_as_activity_provider :scope => preload(:project, :author, :tracker, :status),
:author_key => :author_id
acts_as_mentionable :attributes => ['description']
DONE_RATIO_OPTIONS = %w(issue_field issue_status)
attr_reader :transition_warning
app/models/journal.rb
joins("LEFT OUTER JOIN #{JournalDetail.table_name} ON #{JournalDetail.table_name}.journal_id = #{Journal.table_name}.id").
where("#{Journal.table_name}.journalized_type = 'Issue' AND" +
" (#{JournalDetail.table_name}.prop_key = 'status_id' OR #{Journal.table_name}.notes <> '')").distinct
acts_as_mentionable :attributes => ['notes']
before_create :split_private_notes
after_create_commit :send_notification
app/models/mailer.rb
end
# Builds a mail for notifying user about a new issue
def issue_add(user, issue)
def issue_add(user, issue, reason = nil)
redmine_headers 'Project' => issue.project.identifier,
'Issue-Tracker' => issue.tracker.name,
'Issue-Id' => issue.id,
......
@author = issue.author
@issue = issue
@user = user
@reason = reason
@issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue)
subject = "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}]"
subject += " (#{issue.status.name})" if Setting.show_status_changes_in_mail_subject?
......
# Mailer.deliver_issue_add(issue)
def self.deliver_issue_add(issue)
users = issue.notified_users | issue.notified_watchers
users.each do |user|
issue_add(user, issue).deliver_later
end
mentions = issue.notified_mentions
users.each do |user|
issue_add(user, issue, 'mentioned').deliver_later
end
end
# Builds a mail for notifying user about an issue update
def issue_edit(user, journal)
def issue_edit(user, journal, reason = nil)
issue = journal.journalized
redmine_headers 'Project' => issue.project.identifier,
'Issue-Tracker' => issue.tracker.name,
......
@issue = issue
@user = user
@journal = journal
@reason = reason
@journal_details = journal.visible_details
@issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue, :anchor => "change-#{journal.id}")
......
# Mailer.deliver_issue_edit(journal)
def self.deliver_issue_edit(journal)
users = journal.notified_users | journal.notified_watchers
users.select! do |user|
journal.notes? || journal.visible_details(user).any?
end
users.each do |user|
issue_edit(user, journal).deliver_later
end
notified_mentions = journal.journalized.notified_mentions | journal.notified_mentions
notified_mentions.each do |user|
issue_edit(user, journal, 'mentioned').deliver_later
end
end
# Builds a mail to user about a new document.
......
# Example:
# Mailer.deliver_news_added(news)
def self.deliver_news_added(news)
users = news.notified_users | news.notified_watchers_for_added_news
users = news.notified_users | news.notified_watchers_for_added_news | news.notified_mentions
users.each do |user|
news_added(user, news).deliver_later
end
......
# Example:
# Mailer.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|
wiki_content_added(user, wiki_content).deliver_later
end
......
users = wiki_content.notified_users
users |= wiki_content.page.notified_watchers
users |= wiki_content.page.wiki.notified_watchers
users |= wiki_content.notified_mentions
users.each do |user|
wiki_content_updated(user, wiki_content).deliver_later
app/models/wiki_content.rb
belongs_to :page, :class_name => 'WikiPage'
belongs_to :author, :class_name => 'User'
has_many :versions, :class_name => 'WikiContentVersion', :dependent => :delete_all
acts_as_mentionable :attributes => ['text']
validates_presence_of :text
validates_length_of :comments, :maximum => 1024, :allow_nil => true
app/views/documents/_form.html.erb
<p><%= 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 => '')
}
%></p>
app/views/issues/_edit.html.erb
<%= f.text_area :notes, :cols => 60, :rows => 10, :class => 'wiki-edit',
:data => {
:auto_complete => true,
:issues_url => auto_complete_issues_path(:project_id => @issue.project, :q => '')
:issues_url => auto_complete_issues_path(:project_id => @issue.project, :q => ''),
:users_url => auto_complete_users_path(:project_id => @issue.project, :q => '')
},
:no_label => true %>
<%= wikitoolbar_for 'issue_notes', preview_issue_path(:project_id => @project, :issue_id => @issue) %>
app/views/issues/_form.html.erb
:rows => [[10, @issue.description.to_s.length / 50].max, 20].min,
:data => {
:auto_complete => true,
:issues_url => auto_complete_issues_path(:project_id => @issue.project, :q => '')
:issues_url => auto_complete_issues_path(:project_id => @issue.project, :q => ''),
:users_url => auto_complete_users_path(:project_id => @issue.project, :q => '')
},
:no_label => true %>
<% end %>
app/views/issues/bulk_edit.html.erb
<%= text_area_tag 'notes', @notes, :cols => 60, :rows => 10, :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 'notes' %>
app/views/journals/_notes_form.html.erb
:rows => (@journal.notes.blank? ? 10 : [[10, @journal.notes.length / 50].max, 100].min),
: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 => '')
}
%>
<% if @journal.safe_attribute? 'private_notes' %>
app/views/mailer/issue_add.html.erb
<%= l(:text_issue_added, :id => link_to("##{@issue.id}", @issue_url), :author => h(@issue.author)).html_safe %>
<% unless @reason.nil? %>
You have been mentioned in it.
<% end %>
<hr />
<%= render :partial => 'issue', :formats => [:html], :locals => { :issue => @issue, :user => @user, :issue_url => @issue_url } %>
app/views/mailer/issue_edit.html.erb
(<%= 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 %>
<hr />
<ul class="journal details">
app/views/messages/_form.html.erb
: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 => '')
}
%></p>
<%= wikitoolbar_for 'message_content', preview_board_message_path(:board_id => @board, :id => @message) %>
app/views/news/_form.html.erb
<p><%= 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 => '')
}
%></p>
<p id="attachments_form"><label><%= l(:label_attachment_plural) %></label><%= render :partial => 'attachments/form', :locals => {:container => @news} %></p>
app/views/news/show.html.erb
<%= 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) %>
app/views/wiki/edit.html.erb
: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 => '')
}
%>
config/routes.rb
# 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'
lib/redmine.rb
end
require 'redmine/acts/positioned'
require 'redmine/acts/mentionable'
require 'redmine/scm/base'
require 'redmine/access_control'
lib/redmine/acts/mentionable.rb
# 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{<pre>(.*?)</pre>}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
public/javascripts/application.js
};
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);
test/functional/auto_completes_controller_test.rb
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 => {
......
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 => {
......
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 => {
......
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 => {
......
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 => {
......
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
......
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
(7-7/12)