refactor: isolate attachment lifecycle

This commit is contained in:
2026-10-09 13:13:59 +03:00
parent ad36ab8a92
commit cc4ac1ddd7
33 changed files with 1672 additions and 982 deletions
+13 -4
View File
@@ -97,11 +97,20 @@ func attachmentsBelongToChat(ctx context.Context, tx pgx.Tx, chatID string, atta
return true, nil
}
const query = `
SELECT count(*)
SELECT id
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 {
WHERE chat_id = $1 AND id::text = ANY($2) AND status = 'ready'
FOR UPDATE`
rows, err := tx.Query(ctx, query, chatID, attachmentIDs)
if err != nil {
return false, fmt.Errorf("validate message attachments: %w", err)
}
defer rows.Close()
count := 0
for rows.Next() {
count++
}
if err := rows.Err(); err != nil {
return false, fmt.Errorf("validate message attachments: %w", err)
}
return count == len(attachmentIDs), nil