Skip to content

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

extract shared code into module

ulfertsf6226524ddf1
diff --git a/app/models/journal_notification_mailer.rb b/app/services/notifications/journal_notification_service.rb
similarity index 72%
rename from app/models/journal_notification_mailer.rb
rename to app/services/notifications/journal_notification_service.rb
index 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::JournalNotificationService
class << 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)
end
end
- 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_mails
return nil unless ::UserMailer.perform_deliveries
aggregated = 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 own
if 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)
end
job = EnqueueWorkPackageNotificationJob.new(journal.id, User.current.id)
@@ -85,14 +83,5 @@ class JournalNotificationMailer
def delivery_time
Setting.journal_aggregation_time_minutes.to_i.minutes.from_now
end
-
- 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
- end
end
end
diff --git a/app/services/notifications/journal_notifier.rb b/app/services/notifications/journal_notifier.rb
new file mode 100644
index 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
+end
diff --git a/app/workers/enqueue_work_package_notification_job.rb b/app/workers/enqueue_work_package_notification_job.rb
index 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 @@
# Enqueues
class 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 < ApplicationJob
def 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)
end
end
private
- 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 < ApplicationJob
def work_package
@work_package ||= raw_journal.journable
end
-
- 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)
- end
end
diff --git a/config/initializers/subscribe_listeners.rb b/config/initializers/subscribe_listeners.rb
index 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])
end
OpenProject::Notifications.subscribe('watcher_added') do |payload|
diff --git a/spec/models/journal_notification_mailer_spec.rb b/spec/services/notifications/journal_notification_service_spec.rb
similarity index 91%
rename from spec/models/journal_notification_mailer_spec.rb
rename to spec/services/notifications/journal_notification_service_spec.rb
index 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 do
let(:project) { FactoryBot.create(:project_with_types) }
let(:user) do
FactoryBot.build(:user,
- mail_notification: 'all',
- member_in_project: project)
+ mail_notification: 'all',
+ member_in_project: project)
end
let(:work_package) do
FactoryBot.create(:work_package,
- project: project,
- author: user,
- type: project.types.first)
+ project: project,
+ author: user,
+ type: project.types.first)
end
let(: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)
end
before do
@@ -273,9 +273,18 @@ describe JournalNotificationMailer do
end
it '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_listener
end
@@ -284,7 +293,8 @@ describe JournalNotificationMailer do
.with(
an_instance_of(EnqueueWorkPackageNotificationJob),
priority: anything,
- run_at: anything)
+ run_at: anything
+ )
call_listener
end
end
@@ -325,7 +335,7 @@ end
describe 'initialization' do
it 'subscribes the listener' do
- expect(JournalNotificationMailer).to receive(:distinguish_journals)
+ expect(Notifications::JournalNotificationService).to receive(:call)
FactoryBot.create(:work_package)
end
end
diff --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.rb
index 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 do
let(:author) { FactoryBot.create(:user, login: "marktwain") }
let(:work_package) do
FactoryBot.create(:work_package,
- project: project,
- author: author,
- assigned_to: recipient)
+ project: project,
+ author: author,
+ assigned_to: recipient)
end
let(:journal) { work_package.journals.first }
subject { described_class.new(journal.id, author.id) }
@@ -222,13 +222,14 @@ describe EnqueueWorkPackageNotificationJob, type: :model do
describe "#text_for_mentions" do
it "returns a text" do
subject.perform
- expect(subject.send(:text_for_mentions)).to be_a String
+ expect(subject.send(:text_for_mentions, journal)).to be_a String
end
context "subject and description changed" do
let(: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 do
end
it "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 notes
end
end
end
@@ -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)
end
context 'for users' do
@@ -279,9 +280,9 @@ describe EnqueueWorkPackageNotificationJob, type: :model do
context "that is an email address" do
let(:recipient) do
FactoryBot.create(:user,
- member_in_project: project,
- member_through_role: role,
- login: "[email protected]")
+ member_in_project: project,
+ member_through_role: role,
+ login: "[email protected]")
end
it "detects the user" do
@@ -303,9 +304,9 @@ describe EnqueueWorkPackageNotificationJob, type: :model do
context "the recipient turned off all mail notifications" do
let(:recipient) do
FactoryBot.create(:user,
- member_in_project: project,
- member_through_role: role,
- mail_notification: 'none')
+ member_in_project: project,
+ member_through_role: role,
+ mail_notification: 'none')
end
let(:added_text) do
@@ -322,7 +323,7 @@ describe EnqueueWorkPackageNotificationJob, type: :model do
context "mentioned user is not allowed to view the work package" do
let(:recipient) do
FactoryBot.create(:user,
- login: "[email protected]")
+ login: "[email protected]")
end
let(:added_text) do
"Hello user:#{recipient.login}, hey user##{recipient.id}"
@@ -343,9 +344,9 @@ describe EnqueueWorkPackageNotificationJob, type: :model do
group.users << group_member
FactoryBot.create(:member,
- project: project,
- principal: group,
- roles: [role])
+ project: project,
+ principal: group,
+ roles: [role])
end
end
@@ -382,9 +383,9 @@ describe EnqueueWorkPackageNotificationJob, type: :model do
context 'but group member is allowed individually' do
before do
FactoryBot.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])])
end
it "group member gets detected" do