From dbf5f354ee9788bbf459aa43a2326d20d682ae8b Mon Sep 17 00:00:00 2001 From: Seongmin Lee Date: Fri, 24 Oct 2025 00:28:31 +0900 Subject: [PATCH] appview/pagination: context based pagination Change-Id: rsqppswwxxkzwqrlswzqksqltykklzwt introduce helper methods: `IntoContext` and `FromContext` these will help using pagination obj form context easier and make it less error-prune by using private empty struct as a key instead of raw string Signed-off-by: Seongmin Lee --- appview/issues/issues.go | 6 +----- appview/middleware/middleware.go | 2 +- appview/notifications/notifications.go | 6 +----- appview/pagination/page.go | 23 +++++++++++++++++++++++ appview/state/gfi.go | 5 +---- 5 files changed, 27 insertions(+), 15 deletions(-) diff --git a/appview/issues/issues.go b/appview/issues/issues.go index fb1acc0a..ff39b8e5 100644 --- a/appview/issues/issues.go +++ b/appview/issues/issues.go @@ -770,11 +770,7 @@ func (rp *Issues) RepoIssues(w http.ResponseWriter, r *http.Request) { isOpen = true } - page, ok := r.Context().Value("page").(pagination.Page) - if !ok { - l.Error("failed to get page") - page = pagination.FirstPage() - } + page := pagination.FromContext(r.Context()) user := rp.oauth.GetUser(r) f, err := rp.repoResolver.Resolve(r) diff --git a/appview/middleware/middleware.go b/appview/middleware/middleware.go index 2d7366ab..c1fb754e 100644 --- a/appview/middleware/middleware.go +++ b/appview/middleware/middleware.go @@ -105,7 +105,7 @@ func Paginate(next http.Handler) http.Handler { } } - ctx := context.WithValue(r.Context(), "page", page) + ctx := pagination.IntoContext(r.Context(), page) next.ServeHTTP(w, r.WithContext(ctx)) }) } diff --git a/appview/notifications/notifications.go b/appview/notifications/notifications.go index a4047521..c0962e77 100644 --- a/appview/notifications/notifications.go +++ b/appview/notifications/notifications.go @@ -49,11 +49,7 @@ func (n *Notifications) notificationsPage(w http.ResponseWriter, r *http.Request l := n.logger.With("handler", "notificationsPage") user := n.oauth.GetUser(r) - page, ok := r.Context().Value("page").(pagination.Page) - if !ok { - l.Error("failed to get page") - page = pagination.FirstPage() - } + page := pagination.FromContext(r.Context()) total, err := db.CountNotifications( n.db, diff --git a/appview/pagination/page.go b/appview/pagination/page.go index b3dc781b..d84f07d5 100644 --- a/appview/pagination/page.go +++ b/appview/pagination/page.go @@ -1,5 +1,7 @@ package pagination +import "context" + type Page struct { Offset int // where to start from Limit int // number of items in a page @@ -12,6 +14,27 @@ func FirstPage() Page { } } +type ctxKey struct{} + +func IntoContext(ctx context.Context, page Page) context.Context { + return context.WithValue(ctx, ctxKey{}, page) +} + +func FromContext(ctx context.Context) Page { + if ctx == nil { + return FirstPage() + } + v := ctx.Value(ctxKey{}) + if v == nil { + return FirstPage() + } + page, ok := v.(Page) + if !ok { + return FirstPage() + } + return page +} + func (p Page) Previous() Page { if p.Offset-p.Limit < 0 { return FirstPage() diff --git a/appview/state/gfi.go b/appview/state/gfi.go index b0106813..d7909e57 100644 --- a/appview/state/gfi.go +++ b/appview/state/gfi.go @@ -18,10 +18,7 @@ import ( func (s *State) GoodFirstIssues(w http.ResponseWriter, r *http.Request) { user := s.oauth.GetUser(r) - page, ok := r.Context().Value("page").(pagination.Page) - if !ok { - page = pagination.FirstPage() - } + page := pagination.FromContext(r.Context()) goodFirstIssueLabel := fmt.Sprintf("at://%s/%s/%s", consts.TangledDid, tangled.LabelDefinitionNSID, "good-first-issue") -- 2.43.0