209 lines
4.3 KiB
Go
209 lines
4.3 KiB
Go
package service
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"slices"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
"mtusi2026-messenger/internal/db"
|
|
"mtusi2026-messenger/internal/model"
|
|
"mtusi2026-messenger/internal/postgres"
|
|
)
|
|
|
|
var (
|
|
ErrChatNotFound = errors.New("chat not found")
|
|
ErrNotChatMember = errors.New("caller must be a member of member_ids")
|
|
ErrDuplicateMember = errors.New("member_ids contains duplicates")
|
|
ErrUnknownMember = errors.New("member_ids contains unknown user")
|
|
)
|
|
|
|
type chatQueries interface {
|
|
AddChatMembers(ctx context.Context, arg db.AddChatMembersParams) error
|
|
CreateChat(ctx context.Context, arg db.CreateChatParams) (db.Chat, error)
|
|
GetChat(ctx context.Context, arg db.GetChatParams) (db.GetChatRow, error)
|
|
ListChatsAfter(ctx context.Context, arg db.ListChatsAfterParams) ([]db.ListChatsAfterRow, error)
|
|
ListChatsFirstPage(ctx context.Context, arg db.ListChatsFirstPageParams) ([]db.ListChatsFirstPageRow, error)
|
|
}
|
|
|
|
type txBeginner interface {
|
|
Begin(ctx context.Context) (pgx.Tx, error)
|
|
}
|
|
|
|
type ChatService struct {
|
|
pool txBeginner
|
|
q chatQueries
|
|
withTx func(pgx.Tx) chatQueries
|
|
}
|
|
|
|
func NewChatService(pool *pgxpool.Pool, q *db.Queries) *ChatService {
|
|
return &ChatService{
|
|
pool: pool,
|
|
q: q,
|
|
withTx: func(tx pgx.Tx) chatQueries {
|
|
return q.WithTx(tx)
|
|
},
|
|
}
|
|
}
|
|
|
|
func (s *ChatService) Create(
|
|
ctx context.Context,
|
|
userID uuid.UUID,
|
|
title string,
|
|
memberIDs []uuid.UUID,
|
|
) (model.Chat, error) {
|
|
if !slices.Contains(memberIDs, userID) {
|
|
return model.Chat{}, ErrNotChatMember
|
|
}
|
|
|
|
tx, err := s.pool.Begin(ctx)
|
|
if err != nil {
|
|
return model.Chat{}, err
|
|
}
|
|
defer tx.Rollback(ctx)
|
|
|
|
q := s.withTx(tx)
|
|
|
|
id, err := uuid.NewV7()
|
|
if err != nil {
|
|
return model.Chat{}, err
|
|
}
|
|
|
|
row, err := q.CreateChat(ctx, db.CreateChatParams{
|
|
ID: id,
|
|
Title: title,
|
|
})
|
|
if err != nil {
|
|
return model.Chat{}, err
|
|
}
|
|
|
|
err = q.AddChatMembers(ctx, db.AddChatMembersParams{
|
|
ChatID: row.ID,
|
|
UserIds: memberIDs,
|
|
})
|
|
switch postgres.ErrCode(err) {
|
|
case postgres.CodeUniqueViolation:
|
|
return model.Chat{}, ErrDuplicateMember
|
|
case postgres.CodeForeignKeyViolation:
|
|
return model.Chat{}, ErrUnknownMember
|
|
}
|
|
if err != nil {
|
|
return model.Chat{}, err
|
|
}
|
|
|
|
if err := tx.Commit(ctx); err != nil {
|
|
return model.Chat{}, err
|
|
}
|
|
|
|
ids := slices.Clone(memberIDs)
|
|
slices.SortFunc(ids, func(a, b uuid.UUID) int {
|
|
return bytes.Compare(a[:], b[:])
|
|
})
|
|
|
|
return model.Chat{
|
|
ID: row.ID,
|
|
Title: row.Title,
|
|
CreatedAt: row.CreatedAt,
|
|
MemberIDs: ids,
|
|
}, nil
|
|
}
|
|
|
|
func (s *ChatService) Get(
|
|
ctx context.Context,
|
|
userID uuid.UUID,
|
|
chatID uuid.UUID,
|
|
) (model.Chat, error) {
|
|
row, err := s.q.GetChat(ctx, db.GetChatParams{
|
|
ID: chatID,
|
|
UserID: userID,
|
|
})
|
|
if postgres.IsNotFound(err) {
|
|
return model.Chat{}, ErrChatNotFound
|
|
}
|
|
if err != nil {
|
|
return model.Chat{}, err
|
|
}
|
|
|
|
return model.Chat{
|
|
ID: row.ID,
|
|
Title: row.Title,
|
|
CreatedAt: row.CreatedAt,
|
|
MemberIDs: row.MemberIds,
|
|
}, nil
|
|
}
|
|
|
|
func (s *ChatService) List(
|
|
ctx context.Context,
|
|
userID uuid.UUID,
|
|
limit int,
|
|
cursor *string,
|
|
) ([]model.Chat, *string, error) {
|
|
var chats []model.Chat
|
|
|
|
if cursor == nil {
|
|
rows, err := s.q.ListChatsFirstPage(ctx, db.ListChatsFirstPageParams{
|
|
UserID: userID,
|
|
Lim: int32(limit + 1),
|
|
})
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
chats = make([]model.Chat, 0, len(rows))
|
|
for _, r := range rows {
|
|
chats = append(chats, model.Chat{
|
|
ID: r.ID,
|
|
Title: r.Title,
|
|
CreatedAt: r.CreatedAt,
|
|
MemberIDs: r.MemberIds,
|
|
})
|
|
}
|
|
} else {
|
|
decoded, derr := decodeCursor(*cursor, uuid.Nil)
|
|
if derr != nil {
|
|
return nil, nil, ErrInvalidCursor
|
|
}
|
|
|
|
rows, err := s.q.ListChatsAfter(ctx, db.ListChatsAfterParams{
|
|
UserID: userID,
|
|
AfterCreatedAt: decoded.CreatedAt,
|
|
AfterID: decoded.ID,
|
|
Lim: int32(limit + 1),
|
|
})
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
|
|
chats = make([]model.Chat, 0, len(rows))
|
|
for _, r := range rows {
|
|
chats = append(chats, model.Chat{
|
|
ID: r.ID,
|
|
Title: r.Title,
|
|
CreatedAt: r.CreatedAt,
|
|
MemberIDs: r.MemberIds,
|
|
})
|
|
}
|
|
}
|
|
|
|
hasMore := len(chats) > limit
|
|
if hasMore {
|
|
chats = chats[:limit]
|
|
}
|
|
|
|
var next *string
|
|
if hasMore {
|
|
last := chats[len(chats)-1]
|
|
c := encodeCursor(Cursor{
|
|
CreatedAt: last.CreatedAt,
|
|
ID: last.ID,
|
|
})
|
|
next = &c
|
|
}
|
|
|
|
return chats, next, nil
|
|
}
|