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

send mention mail immediately

ulfertsa74ef924fea5
diff --git a/app/workers/notifications/workflow_job.rb b/app/workers/notifications/workflow_job.rb
index f5259a24dbc..f2c68968025 100644
--- a/app/workers/notifications/workflow_job.rb
+++ b/app/workers/notifications/workflow_job.rb
@@ -32,9 +32,9 @@
# 1) The notifications for any event (e.g. journal creation) is to be created as fast as possible
# so that it becomes visible as an in app notification. If the resource passed in is indeed a journal,
# it might get replaced later on (by a subsequent journal). This will lead to notifications being removed.
-# 2) After the journal aggregation time has passed as well as the desired delay, the direct email is sent out.
-# 3) At the same time (TODO: but it could already have been triggered after the aggregation time has passed)
-# the digest is scheduled.
+# In case the notification has a mentioned-reason, the mail is to be sent right away. This accepts the possibility
+# of the journal being deleted later on.
+# 2) After the journal aggregation time has passed direct mails are scheduled.
# This order has to be kept to ensure that the notifications are created before email sending is attempted. If it weren't
# guaranteed, with the notifications created in one job and the mails send in another, the mail sending job might get executed
# without any notifications being created which would result in no emails being sent at all. An alternative would be to
@@ -52,10 +52,21 @@ class Notifications::WorkflowJob < ApplicationJob
state :create_notifications,
to: :send_mails do |resource, send_notification|
- Notifications::CreateFromModelService
- .new(resource)
- .call(send_notification)
- .all_results
+ mentioned, delayed = Notifications::CreateFromModelService
+ .new(resource)
+ .call(send_notification)
+ .all_results
+ .partition(&:mail_mentioned?)
+
+ mentioned
+ .select { |n| n.read_mail == false }
+ .each do |notification|
+ Notifications::MailService
+ .new(notification)
+ .call
+ end
+
+ delayed
.map(&:id)
end
diff --git a/spec/workers/notifications/workflow_job_spec.rb b/spec/workers/notifications/workflow_job_spec.rb
index cf919f03391..b8e9d378a66 100644
--- a/spec/workers/notifications/workflow_job_spec.rb
+++ b/spec/workers/notifications/workflow_job_spec.rb
@@ -38,8 +38,13 @@ describe Notifications::WorkflowJob, type: :model do
let(:send_notification) { true }
let(:notifications) do
- [FactoryBot.build_stubbed(:notification),
- FactoryBot.build_stubbed(:notification)]
+ [FactoryBot.build_stubbed(:notification, reason_ian: :assigned, reason_mail: :assigned),
+ mentioned_notification,
+ FactoryBot.build_stubbed(:notification, reason_ian: :watched, reason_mail: :watched)]
+ end
+
+ let(:mentioned_notification) do
+ FactoryBot.build_stubbed(:notification, reason_ian: :mentioned, reason_mail: :mentioned)
end
describe '#perform' do
@@ -69,6 +74,18 @@ describe Notifications::WorkflowJob, type: :model do
service_instance
end
+ let!(:mail_service) do
+ service_instance = instance_double(Notifications::MailService,
+ call: nil)
+
+ allow(Notifications::MailService)
+ .to receive(:new)
+ .with(mentioned_notification)
+ .and_return(service_instance)
+
+ service_instance
+ end
+
it 'calls the service to create notifications' do
perform_job
@@ -77,7 +94,14 @@ describe Notifications::WorkflowJob, type: :model do
.with(send_notification)
end
- it 'schedules a delayed WorkflowJob' do
+ it 'sends mails for all notifications that are marked to send mails and that have a mention reason' do
+ perform_job
+
+ expect(mail_service)
+ .to have_received(:call)
+ end
+
+ it 'schedules a delayed WorkflowJob for those notifications not to be sent directly' do
allow(Time)
.to receive(:current)
.and_return(Time.current)
@@ -87,7 +111,7 @@ describe Notifications::WorkflowJob, type: :model do
expect { perform_job }
.to enqueue_job(described_class)
- .with(:send_mails, *notifications.map(&:id))
+ .with(:send_mails, *(notifications - [mentioned_notification]).map(&:id))
.at(expected_time)
end
end