lab4: S3-вложения, presigned upload и проверки
This commit is contained in:
+37
-11
@@ -3,17 +3,22 @@ package domain
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// MaxMessageAttachments is the contract limit for attachment_ids on a message.
|
||||
const MaxMessageAttachments = 10
|
||||
|
||||
type Message struct {
|
||||
ID uuid.UUID
|
||||
ChatID uuid.UUID
|
||||
SenderID uuid.UUID
|
||||
Text string
|
||||
CreatedAt time.Time
|
||||
ID uuid.UUID
|
||||
ChatID uuid.UUID
|
||||
SenderID uuid.UUID
|
||||
Text string
|
||||
AttachmentIDs []uuid.UUID
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
// MessageIdempotency is the client Idempotency-Key together with the fingerprint
|
||||
@@ -25,15 +30,36 @@ type MessageIdempotency struct {
|
||||
}
|
||||
|
||||
type MessageDraft struct {
|
||||
ChatID uuid.UUID
|
||||
SenderID uuid.UUID
|
||||
Text string
|
||||
Idempotency MessageIdempotency
|
||||
ChatID uuid.UUID
|
||||
SenderID uuid.UUID
|
||||
Text string
|
||||
AttachmentIDs []uuid.UUID
|
||||
Idempotency MessageIdempotency
|
||||
}
|
||||
|
||||
// Validate enforces the contract rule that a message carries either non-blank
|
||||
// text or at least one attachment, never neither.
|
||||
func (d MessageDraft) Validate() error {
|
||||
if err := validateNotBlank("text", d.Text); err != nil {
|
||||
return err
|
||||
if strings.TrimSpace(d.Text) == "" && len(d.AttachmentIDs) == 0 {
|
||||
return fmt.Errorf("message has neither text nor attachments: %w", ErrValidation)
|
||||
}
|
||||
|
||||
if len(d.AttachmentIDs) > MaxMessageAttachments {
|
||||
return fmt.Errorf("too many attachments: %w", ErrValidation)
|
||||
}
|
||||
|
||||
seen := make(map[uuid.UUID]struct{}, len(d.AttachmentIDs))
|
||||
|
||||
for _, id := range d.AttachmentIDs {
|
||||
if id == uuid.Nil {
|
||||
return fmt.Errorf("attachment id is empty: %w", ErrValidation)
|
||||
}
|
||||
|
||||
if _, ok := seen[id]; ok {
|
||||
return fmt.Errorf("duplicate attachment id %s: %w", id, ErrValidation)
|
||||
}
|
||||
|
||||
seen[id] = struct{}{}
|
||||
}
|
||||
|
||||
if d.Idempotency.Key == "" {
|
||||
|
||||
Reference in New Issue
Block a user