H-337: restore Web widget reply visibility (#64)

* H-337: restore widget reply delivery

* H-337: harden widget conversation ownership

---------

Co-authored-by: Rogee <rogee@ipao.vip>
This commit is contained in:
Rogee
2026-08-20 22:22:27 +08:00
committed by GitHub
co-authored by rogee
parent abb1bed424
commit b31b1b9562
22 changed files with 847 additions and 83 deletions
@@ -204,7 +204,11 @@ func (h *WidgetHandler) SendMessage(c *gin.Context) {
c.JSON(http.StatusOK, resp)
return
}
payload := widgetMessagePayload(resp.Message, resp.ConversationID)
conversationID := resp.ConversationID
if conversation, lookupErr := h.widgetService.GetConversation(c.Request.Context(), widgetToken, resp.ConversationID); lookupErr == nil {
conversationID = widgetConversationID(*conversation)
}
payload := widgetMessagePayload(resp.Message, conversationID)
if len(resp.Attachments) > 0 {
payload["attachments"] = widgetAttachmentPayloads(resp.Attachments)
}
@@ -271,8 +275,12 @@ func (h *WidgetHandler) GetLatestMessages(c *gin.Context) {
}
payload := make([]gin.H, 0, len(messages))
conversationID := uint(0)
if conversation != nil {
conversationID = widgetConversationID(*conversation)
}
for _, msg := range messages {
messagePayload := widgetMessagePayload(msg, msg.ConversationID)
messagePayload := widgetMessagePayload(msg, conversationID)
if attachments, err := h.widgetService.GetMessageAttachments(c.Request.Context(), msg.ID); err == nil && len(attachments) > 0 {
messagePayload["attachments"] = widgetAttachmentPayloads(attachments)
}
@@ -350,7 +358,7 @@ func (h *WidgetHandler) CreateConversation(c *gin.Context) {
return
}
payload := widgetConversationPayload(*conversation)
payload["messages"] = []gin.H{widgetMessagePayload(resp.Message, resp.ConversationID)}
payload["messages"] = []gin.H{widgetMessagePayload(resp.Message, widgetConversationID(*conversation))}
c.JSON(http.StatusOK, payload)
}
@@ -1275,7 +1283,7 @@ func widgetMessagePayload(message model.Message, conversationID uint) gin.H {
"content": message.Content,
"inbox_id": message.InboxID,
"conversation_id": conversationID,
"message_type": message.MessageType,
"message_type": widgetMessageType(message.MessageType),
"content_type": message.ContentType,
"content_attributes": webhookutil.SanitizeOutboundJSON(message.ContentAttributes),
"created_at": message.CreatedAt.Unix(),
@@ -1284,6 +1292,26 @@ func widgetMessagePayload(message model.Message, conversationID uint) gin.H {
}
}
func widgetMessageType(value string) int {
switch strings.ToLower(strings.TrimSpace(value)) {
case "incoming":
return 0
case "activity":
return 2
case "template":
return 3
default:
return 1
}
}
func widgetConversationID(conversation model.Conversation) uint {
if conversation.DisplayID != nil && *conversation.DisplayID != 0 {
return *conversation.DisplayID
}
return conversation.ID
}
func widgetAttachmentPayloads(attachments []model.Attachment) []gin.H {
payload := make([]gin.H, 0, len(attachments))
for _, attachment := range attachments {