2019-09-02 10:52:54 +00:00
|
|
|
# frozen_string_literal: true
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2019-11-30 13:39:55 +00:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: inboxes
|
|
|
|
#
|
2020-02-19 09:10:03 +00:00
|
|
|
# id :integer not null, primary key
|
|
|
|
# channel_type :string
|
|
|
|
# enable_auto_assignment :boolean default(TRUE)
|
|
|
|
# name :string not null
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
# account_id :integer not null
|
|
|
|
# channel_id :integer not null
|
2019-11-30 13:39:55 +00:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_inboxes_on_account_id (account_id)
|
|
|
|
#
|
|
|
|
|
2019-09-02 10:52:54 +00:00
|
|
|
class Inbox < ApplicationRecord
|
2020-03-18 11:23:35 +00:00
|
|
|
include Reportable
|
2020-04-07 04:49:19 +00:00
|
|
|
include Avatarable
|
2020-03-18 11:23:35 +00:00
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
validates :account_id, presence: true
|
|
|
|
|
|
|
|
belongs_to :account
|
2019-10-30 05:19:23 +00:00
|
|
|
|
|
|
|
# TODO: should add associations for the channel types
|
2019-08-14 09:48:44 +00:00
|
|
|
belongs_to :channel, polymorphic: true, dependent: :destroy
|
|
|
|
|
2019-10-27 07:44:36 +00:00
|
|
|
has_many :contact_inboxes, dependent: :destroy
|
|
|
|
has_many :contacts, through: :contact_inboxes
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
has_many :inbox_members, dependent: :destroy
|
|
|
|
has_many :members, through: :inbox_members, source: :user
|
|
|
|
has_many :conversations, dependent: :destroy
|
|
|
|
has_many :messages, through: :conversations
|
2020-03-05 20:13:12 +00:00
|
|
|
|
|
|
|
has_one :agent_bot_inbox, dependent: :destroy
|
2020-04-07 05:11:18 +00:00
|
|
|
has_one :agent_bot, through: :agent_bot_inbox
|
2020-02-26 04:14:24 +00:00
|
|
|
has_many :webhooks, dependent: :destroy
|
2020-03-05 20:13:12 +00:00
|
|
|
|
2019-08-19 08:19:57 +00:00
|
|
|
after_create :subscribe_webhook, if: :facebook?
|
|
|
|
after_destroy :delete_round_robin_agents
|
2019-08-14 09:48:44 +00:00
|
|
|
|
|
|
|
def add_member(user_id)
|
|
|
|
member = inbox_members.new(user_id: user_id)
|
|
|
|
member.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_member(user_id)
|
|
|
|
member = inbox_members.find_by(user_id: user_id)
|
|
|
|
member.try(:destroy)
|
|
|
|
end
|
|
|
|
|
|
|
|
def facebook?
|
2019-10-20 10:49:12 +00:00
|
|
|
channel.class.name.to_s == 'Channel::FacebookPage'
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
def web_widget?
|
|
|
|
channel.class.name.to_s == 'Channel::WebWidget'
|
|
|
|
end
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
def next_available_agent
|
2019-09-02 10:52:54 +00:00
|
|
|
user_id = Redis::Alfred.rpoplpush(round_robin_key, round_robin_key)
|
2019-08-14 09:48:44 +00:00
|
|
|
account.users.find_by(id: user_id)
|
|
|
|
end
|
|
|
|
|
2020-02-26 04:14:24 +00:00
|
|
|
def webhook_data
|
|
|
|
{
|
|
|
|
id: id,
|
|
|
|
name: name
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def delete_round_robin_agents
|
|
|
|
Redis::Alfred.delete(round_robin_key)
|
|
|
|
end
|
|
|
|
|
|
|
|
def round_robin_key
|
2019-10-20 08:47:26 +00:00
|
|
|
format(Constants::RedisKeys::ROUND_ROBIN_AGENTS, inbox_id: id)
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def subscribe_webhook
|
2019-10-21 05:48:47 +00:00
|
|
|
Facebook::Messenger::Subscriptions.subscribe(
|
|
|
|
access_token: channel.page_access_token,
|
|
|
|
subscribed_fields: %w[
|
2020-01-07 17:29:17 +00:00
|
|
|
messages messaging_postbacks messaging_optins message_deliveries
|
|
|
|
message_reads messaging_payments messaging_pre_checkouts messaging_checkout_updates
|
|
|
|
messaging_account_linking messaging_referrals message_echoes messaging_game_plays
|
|
|
|
standby messaging_handovers messaging_policy_enforcement message_reactions
|
2019-10-21 05:48:47 +00:00
|
|
|
]
|
|
|
|
)
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
end
|