156 lines
4.6 KiB
Go
156 lines
4.6 KiB
Go
// Package store holds the repository implementations of the chats module.
|
|
//
|
|
// Keyset pages use `id > COALESCE($after, nil uuid)`: every UUIDv7 is
|
|
// greater than the nil UUID, so one query serves the first page and the
|
|
// following ones, and the condition stays a range the index can scan. Ids
|
|
// are UUIDv7 and created_at derives from them, so ordering by id is the
|
|
// contract order (created_at, id).
|
|
package store
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
"messenger/internal/chat"
|
|
)
|
|
|
|
type Chats struct {
|
|
pool *pgxpool.Pool
|
|
}
|
|
|
|
var _ chat.ChatRepository = (*Chats)(nil)
|
|
|
|
func NewChats(pool *pgxpool.Pool) *Chats {
|
|
return &Chats{pool: pool}
|
|
}
|
|
|
|
// chatWithMembers selects the projection from a chats row aliased c.
|
|
const chatWithMembers = `c.id, c.title, c.created_at,
|
|
ARRAY(SELECT m.user_id FROM chat_members m WHERE m.chat_id = c.id ORDER BY m.user_id)`
|
|
|
|
func (r *Chats) Create(ctx context.Context, c chat.Chat, memberIDs []uuid.UUID) error {
|
|
return pgx.BeginFunc(ctx, r.pool, func(tx pgx.Tx) error {
|
|
if _, err := tx.Exec(ctx,
|
|
`INSERT INTO chats (id, title, created_at) VALUES ($1, $2, $3)`,
|
|
c.ID, c.Title, c.CreatedAt); err != nil {
|
|
return fmt.Errorf("insert chat: %w", err)
|
|
}
|
|
if _, err := tx.Exec(ctx,
|
|
`INSERT INTO chat_members (chat_id, user_id) SELECT $1, unnest($2::uuid[])`,
|
|
c.ID, memberIDs); err != nil {
|
|
return fmt.Errorf("insert chat members: %w", err)
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func (r *Chats) GetForMember(ctx context.Context, chatID, userID uuid.UUID) (chat.ChatWithMembers, error) {
|
|
row := r.pool.QueryRow(ctx, `
|
|
SELECT `+chatWithMembers+`
|
|
FROM chats c
|
|
WHERE c.id = $1
|
|
AND EXISTS (SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2)`,
|
|
chatID, userID)
|
|
c, err := scanChat(row)
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return chat.ChatWithMembers{}, chat.ErrNotFound
|
|
}
|
|
if err != nil {
|
|
return chat.ChatWithMembers{}, fmt.Errorf("select chat: %w", err)
|
|
}
|
|
return c, nil
|
|
}
|
|
|
|
func (r *Chats) IsMember(ctx context.Context, chatID, userID uuid.UUID) (bool, error) {
|
|
var ok bool
|
|
err := r.pool.QueryRow(ctx,
|
|
`SELECT EXISTS (SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2)`,
|
|
chatID, userID).Scan(&ok)
|
|
if err != nil {
|
|
return false, fmt.Errorf("check membership: %w", err)
|
|
}
|
|
return ok, nil
|
|
}
|
|
|
|
func (r *Chats) ListByMember(ctx context.Context, userID uuid.UUID, after *uuid.UUID, limit int) ([]chat.ChatWithMembers, error) {
|
|
rows, err := r.pool.Query(ctx, `
|
|
SELECT `+chatWithMembers+`
|
|
FROM chat_members own
|
|
JOIN chats c ON c.id = own.chat_id
|
|
WHERE own.user_id = $1
|
|
AND own.chat_id > COALESCE($2, '00000000-0000-0000-0000-000000000000'::uuid)
|
|
ORDER BY own.chat_id
|
|
LIMIT $3`,
|
|
userID, after, limit)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("select chats: %w", err)
|
|
}
|
|
chats, err := pgx.CollectRows(rows, func(row pgx.CollectableRow) (chat.ChatWithMembers, error) {
|
|
return scanChat(row)
|
|
})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("read chats: %w", err)
|
|
}
|
|
return chats, nil
|
|
}
|
|
|
|
func scanChat(row pgx.Row) (chat.ChatWithMembers, error) {
|
|
var c chat.ChatWithMembers
|
|
if err := row.Scan(&c.ID, &c.Title, &c.CreatedAt, &c.MemberIDs); err != nil {
|
|
return chat.ChatWithMembers{}, err
|
|
}
|
|
c.CreatedAt = c.CreatedAt.UTC()
|
|
return c, nil
|
|
}
|
|
|
|
type Messages struct {
|
|
pool *pgxpool.Pool
|
|
}
|
|
|
|
var _ chat.MessageRepository = (*Messages)(nil)
|
|
|
|
func NewMessages(pool *pgxpool.Pool) *Messages {
|
|
return &Messages{pool: pool}
|
|
}
|
|
|
|
func (r *Messages) Append(ctx context.Context, m chat.Message) error {
|
|
_, err := r.pool.Exec(ctx,
|
|
`INSERT INTO messages (id, chat_id, sender_id, text, created_at) VALUES ($1, $2, $3, $4, $5)`,
|
|
m.ID, m.ChatID, m.SenderID, m.Text, m.CreatedAt)
|
|
if err != nil {
|
|
return fmt.Errorf("insert message: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ListByChat is the history query; scripts/explain-history.sql runs the same
|
|
// statement for the EXPLAIN evidence.
|
|
func (r *Messages) ListByChat(ctx context.Context, chatID uuid.UUID, after *uuid.UUID, limit int) ([]chat.Message, error) {
|
|
rows, err := r.pool.Query(ctx, `
|
|
SELECT id, chat_id, sender_id, text, created_at
|
|
FROM messages
|
|
WHERE chat_id = $1
|
|
AND id > COALESCE($2, '00000000-0000-0000-0000-000000000000'::uuid)
|
|
ORDER BY id
|
|
LIMIT $3`,
|
|
chatID, after, limit)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("select messages: %w", err)
|
|
}
|
|
messages, err := pgx.CollectRows(rows, func(row pgx.CollectableRow) (chat.Message, error) {
|
|
var m chat.Message
|
|
err := row.Scan(&m.ID, &m.ChatID, &m.SenderID, &m.Text, &m.CreatedAt)
|
|
m.CreatedAt = m.CreatedAt.UTC()
|
|
return m, err
|
|
})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("read messages: %w", err)
|
|
}
|
|
return messages, nil
|
|
}
|