fix: api rate limit
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/prabhupadasays/spsays-bff/internal/middleware"
|
||||
@@ -258,6 +259,17 @@ func (h *PostHandler) Unhide(w http.ResponseWriter, r *http.Request) {
|
||||
func (h *PostHandler) ListMine(w http.ResponseWriter, r *http.Request) {
|
||||
uid := middleware.UserID(r.Context())
|
||||
limit, offset := parseLimitOffset(r)
|
||||
// Keyset path (scroll feed): a `before` cursor pages older posts. Without a
|
||||
// cursor we keep the offset+meta path the profile grid relies on.
|
||||
if beforeAt, beforeID, ok := parseCursor(r); ok {
|
||||
posts, err := h.svc.ListMineKeyset(r.Context(), uid, beforeAt, beforeID, limit)
|
||||
if err != nil {
|
||||
mapSocialErr(w, err)
|
||||
return
|
||||
}
|
||||
response.JSON(w, http.StatusOK, posts)
|
||||
return
|
||||
}
|
||||
posts, total, err := h.svc.ListMine(r.Context(), uid, limit, offset)
|
||||
if err != nil {
|
||||
mapSocialErr(w, err)
|
||||
@@ -719,6 +731,22 @@ func (h *ProfileHandler) Feed(w http.ResponseWriter, r *http.Request) {
|
||||
// gateByBlock already resolved the owner, so list by id rather than
|
||||
// re-resolving the handle. Hidden posts never appear here (they live in
|
||||
// the owner's separate Hidden tab).
|
||||
//
|
||||
// Keyset path (scroll feed): a `before` cursor pages older posts. Without a
|
||||
// cursor we keep the offset+meta path the profile grid relies on.
|
||||
if beforeAt, beforeID, ok := parseCursor(r); ok {
|
||||
posts, err := h.posts.ListByUserKeyset(r.Context(), owner.ID, beforeAt, beforeID, limit)
|
||||
if err != nil {
|
||||
mapSocialErr(w, err)
|
||||
return
|
||||
}
|
||||
if err := h.hydrateFeed(r, posts); err != nil {
|
||||
mapSocialErr(w, err)
|
||||
return
|
||||
}
|
||||
response.JSON(w, http.StatusOK, posts)
|
||||
return
|
||||
}
|
||||
posts, total, err := h.posts.ListByUser(r.Context(), owner.ID, limit, offset)
|
||||
if err != nil {
|
||||
mapSocialErr(w, err)
|
||||
@@ -896,6 +924,22 @@ func parseLimitOffset(r *http.Request) (limit, offset int) {
|
||||
return limit, offset
|
||||
}
|
||||
|
||||
// parseCursor reads an optional keyset cursor for the scroll feed: `before` is
|
||||
// the last seen post's created_at (RFC3339Nano) and `before_id` its id. ok is
|
||||
// false when either is missing or unparseable (callers then page by offset).
|
||||
func parseCursor(r *http.Request) (beforeAt time.Time, beforeID string, ok bool) {
|
||||
at := strings.TrimSpace(r.URL.Query().Get("before"))
|
||||
id := strings.TrimSpace(r.URL.Query().Get("before_id"))
|
||||
if at == "" || id == "" {
|
||||
return time.Time{}, "", false
|
||||
}
|
||||
t, err := time.Parse(time.RFC3339Nano, at)
|
||||
if err != nil {
|
||||
return time.Time{}, "", false
|
||||
}
|
||||
return t, id, true
|
||||
}
|
||||
|
||||
func paginationMeta(total, limit, offset int) *response.Meta {
|
||||
if limit == 0 {
|
||||
limit = 20
|
||||
|
||||
Reference in New Issue
Block a user