[Feature] Email collect message hooks (#331)

- Add email collect hook on creating conversation
- Merge contact if it already exist
This commit is contained in:
Sojan Jose
2020-01-09 13:06:40 +05:30
committed by Pranav Raj S
parent 59d4eaeca7
commit 722f540b03
68 changed files with 1111 additions and 544 deletions

View File

@@ -17,31 +17,29 @@
# index_channel_facebook_pages_on_page_id_and_account_id (page_id,account_id) UNIQUE
#
module Channel
class FacebookPage < ApplicationRecord
include Avatarable
class Channel::FacebookPage < ApplicationRecord
include Avatarable
self.table_name = 'channel_facebook_pages'
self.table_name = 'channel_facebook_pages'
validates :account_id, presence: true
validates :page_id, uniqueness: { scope: :account_id }
has_one_attached :avatar
belongs_to :account
validates :account_id, presence: true
validates :page_id, uniqueness: { scope: :account_id }
has_one_attached :avatar
belongs_to :account
has_one :inbox, as: :channel, dependent: :destroy
has_one :inbox, as: :channel, dependent: :destroy
before_destroy :unsubscribe
before_destroy :unsubscribe
def name
'Facebook'
end
def name
'Facebook'
end
private
private
def unsubscribe
Facebook::Messenger::Subscriptions.unsubscribe(access_token: page_access_token)
rescue => e
true
end
def unsubscribe
Facebook::Messenger::Subscriptions.unsubscribe(access_token: page_access_token)
rescue => e
true
end
end

View File

@@ -16,33 +16,31 @@
# index_channel_web_widgets_on_website_token (website_token) UNIQUE
#
module Channel
class WebWidget < ApplicationRecord
self.table_name = 'channel_web_widgets'
class Channel::WebWidget < ApplicationRecord
self.table_name = 'channel_web_widgets'
validates :website_name, presence: true
validates :website_url, presence: true
validates :widget_color, presence: true
validates :website_name, presence: true
validates :website_url, presence: true
validates :widget_color, presence: true
belongs_to :account
has_one :inbox, as: :channel, dependent: :destroy
has_secure_token :website_token
belongs_to :account
has_one :inbox, as: :channel, dependent: :destroy
has_secure_token :website_token
def name
'Website'
end
def name
'Website'
end
def create_contact_inbox
ActiveRecord::Base.transaction do
contact = inbox.account.contacts.create!(name: ::Haikunator.haikunate(1000))
::ContactInbox.create!(
contact_id: contact.id,
inbox_id: inbox.id,
source_id: SecureRandom.uuid
)
rescue StandardError => e
Rails.logger e
end
def create_contact_inbox
ActiveRecord::Base.transaction do
contact = inbox.account.contacts.create!(name: ::Haikunator.haikunate(1000))
::ContactInbox.create!(
contact_id: contact.id,
inbox_id: inbox.id,
source_id: SecureRandom.uuid
)
rescue StandardError => e
Rails.logger e
end
end
end

View File

@@ -29,4 +29,6 @@ class ContactInbox < ApplicationRecord
belongs_to :contact
belongs_to :inbox
has_many :conversations, dependent: :destroy
end

View File

@@ -13,6 +13,7 @@
# account_id :integer not null
# assignee_id :integer
# contact_id :bigint
# contact_inbox_id :bigint
# display_id :integer not null
# inbox_id :integer not null
#
@@ -20,6 +21,11 @@
#
# index_conversations_on_account_id (account_id)
# index_conversations_on_account_id_and_display_id (account_id,display_id) UNIQUE
# index_conversations_on_contact_inbox_id (contact_inbox_id)
#
# Foreign Keys
#
# fk_rails_... (contact_inbox_id => contact_inboxes.id)
#
class Conversation < ApplicationRecord
@@ -38,6 +44,7 @@ class Conversation < ApplicationRecord
belongs_to :inbox
belongs_to :assignee, class_name: 'User', optional: true
belongs_to :contact
belongs_to :contact_inbox
has_many :messages, dependent: :destroy, autosave: true

View File

@@ -49,6 +49,10 @@ class Inbox < ApplicationRecord
channel.class.name.to_s == 'Channel::FacebookPage'
end
def web_widget?
channel.class.name.to_s == 'Channel::WebWidget'
end
def next_available_agent
user_id = Redis::Alfred.rpoplpush(round_robin_key, round_robin_key)
account.users.find_by(id: user_id)

View File

@@ -2,18 +2,20 @@
#
# Table name: messages
#
# id :integer not null, primary key
# content :text
# message_type :integer not null
# private :boolean default(FALSE)
# status :integer default("sent")
# created_at :datetime not null
# updated_at :datetime not null
# account_id :integer not null
# conversation_id :integer not null
# fb_id :string
# inbox_id :integer not null
# user_id :integer
# id :integer not null, primary key
# content :text
# content_attributes :json
# content_type :integer default("text")
# message_type :integer not null
# private :boolean default(FALSE)
# status :integer default("sent")
# created_at :datetime not null
# updated_at :datetime not null
# account_id :integer not null
# conversation_id :integer not null
# fb_id :string
# inbox_id :integer not null
# user_id :integer
#
# Indexes
#
@@ -27,8 +29,10 @@ class Message < ApplicationRecord
validates :inbox_id, presence: true
validates :conversation_id, presence: true
enum message_type: [:incoming, :outgoing, :activity]
enum status: [:sent, :delivered, :read, :failed]
enum message_type: { incoming: 0, outgoing: 1, activity: 2, template: 3 }
enum content_type: { text: 0, input: 1, input_textarea: 2, input_email: 3 }
enum status: { sent: 0, delivered: 1, read: 2, failed: 3 }
store :content_attributes, accessors: [:submitted_email], coder: JSON, prefix: :input
# .succ is a hack to avoid https://makandracards.com/makandra/1057-why-two-ruby-time-objects-are-not-equal-although-they-appear-to-be
scope :unread_since, ->(datetime) { where('EXTRACT(EPOCH FROM created_at) > (?)', datetime.to_i.succ) }
@@ -44,7 +48,8 @@ class Message < ApplicationRecord
after_create :reopen_conversation,
:dispatch_event,
:send_reply
:send_reply,
:execute_message_template_hooks
def channel_token
@token ||= inbox.channel.try(:page_access_token)
@@ -81,4 +86,8 @@ class Message < ApplicationRecord
Rails.configuration.dispatcher.dispatch(CONVERSATION_REOPENED, Time.zone.now, conversation: conversation)
end
end
def execute_message_template_hooks
::MessageTemplates::HookExecutionService.new(message: self).perform
end
end