forked from mtusi2026/mtusi2026lab2
41 lines
892 B
Go
41 lines
892 B
Go
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))
|
|
}
|
|
return Message{
|
|
ID: id,
|
|
ChatID: chatID,
|
|
SenderID: senderID,
|
|
Text: text,
|
|
CreatedAt: ident.TimeOf(id),
|
|
}, nil
|
|
}
|