Выполнение lab4 на 4

This commit is contained in:
Daria0w0
2026-11-06 17:35:48 +03:00
parent 9197e151ba
commit 879463c947
89 changed files with 5551 additions and 108 deletions
+17 -3
View File
@@ -8,6 +8,7 @@ import (
"gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/store"
"regexp"
"strings"
"unicode/utf8"
)
type PersistentMessenger struct{ db *store.PostgresStore }
@@ -96,11 +97,24 @@ func (m *PersistentMessenger) ListChats(ctx context.Context, actor string, limit
var idempotencyKeyPattern = regexp.MustCompile(`^[A-Za-z0-9._:-]{1,128}$`)
func (m *PersistentMessenger) CreateMessage(ctx context.Context, actor, chatID, text, key string) (model.Message, error) {
if !uuidPattern.MatchString(chatID) || !validText(text, 4000) || !idempotencyKeyPattern.MatchString(key) {
func (m *PersistentMessenger) CreateMessage(ctx context.Context, actor, chatID, text, key string, attachmentIDs ...string) (model.Message, error) {
if !uuidPattern.MatchString(chatID) || !utf8.ValidString(text) || utf8.RuneCountInString(text) > 4000 || (strings.TrimSpace(text) == "" && len(attachmentIDs) == 0) || len(attachmentIDs) > 10 || !idempotencyKeyPattern.MatchString(key) {
return model.Message{}, ErrInvalidRequest
}
return m.db.CreateMessage(ctx, actor, strings.ToLower(chatID), text, key)
normalized := make([]string, len(attachmentIDs))
seen := make(map[string]bool, len(attachmentIDs))
for i, id := range attachmentIDs {
if !uuidPattern.MatchString(id) {
return model.Message{}, ErrInvalidRequest
}
id = strings.ToLower(id)
if seen[id] {
return model.Message{}, ErrInvalidRequest
}
seen[id] = true
normalized[i] = id
}
return m.db.CreateMessage(ctx, actor, strings.ToLower(chatID), text, key, normalized...)
}
func (m *PersistentMessenger) ListMessages(ctx context.Context, actor, chatID string, limit int, cursor string) (Page[model.Message], error) {
chat, err := m.GetChat(ctx, actor, chatID)