Добавьте S3-вложения с дедупликацией и безопасной сверкой хранилищ
This commit is contained in:
+34
-15
@@ -2,6 +2,7 @@ package chat
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
@@ -12,19 +13,36 @@ import (
|
||||
"messenger/internal/platform/ident"
|
||||
)
|
||||
|
||||
const MaxTextCodePoints = 4000
|
||||
const (
|
||||
MaxTextCodePoints = 4000
|
||||
MaxAttachments = 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
|
||||
CreatedAt time.Time
|
||||
AttachmentIDs []uuid.UUID
|
||||
}
|
||||
|
||||
// NewMessage keeps the text verbatim; only blank or oversized text is rejected.
|
||||
func NewMessage(id, chatID, senderID uuid.UUID, text string) (Message, error) {
|
||||
if strings.TrimSpace(text) == "" {
|
||||
// NewMessage keeps text and attachment order verbatim; it owns its attachment slice.
|
||||
func NewMessage(id, chatID, senderID uuid.UUID, text string, attachmentIDs ...uuid.UUID) (Message, error) {
|
||||
if len(attachmentIDs) > MaxAttachments {
|
||||
return Message{}, apperr.Invalid("attachment_ids must have at most 10 entries")
|
||||
}
|
||||
seen := make(map[uuid.UUID]struct{}, len(attachmentIDs))
|
||||
for _, id := range attachmentIDs {
|
||||
if id == uuid.Nil {
|
||||
return Message{}, apperr.Invalid("attachment_ids must contain non-nil UUIDs")
|
||||
}
|
||||
if _, duplicate := seen[id]; duplicate {
|
||||
return Message{}, apperr.Invalid("attachment_ids must be unique")
|
||||
}
|
||||
seen[id] = struct{}{}
|
||||
}
|
||||
if strings.TrimSpace(text) == "" && len(attachmentIDs) == 0 {
|
||||
return Message{}, apperr.Invalid("text must not be blank")
|
||||
}
|
||||
if utf8.RuneCountInString(text) > MaxTextCodePoints {
|
||||
@@ -35,16 +53,17 @@ func NewMessage(id, chatID, senderID uuid.UUID, text string) (Message, error) {
|
||||
return Message{}, apperr.Invalid("text must not contain the NUL character")
|
||||
}
|
||||
return Message{
|
||||
ID: id,
|
||||
ChatID: chatID,
|
||||
SenderID: senderID,
|
||||
Text: text,
|
||||
CreatedAt: ident.TimeOf(id),
|
||||
ID: id,
|
||||
ChatID: chatID,
|
||||
SenderID: senderID,
|
||||
Text: text,
|
||||
CreatedAt: ident.TimeOf(id),
|
||||
AttachmentIDs: slices.Clone(attachmentIDs),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// SameContent reports whether two messages say the same thing, whatever their
|
||||
// ids and times.
|
||||
func (m Message) SameContent(other Message) bool {
|
||||
return m.Text == other.Text
|
||||
return m.Text == other.Text && slices.Equal(m.AttachmentIDs, other.AttachmentIDs)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user