summaryrefslogtreecommitdiff
path: root/internal/paging/parse.go
blob: ce6391708d530a33a51b1e084e32735d9ed37260 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// GoToSocial
// Copyright (C) GoToSocial Authors admin@gotosocial.org
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

package paging

import (
	"strconv"

	"github.com/gin-gonic/gin"
	"github.com/superseriousbusiness/gotosocial/internal/gtserror"
)

// ParseIDPage parses an ID Page from a request context, returning BadRequest on error parsing.
// The min, max and default parameters define the page size limit minimum, maximum and default
// value, where a non-zero default will enforce paging for the endpoint on which this is called.
// While conversely, a zero default limit will not enforce paging, returning a nil page value.
func ParseIDPage(c *gin.Context, min, max, _default int) (*Page, gtserror.WithCode) {
	// Extract request query params.
	sinceID, haveSince := c.GetQuery("since_id")
	minID, haveMin := c.GetQuery("min_id")
	maxID, haveMax := c.GetQuery("max_id")

	// Extract request limit parameter.
	limit, errWithCode := ParseLimit(c, min, max, _default)
	if errWithCode != nil {
		return nil, errWithCode
	}

	switch {
	case haveMin:
		// A min_id was supplied, even if the value
		// itself is empty. This indicates ASC order.
		return &Page{
			Min:   MinID(minID),
			Max:   MaxID(maxID),
			Limit: limit,
		}, nil

	case haveMax || haveSince:
		// A max_id or since_id was supplied, even if the
		// value itself is empty. This indicates DESC order.
		return &Page{
			Min:   SinceID(sinceID),
			Max:   MaxID(maxID),
			Limit: limit,
		}, nil

	case limit == 0:
		// No ID paging params provided, and no default
		// limit value which indicates paging not enforced.
		return nil, nil

	default:
		// only limit.
		return &Page{
			Min:   SinceID(""),
			Max:   MaxID(""),
			Limit: limit,
		}, nil
	}
}

// ParseShortcodeDomainPage parses an emoji shortcode domain Page from a request context, returning BadRequest
// on error parsing. The min, max and default parameters define the page size limit minimum, maximum and default
// value where a non-zero default will enforce paging for the endpoint on which this is called. While conversely,
// a zero default limit will not enforce paging, returning a nil page value.
func ParseShortcodeDomainPage(c *gin.Context, min, max, _default int) (*Page, gtserror.WithCode) {
	// Extract request query parameters.
	minShortcode, haveMin := c.GetQuery("min_shortcode_domain")
	maxShortcode, haveMax := c.GetQuery("max_shortcode_domain")

	// Extract request limit parameter.
	limit, errWithCode := ParseLimit(c, min, max, _default)
	if errWithCode != nil {
		return nil, errWithCode
	}

	if !haveMin &&
		!haveMax &&
		limit == 0 {
		// No ID paging params provided, and no default
		// limit value which indicates paging not enforced.
		return nil, nil
	}

	return &Page{
		Min:   MinShortcodeDomain(minShortcode),
		Max:   MaxShortcodeDomain(maxShortcode),
		Limit: limit,
	}, nil
}

// ParseLimit parses the limit query parameter from a request context, returning BadRequest on error parsing and _default if zero limit given.
func ParseLimit(c *gin.Context, min, max, _default int) (int, gtserror.WithCode) {
	// Get limit query param.
	str, ok := c.GetQuery("limit")
	if !ok {
		return _default, nil
	}

	// Attempt to parse limit int.
	i, err := strconv.Atoi(str)
	if err != nil {
		const help = "bad integer limit value"
		return 0, gtserror.NewErrorBadRequest(err, help)
	}

	switch {
	case i == 0:
		return _default, nil
	case i < min:
		return min, nil
	case i > max:
		return max, nil
	default:
		return i, nil
	}
}