lab4 done

This commit is contained in:
Khakimov Dmitrij
2026-10-28 22:41:10 +03:00
parent a7bedda183
commit dbc700fafe
75 changed files with 5611 additions and 227 deletions
+26 -2
View File
@@ -3,7 +3,9 @@ package chat
import (
"context"
"errors"
"strings"
"time"
"unicode/utf8"
"gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/domain"
"gitflic.ru/akselbant/mtusi2026lab1-fork/internal/pkg/password"
@@ -105,10 +107,32 @@ func (u *UseCase) CreateMessage(ctx context.Context, callerID string, message do
if _, err := u.chatForUser(ctx, chatID, callerID); err != nil {
return domain.Message{}, err
}
if !validRequiredText(message.Text, 4000) {
if !utf8.ValidString(message.Text) || utf8.RuneCountInString(message.Text) > 4000 || (strings.TrimSpace(message.Text) == "" && len(message.AttachmentIDs) == 0) {
return domain.Message{}, invalid("text должен содержать от 1 до 4000 символов")
}
created, err := u.repository.CreateMessage(ctx, chatID, callerID, message.Text, message.IdempotencyKey)
if len(message.AttachmentIDs) > 10 {
return domain.Message{}, invalid("Не более 10 вложений")
}
seen := map[string]bool{}
for i, id := range message.AttachmentIDs {
normalized, ok := normalizeUUID(id)
if !ok || seen[normalized] {
return domain.Message{}, invalid("Некорректные или повторные attachment_ids")
}
seen[normalized] = true
message.AttachmentIDs[i] = normalized
}
var created domain.Message
var err error
if repository, ok := u.repository.(interface {
CreateMessageWithAttachments(context.Context, string, string, string, string, []string) (domain.Message, error)
}); ok {
created, err = repository.CreateMessageWithAttachments(ctx, chatID, callerID, message.Text, message.IdempotencyKey, message.AttachmentIDs)
} else if len(message.AttachmentIDs) > 0 {
return domain.Message{}, notFound()
} else {
created, err = u.repository.CreateMessage(ctx, chatID, callerID, message.Text, message.IdempotencyKey)
}
if errors.Is(err, ErrChatNotFound) || errors.Is(err, ErrNotMember) {
return domain.Message{}, notFound()
}