forked from mtusi2026/mtusi2026lab3
на оценку 3
This commit is contained in:
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user