Feature: Rich Message Types (#610)
Co-authored-by: Pranav Raj S <pranavrajs@gmail.com> Co-authored-by: Nithin David Thomas <webofnithin@gmail.com>
This commit is contained in:
@@ -51,11 +51,12 @@ class Channel::WebWidget < ApplicationRecord
|
||||
def create_contact_inbox
|
||||
ActiveRecord::Base.transaction do
|
||||
contact = inbox.account.contacts.create!(name: ::Haikunator.haikunate(1000))
|
||||
::ContactInbox.create!(
|
||||
contact_inbox = ::ContactInbox.create!(
|
||||
contact_id: contact.id,
|
||||
inbox_id: inbox.id,
|
||||
source_id: SecureRandom.uuid
|
||||
)
|
||||
contact_inbox
|
||||
rescue StandardError => e
|
||||
Rails.logger e
|
||||
end
|
||||
|
||||
52
app/models/concerns/content_attribute_validator.rb
Normal file
52
app/models/concerns/content_attribute_validator.rb
Normal file
@@ -0,0 +1,52 @@
|
||||
class ContentAttributeValidator < ActiveModel::Validator
|
||||
ALLOWED_SELECT_ITEM_KEYS = [:title, :value].freeze
|
||||
ALLOWED_CARD_ITEM_KEYS = [:title, :description, :media_url, :actions].freeze
|
||||
ALLOWED_CARD_ITEM_ACTION_KEYS = [:text, :type, :payload, :uri].freeze
|
||||
ALLOWED_FORM_ITEM_KEYS = [:type, :placeholder, :label, :name, :options].freeze
|
||||
ALLOWED_ARTICLE_KEYS = [:title, :description, :link].freeze
|
||||
|
||||
def validate(record)
|
||||
case record.content_type
|
||||
when 'input_select'
|
||||
validate_items!(record)
|
||||
validate_item_attributes!(record, ALLOWED_SELECT_ITEM_KEYS)
|
||||
when 'cards'
|
||||
validate_items!(record)
|
||||
validate_item_attributes!(record, ALLOWED_CARD_ITEM_KEYS)
|
||||
validate_item_actions!(record)
|
||||
when 'form'
|
||||
validate_items!(record)
|
||||
validate_item_attributes!(record, ALLOWED_FORM_ITEM_KEYS)
|
||||
when 'article'
|
||||
validate_items!(record)
|
||||
validate_item_attributes!(record, ALLOWED_ARTICLE_KEYS)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def validate_items!(record)
|
||||
record.errors.add(:content_attributes, 'At least one item is required.') if record.items.blank?
|
||||
record.errors.add(:content_attributes, 'Items should be a hash.') if record.items.reject { |item| item.is_a?(Hash) }.present?
|
||||
end
|
||||
|
||||
def validate_item_attributes!(record, valid_keys)
|
||||
item_keys = record.items.collect(&:keys).flatten.map(&:to_sym).compact
|
||||
invalid_keys = item_keys - valid_keys
|
||||
record.errors.add(:content_attributes, "contains invalid keys for items : #{invalid_keys}") if invalid_keys.present?
|
||||
end
|
||||
|
||||
def validate_item_actions!(record)
|
||||
if record.items.select { |item| item[:actions].blank? }.present?
|
||||
record.errors.add(:content_attributes, 'contains items missing actions') && return
|
||||
end
|
||||
|
||||
validate_item_action_attributes!(record)
|
||||
end
|
||||
|
||||
def validate_item_action_attributes!(record)
|
||||
item_action_keys = record.items.collect { |item| item[:actions].collect(&:keys) }
|
||||
invalid_keys = item_action_keys.flatten.compact.map(&:to_sym) - ALLOWED_CARD_ITEM_ACTION_KEYS
|
||||
record.errors.add(:content_attributes, "contains invalid keys for actions: #{invalid_keys}") if invalid_keys.present?
|
||||
end
|
||||
end
|
||||
@@ -31,4 +31,19 @@ class ContactInbox < ApplicationRecord
|
||||
belongs_to :inbox
|
||||
|
||||
has_many :conversations, dependent: :destroy
|
||||
|
||||
def webhook_data
|
||||
{
|
||||
id: id,
|
||||
contact: contact.try(:webhook_data),
|
||||
inbox: inbox.webhook_data,
|
||||
account: inbox.account.webhook_data,
|
||||
current_conversation: current_conversation.try(:webhook_data),
|
||||
source_id: source_id
|
||||
}
|
||||
end
|
||||
|
||||
def current_conversation
|
||||
conversations.last
|
||||
end
|
||||
end
|
||||
|
||||
@@ -38,11 +38,21 @@ class Message < ApplicationRecord
|
||||
validates :account_id, presence: true
|
||||
validates :inbox_id, presence: true
|
||||
validates :conversation_id, presence: true
|
||||
validates_with ContentAttributeValidator
|
||||
|
||||
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 content_type: {
|
||||
text: 0,
|
||||
input_text: 1,
|
||||
input_textarea: 2,
|
||||
input_email: 3,
|
||||
input_select: 4,
|
||||
cards: 5,
|
||||
form: 6,
|
||||
article: 7
|
||||
}
|
||||
enum status: { sent: 0, delivered: 1, read: 2, failed: 3 }
|
||||
store :content_attributes, accessors: [:submitted_email], coder: JSON, prefix: :input
|
||||
store :content_attributes, accessors: [:submitted_email, :items, :submitted_values], coder: JSON
|
||||
|
||||
# .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) }
|
||||
@@ -63,6 +73,8 @@ class Message < ApplicationRecord
|
||||
:execute_message_template_hooks,
|
||||
:notify_via_mail
|
||||
|
||||
after_update :dispatch_update_event
|
||||
|
||||
def channel_token
|
||||
@token ||= inbox.channel.try(:page_access_token)
|
||||
end
|
||||
@@ -88,6 +100,8 @@ class Message < ApplicationRecord
|
||||
content: content,
|
||||
created_at: created_at,
|
||||
message_type: message_type,
|
||||
content_type: content_type,
|
||||
content_attributes: content_attributes,
|
||||
source_id: source_id,
|
||||
sender: user.try(:webhook_data),
|
||||
contact: contact.try(:webhook_data),
|
||||
@@ -107,6 +121,10 @@ class Message < ApplicationRecord
|
||||
end
|
||||
end
|
||||
|
||||
def dispatch_update_event
|
||||
Rails.configuration.dispatcher.dispatch(MESSAGE_UPDATED, Time.zone.now, message: self)
|
||||
end
|
||||
|
||||
def send_reply
|
||||
channel_name = conversation.inbox.channel.class.to_s
|
||||
if channel_name == 'Channel::FacebookPage'
|
||||
|
||||
Reference in New Issue
Block a user