summaryrefslogtreecommitdiff
path: root/internal/paging
diff options
context:
space:
mode:
Diffstat (limited to 'internal/paging')
-rw-r--r--internal/paging/boundary.go14
-rw-r--r--internal/paging/page.go77
2 files changed, 42 insertions, 49 deletions
diff --git a/internal/paging/boundary.go b/internal/paging/boundary.go
index bf4508aff..8a3187d7f 100644
--- a/internal/paging/boundary.go
+++ b/internal/paging/boundary.go
@@ -54,6 +54,13 @@ func EitherMinID(minID, sinceID string) Boundary {
the cursor value, and max_id provides a
limiting value to the results.
+ But to further complicate it...
+
+ The "next" and "prev" relative links provided
+ in the link header are ALWAYS DESCENDING. Which
+ means we will ALWAYS provide next=?max_id and
+ prev=?min_id. *shakes fist at mastodon api*
+
*/
switch {
case minID != "":
@@ -67,7 +74,12 @@ func EitherMinID(minID, sinceID string) Boundary {
// SinceID ...
func SinceID(sinceID string) Boundary {
return Boundary{
- Name: "since_id",
+ // even when a since_id query is
+ // provided, the next / prev rel
+ // links are DESCENDING with
+ // next:max_id and prev:min_id.
+ // so ALWAYS use min_id as name.
+ Name: "min_id",
Value: sinceID,
Order: OrderDescending,
}
diff --git a/internal/paging/page.go b/internal/paging/page.go
index 8e8261396..082012879 100644
--- a/internal/paging/page.go
+++ b/internal/paging/page.go
@@ -202,8 +202,9 @@ func Page_PageFunc[WithID any](p *Page, in []WithID, get func(WithID) string) []
return in
}
-// Next creates a new instance for the next returnable page, using
-// given max value. This preserves original limit and max key name.
+// Prev creates a new instance for the next returnable page, using
+// given max value. This will always assume DESCENDING for Mastodon
+// API compatibility, but in case of change it can support both.
func (p *Page) Next(lo, hi string) *Page {
if p == nil || lo == "" || hi == "" {
// no paging.
@@ -216,25 +217,22 @@ func (p *Page) Next(lo, hi string) *Page {
// Set original limit.
p2.Limit = p.Limit
- if p.order().Ascending() {
- // When ascending, next page
- // needs to start with min at
- // the next highest value.
- p2.Min = p.Min.new(hi)
- p2.Max = p.Max.new("")
- } else {
- // When descending, next page
- // needs to start with max at
- // the next lowest value.
- p2.Min = p.Min.new("")
- p2.Max = p.Max.new(lo)
- }
+ // NOTE:
+ // We ALWAYS assume the order
+ // when creating next / prev
+ // links is DESCENDING. It will
+ // always use prev: ?max_name
+ p2.Min = p.Min.new("")
+ p2.Max = p.Max.new(lo)
+ p2.Min.Order = OrderDescending
+ p2.Max.Order = OrderDescending
return p2
}
// Prev creates a new instance for the prev returnable page, using
-// given min value. This preserves original limit and min key name.
+// given min value. This will always assume DESCENDING for Mastodon
+// API compatibility, but in case of change it can support both.
func (p *Page) Prev(lo, hi string) *Page {
if p == nil || lo == "" || hi == "" {
// no paging.
@@ -247,19 +245,15 @@ func (p *Page) Prev(lo, hi string) *Page {
// Set original limit.
p2.Limit = p.Limit
- if p.order().Ascending() {
- // When ascending, prev page
- // needs to start with max at
- // the next lowest value.
- p2.Min = p.Min.new("")
- p2.Max = p.Max.new(lo)
- } else {
- // When descending, next page
- // needs to start with max at
- // the next lowest value.
- p2.Min = p.Min.new(hi)
- p2.Max = p.Max.new("")
- }
+ // NOTE:
+ // We ALWAYS assume the order
+ // when creating next / prev
+ // links is DESCENDING. It will
+ // always use prev: ?min_name
+ p2.Min = p.Min.new(hi)
+ p2.Max = p.Max.new("")
+ p2.Min.Order = OrderDescending
+ p2.Max.Order = OrderDescending
return p2
}
@@ -289,27 +283,14 @@ func (p *Page) ToLinkURL(proto, host, path string, queryParams url.Values) *url.
queryParams = cloneQuery(queryParams)
}
- var cursor string
-
- // Depending on page ordering, the
- // page will be cursored by either
- // the min or max query parameter.
- if p.order().Ascending() {
- cursor = p.Min.Name
- } else {
- cursor = p.Max.Name
+ if p.Min.Value != "" {
+ // Set page-minimum cursor value.
+ queryParams.Set(p.Min.Name, p.Min.Value)
}
- if cursor != "" {
- if p.Min.Value != "" {
- // Set page-minimum cursor value.
- queryParams.Set(cursor, p.Min.Value)
- }
-
- if p.Max.Value != "" {
- // Set page-maximum cursor value.
- queryParams.Set(cursor, p.Max.Value)
- }
+ if p.Max.Value != "" {
+ // Set page-maximum cursor value.
+ queryParams.Set(p.Max.Name, p.Max.Value)
}
if p.Limit > 0 {