1package server
2
3import (
4 "github.com/Azure/go-autorest/autorest/to"
5 "github.com/haileyok/cocoon/internal/helpers"
6 "github.com/haileyok/cocoon/models"
7 "github.com/ipfs/go-cid"
8 "github.com/labstack/echo/v4"
9)
10
11type ComAtprotoSyncListBlobsResponse struct {
12 Cursor *string `json:"cursor,omitempty"`
13 Cids []string `json:"cids"`
14}
15
16func (s *Server) handleSyncListBlobs(e echo.Context) error {
17 ctx := e.Request().Context()
18
19 did := e.QueryParam("did")
20 if did == "" {
21 return helpers.InputError(e, nil)
22 }
23
24 // TODO: add tid param
25 cursor := e.QueryParam("cursor")
26 limit, err := getLimitFromContext(e, 50)
27 if err != nil {
28 return helpers.InputError(e, nil)
29 }
30
31 cursorquery := ""
32
33 params := []any{did}
34 if cursor != "" {
35 params = append(params, cursor)
36 cursorquery = "AND created_at < ?"
37 }
38 params = append(params, limit)
39
40 urepo, err := s.getRepoActorByDid(ctx, did)
41 if err != nil {
42 s.logger.Error("could not find user for requested blobs", "error", err)
43 return helpers.InputError(e, nil)
44 }
45
46 status := urepo.Status()
47 if status != nil {
48 if *status == "deactivated" {
49 return helpers.InputError(e, to.StringPtr("RepoDeactivated"))
50 }
51 }
52
53 var blobs []models.Blob
54 if err := s.db.Raw(ctx, "SELECT * FROM blobs WHERE did = ? "+cursorquery+" ORDER BY created_at DESC LIMIT ?", nil, params...).Scan(&blobs).Error; err != nil {
55 s.logger.Error("error getting records", "error", err)
56 return helpers.ServerError(e, nil)
57 }
58
59 var cstrs []string
60 for _, b := range blobs {
61 c, err := cid.Cast(b.Cid)
62 if err != nil {
63 s.logger.Error("error casting cid", "error", err)
64 return helpers.ServerError(e, nil)
65 }
66 cstrs = append(cstrs, c.String())
67 }
68
69 var newcursor *string
70 if len(blobs) == 50 {
71 newcursor = &blobs[len(blobs)-1].CreatedAt
72 }
73
74 return e.JSON(200, ComAtprotoSyncListBlobsResponse{
75 Cursor: newcursor,
76 Cids: cstrs,
77 })
78}