Выполнение lab4 на 4
This commit is contained in:
@@ -3,6 +3,7 @@ package store
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/model"
|
||||
@@ -97,8 +98,12 @@ func (s *PostgresStore) ChatPage(ctx context.Context, actor string, limit int, a
|
||||
}
|
||||
|
||||
var ErrIdempotencyConflict = errors.New("idempotency key already used for another message")
|
||||
var ErrAttachmentNotReady = errors.New("attachment is not ready")
|
||||
|
||||
func (s *PostgresStore) CreateMessage(ctx context.Context, actor, chatID, text, key string) (model.Message, error) {
|
||||
const messageColumns = `m.id::text,m.chat_id::text,m.sender_id::text,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)`
|
||||
|
||||
func (s *PostgresStore) CreateMessage(ctx context.Context, actor, chatID, text, key string, attachmentIDs ...string) (model.Message, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
|
||||
defer cancel()
|
||||
id, err := newUUID()
|
||||
@@ -121,12 +126,12 @@ func (s *PostgresStore) CreateMessage(ctx context.Context, actor, chatID, text,
|
||||
}
|
||||
var message model.Message
|
||||
|
||||
err = tx.QueryRow(ctx, `SELECT m.id::text,m.chat_id::text,m.sender_id::text,m.text,m.created_at
|
||||
err = tx.QueryRow(ctx, `SELECT `+messageColumns+`
|
||||
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()`,
|
||||
actor, chatID, key).Scan(&message.ID, &message.ChatID, &message.SenderID, &message.Text, &message.CreatedAt)
|
||||
actor, chatID, key).Scan(&message.ID, &message.ChatID, &message.SenderID, &message.Text, &message.CreatedAt, &message.AttachmentIDs)
|
||||
if err == nil {
|
||||
if message.Text != text {
|
||||
if message.Text != text || !slices.Equal(message.AttachmentIDs, attachmentIDs) {
|
||||
return model.Message{}, ErrIdempotencyConflict
|
||||
}
|
||||
message.CreatedAt = message.CreatedAt.UTC()
|
||||
@@ -135,6 +140,19 @@ func (s *PostgresStore) CreateMessage(ctx context.Context, actor, chatID, text,
|
||||
if !errors.Is(err, pgx.ErrNoRows) {
|
||||
return model.Message{}, ErrUnavailable
|
||||
}
|
||||
for _, attachmentID := range attachmentIDs {
|
||||
var status string
|
||||
err := tx.QueryRow(ctx, `SELECT status FROM attachments WHERE id=$1 AND chat_id=$2 FOR SHARE`, attachmentID, chatID).Scan(&status)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return model.Message{}, ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return model.Message{}, ErrUnavailable
|
||||
}
|
||||
if status != "ready" {
|
||||
return model.Message{}, ErrAttachmentNotReady
|
||||
}
|
||||
}
|
||||
err = tx.QueryRow(ctx, `INSERT INTO messages(id,chat_id,sender_id,text,created_at)
|
||||
VALUES($1,$2,$3,$4,GREATEST(clock_timestamp(),
|
||||
COALESCE((SELECT created_at + INTERVAL '1 microsecond' FROM messages WHERE chat_id=$2 ORDER BY created_at DESC,id DESC LIMIT 1),clock_timestamp())))
|
||||
@@ -142,6 +160,14 @@ func (s *PostgresStore) CreateMessage(ctx context.Context, actor, chatID, text,
|
||||
if err != nil {
|
||||
return model.Message{}, ErrUnavailable
|
||||
}
|
||||
message.AttachmentIDs = make([]string, len(attachmentIDs))
|
||||
copy(message.AttachmentIDs, attachmentIDs)
|
||||
for position, attachmentID := range attachmentIDs {
|
||||
_, err := tx.Exec(ctx, `INSERT INTO message_attachments(message_id,attachment_id,chat_id,position) VALUES($1,$2,$3,$4)`, message.ID, attachmentID, chatID, position)
|
||||
if err != nil {
|
||||
return model.Message{}, ErrUnavailable
|
||||
}
|
||||
}
|
||||
_, err = tx.Exec(ctx, `INSERT INTO message_keys(user_id,chat_id,key,message_id,expires_at)
|
||||
VALUES($1,$2,$3,$4,clock_timestamp()+INTERVAL '24 hours')
|
||||
ON CONFLICT(user_id,chat_id,key) DO UPDATE
|
||||
@@ -159,13 +185,13 @@ func (s *PostgresStore) CreateMessage(ctx context.Context, actor, chatID, text,
|
||||
func (s *PostgresStore) MessagePage(ctx context.Context, chatID string, limit int, after *Position) ([]model.Message, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
|
||||
defer cancel()
|
||||
query := `SELECT id::text,chat_id::text,sender_id::text,text,created_at FROM messages WHERE chat_id=$1`
|
||||
query := `SELECT ` + messageColumns + ` FROM messages m WHERE m.chat_id=$1`
|
||||
args := []any{chatID, limit}
|
||||
if after != nil {
|
||||
query += ` AND (created_at,id)>($3,$4::uuid)`
|
||||
query += ` AND (m.created_at,m.id)>($3,$4::uuid)`
|
||||
args = append(args, after.CreatedAt, after.ID)
|
||||
}
|
||||
query += ` ORDER BY messages.created_at,messages.id LIMIT $2`
|
||||
query += ` ORDER BY m.created_at,m.id LIMIT $2`
|
||||
rows, err := s.pool.Query(ctx, query, args...)
|
||||
if err != nil {
|
||||
return nil, ErrUnavailable
|
||||
@@ -174,7 +200,7 @@ func (s *PostgresStore) MessagePage(ctx context.Context, chatID string, limit in
|
||||
items := make([]model.Message, 0)
|
||||
for rows.Next() {
|
||||
var m model.Message
|
||||
if err := rows.Scan(&m.ID, &m.ChatID, &m.SenderID, &m.Text, &m.CreatedAt); err != nil {
|
||||
if err := rows.Scan(&m.ID, &m.ChatID, &m.SenderID, &m.Text, &m.CreatedAt, &m.AttachmentIDs); err != nil {
|
||||
return nil, ErrUnavailable
|
||||
}
|
||||
m.CreatedAt = m.CreatedAt.UTC()
|
||||
|
||||
Reference in New Issue
Block a user