diff --git a/app/javascript/dashboard/i18n/locale/en/integrations.json b/app/javascript/dashboard/i18n/locale/en/integrations.json index 2094f269b..54e352ac7 100644 --- a/app/javascript/dashboard/i18n/locale/en/integrations.json +++ b/app/javascript/dashboard/i18n/locale/en/integrations.json @@ -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": { diff --git a/app/javascript/dashboard/routes/dashboard/settings/integrations/Webhooks/WebhookForm.vue b/app/javascript/dashboard/routes/dashboard/settings/integrations/Webhooks/WebhookForm.vue index 3d6be7ae0..6ccd07496 100644 --- a/app/javascript/dashboard/routes/dashboard/settings/integrations/Webhooks/WebhookForm.vue +++ b/app/javascript/dashboard/routes/dashboard/settings/integrations/Webhooks/WebhookForm.vue @@ -61,6 +61,8 @@ const SUPPORTED_WEBHOOK_EVENTS = [ 'message_created', 'message_updated', 'webwidget_triggered', + 'label_added', + 'label_removed', ]; export default { diff --git a/app/listeners/base_listener.rb b/app/listeners/base_listener.rb index ffd57d2a3..974a0f21c 100644 --- a/app/listeners/base_listener.rb +++ b/app/listeners/base_listener.rb @@ -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] diff --git a/app/listeners/webhook_listener.rb b/app/listeners/webhook_listener.rb index 11a86e639..7692cb82c 100644 --- a/app/listeners/webhook_listener.rb +++ b/app/listeners/webhook_listener.rb @@ -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 diff --git a/app/models/concerns/labelable.rb b/app/models/concerns/labelable.rb index 5a71fe84f..da5013923 100644 --- a/app/models/concerns/labelable.rb +++ b/app/models/concerns/labelable.rb @@ -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 diff --git a/app/models/contact.rb b/app/models/contact.rb index 2261d4230..745515990 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -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| diff --git a/app/models/conversation.rb b/app/models/conversation.rb index cf3a4c256..9f1b7e2aa 100644 --- a/app/models/conversation.rb +++ b/app/models/conversation.rb @@ -177,6 +177,7 @@ class Conversation < ApplicationRecord notify_status_change create_activity notify_conversation_updation + notify_label_update end def ensure_snooze_until_reset diff --git a/app/models/webhook.rb b/app/models/webhook.rb index fe97fe583..033849744 100644 --- a/app/models/webhook.rb +++ b/app/models/webhook.rb @@ -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 diff --git a/lib/events/types.rb b/lib/events/types.rb index b015cd3c5..117d22f8a 100644 --- a/lib/events/types.rb +++ b/lib/events/types.rb @@ -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'