feat: add S3 attachments and reconciliation

This commit is contained in:
2026-10-02 19:55:52 +03:00
parent 7e60f099d3
commit efaeb593e8
21 changed files with 1623 additions and 85 deletions
+72 -6
View File
@@ -4,6 +4,7 @@ import (
"bytes"
"context"
"crypto/sha256"
"encoding/json"
"errors"
"fmt"
"time"
@@ -21,6 +22,7 @@ func (s *Store) CreateMessage(
chatID string,
senderID string,
text string,
attachmentIDs []string,
idempotencyKey string,
idempotencyTTL time.Duration,
) (model.Message, bool, error) {
@@ -46,13 +48,23 @@ func (s *Store) CreateMessage(
if !visible {
return model.Message{}, false, ErrNotFound
}
validAttachments, err := attachmentsBelongToChat(ctx, tx, chatID, attachmentIDs)
if err != nil {
return model.Message{}, false, err
}
if !validAttachments {
return model.Message{}, false, ErrNotFound
}
if err = deleteExpiredIdempotencyRecords(ctx, tx, expiredIdempotencyCleanupBatch); err != nil {
return model.Message{}, false, err
}
requestHash = sha256.Sum256([]byte(text))
requestHash, err = messageRequestHash(text, attachmentIDs)
if err != nil {
return model.Message{}, false, err
}
message, replay, err = createIdempotentMessage(
ctx, tx, chatID, senderID, text, idempotencyKey, requestHash[:], idempotencyTTL,
ctx, tx, chatID, senderID, text, attachmentIDs, idempotencyKey, requestHash[:], idempotencyTTL,
)
if err != nil {
return model.Message{}, false, err
@@ -63,6 +75,35 @@ func (s *Store) CreateMessage(
return message, replay, nil
}
func messageRequestHash(text string, attachmentIDs []string) ([sha256.Size]byte, error) {
if attachmentIDs == nil {
attachmentIDs = []string{}
}
payload, err := json.Marshal(struct {
Text string `json:"text"`
AttachmentIDs []string `json:"attachment_ids"`
}{text, attachmentIDs})
if err != nil {
return [sha256.Size]byte{}, fmt.Errorf("marshal message request: %w", err)
}
return sha256.Sum256(payload), nil
}
func attachmentsBelongToChat(ctx context.Context, tx pgx.Tx, chatID string, attachmentIDs []string) (bool, error) {
if len(attachmentIDs) == 0 {
return true, nil
}
const query = `
SELECT count(*)
FROM attachments
WHERE chat_id = $1 AND id::text = ANY($2)`
var count int
if err := tx.QueryRow(ctx, query, chatID, attachmentIDs).Scan(&count); err != nil {
return false, fmt.Errorf("validate message attachments: %w", err)
}
return count == len(attachmentIDs), nil
}
func chatVisibleInTransaction(ctx context.Context, tx pgx.Tx, chatID, userID string) (bool, error) {
const query = `
SELECT EXISTS (
@@ -84,6 +125,7 @@ func createIdempotentMessage(
chatID string,
senderID string,
text string,
attachmentIDs []string,
idempotencyKey string,
requestHash []byte,
idempotencyTTL time.Duration,
@@ -126,7 +168,7 @@ func createIdempotentMessage(
continue
}
message, err = insertMessage(ctx, tx, messageID, chatID, senderID, text)
message, err = insertMessage(ctx, tx, messageID, chatID, senderID, text, attachmentIDs)
return message, false, err
}
}
@@ -235,6 +277,7 @@ func insertMessage(
chatID string,
senderID string,
text string,
attachmentIDs []string,
) (model.Message, error) {
const query = `
INSERT INTO messages (id, chat_id, sender_id, text)
@@ -255,14 +298,36 @@ func insertMessage(
if err != nil {
return model.Message{}, fmt.Errorf("insert message: %w", err)
}
if err := insertMessageAttachments(ctx, tx, messageID, chatID, attachmentIDs); err != nil {
return model.Message{}, err
}
message.AttachmentIDs = append([]string(nil), attachmentIDs...)
return message, nil
}
func insertMessageAttachments(ctx context.Context, tx pgx.Tx, messageID, chatID string, attachmentIDs []string) error {
const query = `
INSERT INTO message_attachments (message_id, chat_id, attachment_id, position)
VALUES ($1, $2, $3, $4)`
for position, attachmentID := range attachmentIDs {
if _, err := tx.Exec(ctx, query, messageID, chatID, attachmentID, position); err != nil {
return fmt.Errorf("insert message attachment: %w", err)
}
}
return nil
}
func messageByID(ctx context.Context, tx pgx.Tx, messageID string) (model.Message, error) {
const query = `
SELECT id, chat_id, sender_id, text, created_at
FROM messages
WHERE id = $1`
SELECT m.id, m.chat_id, m.sender_id, m.text, m.created_at,
ARRAY(
SELECT ma.attachment_id::text
FROM message_attachments ma
WHERE ma.message_id = m.id
ORDER BY ma.position
)
FROM messages m
WHERE m.id = $1`
var (
message model.Message
@@ -274,6 +339,7 @@ func messageByID(ctx context.Context, tx pgx.Tx, messageID string) (model.Messag
&message.SenderID,
&message.Text,
&message.CreatedAt,
&message.AttachmentIDs,
)
if errors.Is(err, pgx.ErrNoRows) {
return model.Message{}, fmt.Errorf("idempotency result message not found")