Monorepo for Tangled tangled.org

appview: use Repo::AsRecord() in all put-record operations

this helps with consistency as we introduce more fields to the repo
lexicon.

Signed-off-by: oppiliappan <me@oppi.li>

oppi.li 87bd77a4 71953ed5

verified
Changed files
+55 -48
appview
db
repo
state
+22 -1
appview/db/repos.go
··· 31 } 32 33 func (r *Repo) AsRecord() tangled.Repo { 34 - return tangled.Repo{} 35 } 36 37 func (r Repo) RepoAt() syntax.ATURI {
··· 31 } 32 33 func (r *Repo) AsRecord() tangled.Repo { 34 + var source, spindle, description *string 35 + 36 + if r.Source != "" { 37 + source = &r.Source 38 + } 39 + 40 + if r.Spindle != "" { 41 + spindle = &r.Spindle 42 + } 43 + 44 + if r.Description != "" { 45 + description = &r.Description 46 + } 47 + 48 + return tangled.Repo{ 49 + Knot: r.Knot, 50 + Name: r.Name, 51 + Description: description, 52 + CreatedAt: r.Created.Format(time.RFC3339), 53 + Source: source, 54 + Spindle: spindle, 55 + } 56 } 57 58 func (r Repo) RepoAt() syntax.ATURI {
+29 -41
appview/repo/repo.go
··· 295 return 296 } 297 298 // this is a bit of a pain because the golang atproto impl does not allow nil SwapRecord field 299 // 300 // SwapRecord is optional and should happen automagically, but given that it does not, we have to perform two requests 301 - ex, err := client.RepoGetRecord(r.Context(), "", tangled.RepoNSID, user.Did, rkey) 302 if err != nil { 303 // failed to get record 304 rp.pages.Notice(w, "repo-notice", "Failed to update description, no record found on PDS.") ··· 306 } 307 _, err = client.RepoPutRecord(r.Context(), &comatproto.RepoPutRecord_Input{ 308 Collection: tangled.RepoNSID, 309 - Repo: user.Did, 310 - Rkey: rkey, 311 SwapRecord: ex.Cid, 312 Record: &lexutil.LexiconTypeDecoder{ 313 - Val: &tangled.Repo{ 314 - Knot: f.Knot, 315 - Name: f.Name, 316 - CreatedAt: f.Created.Format(time.RFC3339), 317 - Description: &newDescription, 318 - Spindle: &f.Spindle, 319 - }, 320 }, 321 }) 322 ··· 894 return 895 } 896 897 - repoAt := f.RepoAt() 898 - rkey := repoAt.RecordKey().String() 899 - if rkey == "" { 900 - fail("Failed to resolve repo. Try again later", err) 901 - return 902 - } 903 - 904 newSpindle := r.FormValue("spindle") 905 removingSpindle := newSpindle == "[[none]]" // see pages/templates/repo/settings/pipelines.html for more info on why we use this value 906 client, err := rp.oauth.AuthorizedClient(r) ··· 923 } 924 } 925 926 spindlePtr := &newSpindle 927 if removingSpindle { 928 spindlePtr = nil 929 } 930 931 // optimistic update 932 - err = db.UpdateSpindle(rp.db, string(repoAt), spindlePtr) 933 if err != nil { 934 fail("Failed to update spindle. Try again later.", err) 935 return 936 } 937 938 - ex, err := client.RepoGetRecord(r.Context(), "", tangled.RepoNSID, user.Did, rkey) 939 if err != nil { 940 fail("Failed to update spindle, no record found on PDS.", err) 941 return 942 } 943 _, err = client.RepoPutRecord(r.Context(), &comatproto.RepoPutRecord_Input{ 944 Collection: tangled.RepoNSID, 945 - Repo: user.Did, 946 - Rkey: rkey, 947 SwapRecord: ex.Cid, 948 Record: &lexutil.LexiconTypeDecoder{ 949 - Val: &tangled.Repo{ 950 - Knot: f.Knot, 951 - Name: f.Name, 952 - CreatedAt: f.Created.Format(time.RFC3339), 953 - Description: &f.Description, 954 - Spindle: spindlePtr, 955 - }, 956 }, 957 }) 958 ··· 1073 return 1074 } 1075 1076 - err = db.AddCollaborator(rp.db, db.Collaborator{ 1077 Did: syntax.DID(currentUser.Did), 1078 Rkey: rkey, 1079 SubjectDid: collaboratorIdent.DID, ··· 1610 // create an atproto record for this fork 1611 rkey := tid.TID() 1612 repo := &db.Repo{ 1613 - Did: user.Did, 1614 - Name: forkName, 1615 - Knot: targetKnot, 1616 - Rkey: rkey, 1617 - Source: sourceAt, 1618 } 1619 1620 xrpcClient, err := rp.oauth.AuthorizedClient(r) 1621 if err != nil { ··· 1624 return 1625 } 1626 1627 - createdAt := time.Now().Format(time.RFC3339) 1628 atresp, err := xrpcClient.RepoPutRecord(r.Context(), &comatproto.RepoPutRecord_Input{ 1629 Collection: tangled.RepoNSID, 1630 Repo: user.Did, 1631 Rkey: rkey, 1632 Record: &lexutil.LexiconTypeDecoder{ 1633 - Val: &tangled.Repo{ 1634 - Knot: repo.Knot, 1635 - Name: repo.Name, 1636 - CreatedAt: createdAt, 1637 - Source: &sourceAt, 1638 - }}, 1639 }) 1640 if err != nil { 1641 l.Error("failed to write to PDS", "err", err)
··· 295 return 296 } 297 298 + newRepo := f.Repo 299 + newRepo.Description = newDescription 300 + record := newRepo.AsRecord() 301 + 302 // this is a bit of a pain because the golang atproto impl does not allow nil SwapRecord field 303 // 304 // SwapRecord is optional and should happen automagically, but given that it does not, we have to perform two requests 305 + ex, err := client.RepoGetRecord(r.Context(), "", tangled.RepoNSID, newRepo.Did, newRepo.Rkey) 306 if err != nil { 307 // failed to get record 308 rp.pages.Notice(w, "repo-notice", "Failed to update description, no record found on PDS.") ··· 310 } 311 _, err = client.RepoPutRecord(r.Context(), &comatproto.RepoPutRecord_Input{ 312 Collection: tangled.RepoNSID, 313 + Repo: newRepo.Did, 314 + Rkey: newRepo.Rkey, 315 SwapRecord: ex.Cid, 316 Record: &lexutil.LexiconTypeDecoder{ 317 + Val: &record, 318 }, 319 }) 320 ··· 892 return 893 } 894 895 newSpindle := r.FormValue("spindle") 896 removingSpindle := newSpindle == "[[none]]" // see pages/templates/repo/settings/pipelines.html for more info on why we use this value 897 client, err := rp.oauth.AuthorizedClient(r) ··· 914 } 915 } 916 917 + newRepo := f.Repo 918 + newRepo.Spindle = newSpindle 919 + record := newRepo.AsRecord() 920 + 921 spindlePtr := &newSpindle 922 if removingSpindle { 923 spindlePtr = nil 924 + newRepo.Spindle = "" 925 } 926 927 // optimistic update 928 + err = db.UpdateSpindle(rp.db, newRepo.RepoAt().String(), spindlePtr) 929 if err != nil { 930 fail("Failed to update spindle. Try again later.", err) 931 return 932 } 933 934 + ex, err := client.RepoGetRecord(r.Context(), "", tangled.RepoNSID, newRepo.Did, newRepo.Rkey) 935 if err != nil { 936 fail("Failed to update spindle, no record found on PDS.", err) 937 return 938 } 939 _, err = client.RepoPutRecord(r.Context(), &comatproto.RepoPutRecord_Input{ 940 Collection: tangled.RepoNSID, 941 + Repo: newRepo.Did, 942 + Rkey: newRepo.Rkey, 943 SwapRecord: ex.Cid, 944 Record: &lexutil.LexiconTypeDecoder{ 945 + Val: &record, 946 }, 947 }) 948 ··· 1063 return 1064 } 1065 1066 + err = db.AddCollaborator(tx, db.Collaborator{ 1067 Did: syntax.DID(currentUser.Did), 1068 Rkey: rkey, 1069 SubjectDid: collaboratorIdent.DID, ··· 1600 // create an atproto record for this fork 1601 rkey := tid.TID() 1602 repo := &db.Repo{ 1603 + Did: user.Did, 1604 + Name: forkName, 1605 + Knot: targetKnot, 1606 + Rkey: rkey, 1607 + Source: sourceAt, 1608 + Description: existingRepo.Description, 1609 + Created: time.Now(), 1610 } 1611 + record := repo.AsRecord() 1612 1613 xrpcClient, err := rp.oauth.AuthorizedClient(r) 1614 if err != nil { ··· 1617 return 1618 } 1619 1620 atresp, err := xrpcClient.RepoPutRecord(r.Context(), &comatproto.RepoPutRecord_Input{ 1621 Collection: tangled.RepoNSID, 1622 Repo: user.Did, 1623 Rkey: rkey, 1624 Record: &lexutil.LexiconTypeDecoder{ 1625 + Val: &record, 1626 + }, 1627 }) 1628 if err != nil { 1629 l.Error("failed to write to PDS", "err", err)
+4 -6
appview/state/state.go
··· 434 Knot: domain, 435 Rkey: rkey, 436 Description: description, 437 } 438 439 xrpcClient, err := s.oauth.AuthorizedClient(r) 440 if err != nil { ··· 443 return 444 } 445 446 - createdAt := time.Now().Format(time.RFC3339) 447 atresp, err := xrpcClient.RepoPutRecord(r.Context(), &comatproto.RepoPutRecord_Input{ 448 Collection: tangled.RepoNSID, 449 Repo: user.Did, 450 Rkey: rkey, 451 Record: &lexutil.LexiconTypeDecoder{ 452 - Val: &tangled.Repo{ 453 - Knot: repo.Knot, 454 - Name: repoName, 455 - CreatedAt: createdAt, 456 - }}, 457 }) 458 if err != nil { 459 l.Info("PDS write failed", "err", err)
··· 434 Knot: domain, 435 Rkey: rkey, 436 Description: description, 437 + Created: time.Now(), 438 } 439 + record := repo.AsRecord() 440 441 xrpcClient, err := s.oauth.AuthorizedClient(r) 442 if err != nil { ··· 445 return 446 } 447 448 atresp, err := xrpcClient.RepoPutRecord(r.Context(), &comatproto.RepoPutRecord_Input{ 449 Collection: tangled.RepoNSID, 450 Repo: user.Did, 451 Rkey: rkey, 452 Record: &lexutil.LexiconTypeDecoder{ 453 + Val: &record, 454 + }, 455 }) 456 if err != nil { 457 l.Info("PDS write failed", "err", err)