119 lines
2.9 KiB
Go
119 lines
2.9 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"mtusi-messenger/internal/domain"
|
|
"mtusi-messenger/internal/storage/postgres/internal/sqlcgen"
|
|
)
|
|
|
|
type MessageRepository struct {
|
|
store *Store
|
|
}
|
|
|
|
func (r *MessageRepository) Create(ctx context.Context, draft domain.MessageDraft) (domain.Message, error) {
|
|
ctx, cancel := r.store.timeout(ctx)
|
|
defer cancel()
|
|
|
|
row, err := r.store.queries.CreateMessage(ctx, sqlcgen.CreateMessageParams{
|
|
ChatID: draft.ChatID,
|
|
SenderID: draft.SenderID,
|
|
Text: draft.Text,
|
|
})
|
|
if err != nil {
|
|
return domain.Message{}, fmt.Errorf("create message: %w", mapError(err))
|
|
}
|
|
|
|
return mapMessage(row.ID, row.ChatID, row.SenderID, row.Text, row.CreatedAt), nil
|
|
}
|
|
|
|
func (r *MessageRepository) ListVisible(
|
|
ctx context.Context,
|
|
chatID, callerID uuid.UUID,
|
|
after *domain.Cursor,
|
|
limit int,
|
|
) (domain.Page[domain.Message], error) {
|
|
ctx, cancel := r.store.timeout(ctx)
|
|
defer cancel()
|
|
|
|
limit = clampLimit(limit)
|
|
fetch := int32(limit + 1) //nolint:gosec // limit is clamped to maxPageSize
|
|
|
|
messages, err := r.fetch(ctx, chatID, callerID, after, fetch)
|
|
if err != nil {
|
|
return domain.Page[domain.Message]{}, err
|
|
}
|
|
|
|
if len(messages) == 0 {
|
|
member, err := r.store.queries.IsChatMember(ctx, sqlcgen.IsChatMemberParams{
|
|
ChatID: chatID,
|
|
UserID: callerID,
|
|
})
|
|
if err != nil {
|
|
return domain.Page[domain.Message]{}, fmt.Errorf("check chat membership: %w", mapError(err))
|
|
}
|
|
|
|
if !member {
|
|
return domain.Page[domain.Message]{}, fmt.Errorf(
|
|
"%w: chat %s for user %s",
|
|
domain.ErrNotFound, chatID, callerID,
|
|
)
|
|
}
|
|
}
|
|
|
|
return buildPage(messages, limit, func(message domain.Message) domain.Cursor {
|
|
return domain.Cursor{Scope: chatID, CreatedAt: message.CreatedAt, ID: message.ID}
|
|
}), nil
|
|
}
|
|
|
|
func (r *MessageRepository) fetch(
|
|
ctx context.Context,
|
|
chatID, callerID uuid.UUID,
|
|
after *domain.Cursor,
|
|
fetch int32,
|
|
) ([]domain.Message, error) {
|
|
if after == nil {
|
|
rows, err := r.store.queries.ListMessagesForChat(ctx, sqlcgen.ListMessagesForChatParams{
|
|
ChatID: chatID,
|
|
CallerID: callerID,
|
|
RowLimit: fetch,
|
|
})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("list messages: %w", mapError(err))
|
|
}
|
|
|
|
return mapAll(rows, mapMessageRow), nil
|
|
}
|
|
|
|
rows, err := r.store.queries.ListMessagesForChatAfter(ctx, sqlcgen.ListMessagesForChatAfterParams{
|
|
ChatID: chatID,
|
|
CallerID: callerID,
|
|
AfterCreatedAt: after.CreatedAt,
|
|
AfterID: after.ID,
|
|
RowLimit: fetch,
|
|
})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("list messages: %w", mapError(err))
|
|
}
|
|
|
|
return mapAll(rows, mapMessageRow), nil
|
|
}
|
|
|
|
func mapMessageRow(row sqlcgen.Message) domain.Message {
|
|
return mapMessage(row.ID, row.ChatID, row.SenderID, row.Text, row.CreatedAt)
|
|
}
|
|
|
|
func mapMessage(id, chatID, senderID uuid.UUID, text string, createdAt time.Time) domain.Message {
|
|
return domain.Message{
|
|
ID: id,
|
|
ChatID: chatID,
|
|
SenderID: senderID,
|
|
Text: text,
|
|
CreatedAt: toUTCTime(createdAt),
|
|
}
|
|
}
|