feat(conversations): finish message mutation parity
This commit is contained in:
@@ -3,6 +3,7 @@ package v1
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -177,6 +178,12 @@ func serializeConversationMeta(ctx context.Context, db *gorm.DB, conversation *m
|
||||
meta.AssigneeType = "User"
|
||||
}
|
||||
}
|
||||
if conversation.TeamID != nil && *conversation.TeamID != 0 {
|
||||
var team model.Team
|
||||
if err := db.WithContext(ctx).First(&team, *conversation.TeamID).Error; err == nil {
|
||||
meta.Team = serializeTeam(&team)
|
||||
}
|
||||
}
|
||||
|
||||
if conversation.ContactInboxID != nil && *conversation.ContactInboxID != 0 {
|
||||
var contactInbox model.ContactInbox
|
||||
@@ -249,9 +256,35 @@ func serializeMessage(ctx context.Context, db *gorm.DB, message *model.Message,
|
||||
}
|
||||
}
|
||||
}
|
||||
if db != nil {
|
||||
var attachments []model.Attachment
|
||||
if err := db.WithContext(ctx).Where("message_id = ?", message.ID).Order("id ASC").Find(&attachments).Error; err == nil && len(attachments) > 0 {
|
||||
payload.Attachments = make([]any, 0, len(attachments))
|
||||
for i := range attachments {
|
||||
payload.Attachments = append(payload.Attachments, serializeAttachment(&attachments[i]))
|
||||
}
|
||||
}
|
||||
}
|
||||
return payload
|
||||
}
|
||||
|
||||
func serializeAttachment(attachment *model.Attachment) map[string]any {
|
||||
extension := strings.TrimPrefix(filepath.Ext(attachment.FileName), ".")
|
||||
dataURL := nonEmpty(attachment.FileURL, attachment.ExternalURL)
|
||||
return map[string]any{
|
||||
"id": attachment.ID,
|
||||
"message_id": attachment.MessageID,
|
||||
"file_type": attachment.FileType,
|
||||
"account_id": attachment.AccountID,
|
||||
"data_url": dataURL,
|
||||
"thumb_url": attachment.ThumbURL,
|
||||
"file_size": attachment.FileSize,
|
||||
"extension": extension,
|
||||
"width": attachment.Width,
|
||||
"height": attachment.Height,
|
||||
}
|
||||
}
|
||||
|
||||
func serializeContact(contact *model.Contact) map[string]any {
|
||||
return map[string]any{
|
||||
"additional_attributes": jsonObject(contact.AdditionalAttributes),
|
||||
@@ -296,6 +329,29 @@ func serializeUserFromDB(ctx context.Context, db *gorm.DB, userID uint, accountI
|
||||
return serializeUser(&user, accountID)
|
||||
}
|
||||
|
||||
func serializeTeam(team *model.Team) map[string]any {
|
||||
return map[string]any{
|
||||
"id": team.ID,
|
||||
"account_id": team.AccountID,
|
||||
"name": team.Name,
|
||||
"description": team.Description,
|
||||
"allow_auto_assignment": team.AllowAutoAssignment,
|
||||
"created_at": team.CreatedAt.Unix(),
|
||||
"updated_at": team.UpdatedAt.Unix(),
|
||||
}
|
||||
}
|
||||
|
||||
func serializeTeamFromDB(ctx context.Context, db *gorm.DB, teamID uint, accountID uint) any {
|
||||
if teamID == 0 || db == nil {
|
||||
return nil
|
||||
}
|
||||
var team model.Team
|
||||
if err := db.WithContext(ctx).Where("id = ? AND account_id = ?", teamID, accountID).First(&team).Error; err != nil {
|
||||
return nil
|
||||
}
|
||||
return serializeTeam(&team)
|
||||
}
|
||||
|
||||
func conversationDisplayID(conversation *model.Conversation) uint {
|
||||
if conversation.DisplayID != nil && *conversation.DisplayID != 0 {
|
||||
return *conversation.DisplayID
|
||||
|
||||
Reference in New Issue
Block a user