Monorepo for Tangled tangled.org
858
fork

Configure Feed

Select the types of activity you want to include in your feed.

appview/oauth,notify: DID-based PDS record rewriting #274

open opened by oyster.cafe targeting master from lt/repo-rename-by-rkey
Labels

None yet.

assignee

None yet.

Participants 1
AT URI
at://did:plc:3fwecdnvtcscjnrx2p4n7alz/sh.tangled.repo.pull/3mjm6w2jh5x22
+107 -112
Interdiff #2 #3
appview/notify/db/db.go

This file has not been changed.

appview/notify/logging/notifier.go

This file has not been changed.

appview/notify/merged_notifier.go

This file has not been changed.

appview/notify/notifier.go

This file has not been changed.

appview/notify/posthog/notifier.go

This file has not been changed.

appview/notify/webhook/notifier.go

This file has not been changed.

+20 -68
appview/oauth/handler.go
··· 313 313 } 314 314 } 315 315 316 - type rawGetRecordOutput struct { 317 - Cid *string `json:"cid,omitempty"` 318 - Uri string `json:"uri"` 319 - Value json.RawMessage `json:"value"` 320 - } 321 - 322 - func (o *OAuth) rewriteStarRecord(ctx context.Context, client *atpclient.APIClient, userDid string, rw db.PdsRewrite) error { 323 - var raw rawGetRecordOutput 324 - params := map[string]any{ 325 - "collection": rw.RecordNsid, 326 - "repo": userDid, 327 - "rkey": rw.RecordRkey, 328 - } 329 - if err := client.LexDo(ctx, lexutil.Query, "", "com.atproto.repo.getRecord", params, nil, &raw); err != nil { 330 - return fmt.Errorf("get record: %w", err) 331 - } 332 - 333 - var oldStar struct { 334 - CreatedAt string `json:"createdAt"` 335 - } 336 - if err := json.Unmarshal(raw.Value, &oldStar); err != nil { 337 - return fmt.Errorf("decode old star fields: %w", err) 338 - } 339 - 340 - newRecord := &tangled.FeedStar{ 341 - CreatedAt: oldStar.CreatedAt, 342 - Subject: &tangled.FeedStar_Subject{ 343 - FeedStar_Repo: &tangled.FeedStar_Repo{Did: rw.RepoDid}, 344 - }, 345 - } 346 - 347 - _, err := comatproto.RepoPutRecord(ctx, client, &comatproto.RepoPutRecord_Input{ 348 - Collection: rw.RecordNsid, 349 - Repo: userDid, 350 - Rkey: rw.RecordRkey, 351 - SwapRecord: raw.Cid, 352 - Record: &lexutil.LexiconTypeDecoder{Val: newRecord}, 353 - }) 354 - if err != nil { 355 - return fmt.Errorf("put record: %w", err) 356 - } 357 - return nil 358 - } 359 - 360 316 func (o *OAuth) rewritePdsRecord(ctx context.Context, client *atpclient.APIClient, userDid string, rw db.PdsRewrite) error { 361 - if rw.RecordNsid == tangled.FeedStarNSID { 362 - return o.rewriteStarRecord(ctx, client, userDid, rw) 363 - } 364 - 365 317 ex, err := comatproto.RepoGetRecord(ctx, client, "", rw.RecordNsid, userDid, rw.RecordRkey) 366 318 if err != nil { 367 319 return fmt.Errorf("get record: %w", err) ··· 378 330 } 379 331 rec.RepoDid = &repoDid 380 332 381 - case tangled.RepoArtifactNSID: 382 - rec, ok := val.(*tangled.RepoArtifact) 383 - if !ok { 384 - return fmt.Errorf("unexpected type for artifact record") 385 - } 386 - rec.RepoDid = &repoDid 387 - 388 333 case tangled.RepoIssueNSID: 389 334 rec, ok := val.(*tangled.RepoIssue) 390 335 if !ok { 391 336 return fmt.Errorf("unexpected type for issue record") 392 337 } 338 + rec.RepoDid = &repoDid 393 - rec.Repo = repoDid 394 339 395 340 case tangled.RepoPullNSID: 396 341 rec, ok := val.(*tangled.RepoPull) 397 342 if !ok { 398 343 return fmt.Errorf("unexpected type for pull record") 399 344 } 345 + if rec.Target != nil { 346 + rec.Target.RepoDid = &repoDid 400 - if rec.Target == nil { 401 - return fmt.Errorf("pull record has nil target") 402 347 } 348 + if rec.Source != nil && rec.Source.Repo != nil && *rec.Source.Repo == rw.OldRepoAt { 349 + rec.Source.RepoDid = &repoDid 403 - rec.Target.Repo = repoDid 404 - if rec.Source != nil && rec.Source.Repo != nil && !strings.HasPrefix(*rec.Source.Repo, "did:") { 405 - sourceRepo, srcErr := db.GetRepoByAtUri(o.Db, *rec.Source.Repo) 406 - if srcErr != nil || sourceRepo.RepoDid == "" { 407 - slog.Warn("skipping source repo rewrite for DID-less repo", 408 - "source", *rec.Source.Repo) 409 - } else { 410 - rec.Source.Repo = &sourceRepo.RepoDid 411 - } 412 350 } 413 351 414 352 case tangled.RepoCollaboratorNSID: ··· 416 354 if !ok { 417 355 return fmt.Errorf("unexpected type for collaborator record") 418 356 } 357 + rec.RepoDid = &repoDid 358 + 359 + case tangled.RepoArtifactNSID: 360 + rec, ok := val.(*tangled.RepoArtifact) 361 + if !ok { 362 + return fmt.Errorf("unexpected type for artifact record") 363 + } 364 + rec.RepoDid = &repoDid 365 + 366 + case tangled.FeedStarNSID: 367 + rec, ok := val.(*tangled.FeedStar) 368 + if !ok { 369 + return fmt.Errorf("unexpected type for star record") 370 + } 371 + rec.SubjectDid = &repoDid 419 - rec.Repo = repoDid 420 372 421 373 case tangled.ActorProfileNSID: 422 374 rec, ok := val.(*tangled.ActorProfile)
+87 -44
appview/migration/migrate_add_repo_did.go
··· 2 2 3 3 import ( 4 4 "context" 5 + "encoding/json" 5 6 "fmt" 6 7 "strings" 7 8 ··· 14 15 ) 15 16 16 17 func (s *Migration) migrateAddRepoDid(ctx context.Context, client *atclient.APIClient, did syntax.DID, record syntax.ATURI) error { 17 - // TODO: use agnostic.RepoGetRecord instead 18 + if record.Collection().String() == tangled.FeedStarNSID { 19 + return s.migrateAddRepoDidStar(ctx, client, did, record) 20 + } 21 + 18 22 ex, err := comatproto.RepoGetRecord(ctx, client, "", record.Collection().String(), did.String(), record.RecordKey().String()) 19 23 if err != nil { 20 24 return fmt.Errorf("pds: %w", err) ··· 22 26 23 27 val := ex.Value.Val 24 28 25 - switch record.Collection() { 29 + switch record.Collection().String() { 26 30 case tangled.RepoNSID: 27 31 rec, ok := val.(*tangled.Repo) 28 32 if !ok { ··· 39 43 if !ok { 40 44 return fmt.Errorf("unexpected type for issue record") 41 45 } 42 - if rec.Repo != nil { 43 - repoAt := *rec.Repo 44 - repo, err := db.GetRepoByAtUri(s.db, repoAt) 45 - if err != nil { 46 - return fmt.Errorf("db: failed to query repo: %w", err) 47 - } 48 - rec.RepoDid = &repo.RepoDid 46 + if strings.HasPrefix(rec.Repo, "did:") { 47 + return nil 49 48 } 49 + repo, err := db.GetRepoByAtUri(s.db, rec.Repo) 50 + if err != nil { 51 + return fmt.Errorf("db: failed to query repo by at_uri %q: %w", rec.Repo, err) 52 + } 53 + rec.Repo = repo.RepoDid 50 54 51 55 case tangled.RepoPullNSID: 52 56 rec, ok := val.(*tangled.RepoPull) 53 57 if !ok { 54 58 return fmt.Errorf("unexpected type for pull record") 55 59 } 56 - if rec.Target != nil && rec.Target.Repo != nil { 57 - repoAt := *rec.Target.Repo 58 - repo, err := db.GetRepoByAtUri(s.db, repoAt) 60 + if rec.Target == nil { 61 + return fmt.Errorf("pull record has nil target") 62 + } 63 + if !strings.HasPrefix(rec.Target.Repo, "did:") { 64 + repo, err := db.GetRepoByAtUri(s.db, rec.Target.Repo) 59 65 if err != nil { 60 - return fmt.Errorf("db: failed to query repo: %w", err) 66 + return fmt.Errorf("db: failed to query target repo by at_uri %q: %w", rec.Target.Repo, err) 61 67 } 62 - rec.Target.RepoDid = &repo.RepoDid 68 + rec.Target.Repo = repo.RepoDid 63 69 } 64 - if rec.Source != nil && rec.Source.Repo != nil { 65 - repoAt := *rec.Source.Repo 66 - repo, err := db.GetRepoByAtUri(s.db, repoAt) 67 - if err != nil { 68 - return fmt.Errorf("db: failed to query repo: %w", err) 70 + if rec.Source != nil && rec.Source.Repo != nil && !strings.HasPrefix(*rec.Source.Repo, "did:") { 71 + sourceRepo, srcErr := db.GetRepoByAtUri(s.db, *rec.Source.Repo) 72 + if srcErr == nil && sourceRepo.RepoDid != "" { 73 + rec.Source.Repo = &sourceRepo.RepoDid 69 74 } 70 - rec.Source.RepoDid = &repo.RepoDid 71 75 } 72 76 73 77 case tangled.RepoCollaboratorNSID: ··· 75 79 if !ok { 76 80 return fmt.Errorf("unexpected type for collaborator record") 77 81 } 78 - if rec.Repo != nil { 79 - repoAt := *rec.Repo 80 - repo, err := db.GetRepoByAtUri(s.db, repoAt) 81 - if err != nil { 82 - return fmt.Errorf("db: failed to query repo: %w", err) 83 - } 84 - rec.RepoDid = &repo.RepoDid 82 + if strings.HasPrefix(rec.Repo, "did:") { 83 + return nil 84 + } 85 + repo, err := db.GetRepoByAtUri(s.db, rec.Repo) 86 + if err != nil { 87 + return fmt.Errorf("db: failed to query repo by at_uri %q: %w", rec.Repo, err) 85 88 } 89 + rec.Repo = repo.RepoDid 86 90 87 91 case tangled.RepoArtifactNSID: 88 92 rec, ok := val.(*tangled.RepoArtifact) ··· 90 94 return fmt.Errorf("unexpected type for artifact record") 91 95 } 92 96 if rec.Repo != nil { 93 - repoAt := *rec.Repo 94 - repo, err := db.GetRepoByAtUri(s.db, repoAt) 97 + repo, err := db.GetRepoByAtUri(s.db, *rec.Repo) 95 98 if err != nil { 96 - return fmt.Errorf("db: failed to query repo: %w", err) 99 + return fmt.Errorf("db: failed to query repo by at_uri %q: %w", *rec.Repo, err) 97 100 } 98 101 rec.RepoDid = &repo.RepoDid 99 102 } 100 103 101 - case tangled.FeedStarNSID: 102 - rec, ok := val.(*tangled.FeedStar) 103 - if !ok { 104 - return fmt.Errorf("unexpected type for star record") 105 - } 106 - if rec.Subject != nil { 107 - repoAt := *rec.Subject 108 - repo, err := db.GetRepoByAtUri(s.db, repoAt) 109 - if err != nil { 110 - return fmt.Errorf("db: failed to query repo: %w", err) 111 - } 112 - rec.SubjectDid = &repo.RepoDid 113 - } 114 - 115 104 case tangled.ActorProfileNSID: 116 105 rec, ok := val.(*tangled.ActorProfile) 117 106 if !ok { ··· 149 138 150 139 return nil 151 140 } 141 + 142 + func (s *Migration) migrateAddRepoDidStar(ctx context.Context, client *atclient.APIClient, did syntax.DID, record syntax.ATURI) error { 143 + var raw struct { 144 + Cid *string `json:"cid,omitempty"` 145 + Uri string `json:"uri"` 146 + Value json.RawMessage `json:"value"` 147 + } 148 + params := map[string]any{ 149 + "collection": record.Collection().String(), 150 + "repo": did.String(), 151 + "rkey": record.RecordKey().String(), 152 + } 153 + if err := client.LexDo(ctx, lexutil.Query, "", "com.atproto.repo.getRecord", params, nil, &raw); err != nil { 154 + return fmt.Errorf("get record: %w", err) 155 + } 156 + 157 + var legacy struct { 158 + CreatedAt string `json:"createdAt"` 159 + Subject *string `json:"subject,omitempty"` 160 + } 161 + if err := json.Unmarshal(raw.Value, &legacy); err != nil { 162 + return fmt.Errorf("decode old star fields: %w", err) 163 + } 164 + if legacy.Subject == nil { 165 + return fmt.Errorf("star record has no subject field") 166 + } 167 + 168 + repo, err := db.GetRepoByAtUri(s.db, *legacy.Subject) 169 + if err != nil { 170 + return fmt.Errorf("db: failed to query repo by at_uri %q: %w", *legacy.Subject, err) 171 + } 172 + if repo.RepoDid == "" { 173 + return fmt.Errorf("repo has no repoDid: %s", *legacy.Subject) 174 + } 175 + 176 + newRecord := &tangled.FeedStar{ 177 + CreatedAt: legacy.CreatedAt, 178 + Subject: &tangled.FeedStar_Subject{ 179 + FeedStar_Repo: &tangled.FeedStar_Repo{Did: repo.RepoDid}, 180 + }, 181 + } 182 + 183 + _, err = comatproto.RepoPutRecord(ctx, client, &comatproto.RepoPutRecord_Input{ 184 + Repo: did.String(), 185 + Collection: record.Collection().String(), 186 + Rkey: record.RecordKey().String(), 187 + SwapRecord: raw.Cid, 188 + Record: &lexutil.LexiconTypeDecoder{Val: newRecord}, 189 + }) 190 + if err != nil { 191 + return fmt.Errorf("put record: %w", err) 192 + } 193 + return nil 194 + }

History

9 rounds 0 comments
sign up or login to add to the discussion
1 commit
expand
appview/oauth,notify: DID-based PDS record rewriting
merge conflicts detected
expand
  • api/tangled/cbor_gen.go:866
  • api/tangled/feedstar.go:5
  • api/tangled/gitrefUpdate.go:29
  • api/tangled/repocollaborator.go:19
  • api/tangled/repoissue.go:22
  • api/tangled/repopull.go:39
  • api/tangled/tangledrepo.go:24
  • cmd/cborgen/cborgen.go:17
  • knotserver/xrpc/merge.go:118
  • lexicons/feed/star.json:10
  • lexicons/git/refUpdate.json:11
  • lexicons/issue/issue.json:9
  • lexicons/pulls/pull.json:65
  • lexicons/repo/collaborator.json:11
  • lexicons/repo/repo.json:6
expand 0 comments
1 commit
expand
appview/oauth,notify: DID-based PDS record rewriting
expand 0 comments
1 commit
expand
appview/oauth,notify: DID-based PDS record rewriting
expand 0 comments
1 commit
expand
appview/oauth,notify: DID-based PDS record rewriting
expand 0 comments
1 commit
expand
appview/oauth,notify: DID-based PDS record rewriting
expand 0 comments
1 commit
expand
appview/oauth,notify: DID-based PDS record rewriting
expand 0 comments
1 commit
expand
appview/oauth,notify: DID-based PDS record rewriting
expand 0 comments
1 commit
expand
appview/oauth,notify: DID-based PDS record rewriting
expand 0 comments
1 commit
expand
appview/oauth,notify: DID-based PDS record rewriting
expand 0 comments