Files
mtusi2026lab5/internal/storage/postgres/attachment_repository.go
T
Vladislav Kunetskiy 4bdcac3dfa lab5: асинхронная очередь, worker и идемпотентный pipeline
API принимает 202 после durable staging и TX intent (job+outbox); отдельные relay и worker публикуют/обрабатывают задания с publisher confirm, lease, retry/backoff, DLQ и replay. Добавлены версии pipeline, варианты thumbnail/crop/optimized, presign в verified staging, status-aware sweeper и admission control.
2026-10-30 22:36:52 +03:00

246 lines
6.8 KiB
Go

package postgres
import (
"context"
"fmt"
"time"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgtype"
"mtusi-messenger/internal/domain"
"mtusi-messenger/internal/storage/postgres/internal/sqlcgen"
)
type AttachmentRepository struct {
store *Store
}
func (r *AttachmentRepository) IsChatMember(ctx context.Context, chatID, userID uuid.UUID) (bool, error) {
ctx, cancel := r.store.timeout(ctx)
defer cancel()
member, err := r.store.queries.IsChatMember(ctx, sqlcgen.IsChatMemberParams{
ChatID: chatID,
UserID: userID,
})
if err != nil {
return false, fmt.Errorf("check chat membership: %w", mapError(err))
}
return member, nil
}
func (r *AttachmentRepository) Create(ctx context.Context, draft domain.AttachmentDraft) (domain.Attachment, error) {
ctx, cancel := r.store.timeout(ctx)
defer cancel()
row, err := r.store.queries.CreateAttachment(ctx, sqlcgen.CreateAttachmentParams{
ID: draft.ID,
ChatID: draft.ChatID,
OwnerID: draft.OwnerID,
ObjectKey: draft.ObjectKey,
Filename: draft.Filename,
ContentType: draft.ContentType,
SizeBytes: draft.SizeBytes,
Sha256: draft.SHA256,
})
if err != nil {
return domain.Attachment{}, fmt.Errorf("create attachment: %w", mapError(err))
}
return mapCreateAttachment(row), nil
}
func (r *AttachmentRepository) MarkReady(
ctx context.Context,
id uuid.UUID,
objectKey string,
) (domain.Attachment, error) {
ctx, cancel := r.store.timeout(ctx)
defer cancel()
row, err := r.store.queries.MarkAttachmentReady(ctx, sqlcgen.MarkAttachmentReadyParams{
ID: id,
ObjectKey: objectKey,
})
if err != nil {
return domain.Attachment{}, fmt.Errorf("mark attachment ready: %w", mapError(err))
}
return mapMarkReadyAttachment(row), nil
}
func (r *AttachmentRepository) DeletePending(ctx context.Context, id uuid.UUID) error {
ctx, cancel := r.store.timeout(ctx)
defer cancel()
if err := r.store.queries.DeletePendingAttachment(ctx, id); err != nil {
return fmt.Errorf("delete pending attachment: %w", mapError(err))
}
return nil
}
func (r *AttachmentRepository) GetForMember(
ctx context.Context,
userID, id uuid.UUID,
) (domain.Attachment, error) {
ctx, cancel := r.store.timeout(ctx)
defer cancel()
row, err := r.store.queries.GetAttachmentForMember(ctx, sqlcgen.GetAttachmentForMemberParams{
ID: id,
UserID: userID,
})
if err != nil {
return domain.Attachment{}, fmt.Errorf("get attachment for member: %w", mapError(err))
}
return mapMemberAttachment(row), nil
}
func (r *AttachmentRepository) GetForOwner(
ctx context.Context,
ownerID, id uuid.UUID,
) (domain.Attachment, error) {
ctx, cancel := r.store.timeout(ctx)
defer cancel()
row, err := r.store.queries.GetAttachmentForOwner(ctx, sqlcgen.GetAttachmentForOwnerParams{
ID: id,
OwnerID: ownerID,
})
if err != nil {
return domain.Attachment{}, fmt.Errorf("get attachment for owner: %w", mapError(err))
}
return mapOwnerAttachment(row), nil
}
func (r *AttachmentRepository) ListStates(
ctx context.Context,
chatID uuid.UUID,
ids []uuid.UUID,
) ([]domain.AttachmentState, error) {
ctx, cancel := r.store.timeout(ctx)
defer cancel()
rows, err := r.store.queries.ListAttachmentStatesForChat(ctx, sqlcgen.ListAttachmentStatesForChatParams{
ChatID: chatID,
Ids: ids,
})
if err != nil {
return nil, fmt.Errorf("list attachment states: %w", mapError(err))
}
states := make([]domain.AttachmentState, 0, len(rows))
for _, row := range rows {
states = append(states, domain.AttachmentState{
ID: row.ID,
Status: domain.AttachmentStatus(row.Status),
})
}
return states, nil
}
func (r *AttachmentRepository) ListStalePending(
ctx context.Context,
createdBefore time.Time,
limit int32,
) ([]domain.Attachment, error) {
ctx, cancel := r.store.timeout(ctx)
defer cancel()
rows, err := r.store.queries.ListStalePendingAttachments(ctx, sqlcgen.ListStalePendingAttachmentsParams{
CreatedBefore: createdBefore,
RowLimit: limit,
})
if err != nil {
return nil, fmt.Errorf("list stale attachments: %w", mapError(err))
}
stale := make([]domain.Attachment, 0, len(rows))
for _, row := range rows {
stale = append(stale, mapStaleAttachment(row))
}
return stale, nil
}
func (r *AttachmentRepository) ListKeys(
ctx context.Context,
limit, offset int32,
) ([]domain.AttachmentKey, error) {
ctx, cancel := r.store.timeout(ctx)
defer cancel()
rows, err := r.store.queries.ListAttachmentKeys(ctx, sqlcgen.ListAttachmentKeysParams{
RowLimit: limit,
RowOffset: offset,
})
if err != nil {
return nil, fmt.Errorf("list attachment keys: %w", mapError(err))
}
keys := make([]domain.AttachmentKey, 0, len(rows))
for _, row := range rows {
keys = append(keys, domain.AttachmentKey{
ID: row.ID,
ObjectKey: row.ObjectKey,
Status: domain.AttachmentStatus(row.Status),
CreatedAt: toUTCTime(row.CreatedAt),
})
}
return keys, nil
}
func mapCreateAttachment(row sqlcgen.CreateAttachmentRow) domain.Attachment {
return legacyAttachment(row.ID, row.ChatID, row.OwnerID, row.ObjectKey, row.Filename,
row.ContentType, row.SizeBytes, row.Sha256, row.Status, row.ErrorCode, row.CreatedAt, row.UpdatedAt)
}
func mapMarkReadyAttachment(row sqlcgen.MarkAttachmentReadyRow) domain.Attachment {
return legacyAttachment(row.ID, row.ChatID, row.OwnerID, row.ObjectKey, row.Filename,
row.ContentType, row.SizeBytes, row.Sha256, row.Status, row.ErrorCode, row.CreatedAt, row.UpdatedAt)
}
func mapMemberAttachment(row sqlcgen.GetAttachmentForMemberRow) domain.Attachment {
return legacyAttachment(row.ID, row.ChatID, row.OwnerID, row.ObjectKey, row.Filename,
row.ContentType, row.SizeBytes, row.Sha256, row.Status, row.ErrorCode, row.CreatedAt, row.UpdatedAt)
}
func mapOwnerAttachment(row sqlcgen.GetAttachmentForOwnerRow) domain.Attachment {
return legacyAttachment(row.ID, row.ChatID, row.OwnerID, row.ObjectKey, row.Filename,
row.ContentType, row.SizeBytes, row.Sha256, row.Status, row.ErrorCode, row.CreatedAt, row.UpdatedAt)
}
func mapStaleAttachment(row sqlcgen.ListStalePendingAttachmentsRow) domain.Attachment {
return legacyAttachment(row.ID, row.ChatID, row.OwnerID, row.ObjectKey, row.Filename,
row.ContentType, row.SizeBytes, row.Sha256, row.Status, row.ErrorCode, row.CreatedAt, row.UpdatedAt)
}
func legacyAttachment(
id, chatID, ownerID uuid.UUID,
objectKey, filename, contentType string,
sizeBytes int64,
sha, status string,
errorCode pgtype.Text,
createdAt, updatedAt time.Time,
) domain.Attachment {
attachment := domain.Attachment{
ID: id, ChatID: chatID, OwnerID: ownerID, ObjectKey: objectKey,
Filename: filename, ContentType: contentType, SizeBytes: sizeBytes,
SHA256: sha, Status: domain.AttachmentStatus(status),
PipelineVersion: 1, CreatedAt: createdAt, UpdatedAt: updatedAt,
}
if errorCode.Valid {
code := errorCode.String
attachment.ErrorCode = &code
}
return attachment
}