An atproto PDS written in Go
at v0.5.1 1.6 kB view raw
1package server 2 3import ( 4 "github.com/haileyok/cocoon/internal/helpers" 5 "github.com/haileyok/cocoon/models" 6 "github.com/labstack/echo/v4" 7) 8 9type ComAtprotoRepoPutRecordRequest struct { 10 Repo string `json:"repo" validate:"required,atproto-did"` 11 Collection string `json:"collection" validate:"required,atproto-nsid"` 12 Rkey string `json:"rkey" validate:"required,atproto-rkey"` 13 Validate *bool `json:"bool,omitempty"` 14 Record MarshalableMap `json:"record" validate:"required"` 15 SwapRecord *string `json:"swapRecord"` 16 SwapCommit *string `json:"swapCommit"` 17} 18 19func (s *Server) handlePutRecord(e echo.Context) error { 20 repo := e.Get("repo").(*models.RepoActor) 21 22 var req ComAtprotoRepoPutRecordRequest 23 if err := e.Bind(&req); err != nil { 24 s.logger.Error("error binding", "error", err) 25 return helpers.ServerError(e, nil) 26 } 27 28 if err := e.Validate(req); err != nil { 29 s.logger.Error("error validating", "error", err) 30 return helpers.InputError(e, nil) 31 } 32 33 if repo.Repo.Did != req.Repo { 34 s.logger.Warn("mismatched repo/auth") 35 return helpers.InputError(e, nil) 36 } 37 38 optype := OpTypeCreate 39 if req.SwapRecord != nil { 40 optype = OpTypeUpdate 41 } 42 43 results, err := s.repoman.applyWrites(repo.Repo, []Op{ 44 { 45 Type: optype, 46 Collection: req.Collection, 47 Rkey: &req.Rkey, 48 Validate: req.Validate, 49 Record: &req.Record, 50 SwapRecord: req.SwapRecord, 51 }, 52 }, req.SwapCommit) 53 if err != nil { 54 s.logger.Error("error applying writes", "error", err) 55 return helpers.ServerError(e, nil) 56 } 57 58 results[0].Type = nil 59 60 return e.JSON(200, results[0]) 61}