this repo has no description

fix: allow handle to be used for repo in listRecords (#18)

authored by hailey and committed by GitHub 01e9ae1e eb5580e9

Changed files
+39 -9
server
+1
.gitignore
··· 3 /cocoon 4 *.key 5 *.secret
··· 3 /cocoon 4 *.key 5 *.secret 6 + .DS_Store
+38 -9
server/handle_repo_list_records.go
··· 2 3 import ( 4 "strconv" 5 - "strings" 6 7 "github.com/Azure/go-autorest/autorest/to" 8 "github.com/bluesky-social/indigo/atproto/data" 9 "github.com/haileyok/cocoon/internal/helpers" 10 "github.com/haileyok/cocoon/models" 11 "github.com/labstack/echo/v4" 12 ) 13 14 type ComAtprotoRepoListRecordsResponse struct { 15 Cursor *string `json:"cursor,omitempty"` ··· 38 } 39 40 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") 45 limit, err := getLimitFromContext(e, 50) 46 if err != nil { 47 return helpers.InputError(e, nil) ··· 51 dir := "<" 52 cursorquery := "" 53 54 - if strings.ToLower(reverse) == "true" { 55 sort = "ASC" 56 dir = ">" 57 } 58 59 - params := []any{did, collection} 60 - if cursor != "" { 61 - params = append(params, cursor) 62 cursorquery = "AND created_at " + dir + " ?" 63 } 64 params = append(params, limit)
··· 2 3 import ( 4 "strconv" 5 6 "github.com/Azure/go-autorest/autorest/to" 7 "github.com/bluesky-social/indigo/atproto/data" 8 + "github.com/bluesky-social/indigo/atproto/syntax" 9 "github.com/haileyok/cocoon/internal/helpers" 10 "github.com/haileyok/cocoon/models" 11 "github.com/labstack/echo/v4" 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 + } 21 22 type ComAtprotoRepoListRecordsResponse struct { 23 Cursor *string `json:"cursor,omitempty"` ··· 46 } 47 48 func (s *Server) handleListRecords(e echo.Context) error { 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 + 65 limit, err := getLimitFromContext(e, 50) 66 if err != nil { 67 return helpers.InputError(e, nil) ··· 71 dir := "<" 72 cursorquery := "" 73 74 + if req.Reverse { 75 sort = "ASC" 76 dir = ">" 77 } 78 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) 91 cursorquery = "AND created_at " + dir + " ?" 92 } 93 params = append(params, limit)