fix: address lab2 review feedback

This commit is contained in:
Fedoroff
2026-10-27 20:03:08 +03:00
parent c251f2cb7d
commit f08011385a
11 changed files with 280 additions and 82 deletions
+14 -25
View File
@@ -2,7 +2,6 @@ package handler
import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"net/http"
@@ -86,17 +85,14 @@ func (h *MessageHandler) Create(w http.ResponseWriter, r *http.Request) {
currentUser, _ := middleware.GetCurrentUser(r.Context())
var req createMessageRequest
decoder := json.NewDecoder(r.Body)
decoder.DisallowUnknownFields()
if err := decoder.Decode(&req); err != nil {
if err := decodeOneObject(r, &req); err != nil {
writeError(w, r, http.StatusBadRequest,
"invalid_request", "invalid request body")
return
}
textLength := utf8.RuneCountInString(req.Text)
if textLength < 1 || textLength > 4000 {
if strings.TrimSpace(req.Text) == "" || textLength > 4000 {
writeError(w, r, http.StatusBadRequest,
"invalid_request", "text must contain from 1 to 4000 characters")
return
@@ -146,7 +142,7 @@ func (h *MessageHandler) List(w http.ResponseWriter, r *http.Request) {
var cursorID *uuid.UUID
if raw := r.URL.Query().Get("cursor"); raw != "" {
t, id, err := decodeCursor(raw)
t, id, err := decodeMessageCursor(raw, chatID)
if err != nil {
writeError(w, r, http.StatusBadRequest,
"invalid_request", "invalid cursor")
@@ -167,7 +163,7 @@ func (h *MessageHandler) List(w http.ResponseWriter, r *http.Request) {
var nextCursor *string
if hasMore && len(items) > 0 {
cursor := encodeCursor(items[len(items)-1])
cursor := encodeMessageCursor(items[len(items)-1])
nextCursor = &cursor
}
@@ -177,35 +173,28 @@ func (h *MessageHandler) List(w http.ResponseWriter, r *http.Request) {
})
}
func encodeCursor(message model.Message) string {
value := fmt.Sprintf(
"%s|%s",
message.CreatedAt.Format(time.RFC3339Nano),
message.ID.String(),
)
// Cursor contains the chat UUID to prevent cross-chat replay.
func encodeMessageCursor(message model.Message) string {
value := fmt.Sprintf("%s|%s|%s", message.ChatID, message.CreatedAt.Format(time.RFC3339Nano), message.ID)
return base64.RawURLEncoding.EncodeToString([]byte(value))
}
func decodeCursor(cursor string) (time.Time, uuid.UUID, error) {
func decodeMessageCursor(cursor string, chatID uuid.UUID) (time.Time, uuid.UUID, error) {
data, err := base64.RawURLEncoding.DecodeString(cursor)
if err != nil {
return time.Time{}, uuid.Nil, err
}
parts := strings.SplitN(string(data), "|", 2)
if len(parts) != 2 {
return time.Time{}, uuid.Nil, fmt.Errorf("invalid cursor")
parts := strings.Split(string(data), "|")
if len(parts) != 3 || parts[0] != chatID.String() {
return time.Time{}, uuid.Nil, fmt.Errorf("invalid cursor for chat")
}
createdAt, err := time.Parse(time.RFC3339Nano, parts[0])
t, err := time.Parse(time.RFC3339Nano, parts[1])
if err != nil {
return time.Time{}, uuid.Nil, err
}
id, err := uuid.Parse(parts[1])
id, err := uuid.Parse(parts[2])
if err != nil {
return time.Time{}, uuid.Nil, err
}
return createdAt, id, nil
return t, id, nil
}