forked from mtusi2026/mtusi2026lab4
feat: attachments over S3 (grade 3)
This commit is contained in:
@@ -6,11 +6,16 @@ import (
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
"uuid"
|
||||
|
||||
"messenger/internal/domain"
|
||||
)
|
||||
|
||||
const maxMessageAttachments = 10
|
||||
|
||||
type MessageService struct {
|
||||
chats *ChatService
|
||||
messages MessageRepository
|
||||
@@ -20,22 +25,25 @@ func NewMessageService(chats *ChatService, messages MessageRepository) *MessageS
|
||||
return &MessageService{chats: chats, messages: messages}
|
||||
}
|
||||
|
||||
func (s *MessageService) Send(ctx context.Context, actor domain.User, chatID uuid.UUID, key, text string) (domain.Message, error) {
|
||||
func (s *MessageService) Send(ctx context.Context, actor domain.User, chatID uuid.UUID, key, text string, attachmentIDs []uuid.UUID) (domain.Message, error) {
|
||||
if _, err := s.chats.Get(ctx, actor, chatID); err != nil {
|
||||
return domain.Message{}, err
|
||||
}
|
||||
if err := checkText("text", text, 4000); err != nil {
|
||||
if err := checkMessageText(text, len(attachmentIDs) > 0); err != nil {
|
||||
return domain.Message{}, err
|
||||
}
|
||||
if err := checkAttachmentIDs(attachmentIDs); err != nil {
|
||||
return domain.Message{}, err
|
||||
}
|
||||
m := domain.Message{
|
||||
ID: uuid.New(),
|
||||
ChatID: chatID,
|
||||
SenderID: actor.ID,
|
||||
Text: text,
|
||||
CreatedAt: now(),
|
||||
ID: uuid.New(),
|
||||
ChatID: chatID,
|
||||
SenderID: actor.ID,
|
||||
Text: text,
|
||||
AttachmentIDs: attachmentIDs,
|
||||
CreatedAt: now(),
|
||||
}
|
||||
sum := sha256.Sum256([]byte(text))
|
||||
stored, err := s.messages.CreateMessageIdempotent(ctx, m, key, hex.EncodeToString(sum[:]))
|
||||
stored, err := s.messages.CreateMessageIdempotent(ctx, m, key, messageHash(text, attachmentIDs))
|
||||
if errors.Is(err, domain.ErrConflict) {
|
||||
return domain.Message{}, fmt.Errorf("%w: Idempotency-Key was already used with a different body", domain.ErrConflict)
|
||||
}
|
||||
@@ -63,3 +71,37 @@ func (s *MessageService) History(ctx context.Context, actor domain.User, chatID
|
||||
}
|
||||
return page, nil
|
||||
}
|
||||
|
||||
func checkMessageText(text string, hasAttachments bool) error {
|
||||
if utf8.RuneCountInString(text) > 4000 {
|
||||
return fmt.Errorf("%w: text must be at most 4000 characters", domain.ErrInvalidInput)
|
||||
}
|
||||
if !hasAttachments && strings.TrimSpace(text) == "" {
|
||||
return fmt.Errorf("%w: text must not be empty without attachments", domain.ErrInvalidInput)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkAttachmentIDs(ids []uuid.UUID) error {
|
||||
if len(ids) > maxMessageAttachments {
|
||||
return fmt.Errorf("%w: attachment_ids must contain at most %d items", domain.ErrInvalidInput, maxMessageAttachments)
|
||||
}
|
||||
seen := make(map[uuid.UUID]struct{}, len(ids))
|
||||
for _, id := range ids {
|
||||
if _, duplicate := seen[id]; duplicate {
|
||||
return fmt.Errorf("%w: attachment_ids must not contain duplicates", domain.ErrInvalidInput)
|
||||
}
|
||||
seen[id] = struct{}{}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func messageHash(text string, ids []uuid.UUID) string {
|
||||
sorted := make([]string, len(ids))
|
||||
for i, id := range ids {
|
||||
sorted[i] = id.String()
|
||||
}
|
||||
sort.Strings(sorted)
|
||||
sum := sha256.Sum256([]byte(text + "\n" + strings.Join(sorted, ",")))
|
||||
return hex.EncodeToString(sum[:])
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user