forked from mtusi2026/mtusi2026lab5
141 lines
4.3 KiB
Go
141 lines
4.3 KiB
Go
package chat
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"messenger/internal/platform/apperr"
|
|
"messenger/internal/platform/ident"
|
|
"messenger/internal/platform/pagination"
|
|
)
|
|
|
|
const (
|
|
keyPurgeBatch = 1000
|
|
// keyPurgeGrace keeps a key past its TTL, so a key that a request has just
|
|
// found valid cannot vanish before the request reads its message back.
|
|
keyPurgeGrace = time.Minute
|
|
)
|
|
|
|
// UserLookup is what this module needs from the users module: one batched
|
|
// existence check. It is declared here, on the consumer side, so this
|
|
// package never imports the users package and tests substitute a map.
|
|
type UserLookup interface {
|
|
ExistingIDs(ctx context.Context, ids []uuid.UUID) ([]uuid.UUID, error)
|
|
}
|
|
|
|
type Service struct {
|
|
chats ChatRepository
|
|
messages MessageRepository
|
|
users UserLookup
|
|
keyTTL time.Duration
|
|
}
|
|
|
|
// NewService takes keyTTL, how long an Idempotency-Key stays bound to its
|
|
// message.
|
|
func NewService(chats ChatRepository, messages MessageRepository, users UserLookup, keyTTL time.Duration) *Service {
|
|
return &Service{chats: chats, messages: messages, users: users, keyTTL: keyTTL}
|
|
}
|
|
|
|
func (s *Service) Create(ctx context.Context, creator uuid.UUID, title string, memberIDs []uuid.UUID) (ChatWithMembers, error) {
|
|
chat, err := NewChat(ident.New(), title)
|
|
if err != nil {
|
|
return ChatWithMembers{}, err
|
|
}
|
|
members, err := ValidateMembers(memberIDs, creator)
|
|
if err != nil {
|
|
return ChatWithMembers{}, err
|
|
}
|
|
found, err := s.users.ExistingIDs(ctx, members)
|
|
if err != nil {
|
|
return ChatWithMembers{}, fmt.Errorf("check members: %w", err)
|
|
}
|
|
if len(found) != len(members) {
|
|
return ChatWithMembers{}, apperr.Invalid("member_ids must reference registered users")
|
|
}
|
|
if err := s.chats.Create(ctx, chat, members); err != nil {
|
|
return ChatWithMembers{}, err
|
|
}
|
|
return ChatWithMembers{Chat: chat, MemberIDs: members}, nil
|
|
}
|
|
|
|
func (s *Service) Get(ctx context.Context, actor, chatID uuid.UUID) (ChatWithMembers, error) {
|
|
return s.chats.GetForMember(ctx, chatID, actor)
|
|
}
|
|
|
|
func (s *Service) List(ctx context.Context, actor uuid.UUID, after *uuid.UUID, limit int) (pagination.Page[ChatWithMembers], error) {
|
|
items, err := s.chats.ListByMember(ctx, actor, after, limit+1)
|
|
if err != nil {
|
|
return pagination.Page[ChatWithMembers]{}, err
|
|
}
|
|
return pagination.Cut(items, limit, func(c ChatWithMembers) uuid.UUID { return c.ID }), nil
|
|
}
|
|
|
|
// MessagePayload is one send attempt: the message and its Idempotency-Key.
|
|
type MessagePayload struct {
|
|
ChatID uuid.UUID
|
|
IdempotencyKey string
|
|
Text string
|
|
AttachmentIDs []uuid.UUID
|
|
}
|
|
|
|
// Send checks access and content on every attempt, a replay included, as the
|
|
// contract requires of a repeat.
|
|
func (s *Service) Send(ctx context.Context, actor uuid.UUID, p MessagePayload) (Message, error) {
|
|
if err := s.requireMember(ctx, actor, p.ChatID); err != nil {
|
|
return Message{}, err
|
|
}
|
|
message, err := NewMessage(ident.New(), p.ChatID, actor, p.Text, p.AttachmentIDs...)
|
|
if err != nil {
|
|
return Message{}, err
|
|
}
|
|
stored, err := s.messages.AppendOnce(ctx, message, p.IdempotencyKey, s.keyTTL)
|
|
if err != nil {
|
|
return Message{}, err
|
|
}
|
|
if !stored.SameContent(message) {
|
|
return Message{}, ErrKeyReused
|
|
}
|
|
return stored, nil
|
|
}
|
|
|
|
func (s *Service) PurgeExpiredKeys(ctx context.Context) (int64, error) {
|
|
var total int64
|
|
for {
|
|
n, err := s.messages.DeleteExpiredKeys(ctx, s.keyTTL+keyPurgeGrace, keyPurgeBatch)
|
|
total += n
|
|
if err != nil {
|
|
return total, err
|
|
}
|
|
if n < keyPurgeBatch {
|
|
return total, nil
|
|
}
|
|
}
|
|
}
|
|
|
|
func (s *Service) History(ctx context.Context, actor, chatID uuid.UUID, after *uuid.UUID, limit int) (pagination.Page[Message], error) {
|
|
if err := s.requireMember(ctx, actor, chatID); err != nil {
|
|
return pagination.Page[Message]{}, err
|
|
}
|
|
items, err := s.messages.ListByChat(ctx, chatID, after, limit+1)
|
|
if err != nil {
|
|
return pagination.Page[Message]{}, err
|
|
}
|
|
return pagination.Cut(items, limit, func(m Message) uuid.UUID { return m.ID }), nil
|
|
}
|
|
|
|
// requireMember runs before any field validation, so an outsider gets the
|
|
// same answer for a foreign chat as for a missing one.
|
|
func (s *Service) requireMember(ctx context.Context, actor, chatID uuid.UUID) error {
|
|
ok, err := s.chats.IsMember(ctx, chatID, actor)
|
|
if err != nil {
|
|
return fmt.Errorf("check membership: %w", err)
|
|
}
|
|
if !ok {
|
|
return ErrNotFound
|
|
}
|
|
return nil
|
|
}
|