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

handling existing notifications on creation -> update or destroy

ulferts67e0039f721e
diff --git a/app/services/notifications/create_from_model_service.rb b/app/services/notifications/create_from_model_service.rb
index 83c6de6f0f4..4a02890b3c9 100644
--- a/app/services/notifications/create_from_model_service.rb
+++ b/app/services/notifications/create_from_model_service.rb
@@ -41,16 +41,29 @@ class Notifications::CreateFromModelService
self.model = model
end
+ # Creates Notifications according to the various settings:
+ # * configured by the individual users
+ # * the send_notifications property provided
+ # and also by the properties of the journal, e.g.:
+ # * a work package mentioning a user
+ # * a news begin watched
+ # This method might be called multiple times, mostly when a journal is aggregated.
+ # On the second run, the potentially existing Notifications need to be taken into account by
+ # * updating them if the user is still to be notified: resetting the read_ian to false if the strategy supports ian
+ # * destroying them if the user is no longer to be notified
def call(send_notifications)
- result = ServiceResult.new success: !abort_sending?(send_notifications)
+ result = ServiceResult.new success: !abort_sending?
return result if result.failure?
- notification_receivers.each do |recipient_id, reasons|
- call = create_notification(recipient_id, reasons)
- result.add_dependent!(call)
+ current_notifications = notification_receivers(send_notifications).transform_values(&:first)
+
+ current_notifications.each do |recipient_id, reason|
+ result.add_dependent!(update_or_create_notification(recipient_id, reason))
end
+ delete_outdated_notifications(current_notifications.keys)
+
result
end
@@ -58,14 +71,23 @@ class Notifications::CreateFromModelService
attr_accessor :model
- def create_notification(recipient_id, reasons)
+ # In case a notification already exists (because the journal was aggregated)
+ def update_or_create_notification(recipient_id, reason)
+ if reason == :mentioned
+ update_notification(recipient_id) or create_notification(recipient_id, reason)
+ else
+ create_notification(recipient_id, reason)
+ end
+ end
+
+ def create_notification(recipient_id, reason)
notification_attributes = {
recipient_id:,
project:,
resource:,
journal:,
actor: user_with_fallback,
- reason: reasons.first,
+ reason: reason,
read_ian: strategy.supports_ian? ? false : nil,
mail_reminder_sent: strategy.supports_mail_digest? ? false : nil,
mail_alert_sent: strategy.supports_mail? ? false : nil
@@ -76,9 +98,29 @@ class Notifications::CreateFromModelService
.call(notification_attributes)
end
- def notification_receivers
+ def update_notification(recipient_id)
+ existing_notification = Notification
+ .find_by(journal_id: journal.id, recipient_id:, reason: :mentioned)
+
+ return unless existing_notification
+
+ Notifications::UpdateService
+ .new(model: existing_notification, user:, contract_class: EmptyContract)
+ .call(read_ian: false)
+ end
+
+ def delete_outdated_notifications(current_recipient_ids)
+ Notification
+ .where(journal: journal)
+ .where.not(recipient_id: current_recipient_ids)
+ .destroy_all
+ end
+
+ def notification_receivers(send_notifications)
receivers = receivers_hash
+ return receivers unless send_notification?(send_notifications)
+
strategy.reasons.each do |reason|
add_receivers_by_reason(receivers, reason)
end
@@ -93,8 +135,6 @@ class Notifications::CreateFromModelService
end
def settings_of_mentioned
- return NotificationSetting.none if has_aggregated_notification?(journal)
-
project_applicable_settings(mentioned_ids,
project,
NotificationSetting::MENTIONED)
@@ -233,11 +273,9 @@ class Notifications::CreateFromModelService
}
end
- def abort_sending?(send_notifications)
- !send_notification?(send_notifications) ||
- model.nil? ||
+ def abort_sending?
+ model.nil? ||
!model.class.exists?(id: model.id) ||
- journal&.noop? ||
!supported?
end
@@ -259,14 +297,6 @@ class Notifications::CreateFromModelService
receivers.delete(user_with_fallback.id)
end
- ##
- # If the journal has any mentioned notification
- # then the mention was aggregated to a new journal
- # by +Notifications::AggregatedJournalService+
- def has_aggregated_notification?(journal)
- Notification.exists?(journal_id: journal.id, reason: :mentioned)
- end
-
def receivers_hash
Hash.new do |hash, user|
hash[user] = []
diff --git a/app/services/notifications/update_service.rb b/app/services/notifications/update_service.rb
new file mode 100644
index 00000000000..5b88ac4eceb
--- /dev/null
+++ b/app/services/notifications/update_service.rb
@@ -0,0 +1,30 @@
+# OpenProject is an open source project management software.
+# Copyright (C) 2010-2022 the OpenProject GmbH
+#
+# 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-2013 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 COPYRIGHT and LICENSE files for more details.
+
+# Not that at the time of writing, the usually used UpdateContract does not exist.
+# Currently, this is only used internally, without user input, so it is safe to hand the EmptyContract in.
+class Notifications::UpdateService < ::BaseServices::Update
+end
diff --git a/spec/services/notifications/aggregated_journal_service_integration_spec.rb b/spec/services/notifications/aggregated_journal_service_integration_spec.rb
index 736af04b3ac..8366e0ff907 100644
--- a/spec/services/notifications/aggregated_journal_service_integration_spec.rb
+++ b/spec/services/notifications/aggregated_journal_service_integration_spec.rb
@@ -41,8 +41,8 @@ describe Notifications::AggregatedJournalService, 'integration', type: :model do
update_assignee!
expect(mentioned_notification.reload).to be_present
- expect(mentioned_notification.journal).not_to eq journal
- expect { journal.reload }.to raise_error(ActiveRecord::RecordNotFound)
+ expect(mentioned_notification.journal).to eq journal
+ expect(mentioned_notification.journal.updated_at).not_to eq journal.updated_at
# Expect only one notification to be present
expect(Notification.where(recipient:, reason: :mentioned).count)
diff --git a/spec/services/notifications/create_from_model_service_work_package_spec.rb b/spec/services/notifications/create_from_model_service_work_package_spec.rb
index 1af58e49e2e..51255b9dd2c 100644
--- a/spec/services/notifications/create_from_model_service_work_package_spec.rb
+++ b/spec/services/notifications/create_from_model_service_work_package_spec.rb
@@ -886,6 +886,38 @@ describe Notifications::CreateFromModelService,
it_behaves_like 'creates no notification'
end
+
+ context 'when there is already a notification for the journal (because it was aggregated)' do
+ let(:note) { "Hello user:\"#{recipient_login}\"" }
+ let!(:existing_notification) do
+ create(:notification, journal:, recipient:, reason: :mentioned, read_ian: true)
+ end
+
+ it_behaves_like 'creates no notification'
+
+ it 'resets the read_ian of the existing notification to false' do
+ call
+
+ expect(existing_notification.reload.read_ian)
+ .to be_falsey
+ end
+ end
+
+ context 'when there is already a notification for the journal (aggregation) but the user is no longer mentioned' do
+ let(:note) { "Hello you" }
+ let!(:existing_notification) do
+ create(:notification, journal:, recipient:, reason: :mentioned, read_ian: true)
+ end
+
+ it_behaves_like 'creates no notification'
+
+ it 'removes the existing notification' do
+ call
+
+ expect(Notification.exists?(id: existing_notification.id))
+ .to be_falsey
+ end
+ end
end
context 'for groups' do
@@ -953,17 +985,6 @@ describe Notifications::CreateFromModelService,
end
it_behaves_like 'mentioned'
-
- context 'when there is a notification for mentioned on the journal' do
- let!(:mentioned_notification) do
- create :notification,
- journal: journal_2_with_notes,
- resource: journal_2_with_notes.journable,
- reason: :mentioned
- end
-
- it_behaves_like 'creates no notification'
- end
end
describe 'in the description' do
@@ -989,17 +1010,26 @@ describe Notifications::CreateFromModelService,
end
end
- context 'when aggregated journal is empty' do
- let(:journal) { journal_2_empty_change }
- let(:journal_2_empty_change) do
- work_package.add_journal(author, 'temp')
- work_package.save(validate: false)
- work_package.journals.last.tap do |j|
- j.update_column(:notes, nil)
- end
+ context 'when on aggregating the journal, sending of notifications is prevented' do
+ let!(:existing_notification) do
+ create(:notification, journal:, recipient:, reason: :mentioned, read_ian: true)
+ end
+ let(:send_notifications) { false }
+ let(:user_property) { :watcher }
+ let(:recipient_notification_settings) do
+ [
+ build(:notification_setting, **notification_settings_all_true)
+ ]
end
it_behaves_like 'creates no notification'
+
+ it 'removes the existing notifications' do
+ call
+
+ expect(Notification.exists?(id: existing_notification.id))
+ .to be_falsey
+ end
end
context 'when the journal is deleted' do