Files
mtusi2026lab4/internal/domain/message.go
T
2026-10-21 21:22:42 +03:00

45 lines
894 B
Go

package domain
import (
"crypto/sha256"
"fmt"
"time"
"github.com/google/uuid"
)
type Message struct {
ID uuid.UUID
ChatID uuid.UUID
SenderID uuid.UUID
Text string
CreatedAt time.Time
}
// MessageIdempotency is the client Idempotency-Key together with the fingerprint
// of the payload it was first used with. The same key and fingerprint replay the
// stored message; the same key with another payload is a conflict.
type MessageIdempotency struct {
Key string
RequestHash [sha256.Size]byte
}
type MessageDraft struct {
ChatID uuid.UUID
SenderID uuid.UUID
Text string
Idempotency MessageIdempotency
}
func (d MessageDraft) Validate() error {
if err := validateNotBlank("text", d.Text); err != nil {
return err
}
if d.Idempotency.Key == "" {
return fmt.Errorf("idempotency key is missing: %w", ErrValidation)
}
return nil
}