forked from mtusi2026/mtusi2026lab5
82 lines
3.1 KiB
Go
82 lines
3.1 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"slices"
|
|
"sort"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
)
|
|
|
|
// The advisory lock serializes this key across processes. Message and result
|
|
// commit together; an interrupted/rolled-back transaction reserves nothing.
|
|
func (a *App) createMessage(ctx context.Context, chat, user, key, text string, ids []string) (Message, error) {
|
|
m := Message{ChatID: chat, SenderID: user, Text: text, AttachmentIDs: ids}
|
|
tx, err := a.db.Begin(ctx)
|
|
if err != nil {
|
|
return m, unavailable()
|
|
}
|
|
defer tx.Rollback(ctx)
|
|
// POST and the canonical chat path are implicit in this dedicated table.
|
|
if _, err = tx.Exec(ctx, "SELECT pg_advisory_xact_lock(hashtextextended($1,0))", user+":"+chat+":"+key); err != nil {
|
|
return m, unavailable()
|
|
}
|
|
var previous Message
|
|
var created time.Time
|
|
err = tx.QueryRow(ctx, `SELECT m.id::text,m.chat_id::text,m.sender_id::text,m.text,m.created_at,ARRAY(SELECT unnest(m.attachment_ids)::text)
|
|
FROM message_keys k JOIN messages m ON m.id=k.message_id
|
|
WHERE k.user_id=$1 AND k.chat_id=$2 AND k.key=$3 AND k.expires_at>clock_timestamp()`, user, chat, key).Scan(&previous.ID, &previous.ChatID, &previous.SenderID, utf8String{&previous.Text}, &created, &previous.AttachmentIDs)
|
|
if err == nil {
|
|
if previous.Text != text || !slices.Equal(previous.AttachmentIDs, ids) {
|
|
return m, apiError{409, "conflict", "Idempotency key has a different message"}
|
|
}
|
|
previous.CreatedAt = stamp(created)
|
|
return previous, nil
|
|
}
|
|
if !errors.Is(err, pgx.ErrNoRows) {
|
|
return m, unavailable()
|
|
}
|
|
if _, err = tx.Exec(ctx, "DELETE FROM message_keys WHERE user_id=$1 AND chat_id=$2 AND key=$3", user, chat, key); err != nil {
|
|
return m, unavailable()
|
|
}
|
|
// Serialize with attachment state transitions; stable ordering prevents cross-upload deadlocks.
|
|
locked := slices.Clone(ids)
|
|
sort.Strings(locked)
|
|
for _, id := range locked {
|
|
if err = attachmentLock(ctx, tx, id); err != nil {
|
|
return m, unavailable()
|
|
}
|
|
var missingSince *time.Time
|
|
var status string
|
|
err = tx.QueryRow(ctx, "SELECT missing_since,status FROM attachments WHERE id=$1 AND chat_id=$2", id, chat).Scan(&missingSince, &status)
|
|
if err != nil {
|
|
return m, dbError(err)
|
|
}
|
|
if status == "failed" {
|
|
return m, apiError{409, "conflict", "Attachment processing failed"}
|
|
}
|
|
if missingSince != nil {
|
|
return m, unavailable()
|
|
}
|
|
}
|
|
m.ID = uuid()
|
|
if err = tx.QueryRow(ctx, "INSERT INTO messages(id,chat_id,sender_id,text,attachment_ids) VALUES($1,$2,$3,$4,$5) RETURNING created_at", m.ID, chat, user, []byte(text), ids).Scan(&created); err != nil {
|
|
return m, dbError(err)
|
|
}
|
|
if _, err = tx.Exec(ctx, "INSERT INTO message_keys(user_id,chat_id,key,message_id,expires_at) VALUES($1,$2,$3,$4,clock_timestamp()+$5::bigint*interval '1 second')", user, chat, key, m.ID, int64(a.idempotencyTTL.Seconds())); err != nil {
|
|
return m, unavailable()
|
|
}
|
|
for _, id := range ids {
|
|
if _, err = tx.Exec(ctx, "INSERT INTO message_attachments(message_id,chat_id,attachment_id) VALUES($1,$2,$3)", m.ID, chat, id); err != nil {
|
|
return m, dbError(err)
|
|
}
|
|
}
|
|
if err = tx.Commit(ctx); err != nil {
|
|
return m, unavailable()
|
|
}
|
|
m.CreatedAt = stamp(created)
|
|
return m, nil
|
|
}
|