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
extract shared code into module
ulfertsf6226524ddf1
diff --git a/app/models/journal_notification_mailer.rb b/app/services/notifications/journal_notification_service.rbsimilarity index 72%rename from app/models/journal_notification_mailer.rbrename to app/services/notifications/journal_notification_service.rbindex a2c033735af..94c829e3d95 100644--- a/app/models/journal_notification_mailer.rb+++ b/app/services/notifications/journal_notification_service.rb@@ -1,4 +1,5 @@#-- encoding: UTF-8+#-- copyright# OpenProject is a project management system.# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)@@ -27,18 +28,20 @@# See docs/COPYRIGHT.rdoc for more details.#++-class JournalNotificationMailer+class Notifications::JournalNotificationServiceclass << self- def distinguish_journals(journal, send_notification)- if send_notification- if journal.journable_type == 'WorkPackage'- handle_work_package_journal(journal)- end+ include Notifications::JournalNotifier++ def call(journal, send_mails)+ if journal.journable_type == 'WorkPackage'+ handle_work_package_journal(journal, send_mails)endend- def handle_work_package_journal(journal)- return nil unless send_notification? journal+ private++ def handle_work_package_journal(journal, send_mails)+ return nil unless send_notification?(journal) && send_mailsreturn nil unless ::UserMailer.perform_deliveriesaggregated = find_aggregated_journal_for(journal)@@ -46,12 +49,7 @@ class JournalNotificationMailer# Send the notification on behalf of the predecessor in case it could not send it on its ownif Journal::AggregatedJournal.hides_notifications?(aggregated, aggregated.predecessor)work_package = aggregated.predecessor.journable- notification_receivers(work_package).each do |recipient|- job = DeliverWorkPackageNotificationJob.new(aggregated.predecessor.id,- recipient.id,- User.current.id)- Delayed::Job.enqueue job, priority: ::ApplicationJob.priority_number(:notification)- end+ notify_journal_complete(work_package, aggregated.predecessor)endjob = EnqueueWorkPackageNotificationJob.new(journal.id, User.current.id)@@ -85,14 +83,5 @@ class JournalNotificationMailerdef delivery_timeSetting.journal_aggregation_time_minutes.to_i.minutes.from_nowend-- def find_aggregated_journal_for(raw_journal)- wp_journals = Journal::AggregatedJournal.aggregated_journals(journable: raw_journal.journable)- wp_journals.detect { |journal| journal.version == raw_journal.version }- end-- def notification_receivers(work_package)- (work_package.recipients + work_package.watcher_recipients).uniq- endendenddiff --git a/app/services/notifications/journal_notifier.rb b/app/services/notifications/journal_notifier.rbnew file mode 100644index 00000000000..775743d92fb--- /dev/null+++ b/app/services/notifications/journal_notifier.rb@@ -0,0 +1,97 @@+#-- 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.+#++++module Notifications::JournalNotifier+ private++ def find_aggregated_journal_for(raw_journal)+ wp_journals = Journal::AggregatedJournal.aggregated_journals(journable: raw_journal.journable)+ wp_journals.detect { |journal| journal.version == raw_journal.version }+ end++ def notify_journal_complete(work_package, journal)+ notification_receivers(work_package, journal).each do |recipient|+ job = DeliverWorkPackageNotificationJob.new(journal.id,+ recipient.id,+ User.current.id)+ Delayed::Job.enqueue job, priority: ::ApplicationJob.priority_number(:notification)+ end++ OpenProject::Notifications.send(+ OpenProject::Events::AGGREGATED_WORK_PACKAGE_JOURNAL_READY,+ journal_id: journal.id,+ initial: journal.initial?+ )+ end++ def notification_receivers(work_package, journal)+ (work_package.recipients + work_package.watcher_recipients + mentioned(work_package, journal)).uniq+ end++ def mentioned(work_package, journal)+ mentioned_ids(journal)+ .where(id: User.allowed(:view_work_packages, work_package.project))+ .where.not(mail_notification: User::USER_MAIL_OPTION_NON.first)+ end++ def text_for_mentions(journal)+ potential_text = ""+ potential_text << journal.notes if journal.try(:notes)++ %i[description subject].each do |field|+ if journal.details[field].try(:any?)+ from = journal.details[field].first+ to = journal.details[field].second+ potential_text << "\n" + Redmine::Helpers::Diff.new(to, from).additions.join(' ')+ end+ end+ potential_text+ end++ def mentioned_ids(journal)+ text = text_for_mentions(journal)+ user_ids, user_login_names, group_ids = text+ .scan(/\b(?:user#([\d]+))|(?:user:"(.+?)")|(?:group#([\d]+))\b/)+ .transpose+ .each(&:compact!)++ base_scope = User+ .includes(:groups)+ .references(:groups_users)++ by_id = base_scope.where(id: user_ids || [])+ by_login = base_scope.where(login: user_login_names || [])+ by_group = base_scope.where(groups_users: { id: group_ids || [] })++ by_id+ .or(by_login)+ .or(by_group)+ end+enddiff --git a/app/workers/enqueue_work_package_notification_job.rb b/app/workers/enqueue_work_package_notification_job.rbindex 6a439c44fe8..811a78cc919 100644--- a/app/workers/enqueue_work_package_notification_job.rb+++ b/app/workers/enqueue_work_package_notification_job.rb@@ -29,6 +29,8 @@# Enqueuesclass EnqueueWorkPackageNotificationJob < ApplicationJob+ include Notifications::JournalNotifier+def initialize(journal_id, author_id)@journal_id = journal_id@author_id = author_id@@ -37,44 +39,27 @@ class EnqueueWorkPackageNotificationJob < ApplicationJobdef perform# if the WP has been deleted the unaggregated journal will have been deleted too# and our job here is done- return nil unless raw_journal+ return unless raw_journal- @journal = find_aggregated_journal+ journal = find_aggregated_journal_for(raw_journal)# If we can't find the aggregated journal, it was superseded by a journal that aggregated ours.# In that case a job for the new journal will have been enqueued that is now responsible for# sending the notification. Our job here is done.- return nil unless @journal-- @author = User.find_by(id: @author_id) || DeletedUser.first+ return unless journal# Do not deliver notifications if a follow-up journal will already have sent a notification# on behalf of this job.- unless Journal::AggregatedJournal.hides_notifications?(@journal.successor, @journal)- deliver_notifications_for(@journal)+ return if Journal::AggregatedJournal.hides_notifications?(journal.successor, journal)++ author = User.find_by(id: @author_id) || DeletedUser.first+ User.execute_as(author) do+ notify_journal_complete(work_package, journal)endendprivate- def find_aggregated_journal- wp_journals = Journal::AggregatedJournal.aggregated_journals(journable: work_package)- wp_journals.detect { |journal| journal.version == raw_journal.version }- end-- def deliver_notifications_for(journal)- notification_receivers(work_package).each do |recipient|- job = DeliverWorkPackageNotificationJob.new(journal.id, recipient.id, @author_id)- Delayed::Job.enqueue job, priority: ::ApplicationJob.priority_number(:notification)- end-- OpenProject::Notifications.send(- OpenProject::Events::AGGREGATED_WORK_PACKAGE_JOURNAL_READY,- journal_id: journal.id,- initial: journal.initial?- )- end-def raw_journal@raw_journal ||= Journal.find_by(id: @journal_id)end@@ -82,48 +67,4 @@ class EnqueueWorkPackageNotificationJob < ApplicationJobdef work_package@work_package ||= raw_journal.journableend-- def notification_receivers(work_package)- (work_package.recipients + work_package.watcher_recipients + mentioned).uniq- end-- def mentioned- mentioned_ids- .where(id: User.allowed(:view_work_packages, @work_package.project))- .where.not(mail_notification: User::USER_MAIL_OPTION_NON.first)- end-- def text_for_mentions- potential_text = ""- potential_text << @journal.notes if @journal.try(:notes)-- %i[description subject].each do |field|- if @journal.details[field].try(:any?)- from = @journal.details[field].first- to = @journal.details[field].second- potential_text << "\n" + Redmine::Helpers::Diff.new(to, from).additions.join(' ')- end- end- potential_text- end-- def mentioned_ids- text = text_for_mentions- user_ids, user_login_names, group_ids = text- .scan(/\b(?:user#([\d]+))|(?:user:"(.+?)")|(?:group#([\d]+))\b/)- .transpose- .each(&:compact!)-- base_scope = User- .includes(:groups)- .references(:groups_users)-- by_id = base_scope.where(id: user_ids || [])- by_login = base_scope.where(login: user_login_names || [])- by_group = base_scope.where(groups_users: { id: group_ids || [] })-- by_id- .or(by_login)- .or(by_group)- endenddiff --git a/config/initializers/subscribe_listeners.rb b/config/initializers/subscribe_listeners.rbindex ee705a4f50f..735fea4c523 100644--- a/config/initializers/subscribe_listeners.rb+++ b/config/initializers/subscribe_listeners.rb@@ -28,7 +28,7 @@#++OpenProject::Notifications.subscribe('journal_created') do |payload|- JournalNotificationMailer.distinguish_journals(payload[:journal], payload[:send_notification])+ Notifications::JournalNotificationService.call(payload[:journal], payload[:send_notification])endOpenProject::Notifications.subscribe('watcher_added') do |payload|diff --git a/spec/models/journal_notification_mailer_spec.rb b/spec/services/notifications/journal_notification_service_spec.rbsimilarity index 91%rename from spec/models/journal_notification_mailer_spec.rbrename to spec/services/notifications/journal_notification_service_spec.rbindex 6154e4c0ff8..69465a4814c 100644--- a/spec/models/journal_notification_mailer_spec.rb+++ b/spec/services/notifications/journal_notification_service_spec.rb@@ -28,25 +28,25 @@#++require 'spec_helper'-describe JournalNotificationMailer do+describe Notifications::JournalNotificationService dolet(:project) { FactoryBot.create(:project_with_types) }let(:user) doFactoryBot.build(:user,- mail_notification: 'all',- member_in_project: project)+ mail_notification: 'all',+ member_in_project: project)endlet(:work_package) doFactoryBot.create(:work_package,- project: project,- author: user,- type: project.types.first)+ project: project,+ author: user,+ type: project.types.first)endlet(:journal) { work_package.journals.last }let(:send_notification) { true }let(:notifications) { [] }def call_listener- described_class.distinguish_journals(journal, send_notification)+ described_class.call(journal, send_notification)endbefore do@@ -273,9 +273,18 @@ describe JournalNotificationMailer doendit 'immediately delivers a mail on behalf of Journal 1' do- expect(Delayed::Job).to receive(:enqueue)- .with(- an_instance_of(DeliverWorkPackageNotificationJob), priority: anything)+ expect(Delayed::Job)+ .to receive(:enqueue)+ .with(an_instance_of(DeliverWorkPackageNotificationJob), priority: anything)+ call_listener+ end++ it 'immediately notifies on behalf of journal 1' do+ expect(OpenProject::Notifications)+ .to receive(:send)+ .with(OpenProject::Events::AGGREGATED_WORK_PACKAGE_JOURNAL_READY,+ journal_id: journal_1.id,+ initial: false)call_listenerend@@ -284,7 +293,8 @@ describe JournalNotificationMailer do.with(an_instance_of(EnqueueWorkPackageNotificationJob),priority: anything,- run_at: anything)+ run_at: anything+ )call_listenerendend@@ -325,7 +335,7 @@ enddescribe 'initialization' doit 'subscribes the listener' do- expect(JournalNotificationMailer).to receive(:distinguish_journals)+ expect(Notifications::JournalNotificationService).to receive(:call)FactoryBot.create(:work_package)endenddiff --git a/spec/workers/mail_notification_jobs/enqueue_work_package_notification_job_spec.rb b/spec/workers/mail_notification_jobs/enqueue_work_package_notification_job_spec.rbindex 077a8e1c5c0..d52499959bc 100644--- a/spec/workers/mail_notification_jobs/enqueue_work_package_notification_job_spec.rb+++ b/spec/workers/mail_notification_jobs/enqueue_work_package_notification_job_spec.rb@@ -38,9 +38,9 @@ describe EnqueueWorkPackageNotificationJob, type: :model dolet(:author) { FactoryBot.create(:user, login: "marktwain") }let(:work_package) doFactoryBot.create(:work_package,- project: project,- author: author,- assigned_to: recipient)+ project: project,+ author: author,+ assigned_to: recipient)endlet(:journal) { work_package.journals.first }subject { described_class.new(journal.id, author.id) }@@ -222,13 +222,14 @@ describe EnqueueWorkPackageNotificationJob, type: :model dodescribe "#text_for_mentions" doit "returns a text" dosubject.perform- expect(subject.send(:text_for_mentions)).to be_a String+ expect(subject.send(:text_for_mentions, journal)).to be_a Stringendcontext "subject and description changed" dolet(:title) { 'New subject' }let(:description) { 'New description' }let(:notes) { 'Nice notes!' }+ let(:journal) { work_package.journals.last }subject { described_class.new(work_package.journals.last.id, author.id) }@@ -244,10 +245,10 @@ describe EnqueueWorkPackageNotificationJob, type: :model doendit "returns notes, subject and description" do- expect(subject.send(:text_for_mentions)).not_to be_blank- expect(subject.send(:text_for_mentions)).to match title- expect(subject.send(:text_for_mentions)).to match description- expect(subject.send(:text_for_mentions)).to match notes+ expect(subject.send(:text_for_mentions, journal)).not_to be_blank+ expect(subject.send(:text_for_mentions, journal)).to match title+ expect(subject.send(:text_for_mentions, journal)).to match description+ expect(subject.send(:text_for_mentions, journal)).to match notesendendend@@ -261,7 +262,7 @@ describe EnqueueWorkPackageNotificationJob, type: :model do.to receive(:text_for_mentions).and_return(added_text)- instance.send(:mentioned)+ instance.send(:mentioned, work_package, journal)endcontext 'for users' do@@ -279,9 +280,9 @@ describe EnqueueWorkPackageNotificationJob, type: :model docontext "that is an email address" dolet(:recipient) doFactoryBot.create(:user,- member_in_project: project,- member_through_role: role,- login: "[email protected]")+ member_in_project: project,+ member_through_role: role,+ login: "[email protected]")endit "detects the user" do@@ -303,9 +304,9 @@ describe EnqueueWorkPackageNotificationJob, type: :model docontext "the recipient turned off all mail notifications" dolet(:recipient) doFactoryBot.create(:user,- member_in_project: project,- member_through_role: role,- mail_notification: 'none')+ member_in_project: project,+ member_through_role: role,+ mail_notification: 'none')endlet(:added_text) do@@ -322,7 +323,7 @@ describe EnqueueWorkPackageNotificationJob, type: :model docontext "mentioned user is not allowed to view the work package" dolet(:recipient) doFactoryBot.create(:user,- login: "[email protected]")+ login: "[email protected]")endlet(:added_text) do"Hello user:#{recipient.login}, hey user##{recipient.id}"@@ -343,9 +344,9 @@ describe EnqueueWorkPackageNotificationJob, type: :model dogroup.users << group_memberFactoryBot.create(:member,- project: project,- principal: group,- roles: [role])+ project: project,+ principal: group,+ roles: [role])endend@@ -382,9 +383,9 @@ describe EnqueueWorkPackageNotificationJob, type: :model docontext 'but group member is allowed individually' dobefore doFactoryBot.create(:member,- project: project,- principal: group_member,- roles: [FactoryBot.create(:role, permissions: [:view_work_packages])])+ project: project,+ principal: group_member,+ roles: [FactoryBot.create(:role, permissions: [:view_work_packages])])endit "group member gets detected" do