forked from mtusi2026/mtusi2026lab2
105 lines
3.1 KiB
Go
105 lines
3.1 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"gitflic.ru/project/bulla-max/mtusi2026lab2/internal/common"
|
|
"gitflic.ru/project/bulla-max/mtusi2026lab2/internal/model"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
// CreateMessage uses INSERT ... SELECT so the membership check and the insert
|
|
// are a single atomic statement: no window where the user is removed between
|
|
// check and write. Zero affected rows means the chat is missing or foreign.
|
|
func (s *Store) CreateMessage(ctx context.Context, chatID, senderID uuid.UUID, text string) (model.Message, error) {
|
|
ctx, cancel := s.withTimeout(ctx)
|
|
defer cancel()
|
|
|
|
message := model.Message{
|
|
ID: uuid.New(),
|
|
ChatID: chatID,
|
|
SenderID: senderID,
|
|
Text: text,
|
|
CreatedAt: common.NowUTC(),
|
|
}
|
|
|
|
tag, err := s.pool.Exec(ctx, `
|
|
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
|
|
SELECT $1, $2, $3, $4, $5
|
|
WHERE EXISTS (SELECT 1 FROM chat_members WHERE chat_id = $2 AND user_id = $3)`,
|
|
message.ID, message.ChatID, message.SenderID, message.Text, time.Time(message.CreatedAt))
|
|
if err != nil {
|
|
return model.Message{}, dbError(err)
|
|
}
|
|
if tag.RowsAffected() == 0 {
|
|
return model.Message{}, common.NotFound("chat not found")
|
|
}
|
|
|
|
return message, nil
|
|
}
|
|
|
|
func (s *Store) ListMessages(ctx context.Context, userID, chatID uuid.UUID, limit int, cursor *common.CursorKeyCtx) (*common.Page[model.Message], error) {
|
|
isMember, err := s.IsMember(ctx, userID, chatID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !isMember {
|
|
return nil, common.NotFound("chat not found")
|
|
}
|
|
// AGENT: Cursor context is checked only after membership, so a foreign
|
|
// caller gets 404 and cannot probe cursor validity against someone else's chat.
|
|
if cursor != nil && cursor.ContextID != chatID {
|
|
return nil, common.BadRequest("bad cursor value")
|
|
}
|
|
|
|
ctx, cancel := s.withTimeout(ctx)
|
|
defer cancel()
|
|
|
|
// AGENT: limit+1 is fetched so next_cursor is known to be non-null without a
|
|
// second COUNT query; the extra row is never returned to the client.
|
|
cursorTime, cursorID := cursorValues(cursor)
|
|
rows, err := s.pool.Query(ctx, `
|
|
SELECT id, chat_id, sender_id, text, created_at
|
|
FROM messages
|
|
WHERE chat_id = $1 AND (created_at, id) > ($2, $3)
|
|
ORDER BY created_at, id
|
|
LIMIT $4`, chatID, cursorTime, cursorID, limit+1)
|
|
if err != nil {
|
|
return nil, dbError(err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
messages := make([]model.Message, 0, limit)
|
|
for rows.Next() {
|
|
message, scanErr := scanMessage(rows)
|
|
if scanErr != nil {
|
|
return nil, dbError(scanErr)
|
|
}
|
|
messages = append(messages, message)
|
|
}
|
|
if err = rows.Err(); err != nil {
|
|
return nil, dbError(err)
|
|
}
|
|
|
|
hasMore := len(messages) > limit
|
|
if hasMore {
|
|
messages = messages[:limit]
|
|
}
|
|
|
|
return &common.Page[model.Message]{
|
|
Items: messages,
|
|
NextCursor: common.NextCursor(messages, hasMore, chatID),
|
|
}, nil
|
|
}
|
|
|
|
func scanMessage(row rowScanner) (model.Message, error) {
|
|
var message model.Message
|
|
var createdAt time.Time
|
|
if err := row.Scan(&message.ID, &message.ChatID, &message.SenderID, &message.Text, &createdAt); err != nil {
|
|
return model.Message{}, err
|
|
}
|
|
message.CreatedAt = common.RFCFormTime(createdAt.UTC())
|
|
return message, nil
|
|
}
|