Перенесите реализацию ЛР 4 на стенд ЛР 5

This commit is contained in:
2026-11-18 15:33:31 +03:00
parent 276dbdad8a
commit 911d97a77a
153 changed files with 17451 additions and 1 deletions
+69
View File
@@ -0,0 +1,69 @@
package chat
import (
"fmt"
"slices"
"strings"
"time"
"unicode/utf8"
"github.com/google/uuid"
"messenger/internal/platform/apperr"
"messenger/internal/platform/ident"
)
const (
MaxTextCodePoints = 4000
MaxAttachments = 10
)
type Message struct {
ID uuid.UUID
ChatID uuid.UUID
SenderID uuid.UUID
Text string
CreatedAt time.Time
AttachmentIDs []uuid.UUID
}
// 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 {
return Message{}, apperr.Invalid(fmt.Sprintf("text must be at most %d characters", MaxTextCodePoints))
}
// PostgreSQL text cannot hold U+0000.
if strings.ContainsRune(text, 0) {
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),
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 && slices.Equal(m.AttachmentIDs, other.AttachmentIDs)
}