Turn notification settings from magic symbols into constants

pull/9685/head
Oliver Günther 3 years ago
parent dc445feecf
commit 021e7aaf0f
No known key found for this signature in database
GPG Key ID: 88872239EB414F99
  1. 42
      app/models/notification_setting.rb
  2. 1
      app/models/users/scopes/notified_globally.rb
  3. 70
      app/services/notifications/create_from_model_service.rb
  4. 6
      app/services/notifications/create_from_model_service/comment_strategy.rb
  5. 6
      app/services/notifications/create_from_model_service/message_strategy.rb
  6. 7
      app/services/notifications/create_from_model_service/news_strategy.rb
  7. 10
      app/services/notifications/create_from_model_service/wiki_content_strategy.rb
  8. 2
      app/services/notifications/create_from_model_service/work_package_strategy.rb
  9. 11
      frontend/src/app/features/user-preferences/reminder-settings/email-alerts/email-alerts-settings.component.ts
  10. 34
      lib/api/v3/user_preferences/notification_setting_representer.rb
  11. 6
      modules/documents/app/services/notifications/create_from_model_service/document_strategy.rb
  12. 5
      spec/factories/user_factory.rb
  13. 27
      spec/services/notifications/create_from_journal_job_shared.rb
  14. 91
      spec/services/notifications/create_from_model_service_work_package_spec.rb

@ -1,4 +1,46 @@
class NotificationSetting < ApplicationRecord
WATCHED = :watched
ASSIGNED = :assigned
RESPONSIBLE = :responsible
MENTIONED = :mentioned
WORK_PACKAGE_CREATED = :work_package_created
WORK_PACKAGE_COMMENTED = :work_package_commented
WORK_PACKAGE_PROCESSED = :work_package_processed
WORK_PACKAGE_PRIORITIZED = :work_package_prioritized
WORK_PACKAGE_SCHEDULED = :work_package_scheduled
NEWS_ADDED = :news_added
NEWS_COMMENTED = :news_commented
DOCUMENT_ADDED = :document_added
FORUM_MESSAGES = :forum_messages
WIKI_PAGE_ADDED = :wiki_page_added
WIKI_PAGE_UPDATED = :wiki_page_updated
MEMBERSHIP_ADDED = :membership_added
MEMBERSHIP_UPDATED = :membership_updated
ALL = :all
def self.all_settings
[
WATCHED,
ASSIGNED,
RESPONSIBLE,
MENTIONED,
WORK_PACKAGE_CREATED,
WORK_PACKAGE_COMMENTED,
WORK_PACKAGE_PROCESSED,
WORK_PACKAGE_PRIORITIZED,
WORK_PACKAGE_SCHEDULED,
NEWS_ADDED,
NEWS_COMMENTED,
DOCUMENT_ADDED,
FORUM_MESSAGES,
WIKI_PAGE_ADDED,
WIKI_PAGE_UPDATED,
MEMBERSHIP_ADDED,
MEMBERSHIP_UPDATED,
ALL
]
end
enum channel: { in_app: 0, mail: 1, mail_digest: 2 }
belongs_to :project

@ -41,6 +41,7 @@ module Users::Scopes
id: NotificationSetting
.where(setting => true)
.where(project: nil)
.select(:user_id)
)
end
end

@ -118,65 +118,67 @@ class Notifications::CreateFromModelService
end
def settings_of_mentioned
applicable_settings(mentioned_ids,
project,
:mentioned)
project_applicable_settings(mentioned_ids,
project,
:mentioned)
end
def settings_of_assigned
applicable_settings(User.where(id: group_or_user_ids(journal.data.assigned_to)),
project,
:involved)
project_applicable_settings(User.where(id: group_or_user_ids(journal.data.assigned_to)),
project,
NotificationSetting::ASSIGNED)
end
def settings_of_responsible
applicable_settings(User.where(id: group_or_user_ids(journal.data.responsible)),
project,
:involved)
project_applicable_settings(User.where(id: group_or_user_ids(journal.data.responsible)),
project,
NotificationSetting::RESPONSIBLE)
end
def settings_of_subscribed
applicable_settings(strategy.subscribed_users(model),
project,
:all)
# Subscribed is a collection of events for non-work packages
# which currently ignore project-specific overrides
settings_for_allowed_users(strategy.subscribed_users(model),
strategy.notification_reason(model))
end
def settings_of_watched
applicable_settings(strategy.watcher_users(model),
project,
:watched)
project_applicable_settings(strategy.watcher_users(model),
project,
NotificationSetting::WATCHED)
end
def settings_of_commented
return NotificationSetting.none unless journal.notes?
applicable_settings(User.all,
project,
:work_package_commented)
project_applicable_settings(User.all,
project,
NotificationSetting::WORK_PACKAGE_COMMENTED)
end
def settings_of_created
return NotificationSetting.none unless journal.initial?
applicable_settings(User.all,
project,
:work_package_created)
project_applicable_settings(User.all,
project,
NotificationSetting::WORK_PACKAGE_CREATED)
end
def settings_of_processed
return NotificationSetting.none unless !journal.initial? && journal.details.has_key?(:status_id)
applicable_settings(User.all,
project,
:work_package_processed)
project_applicable_settings(User.all,
project,
NotificationSetting::WORK_PACKAGE_PROCESSED)
end
def settings_of_prioritized
return NotificationSetting.none unless !journal.initial? && journal.details.has_key?(:priority_id)
applicable_settings(User.all,
project,
:work_package_prioritized)
project_applicable_settings(User.all,
project,
NotificationSetting::WORK_PACKAGE_PRIORITIZED)
end
def settings_of_scheduled
@ -184,14 +186,18 @@ class Notifications::CreateFromModelService
return NotificationSetting.none
end
applicable_settings(User.all,
project,
:work_package_scheduled)
project_applicable_settings(User.all,
project,
NotificationSetting::WORK_PACKAGE_SCHEDULED)
end
def applicable_settings(user_scope, project, reason)
NotificationSetting
def project_applicable_settings(user_scope, project, reason)
settings_for_allowed_users(user_scope, reason)
.applicable(project)
end
def settings_for_allowed_users(user_scope, reason)
NotificationSetting
.where(reason => true)
.where(user: user_scope.where(id: User.allowed(strategy.permission, project)))
end

@ -50,7 +50,11 @@ module Notifications::CreateFromModelService::CommentStrategy
end
def self.subscribed_users(comment)
User.notified_on_all(project(comment))
User.notified_globally notification_reason(comment)
end
def self.notification_reason(_comment)
NotificationSetting::NEWS_COMMENTED
end
def self.watcher_users(comment)

@ -50,7 +50,11 @@ module Notifications::CreateFromModelService::MessageStrategy
end
def self.subscribed_users(journal)
User.notified_on_all(journal.data.project)
User.notified_globally notification_reason(journal)
end
def self.notification_reason(_journal)
NotificationSetting::FORUM_MESSAGES
end
def self.watcher_users(journal)

@ -51,13 +51,18 @@ module Notifications::CreateFromModelService::NewsStrategy
def self.subscribed_users(journal)
if journal.initial?
User.notified_on_all(journal.data.project)
User.notified_globally notification_reason(journal)
else
# No notification on updating a news
User.none
end
end
def self.notification_reason(_journal)
NotificationSetting::NEWS_ADDED
end
def self.project(journal)
journal.data.project
end

@ -50,7 +50,15 @@ module Notifications::CreateFromModelService::WikiContentStrategy
end
def self.subscribed_users(journal)
User.notified_on_all(journal.data.project)
User.notified_globally notification_reason(journal)
end
def self.notification_reason(journal)
if journal.initial?
NotificationSetting::WIKI_PAGE_ADDED
else
NotificationSetting::WIKI_PAGE_UPDATED
end
end
def self.watcher_users(journal)

@ -30,7 +30,7 @@
module Notifications::CreateFromModelService::WorkPackageStrategy
def self.reasons
%i(mentioned assigned responsible watched subscribed commented created processed prioritized scheduled)
%i(mentioned assigned responsible watched commented created processed prioritized scheduled)
end
def self.permission

@ -4,7 +4,6 @@ import {
} from '@angular/core';
import { I18nService } from 'core-app/core/i18n/i18n.service';
import { UserPreferencesService } from 'core-app/features/user-preferences/state/user-preferences.service';
import { ImmediateRemindersSettings } from 'core-app/features/user-preferences/state/user-preferences.model';
import { NotificationSetting } from 'core-app/features/user-preferences/state/notification-setting.model';
import { arrayUpdate } from '@datorama/akita';
@ -44,18 +43,16 @@ export class EmailAlertsSettingsComponent {
) {
}
toggleEnabled(key:string, enabled:boolean) {
toggleEnabled(key:string, enabled:boolean):void {
const delta = { [key]: enabled };
this.storeService.store.update(
({ notifications }) => ({
notifications: arrayUpdate(
notifications, this.matcherFn.bind(this), delta,
notifications,
(notification:NotificationSetting) => !notification._links.project.href,
delta,
),
}),
);
}
private matcherFn(notification:NotificationSetting) {
return !notification._links.project.href;
}
}

@ -35,23 +35,23 @@ module API
include API::Decorators::LinkedResource
property :channel
property :watched
property :involved
property :mentioned
property :work_package_created
property :work_package_commented
property :work_package_processed
property :work_package_prioritized
property :work_package_scheduled
property :news_added
property :news_commented
property :document_added
property :forum_messages
property :wiki_page_added
property :wiki_page_updated
property :membership_added
property :membership_updated
property :all
property NotificationSetting::WATCHED
property NotificationSetting::INVOLVED
property NotificationSetting::MENTIONED
property NotificationSetting::WORK_PACKAGE_CREATED
property NotificationSetting::WORK_PACKAGE_COMMENTED
property NotificationSetting::WORK_PACKAGE_PROCESSED
property NotificationSetting::WORK_PACKAGE_PRIORITIZED
property NotificationSetting::WORK_PACKAGE_SCHEDULED
property NotificationSetting::NEWS_ADDED
property NotificationSetting::NEWS_COMMENTED
property NotificationSetting::DOCUMENT_ADDED
property NotificationSetting::FORUM_MESSAGES
property NotificationSetting::WIKI_PAGE_ADDED
property NotificationSetting::WIKI_PAGE_UPDATED
property NotificationSetting::MEMBERSHIP_ADDED
property NotificationSetting::MEMBERSHIP_UPDATED
property NotificationSetting::ALL
associated_resource :project,
skip_render: ->(*) { true },

@ -50,7 +50,11 @@ module Notifications::CreateFromModelService::DocumentStrategy
end
def self.subscribed_users(journal)
User.notified_on_all(project(journal))
User.notified_globally notification_reason(journal)
end
def self.notification_reason(_journal)
NotificationSetting::DOCUMENT_ADDED
end
def self.project(journal)

@ -58,9 +58,10 @@ FactoryBot.define do
user.pref.save unless factory.preferences&.empty?
if user.notification_settings.empty?
all_true = NotificationSetting.all_settings.index_with(true)
user.notification_settings = [
FactoryBot.create(:mail_notification_setting, user: user, all: true),
FactoryBot.create(:in_app_notification_setting, user: user, all: true),
FactoryBot.create(:mail_notification_setting, user: user, **all_true),
FactoryBot.create(:in_app_notification_setting, user: user, **all_true),
FactoryBot.create(:mail_digest_notification_setting, user: user)
]
end

@ -51,23 +51,22 @@ shared_context 'with CreateFromJournalJob context' do
notification_settings: notification_settings)
end
let(:notification_settings_all_false) do
{
all: false,
involved: false,
watched: false,
mentioned: false,
work_package_commented: false,
work_package_processed: false,
work_package_created: false,
work_package_scheduled: false,
work_package_prioritized: false
}
NotificationSetting
.all_settings
.index_with(false)
end
let(:notification_settings_all_true) do
NotificationSetting
.all_settings
.index_with(true)
end
let(:recipient_notification_settings) do
[
FactoryBot.build(:mail_notification_setting, all: true),
FactoryBot.build(:in_app_notification_setting, all: true),
FactoryBot.build(:mail_digest_notification_setting, all: true)
FactoryBot.build(:mail_notification_setting, **notification_settings_all_true),
FactoryBot.build(:in_app_notification_setting, **notification_settings_all_true),
FactoryBot.build(:mail_digest_notification_setting, **notification_settings_all_true)
]
end
let(:send_notifications) { true }

@ -127,7 +127,7 @@ describe Notifications::CreateFromModelService,
context 'when assignee has in app notifications disabled' do
let(:recipient_notification_settings) do
[
FactoryBot.build(:mail_notification_setting, **notification_settings_all_false.merge(involved: false, all: true)),
FactoryBot.build(:mail_notification_setting, **notification_settings_all_true),
FactoryBot.build(:in_app_notification_setting, **notification_settings_all_false.merge(involved: false)),
FactoryBot.build(:mail_digest_notification_setting, **notification_settings_all_false.merge(involved: true))
]
@ -139,7 +139,7 @@ describe Notifications::CreateFromModelService,
read_ian: nil,
reason_ian: nil,
read_mail: false,
reason_mail: :subscribed,
reason_mail: :involved,
read_mail_digest: false,
reason_mail_digest: :assigned
}
@ -186,7 +186,7 @@ describe Notifications::CreateFromModelService,
let(:recipient_notification_settings) do
[
FactoryBot.build(:mail_notification_setting, **notification_settings_all_false.merge(involved: true)),
FactoryBot.build(:in_app_notification_setting, **notification_settings_all_false.merge(involved: false, all: true)),
FactoryBot.build(:in_app_notification_setting, **notification_settings_all_true),
FactoryBot.build(:mail_digest_notification_setting, **notification_settings_all_false.merge(involved: true))
]
end
@ -195,7 +195,7 @@ describe Notifications::CreateFromModelService,
let(:notification_channel_reasons) do
{
read_ian: false,
reason_ian: :subscribed,
reason_ian: :involved,
read_mail: false,
reason_mail: :assigned,
read_mail_digest: false,
@ -220,9 +220,9 @@ describe Notifications::CreateFromModelService,
context 'when assignee has all notifications enabled but made the change himself' do
let(:recipient_notification_settings) do
[
FactoryBot.build(:mail_notification_setting, **notification_settings_all_false.merge(involved: true, all: true)),
FactoryBot.build(:in_app_notification_setting, **notification_settings_all_false.merge(involved: true, all: true)),
FactoryBot.build(:mail_digest_notification_setting, **notification_settings_all_false.merge(involved: false, all: true))
FactoryBot.build(:mail_notification_setting, **notification_settings_all_true),
FactoryBot.build(:in_app_notification_setting, **notification_settings_all_true),
FactoryBot.build(:mail_digest_notification_setting, **notification_settings_all_true)
]
end
let(:author) { recipient }
@ -257,9 +257,9 @@ describe Notifications::CreateFromModelService,
context 'when responsible has in app notifications disabled' do
let(:recipient_notification_settings) do
[
FactoryBot.build(:mail_notification_setting, **notification_settings_all_false.merge(involved: false, all: true)),
FactoryBot.build(:mail_notification_setting, **notification_settings_all_false.merge(involved: false)),
FactoryBot.build(:in_app_notification_setting, **notification_settings_all_false),
FactoryBot.build(:mail_digest_notification_setting, **notification_settings_all_false.merge(all: true))
FactoryBot.build(:mail_digest_notification_setting, **notification_settings_all_true)
]
end
@ -268,10 +268,10 @@ describe Notifications::CreateFromModelService,
{
read_ian: nil,
reason_ian: nil,
read_mail: false,
reason_mail: :subscribed,
read_mail: nil,
reason_mail: nil,
read_mail_digest: false,
reason_mail_digest: :subscribed
reason_mail_digest: :involved
}
end
end
@ -327,9 +327,9 @@ describe Notifications::CreateFromModelService,
context 'when responsible has all notifications enabled but made the change himself' do
let(:recipient_notification_settings) do
[
FactoryBot.build(:mail_notification_setting, involved: true, all: true),
FactoryBot.build(:mail_digest_notification_setting, involved: true, all: true),
FactoryBot.build(:in_app_notification_setting, involved: true, all: true)
FactoryBot.build(:mail_notification_setting, **notification_settings_all_true),
FactoryBot.build(:mail_digest_notification_setting, **notification_settings_all_true),
FactoryBot.build(:in_app_notification_setting, **notification_settings_all_true)
]
end
let(:author) { recipient }
@ -342,9 +342,9 @@ describe Notifications::CreateFromModelService,
let(:user_property) { :watcher }
let(:recipient_notification_settings) do
[
FactoryBot.build(:mail_notification_setting, **notification_settings_all_false.merge(watched: true)),
FactoryBot.build(:in_app_notification_setting, **notification_settings_all_false.merge(watched: true)),
FactoryBot.build(:mail_digest_notification_setting, **notification_settings_all_false.merge(watched: true))
FactoryBot.build(:mail_notification_setting, **notification_settings_all_true),
FactoryBot.build(:in_app_notification_setting, **notification_settings_all_true),
FactoryBot.build(:mail_digest_notification_setting, **notification_settings_all_true)
]
end
@ -428,9 +428,9 @@ describe Notifications::CreateFromModelService,
context 'when watcher has all notifications enabled but made the change himself' do
let(:recipient_notification_settings) do
[
FactoryBot.build(:mail_notification_setting, **notification_settings_all_false.merge(watched: true, all: true)),
FactoryBot.build(:in_app_notification_setting, **notification_settings_all_false.merge(watched: true, all: true)),
FactoryBot.build(:mail_digest_notification_setting, **notification_settings_all_false.merge(watched: true, all: true))
FactoryBot.build(:mail_notification_setting, **notification_settings_all_true),
FactoryBot.build(:in_app_notification_setting, **notification_settings_all_true),
FactoryBot.build(:mail_digest_notification_setting, **notification_settings_all_true)
]
end
let(:author) { recipient }
@ -444,9 +444,9 @@ describe Notifications::CreateFromModelService,
let(:recipient_notification_settings) do
[
FactoryBot.build(:mail_notification_setting, all: true),
FactoryBot.build(:mail_digest_notification_setting, all: true),
FactoryBot.build(:in_app_notification_setting, all: true)
FactoryBot.build(:mail_notification_setting, **notification_settings_all_true),
FactoryBot.build(:mail_digest_notification_setting, **notification_settings_all_true),
FactoryBot.build(:in_app_notification_setting, **notification_settings_all_true)
]
end
@ -454,11 +454,11 @@ describe Notifications::CreateFromModelService,
let(:notification_channel_reasons) do
{
read_ian: false,
reason_ian: :subscribed,
reason_ian: :created,
read_mail: false,
reason_mail: :subscribed,
reason_mail: :created,
read_mail_digest: false,
reason_mail_digest: :subscribed
reason_mail_digest: :created
}
end
end
@ -466,8 +466,8 @@ describe Notifications::CreateFromModelService,
context 'with in app notifications disabled' do
let(:recipient_notification_settings) do
[
FactoryBot.build(:mail_notification_setting, **notification_settings_all_false.merge(all: true)),
FactoryBot.build(:mail_digest_notification_setting, **notification_settings_all_false.merge(all: true)),
FactoryBot.build(:mail_notification_setting, **notification_settings_all_true),
FactoryBot.build(:mail_digest_notification_setting, **notification_settings_all_true),
FactoryBot.build(:in_app_notification_setting, **notification_settings_all_false)
]
end
@ -478,9 +478,9 @@ describe Notifications::CreateFromModelService,
read_ian: nil,
reason_ian: nil,
read_mail: false,
reason_mail: :subscribed,
reason_mail: :created,
read_mail_digest: false,
reason_mail_digest: :subscribed
reason_mail_digest: :created
}
end
end
@ -491,7 +491,7 @@ describe Notifications::CreateFromModelService,
[
FactoryBot.build(:mail_notification_setting, **notification_settings_all_false),
FactoryBot.build(:mail_digest_notification_setting, **notification_settings_all_false),
FactoryBot.build(:in_app_notification_setting, **notification_settings_all_false.merge(all: true))
FactoryBot.build(:in_app_notification_setting, **notification_settings_all_true)
]
end
@ -499,7 +499,7 @@ describe Notifications::CreateFromModelService,
let(:notification_channel_reasons) do
{
read_ian: false,
reason_ian: :subscribed,
reason_ian: :created,
read_mail: nil,
reason_mail: nil,
read_mail_digest: nil,
@ -527,10 +527,9 @@ describe Notifications::CreateFromModelService,
FactoryBot.build(:mail_notification_setting, **notification_settings_all_false),
FactoryBot.build(:mail_digest_notification_setting, **notification_settings_all_false),
FactoryBot.build(:in_app_notification_setting, **notification_settings_all_false),
FactoryBot.build(:mail_notification_setting, project: project, **notification_settings_all_false.merge(all: true)),
FactoryBot.build(:mail_digest_notification_setting, project: project, **notification_settings_all_false
.merge(all: true)),
FactoryBot.build(:in_app_notification_setting, project: project, **notification_settings_all_false.merge(all: true))
FactoryBot.build(:mail_notification_setting, project: project, **notification_settings_all_true),
FactoryBot.build(:mail_digest_notification_setting, project: project, **notification_settings_all_true),
FactoryBot.build(:in_app_notification_setting, project: project, **notification_settings_all_true)
]
end
@ -538,11 +537,11 @@ describe Notifications::CreateFromModelService,
let(:notification_channel_reasons) do
{
read_ian: false,
reason_ian: :subscribed,
reason_ian: :created,
read_mail: false,
reason_mail: :subscribed,
reason_mail: :created,
read_mail_digest: false,
reason_mail_digest: :subscribed
reason_mail_digest: :created
}
end
end
@ -551,9 +550,9 @@ describe Notifications::CreateFromModelService,
context 'with all enabled as a default but disabled in the project' do
let(:recipient_notification_settings) do
[
FactoryBot.build(:mail_notification_setting, **notification_settings_all_false.merge(all: true)),
FactoryBot.build(:mail_digest_notification_setting, **notification_settings_all_false.merge(all: true)),
FactoryBot.build(:in_app_notification_setting, **notification_settings_all_false.merge(all: true)),
FactoryBot.build(:mail_notification_setting, **notification_settings_all_true),
FactoryBot.build(:mail_digest_notification_setting, **notification_settings_all_true),
FactoryBot.build(:in_app_notification_setting, **notification_settings_all_true),
FactoryBot.build(:mail_notification_setting, project: project, **notification_settings_all_false),
FactoryBot.build(:mail_digest_notification_setting, project: project, **notification_settings_all_false),
FactoryBot.build(:in_app_notification_setting, project: project, **notification_settings_all_false)
@ -572,9 +571,9 @@ describe Notifications::CreateFromModelService,
context 'when recipient has all notifications enabled but made the change himself' do
let(:recipient_notification_settings) do
[
FactoryBot.build(:mail_notification_setting, all: true),
FactoryBot.build(:mail_digest_notification_setting, all: true),
FactoryBot.build(:in_app_notification_setting, all: true)
FactoryBot.build(:mail_notification_setting, **notification_settings_all_true),
FactoryBot.build(:mail_digest_notification_setting, **notification_settings_all_true),
FactoryBot.build(:in_app_notification_setting, **notification_settings_all_true)
]
end
let(:author) { recipient }

Loading…
Cancel
Save