OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Send email on watcher being removed
Yegor Tovstikee2762e8ef24
diff --git a/app/mailers/user_mailer.rb b/app/mailers/user_mailer.rbindex f90bc0ddb1e..29e801f11bc 100644--- a/app/mailers/user_mailer.rb+++ b/app/mailers/user_mailer.rb@@ -89,6 +89,21 @@ class UserMailer < BaseMailerendend+ def work_package_watcher_removed(work_package, user, watcher_remover)+ User.execute_as user do+ @issue = work_package+ @watcher_remover = watcher_remover++ set_work_package_headers(work_package)+ message_id work_package, user+ references work_package, user++ with_locale_for(user) do+ mail to: user.mail, subject: subject_for_work_package(work_package)+ end+ end+ end+def password_lost(token)return unless token.user # token's can have no userdiff --git a/app/models/watcher_added_notification_mailer.rb b/app/models/watcher_added_notification_mailer.rbnew file mode 100644index 00000000000..43f97fcf9a4--- /dev/null+++ b/app/models/watcher_added_notification_mailer.rb@@ -0,0 +1,39 @@+#-- encoding: UTF-8+#-- copyright+# OpenProject is a project management system.+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2017 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# 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.+#+# See docs/COPYRIGHT.rdoc for more details.+#++++class WatcherAddedNotificationMailer < WatcherNotificationMailer+ class << self+ private++ def perform_notification_job(watcher, watcher_changer)+ DeliverWatcherAddedNotificationJob.+ perform_later(watcher.id, watcher.user.id, watcher_changer.id)+ end+ end+enddiff --git a/app/models/watcher_notification_mailer.rb b/app/models/watcher_notification_mailer.rbindex 64df749f223..a45a9da6f54 100644--- a/app/models/watcher_notification_mailer.rb+++ b/app/models/watcher_notification_mailer.rb@@ -29,18 +29,22 @@class WatcherNotificationMailerclass << self- def handle_watcher(watcher, watcher_setter)+ def handle_watcher(watcher, watcher_changer)# We only handle this watcher setting if associated user wants to be notified# about it.- return unless notify_about_watcher_added?(watcher, watcher_setter)+ return unless notify_about_watcher_changed?(watcher, watcher_changer)unless other_jobs_queued?(watcher.watchable)- DeliverWatcherNotificationJob.perform_later(watcher.id, watcher.user.id, watcher_setter.id)+ perform_notification_job(watcher, watcher_changer)endendprivate+ def perform_notification_job(watcher, watcher_changer)+ raise NotImplementedError, 'Subclass has to implement #notification_job'+ end+# HACK: TODO this needs generalization as well as performance improvements# We need to make sure no work package created or updated job is queued to avoid sending two# mails in short succession.@@ -49,8 +53,8 @@ class WatcherNotificationMailer"%NotificationJob%journal_id: #{work_package.journals.last.id}%").exists?end- def notify_about_watcher_added?(watcher, watcher_setter)- return false if notify_about_self_watching?(watcher, watcher_setter)+ def notify_about_watcher_changed?(watcher, watcher_changer)+ return false if notify_about_self_watching?(watcher, watcher_changer)case watcher.user.mail_notificationwhen 'only_my_events'@@ -62,8 +66,8 @@ class WatcherNotificationMailerendend- def notify_about_self_watching?(watcher, watcher_setter)- watcher.user == watcher_setter && !watcher.user.pref.self_notified?+ def notify_about_self_watching?(watcher, watcher_changer)+ watcher.user == watcher_changer && !watcher.user.pref.self_notified?enddef watching_selected_includes_project?(watcher)diff --git a/app/models/watcher_removed_notification_mailer.rb b/app/models/watcher_removed_notification_mailer.rbnew file mode 100644index 00000000000..b6861a4437c--- /dev/null+++ b/app/models/watcher_removed_notification_mailer.rb@@ -0,0 +1,40 @@+#-- encoding: UTF-8+#-- copyright+# OpenProject is a project management system.+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2017 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# 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.+#+# See docs/COPYRIGHT.rdoc for more details.+#++++class WatcherRemovedNotificationMailer < WatcherNotificationMailer+ class << self+ private++ def perform_notification_job(watcher, watcher_changer)+ # As watcher is already destroyed we need to pass a hash+ DeliverWatcherRemovedNotificationJob.+ perform_later(watcher.attributes, watcher.user.id, watcher_changer.id)+ end+ end+enddiff --git a/app/views/user_mailer/work_package_watcher_removed.html.erb b/app/views/user_mailer/work_package_watcher_removed.html.erbnew file mode 100644index 00000000000..14a428323c4--- /dev/null+++ b/app/views/user_mailer/work_package_watcher_removed.html.erb@@ -0,0 +1,37 @@+<%#-- copyright+OpenProject is a project management system.+Copyright (C) 2012-2018 the OpenProject Foundation (OPF)++This program is free software; you can redistribute it and/or+modify it under the terms of the GNU General Public License version 3.++OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+Copyright (C) 2006-2017 Jean-Philippe Lang+Copyright (C) 2010-2013 the ChiliProject Team++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.++See docs/COPYRIGHT.rdoc for more details.++++#%>+<%= t(:text_work_package_watcher_removed, id: "##{@issue.id}", watcher_remover: @watcher_remover) %>+<hr />+<%= render partial: 'issue_details', locals: { issue: @issue } %>+<p>+ <%= format_text(t(:text_latest_note, note: last_issue_note(@issue)),+ only_path: false,+ object: @issue,+ project: @issue.project) %>+</p>diff --git a/app/views/user_mailer/work_package_watcher_removed.text.erb b/app/views/user_mailer/work_package_watcher_removed.text.erbnew file mode 100644index 00000000000..e0be3dd88d8--- /dev/null+++ b/app/views/user_mailer/work_package_watcher_removed.text.erb@@ -0,0 +1,34 @@+<%#-- copyright+OpenProject is a project management system.+Copyright (C) 2012-2018 the OpenProject Foundation (OPF)++This program is free software; you can redistribute it and/or+modify it under the terms of the GNU General Public License version 3.++OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+Copyright (C) 2006-2017 Jean-Philippe Lang+Copyright (C) 2010-2013 the ChiliProject Team++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.++See docs/COPYRIGHT.rdoc for more details.++++#%>++<%= t(:text_work_package_watcher_added, id: "##{@issue.id}", watcher_setter: @watcher_setter) %>++----------------------------------------+<%= render partial: 'issue_details', locals: { issue: @issue } %>+<%= t(:text_latest_note, note: last_issue_note(@issue)) %>diff --git a/app/workers/deliver_watcher_added_notification_job.rb b/app/workers/deliver_watcher_added_notification_job.rbnew file mode 100644index 00000000000..e7534a63400--- /dev/null+++ b/app/workers/deliver_watcher_added_notification_job.rb@@ -0,0 +1,36 @@+#-- encoding: UTF-8+#-- copyright+# OpenProject is a project management system.+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2017 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# 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.+#+# See docs/COPYRIGHT.rdoc for more details.+#++++class DeliverWatcherAddedNotificationJob < DeliverWatcherNotificationJob+ def render_mail(recipient:, sender:)+ return unless watcher++ UserMailer.work_package_watcher_added(watcher.watchable, recipient, sender)+ end+enddiff --git a/app/workers/deliver_watcher_notification_job.rb b/app/workers/deliver_watcher_notification_job.rbindex b06e5dc6087..5b62672903e 100644--- a/app/workers/deliver_watcher_notification_job.rb+++ b/app/workers/deliver_watcher_notification_job.rb@@ -28,17 +28,14 @@#++class DeliverWatcherNotificationJob < DeliverNotificationJob-- def perform(watcher_id, recipient_id, watcher_setter_id)+ def perform(watcher_id, recipient_id, watcher_changer_id)@watcher_id = watcher_id- super(recipient_id, watcher_setter_id)+ super(recipient_id, watcher_changer_id)enddef render_mail(recipient:, sender:)- return nil unless watcher-- UserMailer.work_package_watcher_added(watcher.watchable, recipient, sender)+ raise NotImplementedError, 'Subclass has to implement #render_mail'endprivatediff --git a/app/workers/deliver_watcher_removed_notification_job.rb b/app/workers/deliver_watcher_removed_notification_job.rbnew file mode 100644index 00000000000..37b61b2b36e--- /dev/null+++ b/app/workers/deliver_watcher_removed_notification_job.rb@@ -0,0 +1,43 @@+#-- encoding: UTF-8+#-- copyright+# OpenProject is a project management system.+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2017 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# 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.+#+# See docs/COPYRIGHT.rdoc for more details.+#++++class DeliverWatcherRemovedNotificationJob < DeliverWatcherNotificationJob+ # As watcher is already destroyed we need to pass a hash+ def perform(watcher_attributes, recipient_id, watcher_remover_id)+ @watcher = Watcher.new(watcher_attributes)++ super(watcher.id, recipient_id, watcher_remover_id)+ end++ def render_mail(recipient:, sender:)+ return unless watcher++ UserMailer.work_package_watcher_removed(watcher.watchable, recipient, sender)+ end+enddiff --git a/config/initializers/subscribe_listeners.rb b/config/initializers/subscribe_listeners.rbindex 362fa8992c9..1ea3163da15 100644--- a/config/initializers/subscribe_listeners.rb+++ b/config/initializers/subscribe_listeners.rb@@ -37,5 +37,9 @@ OpenProject::Notifications.subscribe(OpenProject::Events::AGGREGATED_WORK_PACKAGendOpenProject::Notifications.subscribe('watcher_added') do |payload|- WatcherNotificationMailer.handle_watcher(payload[:watcher], payload[:watcher_setter])+ WatcherAddedNotificationMailer.handle_watcher(payload[:watcher], payload[:watcher_setter])+end++OpenProject::Notifications.subscribe('watcher_removed') do |payload|+ WatcherRemovedNotificationMailer.handle_watcher(payload[:watcher], payload[:watcher_remover])enddiff --git a/config/locales/en.yml b/config/locales/en.ymlindex 13483bf5d62..99b042b5188 100644--- a/config/locales/en.yml+++ b/config/locales/en.yml@@ -2316,6 +2316,7 @@ en:text_work_package_category_reassign_to: "Reassign work packages to this category"text_work_package_updated: "Work package %{id} has been updated by %{author}."text_work_package_watcher_added: "You have been added as a watcher to Work package %{id} by %{watcher_setter}."+ text_work_package_watcher_removed: "You have been removed from watchers of Work package %{id} by %{watcher_remover}."text_work_packages_destroy_confirmation: "Are you sure you want to delete the selected work package(s)?"text_work_packages_ref_in_commit_messages: "Referencing and fixing work packages in commit messages"text_journal_added: "%{label} %{value} added"diff --git a/lib/services/remove_watcher.rb b/lib/services/remove_watcher.rbindex c2cd8348686..e52dccce45f 100644--- a/lib/services/remove_watcher.rb+++ b/lib/services/remove_watcher.rb@@ -34,8 +34,12 @@ class Services::RemoveWatcherdef run(success: -> {}, failure: -> {})if @work_package.watcher_users.include?(@user)+ @watcher = @work_package.watchers.find_by_user_id(@user.id)@work_package.watcher_users.delete(@user)success.call+ OpenProject::Notifications.send('watcher_removed',+ watcher: @watcher,+ watcher_remover: User.current)elsefailure.callenddiff --git a/spec/mailers/user_mailer_spec.rb b/spec/mailers/user_mailer_spec.rbindex 6df6ef44c32..2023c07a733 100644--- a/spec/mailers/user_mailer_spec.rb+++ b/spec/mailers/user_mailer_spec.rb@@ -143,6 +143,19 @@ describe UserMailer, type: :mailer doendend+ describe '#work_package_watcher_removed' do+ let(:watcher_remover) { user }+ before do+ UserMailer.work_package_watcher_removed(work_package, recipient, watcher_remover).deliver_now+ end++ it_behaves_like 'mail is sent'++ it 'contains the WP subject in the mail subject' do+ expect(ActionMailer::Base.deliveries.first.subject).to include(work_package.subject)+ end+ end+describe :wiki_content_added dolet(:wiki_content) { FactoryBot.create(:wiki_content) }diff --git a/spec/models/watcher_notification_mailer_spec.rb b/spec/models/watcher_notification_mailer_spec.rbindex be27098e0f1..5de49260fbf 100644--- a/spec/models/watcher_notification_mailer_spec.rb+++ b/spec/models/watcher_notification_mailer_spec.rb@@ -28,9 +28,11 @@#++require 'spec_helper'-describe WatcherNotificationMailer do- def call_listener(watcher, watcher_setter)- described_class.handle_watcher(watcher, watcher_setter)+shared_examples 'WatcherNotificationMailer' do |watcher_notification_job|+ let(:watcher_notification_job) { watcher_notification_job }++ def call_listener(watcher, watcher_changer)+ described_class.handle_watcher(watcher, watcher_changer)endbefore do@@ -47,7 +49,7 @@ describe WatcherNotificationMailer dowork_package}- let(:watcher_setter) do+ let(:watcher_changer) doFactoryBot.build_stubbed(:user,mail_notification: watching_setting,preference: user_pref)@@ -81,8 +83,8 @@ describe WatcherNotificationMailer dolet(:self_notified) { true }it 'notifies the watcher' do- expect(DeliverWatcherNotificationJob).to receive(:perform_later)- call_listener(watcher, watcher_setter)+ expect(watcher_notification_job).to receive(:perform_later)+ call_listener(watcher, watcher_changer)endend@@ -91,30 +93,30 @@ describe WatcherNotificationMailer dolet(:self_notified) { false }it 'notifies the watcher' do- expect(DeliverWatcherNotificationJob).to receive(:perform_later)- call_listener(watcher, watcher_setter)+ expect(watcher_notification_job).to receive(:perform_later)+ call_listener(watcher, watcher_changer)endendcontext 'but when watcher is added by theirselfand has self_notified deactivated' do- let(:watching_user) { watcher_setter }+ let(:watching_user) { watcher_changer }let(:self_notified) { false }it 'does not notify the watcher' do- expect(DeliverWatcherNotificationJob).not_to receive(:perform_later)- call_listener(watcher, watcher_setter)+ expect(watcher_notification_job).not_to receive(:perform_later)+ call_listener(watcher, watcher_changer)endendcontext 'but when watcher is added by theirselfand has self_notified activated' do- let(:watching_user) { watcher_setter }+ let(:watching_user) { watcher_changer }let(:self_notified) { true }it 'notifies the watcher' do- expect(DeliverWatcherNotificationJob).to receive(:perform_later)- call_listener(watcher, watcher_setter)+ expect(watcher_notification_job).to receive(:perform_later)+ call_listener(watcher, watcher_changer)endendend@@ -124,18 +126,18 @@ describe WatcherNotificationMailer docontext 'when added by a different user' doit 'does not notify the watcher' do- expect(DeliverWatcherNotificationJob).not_to receive(:perform_later)- call_listener(watcher, watcher_setter)+ expect(watcher_notification_job).not_to receive(:perform_later)+ call_listener(watcher, watcher_changer)endendcontext 'when watcher is added by theirself' do- let(:watching_user) { watcher_setter }+ let(:watching_user) { watcher_changer }let(:self_notified) { false }it 'does not notify the watcher' do- expect(DeliverWatcherNotificationJob).not_to receive(:perform_later)- call_listener(watcher, watcher_setter)+ expect(watcher_notification_job).not_to receive(:perform_later)+ call_listener(watcher, watcher_changer)endendend@@ -153,7 +155,7 @@ describe WatcherNotificationMailer doendit_behaves_like 'does not notify the added watcher for', 'only_owner' dobefore do- work_package.author = watcher_setter+ work_package.author = watcher_changerendend@@ -164,7 +166,7 @@ describe WatcherNotificationMailer doendit_behaves_like 'does not notify the added watcher for', 'only_assigned' dobefore do- work_package.assigned_to = watcher_setter+ work_package.assigned_to = watcher_changerendend@@ -190,3 +192,11 @@ describe WatcherNotificationMailer doendendend++describe WatcherRemovedNotificationMailer do+ include_examples "WatcherNotificationMailer", DeliverWatcherRemovedNotificationJob+end++describe WatcherAddedNotificationMailer do+ include_examples "WatcherNotificationMailer", DeliverWatcherAddedNotificationJob+enddiff --git a/spec/workers/mail_notification_jobs/deliver_watcher_notification_job_spec.rb b/spec/workers/mail_notification_jobs/deliver_watcher_notification_job_spec.rbindex 669a1f00eae..bdaccdd5b03 100644--- a/spec/workers/mail_notification_jobs/deliver_watcher_notification_job_spec.rb+++ b/spec/workers/mail_notification_jobs/deliver_watcher_notification_job_spec.rb@@ -29,28 +29,29 @@require 'spec_helper'-describe DeliverWatcherNotificationJob, type: :model do+shared_examples "DeliverWatcherNotificationJob" do |mailer_method_name|+ let(:mailer_method_name) { mailer_method_name }let(:project) { FactoryBot.create(:project) }let(:role) { FactoryBot.create(:role, permissions: [:view_work_packages]) }- let(:watcher_setter) { FactoryBot.create(:user) }+ let(:watcher_changer) { FactoryBot.create(:user) }let(:watcher_user) doFactoryBot.create(:user, member_in_project: project, member_through_role: role)endlet(:work_package) { FactoryBot.build(:work_package, project: project) }let(:watcher) { FactoryBot.create(:watcher, watchable: work_package, user: watcher_user) }- subject { described_class.new.perform(watcher.id, watcher_user.id, watcher_setter.id) }+ subject { described_class.new.perform(watcher_parameter, watcher_user.id, watcher_changer.id) }before do# make sure no actual calls make it into the UserMailer- allow(UserMailer).to receive(:work_package_watcher_added)+ allow(UserMailer).to receive(mailer_method_name).and_return(double('mail', deliver_now: nil))endit 'sends a mail' do- expect(UserMailer).to receive(:work_package_watcher_added).with(work_package,- watcher_user,- watcher_setter)+ expect(UserMailer).to receive(mailer_method_name).with(work_package,+ watcher_user,+ watcher_changer)subjectend@@ -59,7 +60,7 @@ describe DeliverWatcherNotificationJob, type: :model dobefore domail = double('mail')allow(mail).to receive(:deliver_now).and_raise(SocketError)- expect(UserMailer).to receive(:work_package_watcher_added).and_return(mail)+ expect(UserMailer).to receive(mailer_method_name).and_return(mail)endit 'raises the error' do@@ -68,3 +69,15 @@ describe DeliverWatcherNotificationJob, type: :model doendendend++describe DeliverWatcherRemovedNotificationJob, type: :model do+ include_examples "DeliverWatcherNotificationJob", :work_package_watcher_removed do+ let(:watcher_parameter) { watcher.attributes }+ end+end++describe DeliverWatcherAddedNotificationJob, type: :model do+ include_examples "DeliverWatcherNotificationJob", :work_package_watcher_added do+ let(:watcher_parameter) { watcher }+ end+end