на оценку 3

This commit is contained in:
2026-10-21 13:51:35 +03:00
parent 3f69970b05
commit bd28f1ae26
24 changed files with 1130 additions and 91 deletions
+6 -3
View File
@@ -18,7 +18,10 @@ type CreateChatCommand struct {
}
type SendMessageCommand struct {
ActorID string
ChatID string
Text string
ActorID string
ChatID string
Text string
Method string
CanonicalPath string
IdempotencyKey string
}
+5 -4
View File
@@ -3,8 +3,9 @@ package application
import "errors"
var (
ErrInvalidCursor = errors.New("invalid cursor")
ErrInvalidCredentials = errors.New("invalid credentials")
ErrInvalidInput = errors.New("invalid input")
ErrCredentialState = errors.New("invalid stored credential state")
ErrInvalidCursor = errors.New("invalid cursor")
ErrInvalidCredentials = errors.New("invalid credentials")
ErrInvalidInput = errors.New("invalid input")
ErrCredentialState = errors.New("invalid stored credential state")
ErrIdempotencyConflict = errors.New("idempotency key is already used for another request")
)
+26 -1
View File
@@ -2,16 +2,41 @@ package application
import (
"context"
"crypto/sha256"
"encoding/json"
"fmt"
"mtusi2026lab3/internal/domain"
)
type canonicalMessageRequest struct {
Text string `json:"text"`
AttachmentIDs []string `json:"attachment_ids"`
}
func (s *Service) SendMessage(ctx context.Context, command SendMessageCommand) (domain.Message, error) {
if err := domain.ValidateMessageText(command.Text); err != nil {
return domain.Message{}, fmt.Errorf("%w: %v", ErrInvalidInput, err)
}
return s.repository.CreateMessage(ctx, command.ChatID, command.ActorID, command.Text)
canonicalBody, err := json.Marshal(canonicalMessageRequest{
Text: command.Text,
AttachmentIDs: make([]string, 0),
})
if err != nil {
return domain.Message{}, fmt.Errorf("canonicalize message request: %w", err)
}
return s.repository.CreateMessageIdempotently(ctx, IdempotentMessageWrite{
ActorID: command.ActorID,
ChatID: command.ChatID,
Text: command.Text,
Method: command.Method,
CanonicalPath: command.CanonicalPath,
Key: command.IdempotencyKey,
RequestHash: sha256.Sum256(canonicalBody),
TTL: s.idempotencyTTL,
})
}
func (s *Service) ListMessages(
+12 -1
View File
@@ -24,8 +24,19 @@ type ChatRepository interface {
GetChatsForUser(ctx context.Context, userID string, limit int, cursor *domain.ChatCursor) ([]domain.Chat, bool, error)
}
type IdempotentMessageWrite struct {
ActorID string
ChatID string
Text string
Method string
CanonicalPath string
Key string
RequestHash [32]byte
TTL time.Duration
}
type MessageRepository interface {
CreateMessage(ctx context.Context, chatID, senderID, text string) (domain.Message, error)
CreateMessageIdempotently(ctx context.Context, write IdempotentMessageWrite) (domain.Message, error)
GetMessages(ctx context.Context, chatID, callerID string, limit int, cursor *domain.MessageCursor) ([]domain.Message, bool, error)
}
+33 -11
View File
@@ -5,14 +5,19 @@ import (
"time"
)
const MinimumIdempotencyTTL = 24 * time.Hour
type ServiceOption func(*Service) error
type Service struct {
repository Repository
cursors *CursorCodec
sessionTTL time.Duration
now func() time.Time
repository Repository
cursors *CursorCodec
sessionTTL time.Duration
idempotencyTTL time.Duration
now func() time.Time
}
func NewService(repository Repository, cursors *CursorCodec, sessionTTL time.Duration) (*Service, error) {
func NewService(repository Repository, cursors *CursorCodec, sessionTTL time.Duration, options ...ServiceOption) (*Service, error) {
if repository == nil {
return nil, fmt.Errorf("repository is required")
}
@@ -22,10 +27,27 @@ func NewService(repository Repository, cursors *CursorCodec, sessionTTL time.Dur
if sessionTTL <= 0 {
return nil, fmt.Errorf("session TTL must be positive")
}
return &Service{
repository: repository,
cursors: cursors,
sessionTTL: sessionTTL,
now: func() time.Time { return time.Now().UTC() },
}, nil
service := &Service{
repository: repository,
cursors: cursors,
sessionTTL: sessionTTL,
idempotencyTTL: MinimumIdempotencyTTL,
now: func() time.Time { return time.Now().UTC() },
}
for _, option := range options {
if err := option(service); err != nil {
return nil, err
}
}
return service, nil
}
func WithIdempotencyTTL(ttl time.Duration) ServiceOption {
return func(service *Service) error {
if ttl < MinimumIdempotencyTTL {
return fmt.Errorf("idempotency TTL must be at least %s", MinimumIdempotencyTTL)
}
service.idempotencyTTL = ttl
return nil
}
}