Skip to content

Commit

Permalink
fix: Add an action cable events for label updates (#5694)
Browse files Browse the repository at this point in the history
Co-authored-by: Pranav Raj S <pranav@chatwoot.com>
  • Loading branch information
tejaswinichile and pranavrajs committed Oct 21, 2022
1 parent 7821654 commit 4a299a9
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 2 deletions.
Expand Up @@ -252,6 +252,12 @@ const actions = {
meta: { sender },
} = conversation;
commit(types.UPDATE_CONVERSATION, conversation);

dispatch('conversationLabels/setConversationLabel', {
id: conversation.id,
data: conversation.labels,
});

dispatch('contacts/setContact', sender);
},

Expand Down
2 changes: 2 additions & 0 deletions app/models/conversation.rb
Expand Up @@ -262,6 +262,8 @@ def create_label_change(user_name)
previous_labels, current_labels = previous_changes[:label_list]
return unless (previous_labels.is_a? Array) && (current_labels.is_a? Array)

dispatcher_dispatch(CONVERSATION_UPDATED, previous_changes)

create_label_added(user_name, current_labels - previous_labels)
create_label_removed(user_name, previous_labels - current_labels)
end
Expand Down
1 change: 1 addition & 0 deletions app/presenters/conversations/event_data_presenter.rb
Expand Up @@ -8,6 +8,7 @@ def push_data
id: display_id,
inbox_id: inbox_id,
messages: push_messages,
labels: label_list,
meta: push_meta,
status: status,
custom_attributes: custom_attributes,
Expand Down
15 changes: 15 additions & 0 deletions spec/listeners/action_cable_listener_spec.rb
Expand Up @@ -133,6 +133,10 @@
let(:event_name) { :'conversation.updated' }
let!(:event) { Events::Base.new(event_name, Time.zone.now, conversation: conversation, user: agent, is_private: false) }

before do
conversation.add_labels(['support'])
end

it 'sends update to inbox members' do
expect(conversation.inbox.reload.inbox_members.count).to eq(1)

Expand All @@ -143,5 +147,16 @@
)
listener.conversation_updated(event)
end

it 'broadcast event with label data' do
expect(conversation.push_event_data[:labels]).to eq(conversation.label_list)

expect(ActionCableBroadcastJob).to receive(:perform_later).with(
[agent.pubsub_token, admin.pubsub_token, conversation.contact_inbox.pubsub_token],
'conversation.updated',
conversation.push_event_data.merge(account_id: account.id)
)
listener.conversation_updated(event)
end
end
end
18 changes: 16 additions & 2 deletions spec/models/conversation_spec.rb
Expand Up @@ -109,12 +109,25 @@
Current.user = old_assignee
end

it 'sends conversation updated event if labels are updated' do
conversation.update(label_list: [label.title])
changed_attributes = conversation.previous_changes
expect(Rails.configuration.dispatcher).to have_received(:dispatch)
.with(
described_class::CONVERSATION_UPDATED,
kind_of(Time),
conversation: conversation,
notifiable_assignee_change: false,
changed_attributes: changed_attributes,
performed_by: nil
)
end

it 'runs after_update callbacks' do
conversation.update(
status: :resolved,
contact_last_seen_at: Time.now,
assignee: new_assignee,
label_list: [label.title]
assignee: new_assignee
)
status_change = conversation.status_change
changed_attributes = conversation.previous_changes
Expand Down Expand Up @@ -433,6 +446,7 @@
},
id: conversation.display_id,
messages: [],
labels: [],
inbox_id: conversation.inbox_id,
status: conversation.status,
contact_inbox: conversation.contact_inbox,
Expand Down
1 change: 1 addition & 0 deletions spec/presenters/conversations/event_data_presenter_spec.rb
Expand Up @@ -19,6 +19,7 @@
id: conversation.display_id,
messages: [],
inbox_id: conversation.inbox_id,
labels: [],
status: conversation.status,
contact_inbox: conversation.contact_inbox,
can_reply: conversation.can_reply?,
Expand Down

0 comments on commit 4a299a9

Please sign in to comment.