Skip to content

Commit 718a9e6

Browse files
committed
feat: Add entire conversation on webhook events, so clients don't have to fetch conversation again.
Closes #208
1 parent 5b393c2 commit 718a9e6

File tree

2 files changed

+33
-19
lines changed

2 files changed

+33
-19
lines changed

internal/conversation/conversation.go

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -541,18 +541,19 @@ func (c *Manager) UpdateConversationUserAssignee(uuid string, assigneeID int, ac
541541
return envelope.NewError(envelope.GeneralError, c.i18n.Ts("globals.messages.errorUpdating", "name", "{globals.terms.conversation}"), nil)
542542
}
543543

544-
c.webhookStore.TriggerEvent(wmodels.EventConversationAssigned, map[string]any{
545-
"conversation_uuid": uuid,
546-
"assigned_to": assigneeID,
547-
"actor_id": actor.ID,
548-
})
549-
550544
// Refetch the conversation to get the updated details.
551545
conversation, err := c.GetConversation(0, uuid, "")
552546
if err != nil {
553547
return err
554548
}
555549

550+
c.webhookStore.TriggerEvent(wmodels.EventConversationAssigned, map[string]any{
551+
"conversation_uuid": uuid,
552+
"assigned_to": assigneeID,
553+
"actor_id": actor.ID,
554+
"conversation": conversation,
555+
})
556+
556557
// Evaluate automation rules.
557558
c.automation.EvaluateConversationUpdateRules(conversation, amodels.EventConversationUserAssigned)
558559

@@ -715,6 +716,12 @@ func (c *Manager) UpdateConversationStatus(uuid string, statusID int, status, sn
715716
return envelope.NewError(envelope.GeneralError, c.i18n.Ts("globals.messages.errorUpdating", "name", "{globals.terms.conversation}"), nil)
716717
}
717718

719+
// Fetch conversation for webhook and automation rules.
720+
conversation, err := c.GetConversation(0, uuid, "")
721+
if err != nil {
722+
c.lo.Error("error fetching conversation after status change", "uuid", uuid, "error", err)
723+
}
724+
718725
// Trigger webhook for conversation status change
719726
var snoozeUntilStr string
720727
if !snoozeUntil.IsZero() {
@@ -726,6 +733,7 @@ func (c *Manager) UpdateConversationStatus(uuid string, statusID int, status, sn
726733
"new_status": status,
727734
"snooze_until": snoozeUntilStr,
728735
"actor_id": actor.ID,
736+
"conversation": conversation,
729737
})
730738

731739
// Record the status change as an activity.
@@ -737,10 +745,7 @@ func (c *Manager) UpdateConversationStatus(uuid string, statusID int, status, sn
737745
c.BroadcastConversationUpdate(uuid, "status", status)
738746

739747
// Evaluate automation rules.
740-
conversation, err := c.GetConversation(0, uuid, "")
741-
if err != nil {
742-
c.lo.Error("error fetching conversation after status change", "uuid", uuid, "error", err)
743-
} else {
748+
if conversation.ID != 0 {
744749
c.automation.EvaluateConversationUpdateRules(conversation, amodels.EventConversationStatusChange)
745750
}
746751

@@ -797,6 +802,12 @@ func (c *Manager) SetConversationTags(uuid string, action string, tagNames []str
797802
return envelope.NewError(envelope.GeneralError, c.i18n.Ts("globals.messages.errorFetching", "name", "{globals.terms.tag}"), nil)
798803
}
799804

805+
// Fetch conversation for webhook.
806+
conversation, err := c.GetConversation(0, uuid, "")
807+
if err != nil {
808+
c.lo.Error("error fetching conversation after tags change", "uuid", uuid, "error", err)
809+
}
810+
800811
// Trigger webhook for conversation tags changed.
801812
if newTags == nil {
802813
newTags = []string{}
@@ -806,6 +817,7 @@ func (c *Manager) SetConversationTags(uuid string, action string, tagNames []str
806817
"previous_tags": prevTags,
807818
"new_tags": newTags,
808819
"actor_id": actor.ID,
820+
"conversation": conversation,
809821
})
810822

811823
// Find actually removed tags.
@@ -1146,9 +1158,14 @@ func (m *Manager) RemoveConversationAssignee(uuid, typ string, actor umodels.Use
11461158

11471159
// Trigger webhook for conversation unassigned from user.
11481160
if typ == models.AssigneeTypeUser {
1161+
conversation, err := m.GetConversation(0, uuid, "")
1162+
if err != nil {
1163+
m.lo.Error("error fetching conversation after unassignment", "uuid", uuid, "error", err)
1164+
}
11491165
m.webhookStore.TriggerEvent(wmodels.EventConversationUnassigned, map[string]any{
11501166
"conversation_uuid": uuid,
11511167
"actor_id": actor.ID,
1168+
"conversation": conversation,
11521169
})
11531170
}
11541171

internal/conversation/message.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -514,15 +514,12 @@ func (m *Manager) InsertMessage(message *models.Message) error {
514514
// Broadcast new message.
515515
m.BroadcastNewMessage(message)
516516

517-
// Refetch message if this message has media attachments, as media gets linked after inserting the message.
518-
if len(message.Media) > 0 {
519-
refetchedMessage, err := m.GetMessage(message.UUID)
520-
if err != nil {
521-
m.lo.Error("error fetching message after insert", "error", err)
522-
} else {
523-
// Replace the message in the struct with the refetched message.
524-
*message = refetchedMessage
525-
}
517+
// Refetch the message to get all fields populated (e.g., author, media URLs).
518+
refetchedMessage, err := m.GetMessage(message.UUID)
519+
if err != nil {
520+
m.lo.Error("error fetching message after insert", "error", err)
521+
} else {
522+
*message = refetchedMessage
526523
}
527524

528525
// Trigger webhook for new message created.

0 commit comments

Comments
 (0)