216 lines
5.2 KiB
Go
216 lines
5.2 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"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 mapAttachment(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 mapAttachment(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 mapAttachment(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 mapAttachment(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))
|
|
}
|
|
|
|
return mapAll(rows, mapAttachment), 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 mapAttachment(row sqlcgen.Attachment) domain.Attachment {
|
|
attachment := domain.Attachment{
|
|
ID: row.ID,
|
|
ChatID: row.ChatID,
|
|
OwnerID: row.OwnerID,
|
|
ObjectKey: row.ObjectKey,
|
|
Filename: row.Filename,
|
|
ContentType: row.ContentType,
|
|
SizeBytes: row.SizeBytes,
|
|
SHA256: row.Sha256,
|
|
Status: domain.AttachmentStatus(row.Status),
|
|
CreatedAt: toUTCTime(row.CreatedAt),
|
|
UpdatedAt: toUTCTime(row.UpdatedAt),
|
|
}
|
|
|
|
if row.ErrorCode.Valid {
|
|
code := row.ErrorCode.String
|
|
attachment.ErrorCode = &code
|
|
}
|
|
|
|
return attachment
|
|
}
|