diff --git a/backend/internal/domain/idempotency.go b/backend/internal/domain/idempotency.go index f7616d9..65d43d5 100644 --- a/backend/internal/domain/idempotency.go +++ b/backend/internal/domain/idempotency.go @@ -17,7 +17,17 @@ type CreateIdempotentResult struct { Replayed bool } -func RequestHash(text string) []byte { - sum := sha256.Sum256([]byte(text)) - return sum[:] +func RequestHash(text string, attachmentIDs []string) []byte { + if len(attachmentIDs) == 0 { + sum := sha256.Sum256([]byte(text)) + return sum[:] + } + h := sha256.New() + h.Write([]byte(text)) + h.Write([]byte{0}) + for _, id := range attachmentIDs { + h.Write([]byte(id)) + h.Write([]byte{0}) + } + return h.Sum(nil) } diff --git a/backend/internal/service/message.go b/backend/internal/service/message.go index 3c178cf..5d51e81 100644 --- a/backend/internal/service/message.go +++ b/backend/internal/service/message.go @@ -32,12 +32,17 @@ func NewMessageService( } } -func (s *MessageService) Create(ctx context.Context, senderID, chatID, text, idempotencyKey, requestPath string, +func (s *MessageService) Create( + ctx context.Context, + senderID, chatID, text string, + attachmentIDs []string, + idempotencyKey, requestPath string, ) (*domain.CreateIdempotentResult, error) { - if strings.TrimSpace(text) == "" { + trimmed := strings.TrimSpace(text) + if trimmed == "" && len(attachmentIDs) == 0 { return nil, &domain.ValidationError{ Field: "text", - Message: "не может состоять только из пробелов", + Message: "text или attachment_ids обязательны", } } if utf8.RuneCountInString(text) > 4000 { @@ -46,17 +51,34 @@ func (s *MessageService) Create(ctx context.Context, senderID, chatID, text, ide Message: "не более 4000 символов", } } + if len(attachmentIDs) > 10 { + return nil, &domain.ValidationError{ + Field: "attachment_ids", + Message: "не более 10", + } + } + seen := make(map[string]struct{}, len(attachmentIDs)) + for _, id := range attachmentIDs { + if _, dup := seen[id]; dup { + return nil, &domain.ValidationError{ + Field: "attachment_ids", + Message: "дубликаты запрещены", + } + } + seen[id] = struct{}{} + } if err := s.ensureMember(ctx, senderID, chatID); err != nil { return nil, err } fresh := &domain.Message{ - ID: uuid.NewString(), - ChatID: chatID, - SenderID: senderID, - Text: text, - CreatedAt: time.Now().UTC(), + ID: uuid.NewString(), + ChatID: chatID, + SenderID: senderID, + Text: text, + AttachmentIDs: append([]string(nil), attachmentIDs...), + CreatedAt: time.Now().UTC(), } body, err := json.Marshal(dto.MessageFromDomain(fresh)) @@ -72,7 +94,7 @@ func (s *MessageService) Create(ctx context.Context, senderID, chatID, text, ide Path: requestPath, Key: idempotencyKey, }, - domain.RequestHash(text), + domain.RequestHash(text, attachmentIDs), fresh, body, s.idempotencyTTL,