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

limit notification disabling to mail only

ulfertse86d757cb53b
diff --git a/app/services/notifications/journal_notification_service.rb b/app/services/notifications/journal_notification_service.rb
index a1eab12b3fd..07732f19ed7 100644
--- a/app/services/notifications/journal_notification_service.rb
+++ b/app/services/notifications/journal_notification_service.rb
@@ -41,52 +41,27 @@ class Notifications::JournalNotificationService
private
def handle_work_package_journal(journal, send_mails)
- return nil unless send_notification?(journal) && send_mails
- return nil unless ::UserMailer.perform_deliveries
-
- notify_for_predecessor(journal)
- enqueue_work_package_notification(journal)
+ notify_for_wp_predecessor(journal, send_mails)
+ enqueue_work_package_notification(journal, send_mails)
end
# Send the notification on behalf of the predecessor in case it could not send it on its own
- def notify_for_predecessor(journal)
+ def notify_for_wp_predecessor(journal, send_mails)
aggregated = find_aggregated_journal_for(journal)
if Journal::AggregatedJournal.hides_notifications?(aggregated, aggregated.predecessor)
work_package = aggregated.predecessor.journable
- notify_journal_complete(work_package, aggregated.predecessor)
+ notify_journal_complete(work_package, aggregated.predecessor, send_mails)
end
end
- def enqueue_work_package_notification(journal)
- job = EnqueueWorkPackageNotificationJob.new(journal.id, User.current.id)
+ def enqueue_work_package_notification(journal, send_mails)
+ job = EnqueueWorkPackageNotificationJob.new(journal.id, User.current.id, send_mails)
Delayed::Job.enqueue job,
run_at: delivery_time,
priority: ::ApplicationJob.priority_number(:notification)
end
- def send_notification?(journal)
- (Setting.notified_events.include?('work_package_added') && journal.initial?) ||
- (Setting.notified_events.include?('work_package_updated') && !journal.initial?) ||
- notify_for_notes?(journal) ||
- notify_for_status?(journal) ||
- notify_for_priority(journal)
- end
-
- def notify_for_notes?(journal)
- Setting.notified_events.include?('work_package_note_added') && journal.notes.present?
- end
-
- def notify_for_status?(journal)
- Setting.notified_events.include?('status_updated') &&
- journal.details.has_key?(:status_id)
- end
-
- def notify_for_priority(journal)
- Setting.notified_events.include?('work_package_priority_updated') &&
- journal.details.has_key?(:priority_id)
- end
-
def delivery_time
Setting.journal_aggregation_time_minutes.to_i.minutes.from_now
end
diff --git a/app/services/notifications/journal_notifier.rb b/app/services/notifications/journal_notifier.rb
index f280614162d..1c6a3ec5063 100644
--- a/app/services/notifications/journal_notifier.rb
+++ b/app/services/notifications/journal_notifier.rb
@@ -35,13 +35,15 @@ module Notifications::JournalNotifier
Journal::AggregatedJournal.with_version(raw_journal)
end
- def notify_journal_complete(work_package, journal)
- journal_complete_mail(work_package, journal)
+ def notify_journal_complete(work_package, journal, send_mails)
+ journal_complete_mail(work_package, journal, send_mails)
journal_complete_notification(journal)
end
- def journal_complete_mail(work_package, journal)
+ def journal_complete_mail(work_package, journal, send_mails)
+ return nil unless send_mail?(journal, send_mails)
+
notification_receivers(work_package, journal).each do |recipient|
job = DeliverWorkPackageNotificationJob.new(journal.id,
recipient.id,
@@ -101,4 +103,30 @@ module Notifications::JournalNotifier
.or(by_login)
.or(by_group)
end
+
+ def send_mail?(journal, send_mails)
+ send_mails && ::UserMailer.perform_deliveries && send_mail_setting?(journal)
+ end
+
+ def send_mail_setting?(journal)
+ (Setting.notified_events.include?('work_package_added') && journal.initial?) ||
+ (Setting.notified_events.include?('work_package_updated') && !journal.initial?) ||
+ notify_for_notes?(journal) ||
+ notify_for_status?(journal) ||
+ notify_for_priority(journal)
+ end
+
+ def notify_for_notes?(journal)
+ Setting.notified_events.include?('work_package_note_added') && journal.notes.present?
+ end
+
+ def notify_for_status?(journal)
+ Setting.notified_events.include?('status_updated') &&
+ journal.details.has_key?(:status_id)
+ end
+
+ def notify_for_priority(journal)
+ Setting.notified_events.include?('work_package_priority_updated') &&
+ journal.details.has_key?(:priority_id)
+ end
end
diff --git a/app/workers/enqueue_work_package_notification_job.rb b/app/workers/enqueue_work_package_notification_job.rb
index 811a78cc919..18ec4d55f8d 100644
--- a/app/workers/enqueue_work_package_notification_job.rb
+++ b/app/workers/enqueue_work_package_notification_job.rb
@@ -31,9 +31,10 @@
class EnqueueWorkPackageNotificationJob < ApplicationJob
include Notifications::JournalNotifier
- def initialize(journal_id, author_id)
+ def initialize(journal_id, author_id, send_mails)
@journal_id = journal_id
@author_id = author_id
+ @send_mails = send_mails
end
def perform
@@ -54,7 +55,7 @@ class EnqueueWorkPackageNotificationJob < ApplicationJob
author = User.find_by(id: @author_id) || DeletedUser.first
User.execute_as(author) do
- notify_journal_complete(work_package, journal)
+ notify_journal_complete(work_package, journal, @send_mails)
end
end
diff --git a/spec/services/notifications/journal_notification_service_spec.rb b/spec/services/notifications/journal_notification_service_spec.rb
index 69465a4814c..4ee6e256d16 100644
--- a/spec/services/notifications/journal_notification_service_spec.rb
+++ b/spec/services/notifications/journal_notification_service_spec.rb
@@ -42,11 +42,11 @@ describe Notifications::JournalNotificationService do
type: project.types.first)
end
let(:journal) { work_package.journals.last }
- let(:send_notification) { true }
+ let(:send_mails) { true }
let(:notifications) { [] }
def call_listener
- described_class.call(journal, send_notification)
+ described_class.call(journal, send_mails)
end
before do
@@ -61,14 +61,22 @@ describe Notifications::JournalNotificationService do
shared_examples_for 'enqueues a regular notification' do
it do
- expect(Delayed::Job).to receive(:enqueue)
- .with(
- an_instance_of(EnqueueWorkPackageNotificationJob),
- run_at: anything, priority: anything)
+ enqueue_job = double('enqueue job')
+
+ expect(EnqueueWorkPackageNotificationJob)
+ .to receive(:new)
+ .with(journal.id, user.id, send_mails)
+ .and_return(enqueue_job)
+ expect(Delayed::Job)
+ .to receive(:enqueue)
+ .with(enqueue_job,
+ run_at: anything,
+ priority: ::ApplicationJob.priority_number(:notification))
# immediate delivery is not part of regular notfications, it only covers an edge-case
- expect(Delayed::Job).not_to receive(:enqueue)
- .with(an_instance_of(DeliverWorkPackageNotificationJob), anything)
+ expect(Delayed::Job)
+ .not_to receive(:enqueue)
+ .with(an_instance_of(DeliverWorkPackageNotificationJob), anything)
call_listener
end
end
@@ -78,31 +86,10 @@ describe Notifications::JournalNotificationService do
let(:notifications) { [notification_setting] }
it_behaves_like 'enqueues a regular notification'
-
- context 'insufficient work package changes' do
- let(:journal) { another_work_package.journals.last }
- let(:another_work_package) do
- FactoryBot.create(:work_package,
- project: project,
- author: user,
- type: project.types.first)
- end
- before do
- another_work_package.add_journal(user)
- another_work_package.description = 'needs more changes'
- another_work_package.save!(validate: false)
- end
-
- it 'sends no notification' do
- expect(Delayed::Job).not_to receive(:enqueue)
- call_listener
- end
- end
end
- it 'sends no notification' do
- expect(Delayed::Job).not_to receive(:enqueue)
- call_listener
+ context 'with setting specified to not send mails' do
+ it_behaves_like 'enqueues a regular notification'
end
end
@@ -126,20 +113,6 @@ describe Notifications::JournalNotificationService do
let(:notifications) { ['work_package_updated'] }
it_behaves_like 'enqueues a regular notification'
-
- context 'WP creation' do
- let(:journal) { FactoryBot.create(:work_package).journals.first }
-
- it 'sends no notification' do
- expect(Delayed::Job).not_to receive(:enqueue)
- call_listener
- end
- end
- end
-
- it 'sends no notification' do
- expect(Delayed::Job).not_to receive(:enqueue)
- call_listener
end
end
@@ -171,15 +144,6 @@ describe Notifications::JournalNotificationService do
it_behaves_like 'handles deliveries', 'work_package_priority_updated'
end
-
- context 'send_notification disabled' do
- let(:send_notification) { false }
-
- it 'sends no notification' do
- expect(Delayed::Job).not_to receive(:enqueue)
- call_listener
- end
- end
end
describe 'mail suppressing aggregation' do
@@ -272,10 +236,17 @@ describe Notifications::JournalNotificationService do
journal_3.update_attribute(:created_at, journal_1.created_at + timeout + 5.seconds)
end
- it 'immediately delivers a mail on behalf of Journal 1' do
+ it 'immediately delivers a mail on behalf of journal 1' do
+ deliver_job = double('deliver job')
+
+ expect(DeliverWorkPackageNotificationJob)
+ .to receive(:new)
+ .with(journal_1.id, user.id, user.id)
+ .and_return(deliver_job)
+
expect(Delayed::Job)
.to receive(:enqueue)
- .with(an_instance_of(DeliverWorkPackageNotificationJob), priority: anything)
+ .with(deliver_job, priority: anything)
call_listener
end
@@ -288,15 +259,87 @@ describe Notifications::JournalNotificationService do
call_listener
end
- it 'also enqueues a regular mail' do
- expect(Delayed::Job).to receive(:enqueue)
- .with(
- an_instance_of(EnqueueWorkPackageNotificationJob),
- priority: anything,
- run_at: anything
- )
+ it 'also enqueues a regular mail for journal 3' do
+ enqueue_job = double('enqueue job')
+
+ expect(EnqueueWorkPackageNotificationJob)
+ .to receive(:new)
+ .with(journal_3.id, user.id, send_mails)
+ .and_return(enqueue_job)
+
+ expect(Delayed::Job)
+ .to receive(:enqueue)
+ .with(
+ enqueue_job,
+ priority: anything,
+ run_at: anything
+ )
call_listener
end
+
+ context 'with the settings not including updates' do
+ let(:notifications) { [] }
+
+ it 'does not deliver a mail on behalf of Journal 1' do
+ expect(DeliverWorkPackageNotificationJob)
+ .not_to receive(:new)
+ .with(journal_1.id, user.id, user.id)
+
+ 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
+
+ it 'also enqueues a regular mail' do
+ expect(Delayed::Job)
+ .to receive(:enqueue)
+ .with(
+ an_instance_of(EnqueueWorkPackageNotificationJob),
+ priority: anything,
+ run_at: anything
+ )
+ call_listener
+ end
+ end
+
+ context 'with send_mails = false provided' do
+ let(:send_mails) { false }
+
+ it 'does not deliver a mail on behalf of Journal 1' do
+ expect(DeliverWorkPackageNotificationJob)
+ .not_to receive(:new)
+ .with(journal_1.id, user.id, user.id)
+
+ 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
+
+ it 'also enqueues a regular mail' do
+ expect(Delayed::Job)
+ .to receive(:enqueue)
+ .with(
+ an_instance_of(EnqueueWorkPackageNotificationJob),
+ priority: anything,
+ run_at: anything
+ )
+ call_listener
+ end
+ end
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 d52499959bc..e2a53c5d0bf 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
@@ -42,41 +42,96 @@ describe EnqueueWorkPackageNotificationJob, type: :model do
author: author,
assigned_to: recipient)
end
- let(:journal) { work_package.journals.first }
- subject { described_class.new(journal.id, author.id) }
+ let(:journal) { journal_1 }
+ let(:journal_1) { work_package.journals.first }
+ let(:journal_2) do
+ work_package.add_journal author, 'something I have to say'
+ work_package.save(validate: false)
+ work_package.journals.last
+ end
+ let(:send_mail) { true }
+ let(:notification_setting) { %w(work_package_added work_package_updated work_package_note_added status_updated work_package_priority_updated) }
+ subject { described_class.new(journal.id, author.id, send_mail) }
before do
# make sure no other calls are made due to WP creation/update
allow(OpenProject::Notifications).to receive(:send) # ... and do nothing
+
+ allow(Setting).to receive(:notified_events).and_return(notification_setting)
end
- it 'sends a mail' do
- expect(Delayed::Job).to receive(:enqueue).with(an_instance_of(DeliverWorkPackageNotificationJob), any_args)
- subject.perform
+ shared_examples_for 'sends notification' do
+ it 'sends a notification' do
+ expect(OpenProject::Notifications)
+ .to receive(:send)
+ .with(OpenProject::Events::AGGREGATED_WORK_PACKAGE_JOURNAL_READY,
+ journal_id: journal.id,
+ initial: journal.initial?)
+
+ subject.perform
+ end
end
- context 'non-existant journal' do
- before do
- journal.destroy
+ shared_examples_for 'sends no notification' do
+ it 'sends no notification' do
+ expect(OpenProject::Notifications)
+ .not_to receive(:send)
+
+ subject.perform
end
+ end
+
+ shared_examples_for 'sends mail' do
+ let(:sender) { author }
+ it 'sends a mail' do
+ deliver_job = double('deliver job')
+
+ expect(DeliverWorkPackageNotificationJob)
+ .to receive(:new)
+ .with(journal.id, recipient.id, sender.id)
+ .and_return(deliver_job)
+
+ expect(Delayed::Job)
+ .to receive(:enqueue)
+ .with(deliver_job, priority: anything)
+
+ subject.perform
+ end
+ end
+
+ shared_examples_for 'sends no mail' do
it 'sends no mail' do
expect(Delayed::Job).not_to receive(:enqueue)
subject.perform
end
end
+ it_behaves_like 'sends mail'
+
+ it_behaves_like 'sends notification'
+
+ context 'non-existant journal' do
+ before do
+ journal.destroy
+ end
+
+ it_behaves_like 'sends no mail'
+ it_behaves_like 'sends no notification'
+ end
+
context 'non-existant author' do
before do
+ allow(Delayed::Job).to receive(:enqueue)
+
author.destroy
end
- it 'sends a mail' do
- expect(Delayed::Job)
- .to receive(:enqueue)
- .with(an_instance_of(DeliverWorkPackageNotificationJob), any_args)
- subject.perform
+ it_behaves_like 'sends mail' do
+ let(:sender) { DeletedUser.first }
end
+
+ it_behaves_like 'sends notification'
end
context 'outdated journal' do
@@ -86,10 +141,26 @@ describe EnqueueWorkPackageNotificationJob, type: :model do
work_package.save!
end
- it 'does not send any mails' do
- expect(Delayed::Job).not_to receive(:enqueue)
- subject.perform
- end
+ it_behaves_like 'sends no mail'
+ it_behaves_like 'sends no notification'
+ end
+
+ context 'notification for work_package_added disabled' do
+ let(:notification_setting) { %w(work_package_updated work_package_note_added) }
+
+ it_behaves_like 'sends no mail'
+
+ it_behaves_like 'sends notification'
+ end
+
+ context 'notification for work_package_updated disabled' do
+ let(:notification_setting) { %w(work_package_added status_updated work_package_priority_updated) }
+
+ let(:journal) { journal_2 }
+
+ it_behaves_like 'sends no mail'
+
+ it_behaves_like 'sends notification'
end
describe 'mail suppressing aggregation' do
@@ -138,12 +209,12 @@ describe EnqueueWorkPackageNotificationJob, type: :model do
.to receive(:enqueue)
.with(an_instance_of(DeliverWorkPackageNotificationJob), any_args)
.once
- described_class.new(journal_1.id, author.id).perform
+ described_class.new(journal_1.id, author.id, send_mail).perform
end
it 'Job 2 sends no mails' do
expect(Delayed::Job).not_to receive(:enqueue)
- described_class.new(journal_2.id, author.id).perform
+ described_class.new(journal_2.id, author.id, send_mail).perform
end
it 'Job 3 sends one mail for journal (2,3)' do
@@ -151,7 +222,7 @@ describe EnqueueWorkPackageNotificationJob, type: :model do
.to receive(:enqueue)
.with(an_instance_of(DeliverWorkPackageNotificationJob), any_args)
.once
- described_class.new(journal_3.id, author.id).perform
+ described_class.new(journal_3.id, author.id, send_mail).perform
end
end
@@ -171,12 +242,12 @@ describe EnqueueWorkPackageNotificationJob, type: :model do
it 'Job 1 sends no mails' do
expect(Delayed::Job).not_to receive(:enqueue)
- described_class.new(journal_1.id, author.id).perform
+ described_class.new(journal_1.id, author.id, send_mail).perform
end
it 'Job 2 sends no mails' do
expect(Delayed::Job).not_to receive(:enqueue)
- described_class.new(journal_2.id, author.id).perform
+ described_class.new(journal_2.id, author.id, send_mail).perform
end
it 'Job 3 sends one mail for (2,3)' do
@@ -184,7 +255,7 @@ describe EnqueueWorkPackageNotificationJob, type: :model do
.to receive(:enqueue)
.with(an_instance_of(DeliverWorkPackageNotificationJob), any_args)
.once
- described_class.new(journal_3.id, author.id).perform
+ described_class.new(journal_3.id, author.id, send_mail).perform
end
end
@@ -200,21 +271,21 @@ describe EnqueueWorkPackageNotificationJob, type: :model do
it 'Job 1 sends no mails' do
expect(Delayed::Job).not_to receive(:enqueue)
- described_class.new(journal_1.id, author.id).perform
+ described_class.new(journal_1.id, author.id, send_mail).perform
end
it 'Job 2 sends one mail for journal (1, 2)' do
expect(Delayed::Job).to receive(:enqueue)
.with(an_instance_of(DeliverWorkPackageNotificationJob), any_args)
.once
- described_class.new(journal_2.id, author.id).perform
+ described_class.new(journal_2.id, author.id, send_mail).perform
end
it 'Job 3 sends one mail for journal 3' do
expect(Delayed::Job).to receive(:enqueue)
.with(an_instance_of(DeliverWorkPackageNotificationJob), any_args)
.once
- described_class.new(journal_3.id, author.id).perform
+ described_class.new(journal_3.id, author.id, send_mail).perform
end
end
end
@@ -231,7 +302,7 @@ describe EnqueueWorkPackageNotificationJob, type: :model do
let(:notes) { 'Nice notes!' }
let(:journal) { work_package.journals.last }
- subject { described_class.new(work_package.journals.last.id, author.id) }
+ subject { described_class.new(work_package.journals.last.id, author.id, send_mail) }
before do
work_package.subject = title
@@ -255,7 +326,7 @@ describe EnqueueWorkPackageNotificationJob, type: :model do
describe "#mentioned" do
subject do
- instance = described_class.new(journal.id, author.id)
+ instance = described_class.new(journal.id, author.id, send_mail)
instance.perform
allow(instance)