Implement changed migration

featuer/26688/in-app-notifications-table-change
Oliver Günther 4 years ago
parent 426f3776a7
commit afbfb25e70
No known key found for this signature in database
GPG Key ID: A3A8BDAD7C0C552C
  1. 4
      app/models/notification.rb
  2. 70
      app/services/notifications/journal_wp_notification_service.rb
  3. 6
      db/migrate/20210616191052_create_notifications.rb
  4. 8
      lib/api/v3/notifications/notification_representer.rb
  5. 6
      spec/factories/notification_factory.rb

@ -2,7 +2,9 @@ class Notification < ApplicationRecord
enum reason: { mentioned: 0, involved: 1, watched: 2, subscribed: 3 }
belongs_to :recipient, class_name: 'User'
belongs_to :context, polymorphic: true
belongs_to :actor, class_name: 'User'
belongs_to :project
belongs_to :journal
belongs_to :resource, polymorphic: true
scope :recipient, ->(user) { where(recipient_id: user.is_a?(User) ? user.id : user) }

@ -53,37 +53,43 @@ class Notifications::JournalWpNotificationService
private
def create_event(journal, recipient, reason, user)
key = case reason
when :subscribed
:all
else
reason
end
channel_attributes = 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
[channel, setting[key] ? false : nil]
end.to_h
notification_attributes = {
recipient: recipient,
reason: reason,
resource: journal
}.merge(channel_attributes)
project: journal.project,
resource: journal.journable,
journal: journal,
actor: journal.user
}.merge(channel_attributes(recipient))
Notifications::CreateService
.new(user: user)
.call(notification_attributes)
end
def channel_attributes(recipient)
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
[channel, setting[key] ? false : nil]
end.to_h
end
def notification_receivers(work_package, journal)
seen = mentioned(journal).each do |user|
yield(user, :mentioned)
@ -110,8 +116,8 @@ class Notifications::JournalWpNotificationService
def involved(journal, seen)
scope = User
.where(id: group_or_user_ids(journal.data.assigned_to))
.or(User.where(id: group_or_user_ids(journal.data.responsible)))
.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,
@ -134,9 +140,9 @@ class Notifications::JournalWpNotificationService
def allowed_and_unique(scope, project, seen = [])
scope
.where(id: User.allowed(:view_work_packages, project))
.where.not(id: seen.map(&:id))
.not_builtin
.where(id: User.allowed(:view_work_packages, project))
.where.not(id: seen.map(&:id))
.not_builtin
end
def text_for_mentions(journal)
@ -157,8 +163,8 @@ class Notifications::JournalWpNotificationService
matches = mention_matches(journal)
base_scope = User
.includes(:groups)
.references(:groups_users)
.includes(:groups)
.references(:groups_users)
by_id = base_scope.where(id: matches[:user_ids])
by_login = base_scope.where(login: matches[:user_login_names])
@ -191,9 +197,9 @@ class Notifications::JournalWpNotificationService
group_ids_tag_after,
group_ids_tag_before,
group_ids_hash = text
.scan(MENTION_PATTERN)
.transpose
.each(&:compact!)
.scan(MENTION_PATTERN)
.transpose
.each(&:compact!)
{
user_ids: [user_ids_tag_after, user_ids_tag_before, user_ids_hash].flatten.compact,

@ -5,9 +5,13 @@ class CreateNotifications < ActiveRecord::Migration[6.1]
t.boolean :read_ian, default: false, index: true
t.boolean :read_email, default: false, index: true
t.integer :reason, limit: 1
t.references :recipient, null: false, index: true, foreign_key: { to_table: :users }
t.references :context, polymorphic: true, null: false
t.references :actor, null: true, foreign_key: { to_table: :users }
t.references :resource, polymorphic: true, null: false
t.references :project
t.references :journal, index: false
t.timestamps
end

@ -87,7 +87,13 @@ module API
}
end
polymorphic_resource :context
associated_resource :project
associated_resource :journal,
as: :activity,
representer: ::API::V3::Activities::ActivityRepresenter,
v3_path: :activity,
skip_render: ->(*) { represented.journal_id.nil? }
polymorphic_resource :resource
def _type

@ -5,7 +5,9 @@ FactoryBot.define do
read_email { false }
reason { :mentioned }
recipient factory: :user
context factory: :project
resource factory: :work_package_journal
project { association :project }
resource { association :work_package, project: project }
journal { association :work_package_journal, journable: resource }
actor { journal.user }
end
end

Loading…
Cancel
Save