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