Добавьте S3-вложения с дедупликацией и безопасной сверкой хранилищ
This commit is contained in:
+56
-12
@@ -1,6 +1,8 @@
|
||||
package chat
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"regexp"
|
||||
|
||||
@@ -36,7 +38,8 @@ type createChatRequest struct {
|
||||
}
|
||||
|
||||
type createMessageRequest struct {
|
||||
Text string `json:"text"`
|
||||
Text *string `json:"text"`
|
||||
AttachmentIDs json.RawMessage `json:"attachment_ids"`
|
||||
}
|
||||
|
||||
type ChatResponse struct {
|
||||
@@ -47,11 +50,12 @@ type ChatResponse struct {
|
||||
}
|
||||
|
||||
type MessageResponse struct {
|
||||
ID string `json:"id"`
|
||||
ChatID string `json:"chat_id"`
|
||||
SenderID string `json:"sender_id"`
|
||||
Text string `json:"text"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
AttachmentIDs []string `json:"attachment_ids"`
|
||||
ID string `json:"id"`
|
||||
ChatID string `json:"chat_id"`
|
||||
SenderID string `json:"sender_id"`
|
||||
Text string `json:"text"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
}
|
||||
|
||||
func toChatResponse(c ChatWithMembers) ChatResponse {
|
||||
@@ -68,12 +72,17 @@ func toChatResponse(c ChatWithMembers) ChatResponse {
|
||||
}
|
||||
|
||||
func toMessageResponse(m Message) MessageResponse {
|
||||
ids := make([]string, 0, len(m.AttachmentIDs))
|
||||
for _, id := range m.AttachmentIDs {
|
||||
ids = append(ids, id.String())
|
||||
}
|
||||
return MessageResponse{
|
||||
ID: m.ID.String(),
|
||||
ChatID: m.ChatID.String(),
|
||||
SenderID: m.SenderID.String(),
|
||||
Text: m.Text,
|
||||
CreatedAt: httpx.Timestamp(m.CreatedAt),
|
||||
AttachmentIDs: ids,
|
||||
ID: m.ID.String(),
|
||||
ChatID: m.ChatID.String(),
|
||||
SenderID: m.SenderID.String(),
|
||||
Text: m.Text,
|
||||
CreatedAt: httpx.Timestamp(m.CreatedAt),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -144,7 +153,16 @@ func (h *Handler) send(c *gin.Context, actor uuid.UUID) {
|
||||
httpx.Error(c, err)
|
||||
return
|
||||
}
|
||||
message, err := h.chats.Send(c.Request.Context(), actor, MessagePayload{ChatID: chatID, IdempotencyKey: key, Text: req.Text})
|
||||
if req.Text == nil {
|
||||
httpx.Error(c, apperr.Invalid("text must be a string"))
|
||||
return
|
||||
}
|
||||
attachments, err := decodeAttachmentIDs(req.AttachmentIDs)
|
||||
if err != nil {
|
||||
httpx.Error(c, err)
|
||||
return
|
||||
}
|
||||
message, err := h.chats.Send(c.Request.Context(), actor, MessagePayload{ChatID: chatID, IdempotencyKey: key, Text: *req.Text, AttachmentIDs: attachments})
|
||||
if err != nil {
|
||||
httpx.Error(c, err)
|
||||
return
|
||||
@@ -207,3 +225,29 @@ func listing(c *gin.Context, scope uuid.UUID) (int, *uuid.UUID, error) {
|
||||
}
|
||||
return limit, &last, nil
|
||||
}
|
||||
|
||||
// RawMessage distinguishes an omitted optional array from an explicit null.
|
||||
func decodeAttachmentIDs(raw json.RawMessage) ([]uuid.UUID, error) {
|
||||
if raw == nil {
|
||||
return nil, nil
|
||||
}
|
||||
if bytes.Equal(bytes.TrimSpace(raw), []byte("null")) {
|
||||
return nil, apperr.Invalid("attachment_ids must be an array")
|
||||
}
|
||||
var values []string
|
||||
if err := json.Unmarshal(raw, &values); err != nil {
|
||||
return nil, apperr.Invalid("attachment_ids must be an array of UUIDs")
|
||||
}
|
||||
if len(values) > MaxAttachments {
|
||||
return nil, apperr.Invalid("attachment_ids must have at most 10 entries")
|
||||
}
|
||||
ids := make([]uuid.UUID, 0, len(values))
|
||||
for _, value := range values {
|
||||
id, err := ident.Parse(value)
|
||||
if err != nil {
|
||||
return nil, apperr.Invalid("attachment_ids must contain valid UUIDs")
|
||||
}
|
||||
ids = append(ids, id)
|
||||
}
|
||||
return ids, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user