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 38059c11 90eb065b

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 ··· 870 return 871 } 872 873 - repoAt := f.RepoAt() 874 - rkey := repoAt.RecordKey().String() 875 - if rkey == "" { 876 - fail("Failed to resolve repo. Try again later", err) 877 - return 878 - } 879 - 880 newSpindle := r.FormValue("spindle") 881 removingSpindle := newSpindle == "[[none]]" // see pages/templates/repo/settings/pipelines.html for more info on why we use this value 882 client, err := rp.oauth.AuthorizedClient(r) ··· 899 } 900 } 901 902 spindlePtr := &newSpindle 903 if removingSpindle { 904 spindlePtr = nil 905 } 906 907 // optimistic update 908 - err = db.UpdateSpindle(rp.db, string(repoAt), spindlePtr) 909 if err != nil { 910 fail("Failed to update spindle. Try again later.", err) 911 return 912 } 913 914 - ex, err := client.RepoGetRecord(r.Context(), "", tangled.RepoNSID, user.Did, rkey) 915 if err != nil { 916 fail("Failed to update spindle, no record found on PDS.", err) 917 return 918 } 919 _, err = client.RepoPutRecord(r.Context(), &comatproto.RepoPutRecord_Input{ 920 Collection: tangled.RepoNSID, 921 - Repo: user.Did, 922 - Rkey: rkey, 923 SwapRecord: ex.Cid, 924 Record: &lexutil.LexiconTypeDecoder{ 925 - Val: &tangled.Repo{ 926 - Knot: f.Knot, 927 - Name: f.Name, 928 - CreatedAt: f.Created.Format(time.RFC3339), 929 - Description: &f.Description, 930 - Spindle: spindlePtr, 931 - }, 932 }, 933 }) 934 ··· 1049 return 1050 } 1051 1052 - err = db.AddCollaborator(rp.db, db.Collaborator{ 1053 Did: syntax.DID(currentUser.Did), 1054 Rkey: rkey, 1055 SubjectDid: collaboratorIdent.DID, ··· 1586 // create an atproto record for this fork 1587 rkey := tid.TID() 1588 repo := &db.Repo{ 1589 - Did: user.Did, 1590 - Name: forkName, 1591 - Knot: targetKnot, 1592 - Rkey: rkey, 1593 - Source: sourceAt, 1594 } 1595 1596 xrpcClient, err := rp.oauth.AuthorizedClient(r) 1597 if err != nil { ··· 1600 return 1601 } 1602 1603 - createdAt := time.Now().Format(time.RFC3339) 1604 atresp, err := xrpcClient.RepoPutRecord(r.Context(), &comatproto.RepoPutRecord_Input{ 1605 Collection: tangled.RepoNSID, 1606 Repo: user.Did, 1607 Rkey: rkey, 1608 Record: &lexutil.LexiconTypeDecoder{ 1609 - Val: &tangled.Repo{ 1610 - Knot: repo.Knot, 1611 - Name: repo.Name, 1612 - CreatedAt: createdAt, 1613 - Source: &sourceAt, 1614 - }}, 1615 }) 1616 if err != nil { 1617 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 ··· 868 return 869 } 870 871 newSpindle := r.FormValue("spindle") 872 removingSpindle := newSpindle == "[[none]]" // see pages/templates/repo/settings/pipelines.html for more info on why we use this value 873 client, err := rp.oauth.AuthorizedClient(r) ··· 890 } 891 } 892 893 + newRepo := f.Repo 894 + newRepo.Spindle = newSpindle 895 + record := newRepo.AsRecord() 896 + 897 spindlePtr := &newSpindle 898 if removingSpindle { 899 spindlePtr = nil 900 + newRepo.Spindle = "" 901 } 902 903 // optimistic update 904 + err = db.UpdateSpindle(rp.db, newRepo.RepoAt().String(), spindlePtr) 905 if err != nil { 906 fail("Failed to update spindle. Try again later.", err) 907 return 908 } 909 910 + ex, err := client.RepoGetRecord(r.Context(), "", tangled.RepoNSID, newRepo.Did, newRepo.Rkey) 911 if err != nil { 912 fail("Failed to update spindle, no record found on PDS.", err) 913 return 914 } 915 _, err = client.RepoPutRecord(r.Context(), &comatproto.RepoPutRecord_Input{ 916 Collection: tangled.RepoNSID, 917 + Repo: newRepo.Did, 918 + Rkey: newRepo.Rkey, 919 SwapRecord: ex.Cid, 920 Record: &lexutil.LexiconTypeDecoder{ 921 + Val: &record, 922 }, 923 }) 924 ··· 1039 return 1040 } 1041 1042 + err = db.AddCollaborator(tx, db.Collaborator{ 1043 Did: syntax.DID(currentUser.Did), 1044 Rkey: rkey, 1045 SubjectDid: collaboratorIdent.DID, ··· 1576 // create an atproto record for this fork 1577 rkey := tid.TID() 1578 repo := &db.Repo{ 1579 + Did: user.Did, 1580 + Name: forkName, 1581 + Knot: targetKnot, 1582 + Rkey: rkey, 1583 + Source: sourceAt, 1584 + Description: existingRepo.Description, 1585 + Created: time.Now(), 1586 } 1587 + record := repo.AsRecord() 1588 1589 xrpcClient, err := rp.oauth.AuthorizedClient(r) 1590 if err != nil { ··· 1593 return 1594 } 1595 1596 atresp, err := xrpcClient.RepoPutRecord(r.Context(), &comatproto.RepoPutRecord_Input{ 1597 Collection: tangled.RepoNSID, 1598 Repo: user.Did, 1599 Rkey: rkey, 1600 Record: &lexutil.LexiconTypeDecoder{ 1601 + Val: &record, 1602 + }, 1603 }) 1604 if err != nil { 1605 l.Error("failed to write to PDS", "err", err)
+4 -6
appview/state/state.go
··· 430 Knot: domain, 431 Rkey: rkey, 432 Description: description, 433 } 434 435 xrpcClient, err := s.oauth.AuthorizedClient(r) 436 if err != nil { ··· 439 return 440 } 441 442 - createdAt := time.Now().Format(time.RFC3339) 443 atresp, err := xrpcClient.RepoPutRecord(r.Context(), &comatproto.RepoPutRecord_Input{ 444 Collection: tangled.RepoNSID, 445 Repo: user.Did, 446 Rkey: rkey, 447 Record: &lexutil.LexiconTypeDecoder{ 448 - Val: &tangled.Repo{ 449 - Knot: repo.Knot, 450 - Name: repoName, 451 - CreatedAt: createdAt, 452 - }}, 453 }) 454 if err != nil { 455 l.Info("PDS write failed", "err", err)
··· 430 Knot: domain, 431 Rkey: rkey, 432 Description: description, 433 + Created: time.Now(), 434 } 435 + record := repo.AsRecord() 436 437 xrpcClient, err := s.oauth.AuthorizedClient(r) 438 if err != nil { ··· 441 return 442 } 443 444 atresp, err := xrpcClient.RepoPutRecord(r.Context(), &comatproto.RepoPutRecord_Input{ 445 Collection: tangled.RepoNSID, 446 Repo: user.Did, 447 Rkey: rkey, 448 Record: &lexutil.LexiconTypeDecoder{ 449 + Val: &record, 450 + }, 451 }) 452 if err != nil { 453 l.Info("PDS write failed", "err", err)