forked from mtusi2026/mtusi2026lab4
feat(attachments): add attachment metadata and message links
This commit is contained in:
+32
-15
@@ -10,15 +10,17 @@ import (
|
||||
)
|
||||
|
||||
type CreateMessageRequest struct {
|
||||
Text string `json:"text"`
|
||||
Text string `json:"text"`
|
||||
AttachmentIDs []string `json:"attachment_ids"`
|
||||
}
|
||||
|
||||
type MessageResponse struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
ChatID uuid.UUID `json:"chat_id"`
|
||||
SenderID uuid.UUID `json:"sender_id"`
|
||||
Text string `json:"text"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
ID uuid.UUID `json:"id"`
|
||||
ChatID uuid.UUID `json:"chat_id"`
|
||||
SenderID uuid.UUID `json:"sender_id"`
|
||||
Text string `json:"text"`
|
||||
AttachmentIDs []uuid.UUID `json:"attachment_ids"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
type MessagePageResponse struct {
|
||||
@@ -27,21 +29,36 @@ type MessagePageResponse struct {
|
||||
}
|
||||
|
||||
func (r CreateMessageRequest) Validate() bool {
|
||||
if strings.TrimSpace(r.Text) == "" ||
|
||||
strings.ContainsRune(r.Text, 0) ||
|
||||
utf8.RuneCountInString(r.Text) > 4000 {
|
||||
if strings.ContainsRune(r.Text, 0) || utf8.RuneCountInString(r.Text) > 4000 {
|
||||
return false
|
||||
}
|
||||
if len(r.AttachmentIDs) > 10 {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
seen := make(map[string]struct{}, len(r.AttachmentIDs))
|
||||
for _, id := range r.AttachmentIDs {
|
||||
if _, ok := seen[id]; ok {
|
||||
return false
|
||||
}
|
||||
seen[id] = struct{}{}
|
||||
}
|
||||
|
||||
hasText := strings.TrimSpace(r.Text) != ""
|
||||
hasAttachment := len(r.AttachmentIDs) > 0
|
||||
|
||||
return hasText || hasAttachment
|
||||
}
|
||||
|
||||
func ToMessageResponse(m model.Message) MessageResponse {
|
||||
ids := make([]uuid.UUID, len(m.AttachmentIDs))
|
||||
copy(ids, m.AttachmentIDs)
|
||||
return MessageResponse{
|
||||
ID: m.ID,
|
||||
ChatID: m.ChatID,
|
||||
SenderID: m.SenderID,
|
||||
Text: m.Text,
|
||||
CreatedAt: m.CreatedAt,
|
||||
ID: m.ID,
|
||||
ChatID: m.ChatID,
|
||||
SenderID: m.SenderID,
|
||||
Text: m.Text,
|
||||
AttachmentIDs: ids,
|
||||
CreatedAt: m.CreatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user