feat(domain,service):lab4 accept attachment_ids in idempotent request hash

This commit is contained in:
Голиков Maximillian Антон
2026-10-22 14:35:04 +03:00
parent 0834ac822a
commit 297a1858a3
2 changed files with 44 additions and 12 deletions
+13 -3
View File
@@ -17,7 +17,17 @@ type CreateIdempotentResult struct {
Replayed bool
}
func RequestHash(text string) []byte {
sum := sha256.Sum256([]byte(text))
return sum[:]
func RequestHash(text string, attachmentIDs []string) []byte {
if len(attachmentIDs) == 0 {
sum := sha256.Sum256([]byte(text))
return sum[:]
}
h := sha256.New()
h.Write([]byte(text))
h.Write([]byte{0})
for _, id := range attachmentIDs {
h.Write([]byte(id))
h.Write([]byte{0})
}
return h.Sum(nil)
}
+31 -9
View File
@@ -32,12 +32,17 @@ func NewMessageService(
}
}
func (s *MessageService) Create(ctx context.Context, senderID, chatID, text, idempotencyKey, requestPath string,
func (s *MessageService) Create(
ctx context.Context,
senderID, chatID, text string,
attachmentIDs []string,
idempotencyKey, requestPath string,
) (*domain.CreateIdempotentResult, error) {
if strings.TrimSpace(text) == "" {
trimmed := strings.TrimSpace(text)
if trimmed == "" && len(attachmentIDs) == 0 {
return nil, &domain.ValidationError{
Field: "text",
Message: "не может состоять только из пробелов",
Message: "text или attachment_ids обязательны",
}
}
if utf8.RuneCountInString(text) > 4000 {
@@ -46,17 +51,34 @@ func (s *MessageService) Create(ctx context.Context, senderID, chatID, text, ide
Message: "не более 4000 символов",
}
}
if len(attachmentIDs) > 10 {
return nil, &domain.ValidationError{
Field: "attachment_ids",
Message: "не более 10",
}
}
seen := make(map[string]struct{}, len(attachmentIDs))
for _, id := range attachmentIDs {
if _, dup := seen[id]; dup {
return nil, &domain.ValidationError{
Field: "attachment_ids",
Message: "дубликаты запрещены",
}
}
seen[id] = struct{}{}
}
if err := s.ensureMember(ctx, senderID, chatID); err != nil {
return nil, err
}
fresh := &domain.Message{
ID: uuid.NewString(),
ChatID: chatID,
SenderID: senderID,
Text: text,
CreatedAt: time.Now().UTC(),
ID: uuid.NewString(),
ChatID: chatID,
SenderID: senderID,
Text: text,
AttachmentIDs: append([]string(nil), attachmentIDs...),
CreatedAt: time.Now().UTC(),
}
body, err := json.Marshal(dto.MessageFromDomain(fresh))
@@ -72,7 +94,7 @@ func (s *MessageService) Create(ctx context.Context, senderID, chatID, text, ide
Path: requestPath,
Key: idempotencyKey,
},
domain.RequestHash(text),
domain.RequestHash(text, attachmentIDs),
fresh,
body,
s.idempotencyTTL,