forked from mtusi2026/mtusi2026lab5
feat: add durable image processing pipeline
This commit is contained in:
@@ -51,13 +51,16 @@ func (s *Store) CreateMessage(
|
||||
if !visible {
|
||||
return model.Message{}, false, ErrNotFound
|
||||
}
|
||||
validAttachments, err := attachmentsBelongToChat(ctx, tx, chatID, attachmentIDs)
|
||||
validAttachments, failedAttachment, err := attachmentsBelongToChat(ctx, tx, chatID, attachmentIDs)
|
||||
if err != nil {
|
||||
return model.Message{}, false, err
|
||||
}
|
||||
if !validAttachments {
|
||||
return model.Message{}, false, ErrNotFound
|
||||
}
|
||||
if failedAttachment {
|
||||
return model.Message{}, false, ErrConflict
|
||||
}
|
||||
if err = deleteExpiredIdempotencyRecords(ctx, tx, expiredIdempotencyCleanupBatch); err != nil {
|
||||
return model.Message{}, false, err
|
||||
}
|
||||
@@ -92,28 +95,35 @@ func messageRequestHash(text string, attachmentIDs []string) ([sha256.Size]byte,
|
||||
return sha256.Sum256(payload), nil
|
||||
}
|
||||
|
||||
func attachmentsBelongToChat(ctx context.Context, tx pgx.Tx, chatID string, attachmentIDs []string) (bool, error) {
|
||||
func attachmentsBelongToChat(ctx context.Context, tx pgx.Tx, chatID string, attachmentIDs []string) (bool, bool, error) {
|
||||
if len(attachmentIDs) == 0 {
|
||||
return true, nil
|
||||
return true, false, nil
|
||||
}
|
||||
const query = `
|
||||
SELECT id
|
||||
SELECT id, status
|
||||
FROM attachments
|
||||
WHERE chat_id = $1 AND id::text = ANY($2) AND status = 'ready'
|
||||
WHERE chat_id = $1 AND id::text = ANY($2)
|
||||
AND status IN ('queued', 'processing', 'ready', 'failed')
|
||||
FOR UPDATE`
|
||||
rows, err := tx.Query(ctx, query, chatID, attachmentIDs)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("validate message attachments: %w", err)
|
||||
return false, false, fmt.Errorf("validate message attachments: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
count := 0
|
||||
var count int
|
||||
var failed bool
|
||||
for rows.Next() {
|
||||
var id, status string
|
||||
if err := rows.Scan(&id, &status); err != nil {
|
||||
return false, false, fmt.Errorf("scan message attachment: %w", err)
|
||||
}
|
||||
count++
|
||||
failed = failed || status == model.AttachmentFailed
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return false, fmt.Errorf("validate message attachments: %w", err)
|
||||
return false, false, fmt.Errorf("validate message attachments: %w", err)
|
||||
}
|
||||
return count == len(attachmentIDs), nil
|
||||
return count == len(attachmentIDs), failed, nil
|
||||
}
|
||||
|
||||
func chatVisibleInTransaction(ctx context.Context, tx pgx.Tx, chatID, userID string) (bool, error) {
|
||||
|
||||
Reference in New Issue
Block a user