chore: Add label webhook events

This commit is contained in:
Pranav Raj S 2022-04-25 10:53:16 +05:30
parent fca600d4c2
commit e11d1e31e5
9 changed files with 65 additions and 6 deletions

View file

@ -14,7 +14,9 @@
"CONVERSATION_UPDATED": "Conversation Updated",
"MESSAGE_CREATED": "Message created",
"MESSAGE_UPDATED": "Message updated",
"WEBWIDGET_TRIGGERED": "Live chat widget opened by the user"
"WEBWIDGET_TRIGGERED": "Live chat widget opened by the user",
"LABEL_ADDED": "Label added to a contact or a conversation",
"LABEL_REMOVED": "Label removed from a contact or a conversation"
}
},
"END_POINT": {

View file

@ -61,6 +61,8 @@ const SUPPORTED_WEBHOOK_EVENTS = [
'message_created',
'message_updated',
'webwidget_triggered',
'label_added',
'label_removed',
];
export default {

View file

@ -24,6 +24,20 @@ class BaseListener
[contact, contact.account]
end
def extract_label_and_account(event)
data = event.data[:data]
labels = event.data[:labels]
type = event.data[:type].downcase
payload = {
labels: labels,
data: data.webhook_data,
type: type
}
[payload, data.account]
end
def extract_changed_attributes(event)
changed_attributes = event.data[:changed_attributes]

View file

@ -51,10 +51,24 @@ class WebhookListener < BaseListener
deliver_webhook_payloads(payload, inbox)
end
def label_added(event)
data, account = extract_label_and_account(event)
payload = data.webhook_data.merge(event: __method__.to_s)
deliver_account_webhooks(payload, account)
end
def label_removed(event)
data, account = extract_label_and_account(event)
payload = data.merge(event: __method__.to_s)
deliver_account_webhooks(payload, account)
end
private
def deliver_account_webhooks(payload, inbox)
inbox.account.webhooks.account.each do |webhook|
def deliver_account_webhooks(payload, account)
account.webhooks.account.each do |webhook|
next unless webhook.subscriptions.include?(payload[:event])
WebhookJob.perform_later(webhook.url, payload)
@ -69,7 +83,7 @@ class WebhookListener < BaseListener
end
def deliver_webhook_payloads(payload, inbox)
deliver_account_webhooks(payload, inbox)
deliver_account_webhooks(payload, inbox.account)
deliver_api_inbox_webhooks(payload, inbox)
end
end

View file

@ -1,5 +1,6 @@
module Labelable
extend ActiveSupport::Concern
include Events::Types
included do
acts_as_taggable_on :labels
@ -13,4 +14,25 @@ module Labelable
new_labels << labels
update!(label_list: new_labels)
end
private
def notify_label_update
return unless saved_change_to_label_list?
previous_labels, current_labels = previous_changes[:label_list]
return unless (previous_labels.is_a? Array) && (current_labels.is_a? Array)
added_labels = current_labels - previous_labels
removed_labels = previous_labels - current_labels
dispatch_label_event(LABEL_ADDED, added_labels)
dispatch_label_event(LABEL_REMOVED, removed_labels)
end
def dispatch_label_event(event_name, labels)
return unless labels.size.positive?
Rails.configuration.dispatcher.dispatch(event_name, Time.zone.now, type: self.class.name, data: self, labels: labels)
end
end

View file

@ -45,7 +45,7 @@ class Contact < ApplicationRecord
before_validation :prepare_contact_attributes
after_create_commit :dispatch_create_event, :ip_lookup
after_update_commit :dispatch_update_event
after_update_commit :dispatch_update_event, :notify_label_update
after_destroy_commit :dispatch_destroy_event
scope :order_on_last_activity_at, lambda { |direction|

View file

@ -177,6 +177,7 @@ class Conversation < ApplicationRecord
notify_status_change
create_activity
notify_conversation_updation
notify_label_update
end
def ensure_snooze_until_reset

View file

@ -26,7 +26,7 @@ class Webhook < ApplicationRecord
enum webhook_type: { account: 0, inbox: 1 }
ALLOWED_WEBHOOK_EVENTS = %w[conversation_status_changed conversation_updated conversation_created message_created message_updated
webwidget_triggered].freeze
webwidget_triggered label_added label_removed].freeze
private

View file

@ -39,6 +39,10 @@ module Events::Types
CONTACT_MERGED = 'contact.merged'
CONTACT_DELETED = 'contact.deleted'
# label events
LABEL_ADDED = 'label.added'
LABEL_REMOVED = 'label.removed'
# notification events
NOTIFICATION_CREATED = 'notification.created'