Feature: User Notification Objects (#752)

Co-authored-by: Pranav Raj S <pranavrajs@gmail.com>
This commit is contained in:
Sojan Jose
2020-05-01 14:53:43 +05:30
committed by GitHub
parent 66aace7c13
commit 96da27f1f6
35 changed files with 461 additions and 110 deletions

View File

@@ -37,7 +37,8 @@ class AccountUser < ApplicationRecord
def create_notification_setting
setting = user.notification_settings.new(account_id: account.id)
setting.selected_email_flags = [:conversation_assignment]
setting.selected_email_flags = [:email_conversation_assignment]
setting.selected_push_flags = [:push_conversation_assignment]
setting.save!
end

View File

@@ -53,7 +53,7 @@ class Conversation < ApplicationRecord
before_create :set_bot_conversation
after_update :notify_status_change, :create_activity, :send_email_notification_to_assignee
after_update :notify_status_change, :create_activity
after_create :notify_conversation_creation, :run_round_robin
@@ -105,16 +105,6 @@ class Conversation < ApplicationRecord
}
end
private
def set_bot_conversation
self.status = :bot if inbox.agent_bot_inbox&.active?
end
def notify_conversation_creation
dispatcher_dispatch(CONVERSATION_CREATED)
end
def notifiable_assignee_change?
return false if self_assign?(assignee_id)
return false unless saved_change_to_assignee_id?
@@ -123,12 +113,14 @@ class Conversation < ApplicationRecord
true
end
def send_email_notification_to_assignee
return unless notifiable_assignee_change?
return if assignee.notification_settings.find_by(account_id: account_id).not_conversation_assignment?
return if bot?
private
AgentNotifications::ConversationNotificationsMailer.conversation_assigned(self, assignee).deliver_later
def set_bot_conversation
self.status = :bot if inbox.agent_bot_inbox&.active?
end
def notify_conversation_creation
dispatcher_dispatch(CONVERSATION_CREATED)
end
def self_assign?(assignee_id)

View File

@@ -75,7 +75,7 @@ class Message < ApplicationRecord
:notify_via_mail
# we need to wait for the active storage attachments to be available
after_create_commit :dispatch_event, :send_reply
after_create_commit :dispatch_create_events, :send_reply
after_update :dispatch_update_event
@@ -117,7 +117,7 @@ class Message < ApplicationRecord
private
def dispatch_event
def dispatch_create_events
Rails.configuration.dispatcher.dispatch(MESSAGE_CREATED, Time.zone.now, message: self)
if outgoing? && conversation.messages.outgoing.count == 1

View File

@@ -0,0 +1,47 @@
# == Schema Information
#
# Table name: notifications
#
# id :bigint not null, primary key
# notification_type :integer not null
# primary_actor_type :string not null
# read_at :datetime
# secondary_actor_type :string
# created_at :datetime not null
# updated_at :datetime not null
# account_id :bigint not null
# primary_actor_id :bigint not null
# secondary_actor_id :bigint
# user_id :bigint not null
#
# Indexes
#
# index_notifications_on_account_id (account_id)
# index_notifications_on_user_id (user_id)
# uniq_primary_actor_per_account_notifications (primary_actor_type,primary_actor_id)
# uniq_secondary_actor_per_account_notifications (secondary_actor_type,secondary_actor_id)
#
class Notification < ApplicationRecord
belongs_to :account
belongs_to :user
belongs_to :primary_actor, polymorphic: true
belongs_to :secondary_actor, polymorphic: true, optional: true
NOTIFICATION_TYPES = {
conversation_creation: 1,
conversation_assignment: 2
}.freeze
enum notification_type: NOTIFICATION_TYPES
after_create_commit :process_notification_delivery
private
def process_notification_delivery
Notification::EmailNotificationService.new(notification: self).perform
# Notification::PushNotificationService.new(notification: self).perform
end
end

View File

@@ -4,6 +4,7 @@
#
# id :bigint not null, primary key
# email_flags :integer default(0), not null
# push_flags :integer default(0), not null
# created_at :datetime not null
# updated_at :datetime not null
# account_id :integer
@@ -25,10 +26,9 @@ class NotificationSetting < ApplicationRecord
flag_query_mode: :bit_operator
}.freeze
EMAIL_NOTIFICATION_FLAGS = {
1 => :conversation_creation,
2 => :conversation_assignment
}.freeze
EMAIL_NOTIFICATION_FLAGS = ::Notification::NOTIFICATION_TYPES.transform_keys { |key| "email_#{key}".to_sym }.invert.freeze
PUSH_NOTIFICATION_FLAGS = ::Notification::NOTIFICATION_TYPES.transform_keys { |key| "push_#{key}".to_sym }.invert.freeze
has_flags EMAIL_NOTIFICATION_FLAGS.merge(column: 'email_flags').merge(DEFAULT_QUERY_SETTING)
has_flags PUSH_NOTIFICATION_FLAGS.merge(column: 'push_flags').merge(DEFAULT_QUERY_SETTING)
end

View File

@@ -0,0 +1,26 @@
# == Schema Information
#
# Table name: notification_subscriptions
#
# id :bigint not null, primary key
# subscription_attributes :jsonb not null
# subscription_type :integer not null
# created_at :datetime not null
# updated_at :datetime not null
# user_id :bigint not null
#
# Indexes
#
# index_notification_subscriptions_on_user_id (user_id)
#
class NotificationSubscription < ApplicationRecord
belongs_to :user
SUBSCRIPTION_TYPES = {
browser_push: 1,
gcm: 2
}.freeze
enum subscription_type: SUBSCRIPTION_TYPES
end

View File

@@ -67,7 +67,10 @@ class User < ApplicationRecord
has_many :assigned_inboxes, through: :inbox_members, source: :inbox
has_many :messages
has_many :invitees, through: :account_users, class_name: 'User', foreign_key: 'inviter_id', dependent: :nullify
has_many :notifications, dependent: :destroy
has_many :notification_settings, dependent: :destroy
has_many :notification_subscriptions, dependent: :destroy
before_validation :set_password_and_uid, on: :create
@@ -119,12 +122,6 @@ class User < ApplicationRecord
Rails.configuration.dispatcher.dispatch(AGENT_ADDED, Time.zone.now, account: account)
end
def create_notification_setting
setting = notification_settings.new(account_id: account.id)
setting.selected_email_flags = [:conversation_assignment]
setting.save!
end
def notify_deletion
Rails.configuration.dispatcher.dispatch(AGENT_REMOVED, Time.zone.now, account: account)
end