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

only store notification resons for in app (#9419)

ulferts8be49d1c62bc
diff --git a/app/services/notifications/journal_wp_notification_service.rb b/app/services/notifications/journal_wp_notification_service.rb
index 6b5575df605..aa75e05573c 100644
--- a/app/services/notifications/journal_wp_notification_service.rb
+++ b/app/services/notifications/journal_wp_notification_service.rb
@@ -45,105 +45,88 @@ class Notifications::JournalWpNotificationService
author = User.find_by(id: journal.user_id) || DeletedUser.first
- notification_receivers(journal.journable, journal) do |recipient, reason|
- create_event(journal, recipient, reason, author)
+ notification_receivers(journal.journable, journal).each do |recipient_id, channel_reasons|
+ create_event(journal,
+ recipient_id,
+ channel_reasons.keys,
+ # TODO: split up into two separate reasons
+ # For now, we prepare the in_app reason since that one is displayed.
+ channel_reasons['in_app'].first || channel_reasons['mail'].first,
+ author)
end
end
private
- def create_event(journal, recipient, reason, user)
+ def create_event(journal, recipient_id, channels, reason, user)
notification_attributes = {
- recipient: recipient,
+ recipient_id: recipient_id,
reason: reason,
project: journal.project,
resource: journal.journable,
journal: journal,
actor: journal.user
- }.merge(channel_attributes(recipient, reason))
+ }.merge(channel_attributes(channels))
Notifications::CreateService
.new(user: user)
.call(notification_attributes)
end
- def channel_attributes(recipient, reason)
- key =
- case reason
- when :subscribed
- :all
- else
- reason
- end
-
- recipient.notification_settings.map do |setting|
- channel =
- case setting.channel
- when 'mail'
- :read_email
- when 'in_app'
- :read_ian
- else
- raise "Unknown notification channel"
- end
+ def channel_attributes(channels)
+ {
+ read_email: channels.include?('mail') ? false : nil,
+ read_ian: channels.include?('in_app') ? false : nil
- [channel, setting[key] ? false : nil]
- end.to_h
+ }
end
def notification_receivers(work_package, journal)
- seen = mentioned(journal).each do |user|
- yield(user, :mentioned)
- end
-
- seen += involved(journal, seen).each do |user|
- yield(user, :involved)
- end
+ receivers = receivers_hash
- seen += subscribed(journal, seen).each do |user|
- yield(user, :subscribed)
- end
+ add_receiver(receivers, mentioned(journal), :mentioned)
+ add_receiver(receivers, involved(journal), :involved)
+ add_receiver(receivers, watched(work_package), :watched)
+ add_receiver(receivers, subscribed(journal), :subscribed)
- watched(work_package, seen).each do |user|
- yield(user, :watched)
- end
+ receivers
end
def mentioned(journal)
allowed_and_unique(mentioned_ids(journal),
journal.data.project,
- [])
+ :mentioned)
end
- def involved(journal, seen)
+ def involved(journal)
scope = User
.where(id: group_or_user_ids(journal.data.assigned_to))
.or(User.where(id: group_or_user_ids(journal.data.responsible)))
allowed_and_unique(scope,
journal.data.project,
- seen)
+ :involved)
end
- def subscribed(journal, seen)
+ def subscribed(journal)
project = journal.data.project
allowed_and_unique(User.notified_on_all(project),
project,
- seen)
+ :all)
end
- def watched(work_package, seen)
+ def watched(work_package)
allowed_and_unique(work_package.watcher_users,
work_package.project,
- seen)
+ :watched)
end
- def allowed_and_unique(scope, project, seen = [])
- scope
- .where(id: User.allowed(:view_work_packages, project))
- .where.not(id: seen.map(&:id))
- .not_builtin
+ def allowed_and_unique(user_scope, project, reason)
+ NotificationSetting
+ .applicable(project)
+ .where(reason => true)
+ .where(user: user_scope.where(id: User.allowed(:view_work_packages, project)))
end
def text_for_mentions(journal)
@@ -242,5 +225,19 @@ class Notifications::JournalWpNotificationService
def group_or_user_ids(association)
association.is_a?(Group) ? association.user_ids : association&.id
end
+
+ def add_receiver(receivers, collection, reason)
+ collection.each do |notification|
+ receivers[notification.user_id][notification.channel] << reason
+ end
+ end
+
+ def receivers_hash
+ Hash.new do |hash, user|
+ hash[user] = Hash.new do |channel_hash, channel|
+ channel_hash[channel] = []
+ end
+ end
+ end
end
end
diff --git a/spec/services/notifications/journal_wp_notification_service_spec.rb b/spec/services/notifications/journal_wp_notification_service_spec.rb
index abeb0169138..d76712dd0d4 100644
--- a/spec/services/notifications/journal_wp_notification_service_spec.rb
+++ b/spec/services/notifications/journal_wp_notification_service_spec.rb
@@ -140,7 +140,7 @@ describe Notifications::JournalWpNotificationService, with_settings: { journal_a
expect(events_service)
.to have_received(:call)
- .with({ recipient: recipient,
+ .with({ recipient_id: recipient.id,
reason: event_reason,
project: journal.project,
actor: journal.user,
@@ -221,13 +221,23 @@ describe Notifications::JournalWpNotificationService, with_settings: { journal_a
]
end
- # Event creation will be prevented by the service
+ it_behaves_like 'creates no notification'
+ end
+
+ context 'assignee has all in app notifications enabled but only involved for mail' do
+ let(:recipient_notification_settings) do
+ [
+ FactoryBot.build(:mail_notification_setting, involved: true),
+ FactoryBot.build(:in_app_notification_setting, involved: false, watched: false, mentioned: false, all: true)
+ ]
+ end
+
it_behaves_like 'creates notification' do
- let(:event_reason) { :involved }
+ let(:event_reason) { :subscribed }
let(:event_channels) do
{
- read_ian: nil,
- read_email: nil
+ read_ian: false,
+ read_email: false
}
end
end
@@ -305,16 +315,7 @@ describe Notifications::JournalWpNotificationService, with_settings: { journal_a
]
end
- # Event creation will be prevented by the service
- it_behaves_like 'creates notification' do
- let(:event_reason) { :involved }
- let(:event_channels) do
- {
- read_ian: nil,
- read_email: nil
- }
- end
- end
+ it_behaves_like 'creates no notification'
end
context 'when responsible is not allowed to view work packages' do
@@ -389,16 +390,7 @@ describe Notifications::JournalWpNotificationService, with_settings: { journal_a
]
end
- # Event creation will be prevented by the service
- it_behaves_like 'creates notification' do
- let(:event_reason) { :watched }
- let(:event_channels) do
- {
- read_ian: nil,
- read_email: nil
- }
- end
- end
+ it_behaves_like 'creates no notification'
end
context 'when watcher is not allowed to view work packages' do
@@ -631,15 +623,7 @@ describe Notifications::JournalWpNotificationService, with_settings: { journal_a
]
end
- # Event creation will be prevented by the service
- it_behaves_like 'creates notification' do
- let(:event_channels) do
- {
- read_ian: nil,
- read_email: nil
- }
- end
- end
+ it_behaves_like 'creates no notification'
end
end
@@ -712,15 +696,7 @@ describe Notifications::JournalWpNotificationService, with_settings: { journal_a
"Hello user:\"#{recipient.login}\", hey user##{recipient.id}"
end
- # Event creation will be prevented by the service
- it_behaves_like 'creates notification' do
- let(:event_channels) do
- {
- read_ian: nil,
- read_email: nil
- }
- end
- end
+ it_behaves_like 'creates no notification'
end
end
diff --git a/spec_legacy/unit/mail_handler_spec.rb b/spec_legacy/unit/mail_handler_spec.rb
index 9113bd83fe7..15e119fb202 100644
--- a/spec_legacy/unit/mail_handler_spec.rb
+++ b/spec_legacy/unit/mail_handler_spec.rb
@@ -274,6 +274,8 @@ describe MailHandler, type: :model do
it 'should add work package should send email notification' do
Setting.notified_events = ['work_package_added']
+ User.find(2).notification_settings.create(channel: :mail, all: true)
+
# This email contains: 'Project: onlinestore'
issue = submit_email('ticket_on_given_project.eml')
assert issue.is_a?(WorkPackage)
@@ -321,6 +323,9 @@ describe MailHandler, type: :model do
end
it 'should add work package note should send email notification' do
+ User.find(2).notification_settings.create(channel: :mail, all: true)
+ User.find(3).notification_settings.create(channel: :mail, involved: true)
+
journal = submit_email('ticket_reply.eml')
assert journal.is_a?(Journal)
assert_equal 2, ActionMailer::Base.deliveries.size