Project

General

Profile

Feature #13919 » draft-acts_as_mentionable.patch

Patch developed based on 0001-Demo-for-acts_as_mentionable.patch - Mizuki ISHIKAWA, 2020-02-13 02:23

View differences:

app/helpers/application_helper.rb
name = h(user.name(options[:format]))
if user.active? || (User.current.admin? && user.logged?)
only_path = options[:only_path].nil? ? true : options[:only_path]
link_to name, user_url(user, :only_path => only_path), :class => user.css_classes
css_classes = options[:class] ? "#{user.css_classes} #{options[:class]}" : user.css_classes
link_to name, user_url(user, :only_path => only_path), :class => css_classes
else
name
end
......
if p = Project.visible.find_by_id(oid)
link = link_to_project(p, {:only_path => only_path}, :class => 'project')
end
when 'user'
u = User.visible.find_by(:id => oid, :type => 'User')
link = link_to_user(u, :only_path => only_path) if u
end
elsif sep == ':'
name = remove_double_quotes(identifier)
......
if p = Project.visible.where("identifier = :s OR LOWER(name) = :s", :s => name.downcase).first
link = link_to_project(p, {:only_path => only_path}, :class => 'project')
end
when 'user'
u = User.visible.find_by("LOWER(login) = :s AND type = 'User'", :s => name.downcase)
link = link_to_user(u, :only_path => only_path) if u
end
elsif sep == "@"
name = remove_double_quotes(identifier)
u = User.visible.find_by("LOWER(login) = :s AND type = 'User'", :s => name.downcase)
link = link_to_user(u, :only_path => only_path) if u
end
if link.nil? && $~
user = User.mentioned_user($~.named_captures.symbolize_keys)
if user
css_classes = (user.notify_mentioned_user?(obj) ? 'notified' : nil)
link = link_to_user(user, :only_path => only_path, :class => css_classes)
end
end
end
(leading + (link || "#{project_prefix}#{prefix}#{repo_prefix}#{sep}#{identifier}#{comment_suffix}"))
app/models/comment.rb
validates_presence_of :commented, :author, :content
acts_as_mentionable :attributes => ['content']
after_create_commit :send_notification
safe_attributes 'comments'
app/models/document.rb
end
def notified_users
project.notified_users.reject {|user| !visible?(user)}
project.notified_users.select {|user| user.allowed_to_view_notify_target?(self) }
end
private
app/models/issue.rb
acts_as_attachable :after_add => :attachment_added, :after_remove => :attachment_removed
acts_as_customizable
acts_as_watchable
acts_as_mentionable :attributes => ['description']
acts_as_searchable :columns => ['subject', "#{table_name}.description"],
:preload => [:project, :status, :tracker],
:scope => lambda {|options| options[:open_issues] ? self.open : self.all}
......
notified += project.users.preload(:preference).select(&:notify_about_high_priority_issues?) if priority.high?
notified.uniq!
# Remove users that can not view the issue
notified.reject! {|user| !visible?(user)}
notified
notified.select {|user| user.allowed_to_view_notify_target?(self)}
end
# Returns the email addresses that should be notified
app/models/journal.rb
has_many :details, :class_name => "JournalDetail", :dependent => :delete_all, :inverse_of => :journal
attr_accessor :indice
acts_as_mentionable :attributes => ['notes']
acts_as_event :title => Proc.new {|o| status = ((s = o.new_status) ? " (#{s})" : nil); "#{o.issue.tracker} ##{o.issue.id}#{status}: #{o.issue.subject}" },
:description => :notes,
:author => :user,
:group => :issue,
:type => Proc.new {|o| (s = o.new_status) ? (s.is_closed? ? 'issue-closed' : 'issue-edit') : 'issue-note' },
:url => Proc.new {|o| {:controller => 'issues', :action => 'show', :id => o.issue.id, :anchor => "change-#{o.id}"}}
acts_as_activity_provider :type => 'issues',
:author_key => :user_id,
:scope => preload({:issue => :project}, :user).
......
def notified_users
notified = journalized.notified_users
if private_notes?
notified = notified.select {|user| user.allowed_to?(:view_private_notes, journalized.project)}
end
notified
notified.select{ |u| u.allowed_to_view_notify_target?(self) }
end
def recipients
app/models/mailer.rb
# Mailer.deliver_issue_add(issue)
def self.deliver_issue_add(issue)
users = issue.notified_users | issue.notified_watchers
users -= issue.mentioned_users_with_latest_changes
users.each do |user|
issue_add(user, issue).deliver_later
end
......
users.select! do |user|
journal.notes? || journal.visible_details(user).any?
end
users -= journal.mentioned_users_with_latest_changes
users -= journal.issue.mentioned_users_with_latest_changes
users.each do |user|
issue_edit(user, journal).deliver_later
end
......
# Mailer.deliver_news_added(news)
def self.deliver_news_added(news)
users = news.notified_users | news.notified_watchers_for_added_news
users -= news.mentioned_users_with_latest_changes
users.each do |user|
news_added(user, news).deliver_later
end
......
def self.deliver_news_comment_added(comment)
news = comment.commented
users = news.notified_users | news.notified_watchers
users -= comment.mentioned_users_with_latest_changes
users.each do |user|
news_comment_added(user, comment).deliver_later
end
......
users = message.notified_users
users |= message.root.notified_watchers
users |= message.board.notified_watchers
users -= message.mentioned_users_with_latest_changes
users.each do |user|
message_posted(user, message).deliver_later
......
end
end
def mail_to_mentioned_users(user, obj, contents)
@contents = contents
mail :to => user,
:subject => "You are mentioned by #{obj.try(:author) || obj.user} in #{obj.class}##{obj.id}"
end
# Notifies mentioned users.
#
# Example:
# Mailer.deliver_mail_to_mentioned_users(users, obj, content)
def self.deliver_mail_to_mentioned_users(users, obj, content)
users.each do |user|
mail_to_mentioned_users(user, obj, content).deliver_later
end
end
# Build a test email to user.
def test_email(user)
@url = url_for(:controller => 'welcome')
app/models/message.rb
end
def notified_users
project.notified_users.reject {|user| !visible?(user)}
project.notified_users.select {|user| user.allowed_to_view_notify_target?(self) }
end
private
app/models/news.rb
:delete_permission => :manage_news
acts_as_searchable :columns => ['title', 'summary', "#{table_name}.description"],
:preload => :project
acts_as_mentionable :attributes => ['description']
acts_as_event :url => Proc.new {|o| {:controller => 'news', :action => 'show', :id => o.id}}
acts_as_activity_provider :scope => preload(:project, :author),
:author_key => :author_id
......
end
def notified_users
project.users.select {|user| user.notify_about?(self) && user.allowed_to?(:view_news, project)}
project.users.select {|user| user.notify_about?(self) && user.allowed_to_view_notify_target?(self)}
end
def recipients
app/models/user.rb
RequestStore.store[:current_user] ||= User.anonymous
end
# Return the mentioned user to based on the match data
# of ApplicationHelper::LINKS_RE.
# user:jsmith -> Link to user with login jsmith
# @jsmith -> Link to user with login jsmith
# user#2 -> Link to user with id 2
def self.mentioned_user(match_data)
return nil if match_data[:esc]
sep = match_data[:sep1] || match_data[:sep2] || match_data[:sep3] || match_data[:sep4]
identifier = match_data[:identifier1] || match_data[:identifier2] || match_data[:identifier3]
prefix = match_data[:prefix]
if ['#', '##'].include?(sep) && prefix == 'user'
User.visible.find_by(:id => identifier.to_i, :type => 'User')
elsif sep == '@' || (sep == ':' && prefix == 'user')
name = identifier.gsub(%r{^"(.*)"$}, "\\1")
User.find_by_login(CGI.unescapeHTML(name).downcase)
end
end
# Return true if notify the mentioned user.
def notify_mentioned_user?(object)
self.active? &&
self.mail.present? &&
self.mail_notification.present? && self.mail_notification != 'none' &&
self.allowed_to_view_notify_target?(object)
end
# Return true if the user is allowed to view the notify target.
def allowed_to_view_notify_target?(object)
case object
when Journal
self.allowed_to_view_notify_target?(object.journalized) &&
(!object.private_notes? || self.allowed_to?(:view_private_notes, object.journalized.project))
when Comment
self.allowed_to_view_notify_target?(object.commented)
when nil
false
else
object.visible?(self)
end
end
# Returns the anonymous user. If the anonymous user does not exist, it is created. There can be only
# one anonymous user per database.
def self.anonymous
app/models/wiki_content.rb
end
def notified_users
project.notified_users.reject {|user| !visible?(user)}
project.notified_users.select {|user| user.allowed_to_view_notify_target?(self) }
end
# Returns the mail addresses of users that should be notified
app/views/mailer/mail_to_mentioned_users.html.erb
<% @contents.each do |key, content| %>
<p><%= textilizable content %></p>
<% end %>
app/views/mailer/mail_to_mentioned_users.text.erb
<% @contents.each do |key, content| %>
<%= textilizable content %>
<% end %>
lib/plugins/acts_as_mentionable/init.rb
# frozen_string_literal: true
# Include hook code here
require File.dirname(__FILE__) + '/lib/acts_as_mentionable'
ActiveRecord::Base.send(:include, Redmine::Acts::Mentionable)
lib/plugins/acts_as_mentionable/lib/acts_as_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 = {})
return if self.included_modules.include?(Redmine::Acts::Mentionable::InstanceMethods)
cattr_accessor :mentionable_attributes
self.mentionable_attributes = options[:attributes]
send :include, Redmine::Acts::Mentionable::InstanceMethods
before_save :was_new_record?
after_save :notify_mentioned_users
end
end
module InstanceMethods
def self.included(base)
base.extend ClassMethods
end
def was_new_record?
@was_new_record = self.new_record?
end
def notify_mentioned_users
attribute_values = mentionable_attributes.map{|attr| [attr, self.saved_changes[attr][1]] if self.saved_changes[attr] }.compact.to_h
users = mentioned_users_with_latest_changes
Mailer.deliver_mail_to_mentioned_users(users, self, attribute_values) if users.present?
end
def mentioned_users_with_latest_changes
changes = self.saved_changes
if @was_new_record
values = mentionable_attributes.map{|attr| changes[attr] && changes[attr][1] }.compact
users = mentioned_users(values)
else
new_values = mentionable_attributes.map{|attr| changes[attr] && changes[attr][1] }.compact
old_values = mentionable_attributes.map{|attr| changes[attr] && changes[attr][0] }.compact
users = mentioned_users(new_values) - mentioned_users(old_values)
end
users
end
def mentioned_users(values)
users = []
values.each do |value|
value.scan(ApplicationHelper::LINKS_RE) do |_|
target = User.mentioned_user($~.named_captures.symbolize_keys)
next if target.blank? || users.include?(target)
users << target if target.notify_mentioned_user?(self)
end
end
users.uniq
end
module ClassMethods
end
end
end
end
end
public/stylesheets/application.css
a, a:link, a:visited{ color: #169; text-decoration: none; }
a:hover, a:active{ color: #c61a1a; text-decoration: underline;}
a img{ border: 0; }
a.user.notified, a.user.notified:link, a.user.notified:visited {padding: 2px; border-radius: 3px; background-color: #bae9f5}
a.issue.closed, a.issue.closed:link, a.issue.closed:visited { color: #999; text-decoration: line-through; }
a.project.closed, a.project.closed:link, a.project.closed:visited { color: #999; }
(5-5/12)