+38
-9
server/handle_repo_list_records.go
+38
-9
server/handle_repo_list_records.go
···
2
2
3
3
import (
4
4
"strconv"
5
-
"strings"
6
5
7
6
"github.com/Azure/go-autorest/autorest/to"
8
7
"github.com/bluesky-social/indigo/atproto/data"
8
+
"github.com/bluesky-social/indigo/atproto/syntax"
9
9
"github.com/haileyok/cocoon/internal/helpers"
10
10
"github.com/haileyok/cocoon/models"
11
11
"github.com/labstack/echo/v4"
12
12
)
13
+
14
+
type ComAtprotoRepoListRecordsRequest struct {
15
+
Repo string `query:"repo" validate:"required"`
16
+
Collection string `query:"collection" validate:"required,atproto-nsid"`
17
+
Limit int64 `query:"limit"`
18
+
Cursor string `query:"cursor"`
19
+
Reverse bool `query:"reverse"`
20
+
}
13
21
14
22
type ComAtprotoRepoListRecordsResponse struct {
15
23
Cursor *string `json:"cursor,omitempty"`
···
38
46
}
39
47
40
48
func (s *Server) handleListRecords(e echo.Context) error {
41
-
did := e.QueryParam("repo")
42
-
collection := e.QueryParam("collection")
43
-
cursor := e.QueryParam("cursor")
44
-
reverse := e.QueryParam("reverse")
49
+
var req ComAtprotoRepoListRecordsRequest
50
+
if err := e.Bind(&req); err != nil {
51
+
s.logger.Error("could not bind list records request", "error", err)
52
+
return helpers.ServerError(e, nil)
53
+
}
54
+
55
+
if err := e.Validate(req); err != nil {
56
+
return helpers.InputError(e, nil)
57
+
}
58
+
59
+
if req.Limit <= 0 {
60
+
req.Limit = 50
61
+
} else if req.Limit > 100 {
62
+
req.Limit = 100
63
+
}
64
+
45
65
limit, err := getLimitFromContext(e, 50)
46
66
if err != nil {
47
67
return helpers.InputError(e, nil)
···
51
71
dir := "<"
52
72
cursorquery := ""
53
73
54
-
if strings.ToLower(reverse) == "true" {
74
+
if req.Reverse {
55
75
sort = "ASC"
56
76
dir = ">"
57
77
}
58
78
59
-
params := []any{did, collection}
60
-
if cursor != "" {
61
-
params = append(params, cursor)
79
+
did := req.Repo
80
+
if _, err := syntax.ParseDID(did); err != nil {
81
+
actor, err := s.getActorByHandle(req.Repo)
82
+
if err != nil {
83
+
return helpers.InputError(e, to.StringPtr("RepoNotFound"))
84
+
}
85
+
did = actor.Did
86
+
}
87
+
88
+
params := []any{did, req.Collection}
89
+
if req.Cursor != "" {
90
+
params = append(params, req.Cursor)
62
91
cursorquery = "AND created_at " + dir + " ?"
63
92
}
64
93
params = append(params, limit)