Перенеси реализацию мессенджера из ЛР 6
Источник — проверенное рабочее дерево ЛР 6 поверх 18878d6, включая изменения после повторного ревью. SHA-256 списка исходных файлов: e45a3d85b41feff1da78dce3f20fd1f4af1c5ee629e6ed703218bc8e21ad58fe.
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"messenger/internal/attachment"
|
||||
"messenger/internal/platform/apperr"
|
||||
"messenger/internal/platform/postgres"
|
||||
"messenger/internal/platform/tracectx"
|
||||
)
|
||||
|
||||
type Attachments struct{ pool *pgxpool.Pool }
|
||||
|
||||
var _ attachment.AttachmentRepository = (*Attachments)(nil)
|
||||
|
||||
func NewAttachments(pool *pgxpool.Pool) *Attachments { return &Attachments{pool: pool} }
|
||||
|
||||
func (r *Attachments) Enqueue(ctx context.Context, a attachment.Attachment, stage string, checkStage func() error) error {
|
||||
return postgres.Classify(pgx.BeginFunc(ctx, r.pool, func(tx pgx.Tx) error {
|
||||
// A request can pause after validation for longer than the GC grace.
|
||||
// Verify the source after serializing with deletion of this exact key.
|
||||
stageHash := sha256.Sum256([]byte(stage))
|
||||
if err := attachment.LockHash(ctx, tx, stageHash[:]); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := checkStage(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := attachment.LockHash(ctx, tx, a.FileHash); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tx.Exec(ctx, `
|
||||
INSERT INTO files(hash,size_bytes,mime_type,status) VALUES ($1,$2,$3,'pending')
|
||||
ON CONFLICT (hash) DO NOTHING`, a.FileHash, a.SizeBytes, a.MIMEType); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tx.Exec(ctx, `
|
||||
INSERT INTO attachments(id,chat_id,owner_id,filename,file_hash,created_at,accepted_at)
|
||||
VALUES ($1,$2,$3,$4,$5,$6,clock_timestamp())`, a.ID, a.ChatID, a.OwnerID, a.Filename, a.FileHash, a.CreatedAt); err != nil {
|
||||
return err
|
||||
}
|
||||
parent, state := tracectx.Fields(ctx)
|
||||
_, err := tx.Exec(ctx, `INSERT INTO attachment_jobs(attachment_id,source_key,traceparent,tracestate,available_at)
|
||||
VALUES ($1,$2,NULLIF($3,''),NULLIF($4,''),clock_timestamp())`, a.ID, stage, parent, state)
|
||||
return err
|
||||
}))
|
||||
}
|
||||
|
||||
func (r *Attachments) GetForMember(ctx context.Context, id, actor uuid.UUID) (attachment.Attachment, error) {
|
||||
var a attachment.Attachment
|
||||
// A single statement returns one snapshot of the published version, even
|
||||
// when a worker completes a newer version during a download.
|
||||
rows, err := r.pool.Query(ctx, `
|
||||
SELECT a.id, a.chat_id, a.owner_id, a.filename, a.file_hash, f.size_bytes, f.mime_type,
|
||||
s.status, s.error_code, a.created_at, s.updated_at,
|
||||
coalesce(v.variant,''),coalesce(v.object_key,''),coalesce(v.mime_type,''),
|
||||
coalesce(v.size_bytes,0),coalesce(v.width,0),coalesce(v.height,0),v.sha256
|
||||
FROM attachments a
|
||||
JOIN chat_members m ON m.chat_id=a.chat_id AND m.user_id=$2
|
||||
JOIN files f ON f.hash=a.file_hash
|
||||
JOIN attachment_states s ON s.id=a.id
|
||||
LEFT JOIN attachment_variants v ON v.attachment_id=a.id AND v.pipeline_version=a.published_version AND s.status='ready'
|
||||
WHERE a.id=$1 ORDER BY v.variant`, id, actor)
|
||||
if err != nil {
|
||||
return a, postgres.Classify(err)
|
||||
}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var v attachment.Variant
|
||||
err := rows.Scan(&a.ID, &a.ChatID, &a.OwnerID, &a.Filename, &a.FileHash, &a.SizeBytes, &a.MIMEType,
|
||||
&a.Status, &a.ErrorCode, &a.CreatedAt, &a.UpdatedAt,
|
||||
&v.Name, &v.ObjectKey, &v.MIMEType, &v.SizeBytes, &v.Width, &v.Height, &v.Hash)
|
||||
if err != nil {
|
||||
return attachment.Attachment{}, postgres.Classify(err)
|
||||
}
|
||||
if v.Name != "" {
|
||||
a.Variants = append(a.Variants, v)
|
||||
}
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return attachment.Attachment{}, postgres.Classify(err)
|
||||
}
|
||||
if a.ID == uuid.Nil {
|
||||
return a, apperr.NotFound("attachment not found")
|
||||
}
|
||||
return a, nil
|
||||
}
|
||||
Reference in New Issue
Block a user