Перенести решение лабораторной 2 как основу для лабораторной 3

This commit is contained in:
2026-11-02 10:00:11 +03:00
parent 0ab3a341aa
commit 5b9f377783
76 changed files with 7008 additions and 0 deletions
+44
View File
@@ -0,0 +1,44 @@
package chat
import (
"fmt"
"strings"
"time"
"unicode/utf8"
"github.com/google/uuid"
"messenger/internal/platform/apperr"
"messenger/internal/platform/ident"
)
const MaxTextCodePoints = 4000
type Message struct {
ID uuid.UUID
ChatID uuid.UUID
SenderID uuid.UUID
Text string
CreatedAt time.Time
}
// 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) == "" {
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),
}, nil
}