this repo has no description
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 ComAtprotoRepoApplyWritesInput struct { 10 Repo string `json:"repo" validate:"required,atproto-did"` 11 Validate *bool `json:"bool,omitempty"` 12 Writes []ComAtprotoRepoApplyWritesItem `json:"writes"` 13 SwapCommit *string `json:"swapCommit"` 14} 15 16type ComAtprotoRepoApplyWritesItem struct { 17 Type string `json:"$type"` 18 Collection string `json:"collection"` 19 Rkey string `json:"rkey"` 20 Value *MarshalableMap `json:"value,omitempty"` 21} 22 23type ComAtprotoRepoApplyWritesOutput struct { 24 Commit RepoCommit `json:"commit"` 25 Results []ApplyWriteResult `json:"results"` 26} 27 28func (s *Server) handleApplyWrites(e echo.Context) error { 29 ctx := e.Request().Context() 30 31 var req ComAtprotoRepoApplyWritesInput 32 if err := e.Bind(&req); err != nil { 33 s.logger.Error("error binding", "error", err) 34 return helpers.ServerError(e, nil) 35 } 36 37 if err := e.Validate(req); err != nil { 38 s.logger.Error("error validating", "error", err) 39 return helpers.InputError(e, nil) 40 } 41 42 repo := e.Get("repo").(*models.RepoActor) 43 44 if repo.Repo.Did != req.Repo { 45 s.logger.Warn("mismatched repo/auth") 46 return helpers.InputError(e, nil) 47 } 48 49 ops := make([]Op, 0, len(req.Writes)) 50 for _, item := range req.Writes { 51 ops = append(ops, Op{ 52 Type: OpType(item.Type), 53 Collection: item.Collection, 54 Rkey: &item.Rkey, 55 Record: item.Value, 56 }) 57 } 58 59 results, err := s.repoman.applyWrites(ctx, repo.Repo, ops, req.SwapCommit) 60 if err != nil { 61 s.logger.Error("error applying writes", "error", err) 62 return helpers.ServerError(e, nil) 63 } 64 65 commit := *results[0].Commit 66 67 for i := range results { 68 results[i].Commit = nil 69 } 70 71 return e.JSON(200, ComAtprotoRepoApplyWritesOutput{ 72 Commit: commit, 73 Results: results, 74 }) 75}