134 lines
4.3 KiB
Go
134 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"
|
|
)
|
|
|
|
// 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 builds the chats service; keyTTL is how long a message
|
|
// 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
|
|
}
|
|
|
|
// Send stores a message under the sender's Idempotency-Key. Access and the
|
|
// text are checked first, on every attempt: a retry is a new request that
|
|
// must still be authorised and valid. A key already bound to a message with
|
|
// the same text returns that message; with a different text it is a conflict.
|
|
func (s *Service) Send(ctx context.Context, actor, chatID uuid.UUID, key, text string) (Message, error) {
|
|
if err := s.requireMember(ctx, actor, chatID); err != nil {
|
|
return Message{}, err
|
|
}
|
|
message, err := NewMessage(ident.New(), chatID, actor, text)
|
|
if err != nil {
|
|
return Message{}, err
|
|
}
|
|
stored, err := s.messages.AppendOnce(ctx, message, key, s.keyTTL)
|
|
if err != nil {
|
|
return Message{}, err
|
|
}
|
|
if stored.ID != message.ID && stored.Text != message.Text {
|
|
return Message{}, ErrKeyReused
|
|
}
|
|
return stored, nil
|
|
}
|
|
|
|
// keyPurgeBatch bounds one delete statement, so the cleanup never holds many
|
|
// row locks or runs long.
|
|
const keyPurgeBatch = 1000
|
|
|
|
// PurgeExpiredKeys deletes every key older than the retention period, batch
|
|
// by batch, and reports how many it deleted.
|
|
func (s *Service) PurgeExpiredKeys(ctx context.Context) (int64, error) {
|
|
var total int64
|
|
for {
|
|
n, err := s.messages.DeleteExpiredKeys(ctx, s.keyTTL, 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
|
|
}
|