lab4 done
This commit is contained in:
@@ -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()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user