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 logger := s.logger.With("name", "handleSyncListBlobs")
19
20 did := e.QueryParam("did")
21 if did == "" {
22 return helpers.InputError(e, nil)
23 }
24
25 // TODO: add tid param
26 cursor := e.QueryParam("cursor")
27 limit, err := getLimitFromContext(e, 50)
28 if err != nil {
29 return helpers.InputError(e, nil)
30 }
31
32 cursorquery := ""
33
34 params := []any{did}
35 if cursor != "" {
36 params = append(params, cursor)
37 cursorquery = "AND created_at < ?"
38 }
39 params = append(params, limit)
40
41 urepo, err := s.getRepoActorByDid(ctx, did)
42 if err != nil {
43 logger.Error("could not find user for requested blobs", "error", err)
44 return helpers.InputError(e, nil)
45 }
46
47 status := urepo.Status()
48 if status != nil {
49 if *status == "deactivated" {
50 return helpers.InputError(e, to.StringPtr("RepoDeactivated"))
51 }
52 }
53
54 var blobs []models.Blob
55 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 {
56 logger.Error("error getting records", "error", err)
57 return helpers.ServerError(e, nil)
58 }
59
60 var cstrs []string
61 for _, b := range blobs {
62 c, err := cid.Cast(b.Cid)
63 if err != nil {
64 logger.Error("error casting cid", "error", err)
65 return helpers.ServerError(e, nil)
66 }
67 cstrs = append(cstrs, c.String())
68 }
69
70 var newcursor *string
71 if len(blobs) == 50 {
72 newcursor = &blobs[len(blobs)-1].CreatedAt
73 }
74
75 return e.JSON(200, ComAtprotoSyncListBlobsResponse{
76 Cursor: newcursor,
77 Cids: cstrs,
78 })
79}