Handle pagination for the federated actions & followers responses (#1731)
* Add pagination for admin social list * Use Paginated API for followers tab on frontend
This commit is contained in:
39
router/middleware/pagination.go
Normal file
39
router/middleware/pagination.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// PaginatedHandlerFunc is a handler for endpoints that require pagination.
|
||||
type PaginatedHandlerFunc func(int, int, http.ResponseWriter, *http.Request)
|
||||
|
||||
// HandlePagination is a middleware handler that pulls pagination values
|
||||
// and passes them along.
|
||||
func HandlePagination(handler PaginatedHandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
// Default 50 items per page
|
||||
limitString := r.URL.Query().Get("limit")
|
||||
if limitString == "" {
|
||||
limitString = "50"
|
||||
}
|
||||
limit, err := strconv.Atoi(limitString)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Default first page 0
|
||||
offsetString := r.URL.Query().Get("offset")
|
||||
if offsetString == "" {
|
||||
offsetString = "0"
|
||||
}
|
||||
offset, err := strconv.Atoi(offsetString)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
handler(offset, limit, w, r)
|
||||
}
|
||||
}
|
||||
@@ -77,7 +77,7 @@ func Start() error {
|
||||
http.HandleFunc("/api/remotefollow", controllers.RemoteFollow)
|
||||
|
||||
// return followers
|
||||
http.HandleFunc("/api/followers", controllers.GetFollowers)
|
||||
http.HandleFunc("/api/followers", middleware.HandlePagination(controllers.GetFollowers))
|
||||
|
||||
// Authenticated admin requests
|
||||
|
||||
@@ -127,7 +127,7 @@ func Start() error {
|
||||
http.HandleFunc("/api/admin/chat/users/moderators", middleware.RequireAdminAuth(admin.GetModerators))
|
||||
|
||||
// return followers
|
||||
http.HandleFunc("/api/admin/followers", middleware.RequireAdminAuth(controllers.GetFollowers))
|
||||
http.HandleFunc("/api/admin/followers", middleware.RequireAdminAuth(middleware.HandlePagination(controllers.GetFollowers)))
|
||||
|
||||
// Get a list of pending follow requests
|
||||
http.HandleFunc("/api/admin/followers/pending", middleware.RequireAdminAuth(admin.GetPendingFollowRequests))
|
||||
@@ -310,7 +310,7 @@ func Start() error {
|
||||
http.HandleFunc("/api/admin/federation/send", middleware.RequireAdminAuth(admin.SendFederatedMessage))
|
||||
|
||||
// Return federated activities
|
||||
http.HandleFunc("/api/admin/federation/actions", middleware.RequireAdminAuth(admin.GetFederatedActions))
|
||||
http.HandleFunc("/api/admin/federation/actions", middleware.RequireAdminAuth(middleware.HandlePagination(admin.GetFederatedActions)))
|
||||
|
||||
// ActivityPub has its own router
|
||||
activitypub.Start(data.GetDatastore())
|
||||
|
||||
Reference in New Issue
Block a user