forked from mtusi2026/mtusi2026lab2
feat(message): store messages in Postgres
This commit is contained in:
+1
-3
@@ -15,7 +15,6 @@ import (
|
|||||||
"mtusi2026-messenger/internal/logger"
|
"mtusi2026-messenger/internal/logger"
|
||||||
"mtusi2026-messenger/internal/postgres"
|
"mtusi2026-messenger/internal/postgres"
|
||||||
"mtusi2026-messenger/internal/service"
|
"mtusi2026-messenger/internal/service"
|
||||||
"mtusi2026-messenger/internal/store"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func Run() error {
|
func Run() error {
|
||||||
@@ -41,12 +40,11 @@ func Run() error {
|
|||||||
defer pool.Close()
|
defer pool.Close()
|
||||||
|
|
||||||
queries := db.New(pool)
|
queries := db.New(pool)
|
||||||
memory := store.New()
|
|
||||||
|
|
||||||
authService := service.NewAuthService(queries, cfg.Server.SessionTTL)
|
authService := service.NewAuthService(queries, cfg.Server.SessionTTL)
|
||||||
userService := service.NewUserService(queries)
|
userService := service.NewUserService(queries)
|
||||||
chatService := service.NewChatService(pool, queries)
|
chatService := service.NewChatService(pool, queries)
|
||||||
messageService := service.NewMessageService(memory)
|
messageService := service.NewMessageService(queries)
|
||||||
|
|
||||||
router := NewRouter(
|
router := NewRouter(
|
||||||
log,
|
log,
|
||||||
|
|||||||
@@ -27,8 +27,9 @@ type MessagePageResponse struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r CreateMessageRequest) Validate() bool {
|
func (r CreateMessageRequest) Validate() bool {
|
||||||
textLen := utf8.RuneCountInString(r.Text)
|
if strings.TrimSpace(r.Text) == "" ||
|
||||||
if strings.TrimSpace(r.Text) == "" || textLen > 4000 {
|
strings.ContainsRune(r.Text, 0) ||
|
||||||
|
utf8.RuneCountInString(r.Text) > 4000 {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ func (h *MessageHandler) Create(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
message, err := h.service.Create(userID, chatID, req.Text)
|
message, err := h.service.Create(r.Context(), userID, chatID, req.Text)
|
||||||
switch {
|
switch {
|
||||||
case errors.Is(err, service.ErrMessageChatNotFound):
|
case errors.Is(err, service.ErrMessageChatNotFound):
|
||||||
httpx.WriteError(
|
httpx.WriteError(
|
||||||
@@ -93,7 +93,7 @@ func (h *MessageHandler) List(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
messages, nextCursor, err := h.service.List(userID, chatID, limit, cursor)
|
messages, nextCursor, err := h.service.List(r.Context(), userID, chatID, limit, cursor)
|
||||||
switch {
|
switch {
|
||||||
case errors.Is(err, service.ErrMessageChatNotFound):
|
case errors.Is(err, service.ErrMessageChatNotFound):
|
||||||
httpx.WriteError(
|
httpx.WriteError(
|
||||||
|
|||||||
@@ -24,8 +24,8 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type chatQueries interface {
|
type chatQueries interface {
|
||||||
CreateChat(ctx context.Context, arg db.CreateChatParams) (db.Chat, error)
|
|
||||||
AddChatMembers(ctx context.Context, arg db.AddChatMembersParams) error
|
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)
|
GetChat(ctx context.Context, arg db.GetChatParams) (db.GetChatRow, error)
|
||||||
ListChats(ctx context.Context, arg db.ListChatsParams) ([]db.ListChatsRow, error)
|
ListChats(ctx context.Context, arg db.ListChatsParams) ([]db.ListChatsRow, error)
|
||||||
}
|
}
|
||||||
@@ -143,7 +143,7 @@ func (s *ChatService) List(
|
|||||||
var afterID *uuid.UUID
|
var afterID *uuid.UUID
|
||||||
|
|
||||||
if cursor != nil {
|
if cursor != nil {
|
||||||
decoded, err := decodeCursor(*cursor)
|
decoded, err := decodeCursor(*cursor, uuid.Nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, ErrInvalidCursor
|
return nil, nil, ErrInvalidCursor
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import (
|
|||||||
var ErrInvalidCursor = errors.New("invalid cursor")
|
var ErrInvalidCursor = errors.New("invalid cursor")
|
||||||
|
|
||||||
type Cursor struct {
|
type Cursor struct {
|
||||||
|
ChatID uuid.UUID `json:"chat_id"`
|
||||||
CreatedAt time.Time `json:"created_at"`
|
CreatedAt time.Time `json:"created_at"`
|
||||||
ID uuid.UUID `json:"id"`
|
ID uuid.UUID `json:"id"`
|
||||||
}
|
}
|
||||||
@@ -21,7 +22,7 @@ func encodeCursor(cursor Cursor) string {
|
|||||||
return base64.RawURLEncoding.EncodeToString(data)
|
return base64.RawURLEncoding.EncodeToString(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func decodeCursor(raw string) (Cursor, error) {
|
func decodeCursor(raw string, chatID uuid.UUID) (Cursor, error) {
|
||||||
data, err := base64.RawURLEncoding.DecodeString(raw)
|
data, err := base64.RawURLEncoding.DecodeString(raw)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Cursor{}, ErrInvalidCursor
|
return Cursor{}, ErrInvalidCursor
|
||||||
@@ -32,7 +33,7 @@ func decodeCursor(raw string) (Cursor, error) {
|
|||||||
return Cursor{}, ErrInvalidCursor
|
return Cursor{}, ErrInvalidCursor
|
||||||
}
|
}
|
||||||
|
|
||||||
if cursor.ID == uuid.Nil || cursor.CreatedAt.IsZero() {
|
if cursor.ID == uuid.Nil || cursor.CreatedAt.IsZero() || cursor.ChatID != chatID {
|
||||||
return Cursor{}, ErrInvalidCursor
|
return Cursor{}, ErrInvalidCursor
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+71
-25
@@ -1,52 +1,89 @@
|
|||||||
package service
|
package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
|
||||||
|
"mtusi2026-messenger/internal/db"
|
||||||
"mtusi2026-messenger/internal/model"
|
"mtusi2026-messenger/internal/model"
|
||||||
"mtusi2026-messenger/internal/store"
|
"mtusi2026-messenger/internal/postgres"
|
||||||
)
|
)
|
||||||
|
|
||||||
var ErrMessageChatNotFound = errors.New("chat not found")
|
var ErrMessageChatNotFound = errors.New("chat not found")
|
||||||
|
|
||||||
type MessageService struct {
|
type messageQueries interface {
|
||||||
store *store.Memory
|
CreateMessage(ctx context.Context, arg db.CreateMessageParams) (db.Message, error)
|
||||||
|
IsChatMember(ctx context.Context, arg db.IsChatMemberParams) (bool, error)
|
||||||
|
ListMessages(ctx context.Context, arg db.ListMessagesParams) ([]db.Message, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMessageService(store *store.Memory) *MessageService {
|
type MessageService struct {
|
||||||
return &MessageService{store: store}
|
q messageQueries
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewMessageService(q *db.Queries) *MessageService {
|
||||||
|
return &MessageService{q: q}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *MessageService) Create(
|
func (s *MessageService) Create(
|
||||||
|
ctx context.Context,
|
||||||
userID uuid.UUID,
|
userID uuid.UUID,
|
||||||
chatID uuid.UUID,
|
chatID uuid.UUID,
|
||||||
text string,
|
text string,
|
||||||
) (model.Message, error) {
|
) (model.Message, error) {
|
||||||
message, err := s.store.CreateMessage(chatID, userID, text)
|
id, err := uuid.NewV7()
|
||||||
if errors.Is(err, store.ErrNotFound) {
|
if err != nil {
|
||||||
|
return model.Message{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
row, err := s.q.CreateMessage(ctx, db.CreateMessageParams{
|
||||||
|
ID: id,
|
||||||
|
ChatID: chatID,
|
||||||
|
SenderID: userID,
|
||||||
|
Text: text,
|
||||||
|
})
|
||||||
|
if postgres.IsNotFound(err) {
|
||||||
return model.Message{}, ErrMessageChatNotFound
|
return model.Message{}, ErrMessageChatNotFound
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return model.Message{}, err
|
return model.Message{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return message, nil
|
return model.Message{
|
||||||
|
ID: row.ID,
|
||||||
|
ChatID: row.ChatID,
|
||||||
|
SenderID: row.SenderID,
|
||||||
|
Text: row.Text,
|
||||||
|
CreatedAt: row.CreatedAt,
|
||||||
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *MessageService) List(
|
func (s *MessageService) List(
|
||||||
|
ctx context.Context,
|
||||||
userID uuid.UUID,
|
userID uuid.UUID,
|
||||||
chatID uuid.UUID,
|
chatID uuid.UUID,
|
||||||
limit int,
|
limit int,
|
||||||
cursor *string,
|
cursor *string,
|
||||||
) ([]model.Message, *string, error) {
|
) ([]model.Message, *string, error) {
|
||||||
|
isMember, err := s.q.IsChatMember(ctx, db.IsChatMemberParams{
|
||||||
|
ChatID: chatID,
|
||||||
|
UserID: userID,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
if !isMember {
|
||||||
|
return nil, nil, ErrMessageChatNotFound
|
||||||
|
}
|
||||||
|
|
||||||
var afterCreatedAt *time.Time
|
var afterCreatedAt *time.Time
|
||||||
var afterID *uuid.UUID
|
var afterID *uuid.UUID
|
||||||
|
|
||||||
if cursor != nil {
|
if cursor != nil {
|
||||||
decoded, err := decodeCursor(*cursor)
|
decoded, err := decodeCursor(*cursor, chatID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, ErrInvalidCursor
|
return nil, nil, ErrInvalidCursor
|
||||||
}
|
}
|
||||||
@@ -55,28 +92,37 @@ func (s *MessageService) List(
|
|||||||
afterID = &decoded.ID
|
afterID = &decoded.ID
|
||||||
}
|
}
|
||||||
|
|
||||||
messages, hasMore, err := s.store.ListMessages(
|
rows, err := s.q.ListMessages(ctx, db.ListMessagesParams{
|
||||||
chatID,
|
ChatID: chatID,
|
||||||
userID,
|
AfterCreatedAt: afterCreatedAt,
|
||||||
afterCreatedAt,
|
AfterID: afterID,
|
||||||
afterID,
|
Lim: int32(limit + 1),
|
||||||
limit,
|
})
|
||||||
)
|
|
||||||
if errors.Is(err, store.ErrNotFound) {
|
|
||||||
return nil, nil, ErrMessageChatNotFound
|
|
||||||
}
|
|
||||||
if errors.Is(err, store.ErrInvalidCursor) {
|
|
||||||
return nil, nil, ErrInvalidCursor
|
|
||||||
}
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var next *string
|
hasMore := len(rows) > limit
|
||||||
if hasMore && len(messages) > 0 {
|
if hasMore {
|
||||||
last := messages[len(messages)-1]
|
rows = rows[:limit]
|
||||||
|
}
|
||||||
|
|
||||||
|
messages := make([]model.Message, 0, len(rows))
|
||||||
|
for _, r := range rows {
|
||||||
|
messages = append(messages, model.Message{
|
||||||
|
ID: r.ID,
|
||||||
|
ChatID: r.ChatID,
|
||||||
|
SenderID: r.SenderID,
|
||||||
|
Text: r.Text,
|
||||||
|
CreatedAt: r.CreatedAt,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
var next *string
|
||||||
|
if hasMore {
|
||||||
|
last := messages[len(messages)-1]
|
||||||
c := encodeCursor(Cursor{
|
c := encodeCursor(Cursor{
|
||||||
|
ChatID: chatID,
|
||||||
CreatedAt: last.CreatedAt,
|
CreatedAt: last.CreatedAt,
|
||||||
ID: last.ID,
|
ID: last.ID,
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user