forked from mtusi2026/mtusi2026lab4
108 lines
3.2 KiB
Go
108 lines
3.2 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"errors"
|
|
"fmt"
|
|
"sort"
|
|
"strings"
|
|
"unicode/utf8"
|
|
"uuid"
|
|
|
|
"messenger/internal/domain"
|
|
)
|
|
|
|
const maxMessageAttachments = 10
|
|
|
|
type MessageService struct {
|
|
chats *ChatService
|
|
messages MessageRepository
|
|
}
|
|
|
|
func NewMessageService(chats *ChatService, messages MessageRepository) *MessageService {
|
|
return &MessageService{chats: chats, messages: messages}
|
|
}
|
|
|
|
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 := 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,
|
|
AttachmentIDs: attachmentIDs,
|
|
CreatedAt: now(),
|
|
}
|
|
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)
|
|
}
|
|
return stored, err
|
|
}
|
|
|
|
func (s *MessageService) History(ctx context.Context, actor domain.User, chatID uuid.UUID, after *domain.Cursor, limit int) (domain.Page[domain.Message], error) {
|
|
if _, err := s.chats.Get(ctx, actor, chatID); err != nil {
|
|
return domain.Page[domain.Message]{}, err
|
|
}
|
|
if err := checkLimit(limit); err != nil {
|
|
return domain.Page[domain.Message]{}, err
|
|
}
|
|
if after != nil && after.Scope != chatID {
|
|
return domain.Page[domain.Message]{}, fmt.Errorf("%w: cursor belongs to another chat", domain.ErrInvalidInput)
|
|
}
|
|
items, more, err := s.messages.ListMessages(ctx, chatID, after, limit)
|
|
if err != nil {
|
|
return domain.Page[domain.Message]{}, err
|
|
}
|
|
page := domain.Page[domain.Message]{Items: items}
|
|
if more {
|
|
last := items[len(items)-1]
|
|
page.Next = &domain.Cursor{Scope: chatID, CreatedAt: last.CreatedAt, ID: last.ID}
|
|
}
|
|
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[:])
|
|
}
|