Files
mtusi2026lab5/internal/service/message/service.go
T
Vladislav Kunetskiy 01b885e846 lab5: очереди и асинхронная обработка изображений
Durable staging handoff, transactional outbox, at-least-once worker with lease fencing, idempotent versioned pipeline, three variants, operator tooling, contract tests and evidence. Includes the review fixes: reprocess source selection and failure settlement, replay guards, stale-job retirement, PNG structural/raster validation, migration rollback safety and audit events.
2026-10-31 14:34:24 +03:00

119 lines
3.4 KiB
Go

package message
import (
"context"
"fmt"
"github.com/google/uuid"
"mtusi-messenger/internal/domain"
)
type Repository interface {
IsChatMember(ctx context.Context, chatID, userID uuid.UUID) (bool, error)
// Replay returns the stored message for a live Idempotency-Key. It must be
// checked before attachment-state validation: a message that was accepted
// while the attachment was queued keeps replaying as 201 even after the
// attachment later fails.
Replay(ctx context.Context, draft domain.MessageDraft) (domain.Message, bool, error)
Create(ctx context.Context, draft domain.MessageDraft) (domain.Message, error)
ListVisible(
ctx context.Context,
chatID, callerID uuid.UUID,
after *domain.Cursor,
limit int,
) (domain.Page[domain.Message], error)
}
// AttachmentChecker validates that requested attachments exist in the chat and
// are ready. The contract returns 404 for unknown/foreign IDs and 409 for
// attachments that cannot be linked.
type AttachmentChecker interface {
ListStates(ctx context.Context, chatID uuid.UUID, ids []uuid.UUID) ([]domain.AttachmentState, error)
}
type Service struct {
messages Repository
attachments AttachmentChecker
}
func NewService(messages Repository, attachments AttachmentChecker) *Service {
return &Service{messages: messages, attachments: attachments}
}
func (s *Service) Send(ctx context.Context, draft domain.MessageDraft) (domain.Message, error) {
if err := draft.Validate(); err != nil {
return domain.Message{}, err
}
member, err := s.messages.IsChatMember(ctx, draft.ChatID, draft.SenderID)
if err != nil {
return domain.Message{}, fmt.Errorf("check chat membership: %w", err)
}
if !member {
return domain.Message{}, fmt.Errorf("%w: chat is not visible", domain.ErrNotFound)
}
replayed, found, err := s.messages.Replay(ctx, draft)
if err != nil {
return domain.Message{}, fmt.Errorf("replay message: %w", err)
}
if found {
return replayed, nil
}
if len(draft.AttachmentIDs) > 0 {
if s.attachments == nil {
return domain.Message{}, fmt.Errorf("attachment checker is not configured: %w", domain.ErrUnavailable)
}
states, err := s.attachments.ListStates(ctx, draft.ChatID, draft.AttachmentIDs)
if err != nil {
return domain.Message{}, fmt.Errorf("check attachments: %w", err)
}
if len(states) != len(draft.AttachmentIDs) {
return domain.Message{}, fmt.Errorf("%w: attachment is not visible", domain.ErrNotFound)
}
for _, state := range states {
switch state.Status {
case domain.AttachmentQueued, domain.AttachmentProcessing, domain.AttachmentReady:
// Accepted work is linkable by contract.
default:
return domain.Message{}, fmt.Errorf(
"%w: attachment %s is %s",
domain.ErrConflict, state.ID, state.Status,
)
}
}
}
sent, err := s.messages.Create(ctx, draft)
if err != nil {
return domain.Message{}, fmt.Errorf("create message: %w", err)
}
return sent, nil
}
func (s *Service) History(
ctx context.Context,
callerID, chatID uuid.UUID,
after *domain.Cursor,
limit int,
) (domain.Page[domain.Message], error) {
if after != nil && !after.BelongsTo(chatID) {
return domain.Page[domain.Message]{}, fmt.Errorf("cursor is bound to another chat: %w", domain.ErrInvalidCursor)
}
page, err := s.messages.ListVisible(ctx, chatID, callerID, after, limit)
if err != nil {
return domain.Page[domain.Message]{}, fmt.Errorf("list messages: %w", err)
}
return page, nil
}