Lewis: May this revision serve well! lewis@tangled.org
+329
-303
Diff
round #3
+4
-4
appview/db/artifact.go
+4
-4
appview/db/artifact.go
···
16
16
`insert or ignore into artifacts (
17
17
did,
18
18
rkey,
19
-
repo_at,
19
+
repo_did,
20
20
tag,
21
21
created,
22
22
blob_cid,
···
27
27
values (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
28
28
artifact.Did,
29
29
artifact.Rkey,
30
-
artifact.RepoAt,
30
+
artifact.RepoDid,
31
31
artifact.Tag[:],
32
32
artifact.CreatedAt.Format(time.RFC3339),
33
33
artifact.BlobCid.String(),
···
56
56
query := fmt.Sprintf(`select
57
57
did,
58
58
rkey,
59
-
repo_at,
59
+
repo_did,
60
60
tag,
61
61
created,
62
62
blob_cid,
···
82
82
if err := rows.Scan(
83
83
&artifact.Did,
84
84
&artifact.Rkey,
85
-
&artifact.RepoAt,
85
+
&artifact.RepoDid,
86
86
&tag,
87
87
&createdAt,
88
88
&blobCid,
+11
-11
appview/db/collaborators.go
+11
-11
appview/db/collaborators.go
···
11
11
12
12
func AddCollaborator(e Execer, c models.Collaborator) error {
13
13
_, err := e.Exec(
14
-
`insert into collaborators (did, rkey, subject_did, repo_at) values (?, ?, ?, ?);`,
15
-
c.Did, c.Rkey, c.SubjectDid, c.RepoAt,
14
+
`insert into collaborators (did, rkey, subject_did, repo_did) values (?, ?, ?, ?);`,
15
+
c.Did, c.Rkey, c.SubjectDid, string(c.RepoDid),
16
16
)
17
17
return err
18
18
}
···
37
37
}
38
38
39
39
func CollaboratingIn(e Execer, collaborator string) ([]models.Repo, error) {
40
-
rows, err := e.Query(`select repo_at from collaborators where subject_did = ?`, collaborator)
40
+
rows, err := e.Query(`select repo_did from collaborators where subject_did = ?`, collaborator)
41
41
if err != nil {
42
42
return nil, err
43
43
}
44
44
defer rows.Close()
45
45
46
-
var repoAts []string
46
+
var repoDids []string
47
47
for rows.Next() {
48
-
var aturi string
49
-
err := rows.Scan(&aturi)
48
+
var repoDid string
49
+
err := rows.Scan(&repoDid)
50
50
if err != nil {
51
51
return nil, err
52
52
}
53
-
repoAts = append(repoAts, aturi)
53
+
repoDids = append(repoDids, repoDid)
54
54
}
55
55
if err := rows.Err(); err != nil {
56
56
return nil, err
57
57
}
58
-
if repoAts == nil {
58
+
if repoDids == nil {
59
59
return nil, nil
60
60
}
61
61
62
-
return GetRepos(e, orm.FilterIn("at_uri", repoAts))
62
+
return GetRepos(e, orm.FilterIn("repo_did", repoDids))
63
63
}
64
64
65
65
func GetCollaborators(e Execer, filters ...orm.Filter) ([]models.Collaborator, error) {
···
79
79
did,
80
80
rkey,
81
81
subject_did,
82
-
repo_at,
82
+
repo_did,
83
83
created
84
84
from collaborators %s`,
85
85
whereClause,
···
97
97
&collaborator.Did,
98
98
&collaborator.Rkey,
99
99
&collaborator.SubjectDid,
100
-
&collaborator.RepoAt,
100
+
&collaborator.RepoDid,
101
101
&createdAt,
102
102
); err != nil {
103
103
return nil, err
+18
-18
appview/db/issues.go
+18
-18
appview/db/issues.go
···
19
19
func PutIssue(tx *sql.Tx, issue *models.Issue) error {
20
20
// ensure sequence exists
21
21
_, err := tx.Exec(`
22
-
insert or ignore into repo_issue_seqs (repo_at, next_issue_id)
22
+
insert or ignore into repo_issue_seqs (repo_did, next_issue_id)
23
23
values (?, 1)
24
-
`, issue.RepoAt)
24
+
`, issue.RepoDid)
25
25
if err != nil {
26
26
return err
27
27
}
···
57
57
err := tx.QueryRow(`
58
58
update repo_issue_seqs
59
59
set next_issue_id = next_issue_id + 1
60
-
where repo_at = ?
60
+
where repo_did = ?
61
61
returning next_issue_id - 1
62
-
`, issue.RepoAt).Scan(&newIssueId)
62
+
`, issue.RepoDid).Scan(&newIssueId)
63
63
if err != nil {
64
64
return err
65
65
}
66
66
67
67
// insert new issue
68
68
row := tx.QueryRow(`
69
-
insert into issues (repo_at, did, rkey, issue_id, title, body)
69
+
insert into issues (repo_did, did, rkey, issue_id, title, body)
70
70
values (?, ?, ?, ?, ?, ?)
71
71
returning rowid, issue_id
72
-
`, issue.RepoAt, issue.Did, issue.Rkey, newIssueId, issue.Title, issue.Body)
72
+
`, issue.RepoDid, issue.Did, issue.Rkey, newIssueId, issue.Title, issue.Body)
73
73
74
74
err = row.Scan(&issue.Id, &issue.IssueId)
75
75
if err != nil {
···
132
132
id,
133
133
did,
134
134
rkey,
135
-
repo_at,
135
+
repo_did,
136
136
issue_id,
137
137
title,
138
138
body,
···
166
166
&issue.Id,
167
167
&issue.Did,
168
168
&issue.Rkey,
169
-
&issue.RepoAt,
169
+
&issue.RepoDid,
170
170
&issue.IssueId,
171
171
&issue.Title,
172
172
&issue.Body,
···
201
201
}
202
202
203
203
// collect reverse repos
204
-
repoAts := make([]string, 0, len(issueMap)) // or just []string{}
204
+
repoDids := make([]string, 0, len(issueMap))
205
205
for _, issue := range issueMap {
206
-
repoAts = append(repoAts, string(issue.RepoAt))
206
+
repoDids = append(repoDids, string(issue.RepoDid))
207
207
}
208
208
209
-
repos, err := GetRepos(e, orm.FilterIn("at_uri", repoAts))
209
+
repos, err := GetRepos(e, orm.FilterIn("repo_did", repoDids))
210
210
if err != nil {
211
211
return nil, fmt.Errorf("failed to build repo mappings: %w", err)
212
212
}
213
213
214
214
repoMap := make(map[string]*models.Repo)
215
215
for i := range repos {
216
-
repoMap[string(repos[i].RepoAt())] = &repos[i]
216
+
repoMap[repos[i].RepoDid] = &repos[i]
217
217
}
218
218
219
219
for issueAt, i := range issueMap {
220
-
if r, ok := repoMap[string(i.RepoAt)]; ok {
220
+
if r, ok := repoMap[string(i.RepoDid)]; ok {
221
221
i.Repo = r
222
222
} else {
223
223
// do not show up the issue if the repo is deleted
···
274
274
return issues, nil
275
275
}
276
276
277
-
func GetIssue(e Execer, repoAt syntax.ATURI, issueId int) (*models.Issue, error) {
277
+
func GetIssue(e Execer, repoDid string, issueId int) (*models.Issue, error) {
278
278
issues, err := GetIssuesPaginated(
279
279
e,
280
280
pagination.Page{},
281
-
orm.FilterEq("repo_at", repoAt),
281
+
orm.FilterEq("repo_did", repoDid),
282
282
orm.FilterEq("issue_id", issueId),
283
283
)
284
284
if err != nil {
···
530
530
return err
531
531
}
532
532
533
-
func GetIssueCount(e Execer, repoAt syntax.ATURI) (models.IssueCount, error) {
533
+
func GetIssueCount(e Execer, repoDid string) (models.IssueCount, error) {
534
534
row := e.QueryRow(`
535
535
select
536
536
count(case when open = 1 then 1 end) as open_count,
537
537
count(case when open = 0 then 1 end) as closed_count
538
538
from issues
539
-
where repo_at = ?`,
540
-
repoAt,
539
+
where repo_did = ?`,
540
+
repoDid,
541
541
)
542
542
543
543
var count models.IssueCount
+6
-6
appview/db/language.go
+6
-6
appview/db/language.go
···
24
24
}
25
25
26
26
query := fmt.Sprintf(
27
-
`select id, repo_at, ref, is_default_ref, language, bytes from repo_languages %s`,
27
+
`select id, repo_did, ref, is_default_ref, language, bytes from repo_languages %s`,
28
28
whereClause,
29
29
)
30
30
rows, err := e.Query(query, args...)
···
40
40
41
41
err := rows.Scan(
42
42
&rl.Id,
43
-
&rl.RepoAt,
43
+
&rl.RepoDid,
44
44
&rl.Ref,
45
45
&isDefaultRef,
46
46
&rl.Language,
···
65
65
66
66
func InsertRepoLanguages(e Execer, langs []models.RepoLanguage) error {
67
67
stmt, err := e.Prepare(
68
-
"insert or replace into repo_languages (repo_at, ref, is_default_ref, language, bytes) values (?, ?, ?, ?, ?)",
68
+
"insert or replace into repo_languages (repo_did, ref, is_default_ref, language, bytes) values (?, ?, ?, ?, ?)",
69
69
)
70
70
if err != nil {
71
71
return err
···
77
77
isDefaultRef = 1
78
78
}
79
79
80
-
_, err := stmt.Exec(l.RepoAt, l.Ref, isDefaultRef, l.Language, l.Bytes)
80
+
_, err := stmt.Exec(l.RepoDid, l.Ref, isDefaultRef, l.Language, l.Bytes)
81
81
if err != nil {
82
82
return err
83
83
}
···
105
105
return err
106
106
}
107
107
108
-
func UpdateRepoLanguages(tx *sql.Tx, repoAt syntax.ATURI, ref string, langs []models.RepoLanguage) error {
108
+
func UpdateRepoLanguages(tx *sql.Tx, repoDid syntax.DID, ref string, langs []models.RepoLanguage) error {
109
109
err := DeleteRepoLanguages(
110
110
tx,
111
-
orm.FilterEq("repo_at", repoAt),
111
+
orm.FilterEq("repo_did", repoDid),
112
112
orm.FilterEq("ref", ref),
113
113
)
114
114
if err != nil {
+1
-1
appview/db/profile.go
+1
-1
appview/db/profile.go
···
489
489
query = `select count(id) from repos where did = ?`
490
490
args = append(args, did)
491
491
case models.VanityStatStarCount:
492
-
query = `select count(id) from stars where subject_at like 'at://' || ? || '%'`
492
+
query = `select count(s.id) from stars s join repos r on s.subject = r.repo_did where s.subject_type = 'repo' and r.did = ?`
493
493
args = append(args, did)
494
494
case models.VanityStatNone:
495
495
return 0, nil
+56
-57
appview/db/pulls.go
+56
-57
appview/db/pulls.go
···
30
30
if existing.Branch != new.Branch {
31
31
return false
32
32
}
33
-
if existing.RepoAt == nil && new.RepoAt == nil {
33
+
if existing.RepoDid == nil && new.RepoDid == nil {
34
34
return true
35
35
}
36
-
if existing.RepoAt == nil || new.RepoAt == nil {
36
+
if existing.RepoDid == nil || new.RepoDid == nil {
37
37
return false
38
38
}
39
-
return *existing.RepoAt == *new.RepoAt
39
+
return *existing.RepoDid == *new.RepoDid
40
40
}
41
41
42
42
func compareSubmissions(existing, new []*models.PullSubmission) bool {
···
60
60
func PutPull(tx *sql.Tx, pull *models.Pull) error {
61
61
// ensure sequence exists
62
62
_, err := tx.Exec(`
63
-
insert or ignore into repo_pull_seqs (repo_at, next_pull_id)
63
+
insert or ignore into repo_pull_seqs (repo_did, next_pull_id)
64
64
values (?, 1)
65
-
`, pull.RepoAt)
65
+
`, pull.RepoDid)
66
66
if err != nil {
67
67
return err
68
68
}
···
94
94
if existingPull.Title == pull.Title &&
95
95
existingPull.Body == pull.Body &&
96
96
existingPull.TargetBranch == pull.TargetBranch &&
97
-
existingPull.RepoAt == pull.RepoAt &&
97
+
existingPull.RepoDid == pull.RepoDid &&
98
98
dependentOnEqual &&
99
99
pullSourceEqual &&
100
100
submissionsEqual {
···
119
119
120
120
func createNewPull(tx *sql.Tx, pull *models.Pull) error {
121
121
_, err := tx.Exec(`
122
-
insert or ignore into repo_pull_seqs (repo_at, next_pull_id)
122
+
insert or ignore into repo_pull_seqs (repo_did, next_pull_id)
123
123
values (?, 1)
124
-
`, pull.RepoAt)
124
+
`, pull.RepoDid)
125
125
if err != nil {
126
126
return err
127
127
}
···
130
130
err = tx.QueryRow(`
131
131
update repo_pull_seqs
132
132
set next_pull_id = next_pull_id + 1
133
-
where repo_at = ?
133
+
where repo_did = ?
134
134
returning next_pull_id - 1
135
-
`, pull.RepoAt).Scan(&nextId)
135
+
`, pull.RepoDid).Scan(&nextId)
136
136
if err != nil {
137
137
return err
138
138
}
···
140
140
pull.PullId = nextId
141
141
pull.State = models.PullOpen
142
142
143
-
var sourceBranch, sourceRepoAt *string
143
+
var sourceBranch, sourceRepoDid *string
144
144
if pull.PullSource != nil {
145
145
sourceBranch = &pull.PullSource.Branch
146
-
if pull.PullSource.RepoAt != nil {
147
-
x := pull.PullSource.RepoAt.String()
148
-
sourceRepoAt = &x
146
+
if pull.PullSource.RepoDid != nil {
147
+
x := string(*pull.PullSource.RepoDid)
148
+
sourceRepoDid = &x
149
149
}
150
150
}
151
151
152
152
result, err := tx.Exec(
153
153
`
154
154
insert into pulls (
155
-
repo_at,
155
+
repo_did,
156
156
owner_did,
157
157
pull_id,
158
158
title,
···
162
162
state,
163
163
dependent_on,
164
164
source_branch,
165
-
source_repo_at
165
+
source_repo_did
166
166
)
167
167
values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
168
-
pull.RepoAt,
168
+
pull.RepoDid,
169
169
pull.OwnerDid,
170
170
pull.PullId,
171
171
pull.Title,
···
175
175
pull.State,
176
176
pull.DependentOn,
177
177
sourceBranch,
178
-
sourceRepoAt,
178
+
sourceRepoDid,
179
179
)
180
180
if err != nil {
181
181
return err
···
224
224
}
225
225
226
226
func updatePull(tx *sql.Tx, pull *models.Pull, existingPull *models.Pull) error {
227
-
var sourceBranch, sourceRepoAt *string
227
+
var sourceBranch, sourceRepoDid *string
228
228
if pull.PullSource != nil {
229
229
sourceBranch = &pull.PullSource.Branch
230
-
if pull.PullSource.RepoAt != nil {
231
-
x := pull.PullSource.RepoAt.String()
232
-
sourceRepoAt = &x
230
+
if pull.PullSource.RepoDid != nil {
231
+
x := string(*pull.PullSource.RepoDid)
232
+
sourceRepoDid = &x
233
233
}
234
234
}
235
235
···
240
240
target_branch = ?,
241
241
dependent_on = ?,
242
242
source_branch = ?,
243
-
source_repo_at = ?
243
+
source_repo_did = ?
244
244
where owner_did = ? and rkey = ?
245
-
`, pull.Title, pull.Body, pull.TargetBranch, pull.DependentOn, sourceBranch, sourceRepoAt, pull.OwnerDid, pull.Rkey)
245
+
`, pull.Title, pull.Body, pull.TargetBranch, pull.DependentOn, sourceBranch, sourceRepoDid, pull.OwnerDid, pull.Rkey)
246
246
if err != nil {
247
247
return err
248
248
}
···
283
283
return nil
284
284
}
285
285
286
-
func NextPullId(e Execer, repoAt syntax.ATURI) (int, error) {
286
+
func NextPullId(e Execer, repoDid string) (int, error) {
287
287
var pullId int
288
-
err := e.QueryRow(`select next_pull_id from repo_pull_seqs where repo_at = ?`, repoAt).Scan(&pullId)
288
+
err := e.QueryRow(`select next_pull_id from repo_pull_seqs where repo_did = ?`, repoDid).Scan(&pullId)
289
289
return pullId - 1, err
290
290
}
291
291
···
316
316
select
317
317
id,
318
318
owner_did,
319
-
repo_at,
319
+
repo_did,
320
320
pull_id,
321
321
created,
322
322
title,
···
325
325
body,
326
326
rkey,
327
327
source_branch,
328
-
source_repo_at,
328
+
source_repo_did,
329
329
dependent_on
330
330
from
331
331
pulls
···
344
344
for rows.Next() {
345
345
var pull models.Pull
346
346
var createdAt string
347
-
var sourceBranch, sourceRepoAt, dependentOn sql.NullString
347
+
var sourceBranch, sourceRepoDid, dependentOn sql.NullString
348
348
err := rows.Scan(
349
349
&pull.ID,
350
350
&pull.OwnerDid,
351
-
&pull.RepoAt,
351
+
&pull.RepoDid,
352
352
&pull.PullId,
353
353
&createdAt,
354
354
&pull.Title,
···
357
357
&pull.Body,
358
358
&pull.Rkey,
359
359
&sourceBranch,
360
-
&sourceRepoAt,
360
+
&sourceRepoDid,
361
361
&dependentOn,
362
362
)
363
363
if err != nil {
···
374
374
pull.PullSource = &models.PullSource{
375
375
Branch: sourceBranch.String,
376
376
}
377
-
if sourceRepoAt.Valid {
378
-
sourceRepoAtParsed, err := syntax.ParseATURI(sourceRepoAt.String)
377
+
if sourceRepoDid.Valid {
378
+
sourceRepoDidParsed, err := syntax.ParseDID(sourceRepoDid.String)
379
379
if err != nil {
380
380
return nil, err
381
381
}
382
-
pull.PullSource.RepoAt = &sourceRepoAtParsed
382
+
pull.PullSource.RepoDid = &sourceRepoDidParsed
383
383
}
384
384
}
385
385
···
417
417
}
418
418
}
419
419
420
-
// build up reverse mappings: p.Repo and p.PullSource
421
-
var repoAts []syntax.ATURI
420
+
// build up reverse mappings: p.Repo and p.PullSource.Repo
421
+
var repoDids []syntax.DID
422
422
for _, p := range pulls {
423
-
repoAts = append(repoAts, p.RepoAt)
424
-
if p.PullSource != nil && p.PullSource.RepoAt != nil {
425
-
repoAts = append(repoAts, *p.PullSource.RepoAt)
423
+
repoDids = append(repoDids, p.RepoDid)
424
+
if p.PullSource != nil && p.PullSource.RepoDid != nil {
425
+
repoDids = append(repoDids, *p.PullSource.RepoDid)
426
426
}
427
427
}
428
428
429
-
repos, err := GetRepos(e, orm.FilterIn("at_uri", repoAts))
429
+
repos, err := GetRepos(e, orm.FilterIn("repo_did", repoDids))
430
430
if err != nil && !errors.Is(err, sql.ErrNoRows) {
431
-
return nil, fmt.Errorf("failed to get source repos: %w", err)
431
+
return nil, fmt.Errorf("failed to get repos: %w", err)
432
432
}
433
433
434
-
repoMap := make(map[syntax.ATURI]*models.Repo)
434
+
repoMap := make(map[syntax.DID]*models.Repo)
435
435
for _, r := range repos {
436
-
repoMap[r.RepoAt()] = &r
436
+
repoMap[syntax.DID(r.RepoDid)] = &r
437
437
}
438
438
439
439
for _, p := range pulls {
440
-
if repo, ok := repoMap[p.RepoAt]; ok {
440
+
if repo, ok := repoMap[p.RepoDid]; ok {
441
441
p.Repo = repo
442
442
}
443
-
444
-
if p.PullSource != nil && p.PullSource.RepoAt != nil {
445
-
if sourceRepo, ok := repoMap[*p.PullSource.RepoAt]; ok {
443
+
if p.PullSource != nil && p.PullSource.RepoDid != nil {
444
+
if sourceRepo, ok := repoMap[*p.PullSource.RepoDid]; ok {
446
445
p.PullSource.Repo = sourceRepo
447
446
}
448
447
}
···
625
624
id,
626
625
pull_id,
627
626
submission_id,
628
-
repo_at,
627
+
repo_did,
629
628
owner_did,
630
629
comment_at,
631
630
body,
···
651
650
&comment.ID,
652
651
&comment.PullId,
653
652
&comment.SubmissionId,
654
-
&comment.RepoAt,
653
+
&comment.RepoDid,
655
654
&comment.OwnerDid,
656
655
&comment.CommentAt,
657
656
&comment.Body,
···
705
704
rows, err := e.Query(`
706
705
select
707
706
p.owner_did,
708
-
p.repo_at,
707
+
p.repo_did,
709
708
p.pull_id,
710
709
p.created,
711
710
p.title,
···
718
717
from
719
718
pulls p
720
719
join
721
-
repos r on p.repo_at = r.at_uri
720
+
repos r on p.repo_did = r.repo_did
722
721
where
723
722
p.owner_did = ? and p.created >= date ('now', ?)
724
723
order by
···
734
733
var pullCreatedAt, repoCreatedAt string
735
734
err := rows.Scan(
736
735
&pull.OwnerDid,
737
-
&pull.RepoAt,
736
+
&pull.RepoDid,
738
737
&pull.PullId,
739
738
&pullCreatedAt,
740
739
&pull.Title,
···
774
773
}
775
774
776
775
func NewPullComment(tx *sql.Tx, comment *models.PullComment) (int64, error) {
777
-
query := `insert into pull_comments (owner_did, repo_at, submission_id, comment_at, pull_id, body) values (?, ?, ?, ?, ?, ?)`
776
+
query := `insert into pull_comments (owner_did, repo_did, submission_id, comment_at, pull_id, body) values (?, ?, ?, ?, ?, ?)`
778
777
res, err := tx.Exec(
779
778
query,
780
779
comment.OwnerDid,
781
-
comment.RepoAt,
780
+
comment.RepoDid,
782
781
comment.SubmissionId,
783
782
comment.CommentAt,
784
783
comment.PullId,
···
888
887
return err
889
888
}
890
889
891
-
func GetPullCount(e Execer, repoAt syntax.ATURI) (models.PullCount, error) {
890
+
func GetPullCount(e Execer, repoDid string) (models.PullCount, error) {
892
891
row := e.QueryRow(`
893
892
select
894
893
count(case when state = ? then 1 end) as open_count,
···
896
895
count(case when state = ? then 1 end) as closed_count,
897
896
count(case when state = ? then 1 end) as deleted_count
898
897
from pulls
899
-
where repo_at = ?`,
898
+
where repo_did = ?`,
900
899
models.PullOpen,
901
900
models.PullMerged,
902
901
models.PullClosed,
903
902
models.PullAbandoned,
904
-
repoAt,
903
+
repoDid,
905
904
)
906
905
907
906
var count models.PullCount
+8
-8
appview/db/reference.go
+8
-8
appview/db/reference.go
···
60
60
on r.did = inp.owner_did
61
61
and r.name = inp.name
62
62
join issues i
63
-
on i.repo_at = r.at_uri
63
+
on i.repo_did = r.repo_did
64
64
and i.issue_id = inp.issue_id
65
65
left join issue_comments c
66
66
on inp.comment_id is not null
···
131
131
on r.did = inp.owner_did
132
132
and r.name = inp.name
133
133
join pulls p
134
-
on p.repo_at = r.at_uri
134
+
on p.repo_did = r.repo_did
135
135
and p.pull_id = inp.pull_id
136
136
left join pull_comments c
137
137
on inp.comment_id is not null
138
-
and c.repo_at = r.at_uri and c.pull_id = p.pull_id
138
+
and c.repo_did = p.repo_did and c.pull_id = p.pull_id
139
139
and c.id = inp.comment_id
140
140
`,
141
141
strings.Join(vals, ","),
···
319
319
`select r.did, r.name, i.issue_id, i.title, i.open
320
320
from issues i
321
321
join repos r
322
-
on r.at_uri = i.repo_at
322
+
on r.repo_did = i.repo_did
323
323
where (i.did, i.rkey) in (%s)`,
324
324
strings.Join(vals, ","),
325
325
),
···
357
357
join issues i
358
358
on i.at_uri = c.issue_at
359
359
join repos r
360
-
on r.at_uri = i.repo_at
360
+
on r.repo_did = i.repo_did
361
361
where %s and %s`,
362
362
filter.Condition(),
363
363
exclude.Condition(),
···
401
401
`select r.did, r.name, p.pull_id, p.title, p.state
402
402
from pulls p
403
403
join repos r
404
-
on r.at_uri = p.repo_at
404
+
on r.repo_did = p.repo_did
405
405
where (p.owner_did, p.rkey) in (%s)`,
406
406
strings.Join(vals, ","),
407
407
),
···
437
437
`select r.did, r.name, p.pull_id, c.id, p.title, p.state
438
438
from repos r
439
439
join pulls p
440
-
on r.at_uri = p.repo_at
440
+
on r.repo_did = p.repo_did
441
441
join pull_comments c
442
-
on r.at_uri = c.repo_at and p.pull_id = c.pull_id
442
+
on p.repo_did = c.repo_did and p.pull_id = c.pull_id
443
443
where %s and %s`,
444
444
filter.Condition(),
445
445
exclude.Condition(),
+59
-58
appview/db/repos.go
+59
-58
appview/db/repos.go
···
66
66
}
67
67
defer rows.Close()
68
68
69
-
repoMap := make(map[syntax.ATURI]*models.Repo)
69
+
repoMap := make(map[string]*models.Repo)
70
70
for rows.Next() {
71
71
var repo models.Repo
72
72
var createdAt string
···
116
116
}
117
117
118
118
repo.RepoStats = &models.RepoStats{}
119
-
repoMap[repo.RepoAt()] = &repo
119
+
repoMap[repo.RepoDid] = &repo
120
120
}
121
121
122
122
if err = rows.Err(); err != nil {
···
133
133
args = make([]any, len(repoMap))
134
134
i := 0
135
135
for _, r := range repoMap {
136
-
args[i] = r.RepoAt()
136
+
args[i] = r.RepoDid
137
137
i++
138
138
}
139
139
140
140
// get labels for all repos
141
141
labelsQuery := fmt.Sprintf(
142
-
`select repo_at, label_at from repo_labels where repo_at in (%s)`,
142
+
`select repo_did, label_at from repo_labels where repo_did in (%s)`,
143
143
inClause,
144
144
)
145
145
···
150
150
defer rows.Close()
151
151
152
152
for rows.Next() {
153
-
var repoat, labelat string
154
-
if err := rows.Scan(&repoat, &labelat); err != nil {
153
+
var repoDid, labelat string
154
+
if err := rows.Scan(&repoDid, &labelat); err != nil {
155
155
continue
156
156
}
157
-
if r, ok := repoMap[syntax.ATURI(repoat)]; ok {
157
+
if r, ok := repoMap[repoDid]; ok {
158
158
r.Labels = append(r.Labels, labelat)
159
159
}
160
160
}
161
161
162
162
// get primary language for all repos
163
163
languageQuery := fmt.Sprintf(`
164
-
select repo_at, language
164
+
select repo_did, language
165
165
from (
166
166
select
167
-
repo_at, language,
167
+
repo_did, language,
168
168
row_number() over (
169
-
partition by repo_at
169
+
partition by repo_did
170
170
order by bytes desc
171
171
) as rn
172
172
from repo_languages
173
-
where repo_at in (%s)
173
+
where repo_did in (%s)
174
174
and is_default_ref = 1
175
175
and language <> ''
176
176
)
···
184
184
defer rows.Close()
185
185
186
186
for rows.Next() {
187
-
var repoat, lang string
188
-
if err := rows.Scan(&repoat, &lang); err != nil {
187
+
var repoDid, lang string
188
+
if err := rows.Scan(&repoDid, &lang); err != nil {
189
189
log.Println("err", "err", err)
190
190
continue
191
191
}
192
-
if r, ok := repoMap[syntax.ATURI(repoat)]; ok {
192
+
if r, ok := repoMap[repoDid]; ok {
193
193
r.RepoStats.Language = lang
194
194
}
195
195
}
···
199
199
200
200
// get star counts
201
201
starCountQuery := fmt.Sprintf(
202
-
`select subject_at, count(1) from stars where subject_at in (%s) group by subject_at`,
202
+
`select subject, count(1) from stars where subject_type = 'repo' and subject in (%s) group by subject`,
203
203
inClause,
204
204
)
205
205
···
210
210
defer rows.Close()
211
211
212
212
for rows.Next() {
213
-
var repoat string
213
+
var repoDid string
214
214
var count int
215
-
if err := rows.Scan(&repoat, &count); err != nil {
215
+
if err := rows.Scan(&repoDid, &count); err != nil {
216
216
log.Println("err", "err", err)
217
217
continue
218
218
}
219
-
if r, ok := repoMap[syntax.ATURI(repoat)]; ok {
219
+
if r, ok := repoMap[repoDid]; ok {
220
220
r.RepoStats.StarCount = count
221
221
}
222
222
}
···
227
227
// get issue counts
228
228
issueCountQuery := fmt.Sprintf(`
229
229
select
230
-
repo_at,
230
+
repo_did,
231
231
count(case when open = 1 then 1 end) as open_count,
232
232
count(case when open = 0 then 1 end) as closed_count
233
233
from issues
234
-
where repo_at in (%s)
235
-
group by repo_at
234
+
where repo_did in (%s)
235
+
group by repo_did
236
236
`, inClause)
237
237
238
238
rows, err = e.Query(issueCountQuery, args...)
···
242
242
defer rows.Close()
243
243
244
244
for rows.Next() {
245
-
var repoat string
245
+
var repoDid string
246
246
var open, closed int
247
-
if err := rows.Scan(&repoat, &open, &closed); err != nil {
247
+
if err := rows.Scan(&repoDid, &open, &closed); err != nil {
248
248
log.Println("err", "err", err)
249
249
continue
250
250
}
251
-
if r, ok := repoMap[syntax.ATURI(repoat)]; ok {
251
+
if r, ok := repoMap[repoDid]; ok {
252
252
r.RepoStats.IssueCount.Open = open
253
253
r.RepoStats.IssueCount.Closed = closed
254
254
}
···
260
260
// get pull counts
261
261
pullCountQuery := fmt.Sprintf(`
262
262
select
263
-
repo_at,
263
+
repo_did,
264
264
count(case when state = ? then 1 end) as open_count,
265
265
count(case when state = ? then 1 end) as merged_count,
266
266
count(case when state = ? then 1 end) as closed_count,
267
267
count(case when state = ? then 1 end) as deleted_count
268
268
from pulls
269
-
where repo_at in (%s)
270
-
group by repo_at
269
+
where repo_did in (%s)
270
+
group by repo_did
271
271
`, inClause)
272
272
273
273
pullArgs := append([]any{
···
284
284
defer rows.Close()
285
285
286
286
for rows.Next() {
287
-
var repoat string
287
+
var repoDid string
288
288
var open, merged, closed, deleted int
289
-
if err := rows.Scan(&repoat, &open, &merged, &closed, &deleted); err != nil {
289
+
if err := rows.Scan(&repoDid, &open, &merged, &closed, &deleted); err != nil {
290
290
log.Println("err", "err", err)
291
291
continue
292
292
}
293
-
if r, ok := repoMap[syntax.ATURI(repoat)]; ok {
293
+
if r, ok := repoMap[repoDid]; ok {
294
294
r.RepoStats.PullCount.Open = open
295
295
r.RepoStats.PullCount.Merged = merged
296
296
r.RepoStats.PullCount.Closed = closed
···
406
406
}
407
407
_, err := tx.Exec(
408
408
`update repos
409
-
set knot = ?, description = ?, website = ?, topics = ?, repo_did = coalesce(?, repo_did)
409
+
set name = ?, knot = ?, description = ?, website = ?, topics = ?, repo_did = coalesce(?, repo_did)
410
410
where did = ? and rkey = ?
411
411
`,
412
-
repo.Knot, repo.Description, repo.Website, repo.TopicStr(), repoDid, repo.Did, repo.Rkey,
412
+
repo.Name, repo.Knot, repo.Description, repo.Website, repo.TopicStr(), repoDid, repo.Did, repo.Rkey,
413
413
)
414
414
return err
415
415
}
···
437
437
438
438
for _, dl := range repo.Labels {
439
439
if err := SubscribeLabel(tx, &models.RepoLabel{
440
-
RepoAt: repo.RepoAt(),
440
+
RepoDid: syntax.DID(repo.RepoDid),
441
441
LabelAt: syntax.ATURI(dl),
442
442
}); err != nil {
443
443
return fmt.Errorf("failed to subscribe to label: %w", err)
···
447
447
return nil
448
448
}
449
449
450
-
func RemoveRepo(e Execer, did, name string) error {
451
-
_, err := e.Exec(`delete from repos where did = ? and name = ?`, did, name)
450
+
func RemoveRepo(e Execer, did, rkey string) error {
451
+
_, err := e.Exec(`delete from repos where did = ? and rkey = ?`, did, rkey)
452
452
return err
453
453
}
454
454
455
-
func GetRepoSource(e Execer, repoAt syntax.ATURI) (string, error) {
455
+
func GetRepoSource(e Execer, repoDid string) (string, error) {
456
456
var nullableSource sql.NullString
457
-
err := e.QueryRow(`select source from repos where at_uri = ?`, repoAt).Scan(&nullableSource)
457
+
err := e.QueryRow(`select source from repos where repo_did = ?`, repoDid).Scan(&nullableSource)
458
458
if err != nil {
459
459
return "", err
460
460
}
461
461
return nullableSource.String, nil
462
462
}
463
463
464
-
func GetRepoSourceRepo(e Execer, repoAt syntax.ATURI) (*models.Repo, error) {
465
-
source, err := GetRepoSource(e, repoAt)
464
+
func GetRepoSourceRepo(e Execer, repoDid string) (*models.Repo, error) {
465
+
source, err := GetRepoSource(e, repoDid)
466
466
if source == "" || errors.Is(err, sql.ErrNoRows) {
467
467
return nil, nil
468
468
}
···
481
481
rows, err := e.Query(
482
482
`select distinct r.id, r.did, r.name, r.knot, r.rkey, r.description, r.website, r.created, r.source, r.repo_did
483
483
from repos r
484
-
left join collaborators c on r.at_uri = c.repo_at
484
+
left join collaborators c on r.repo_did = c.repo_did
485
485
where (r.did = ? or c.subject_did = ?)
486
486
and r.source is not null
487
487
and r.source != ''
···
537
537
return repos, nil
538
538
}
539
539
540
-
func GetForkByDid(e Execer, did string, name string) (*models.Repo, error) {
540
+
func GetForkByDid(e Execer, did string, rkey string) (*models.Repo, error) {
541
541
var repo models.Repo
542
542
var createdAt string
543
543
var nullableDescription sql.NullString
···
549
549
row := e.QueryRow(
550
550
`select id, did, name, knot, rkey, description, website, topics, created, source, repo_did
551
551
from repos
552
-
where did = ? and name = ? and source is not null and source != ''`,
553
-
did, name,
552
+
where did = ? and rkey = ? and source is not null and source != ''`,
553
+
did, rkey,
554
554
)
555
555
556
556
err := row.Scan(&repo.Id, &repo.Did, &repo.Name, &repo.Knot, &repo.Rkey, &nullableDescription, &nullableWebsite, &nullableTopicStr, &createdAt, &nullableSource, &nullableRepoDid)
···
599
599
table string
600
600
nsid syntax.NSID
601
601
fkCol string
602
+
fkVal string
602
603
}
603
604
sources := []record{
604
-
{"did", "repos", tangled.RepoNSID, "at_uri"},
605
-
{"did", "issues", tangled.RepoIssueNSID, "repo_at"},
606
-
{"owner_did", "pulls", tangled.RepoPullNSID, "repo_at"},
607
-
{"did", "collaborators", tangled.RepoCollaboratorNSID, "repo_at"},
608
-
{"did", "artifacts", tangled.RepoArchiveNSID, "repo_at"},
609
-
{"did", "stars", tangled.FeedStarNSID, "subject_at"},
605
+
{"did", "repos", tangled.RepoNSID, "at_uri", repoAtUri},
606
+
{"did", "issues", tangled.RepoIssueNSID, "repo_did", repoDid},
607
+
{"owner_did", "pulls", tangled.RepoPullNSID, "repo_did", repoDid},
608
+
{"did", "collaborators", tangled.RepoCollaboratorNSID, "repo_did", repoDid},
609
+
{"did", "artifacts", tangled.RepoArchiveNSID, "repo_did", repoDid},
610
+
{"did", "stars", tangled.FeedStarNSID, "subject", repoDid},
610
611
}
611
612
612
613
for _, src := range sources {
613
614
rows, err := tx.Query(
614
615
fmt.Sprintf(`SELECT %s, rkey FROM %s WHERE %s = ?`, src.userDidCol, src.table, src.fkCol),
615
-
repoAtUri,
616
+
src.fkVal,
616
617
)
617
618
if err != nil {
618
619
return fmt.Errorf("query %s for pds rewrites: %w", src.table, err)
···
689
690
return nil
690
691
}
691
692
692
-
func UpdateDescription(e Execer, repoAt, newDescription string) error {
693
+
func UpdateDescription(e Execer, repoDid, newDescription string) error {
693
694
_, err := e.Exec(
694
-
`update repos set description = ? where at_uri = ?`, newDescription, repoAt)
695
+
`update repos set description = ? where repo_did = ?`, newDescription, repoDid)
695
696
return err
696
697
}
697
698
698
-
func UpdateSpindle(e Execer, repoAt string, spindle *string) error {
699
+
func UpdateSpindle(e Execer, repoDid string, spindle *string) error {
699
700
_, err := e.Exec(
700
-
`update repos set spindle = ? where at_uri = ?`, spindle, repoAt)
701
+
`update repos set spindle = ? where repo_did = ?`, spindle, repoDid)
701
702
return err
702
703
}
703
704
704
705
func SubscribeLabel(e Execer, rl *models.RepoLabel) error {
705
-
query := `insert or ignore into repo_labels (repo_at, label_at) values (?, ?)`
706
+
query := `insert or ignore into repo_labels (repo_did, label_at) values (?, ?)`
706
707
707
-
_, err := e.Exec(query, rl.RepoAt.String(), rl.LabelAt.String())
708
+
_, err := e.Exec(query, string(rl.RepoDid), rl.LabelAt.String())
708
709
return err
709
710
}
710
711
···
739
740
whereClause = " where " + strings.Join(conditions, " and ")
740
741
}
741
742
742
-
query := fmt.Sprintf(`select id, repo_at, label_at from repo_labels %s`, whereClause)
743
+
query := fmt.Sprintf(`select id, repo_did, label_at from repo_labels %s`, whereClause)
743
744
744
745
rows, err := e.Query(query, args...)
745
746
if err != nil {
···
751
752
for rows.Next() {
752
753
var label models.RepoLabel
753
754
754
-
err := rows.Scan(&label.Id, &label.RepoAt, &label.LabelAt)
755
+
err := rows.Scan(&label.Id, &label.RepoDid, &label.LabelAt)
755
756
if err != nil {
756
757
return nil, err
757
758
}
+7
-7
appview/db/site_deploys.go
+7
-7
appview/db/site_deploys.go
···
11
11
func AddSiteDeploy(e Execer, deploy *models.SiteDeploy) error {
12
12
result, err := e.Exec(`
13
13
insert into site_deploys (
14
-
repo_at,
14
+
repo_did,
15
15
branch,
16
16
dir,
17
17
commit_sha,
···
20
20
error
21
21
) values (?, ?, ?, ?, ?, ?, ?)
22
22
`,
23
-
deploy.RepoAt,
23
+
deploy.RepoDid,
24
24
deploy.Branch,
25
25
deploy.Dir,
26
26
deploy.CommitSHA,
···
42
42
}
43
43
44
44
// GetSiteDeploys returns recent deploy records for a repository, newest first.
45
-
func GetSiteDeploys(e Execer, repoAt string, limit int) ([]models.SiteDeploy, error) {
45
+
func GetSiteDeploys(e Execer, repoDid string, limit int) ([]models.SiteDeploy, error) {
46
46
if limit <= 0 {
47
47
limit = 20
48
48
}
···
50
50
rows, err := e.Query(`
51
51
select
52
52
id,
53
-
repo_at,
53
+
repo_did,
54
54
branch,
55
55
dir,
56
56
commit_sha,
···
59
59
error,
60
60
created_at
61
61
from site_deploys
62
-
where repo_at = ?
62
+
where repo_did = ?
63
63
order by created_at desc
64
64
limit ?
65
-
`, repoAt, limit)
65
+
`, repoDid, limit)
66
66
if err != nil {
67
67
return nil, fmt.Errorf("failed to query site deploys: %w", err)
68
68
}
···
75
75
76
76
if err := rows.Scan(
77
77
&d.Id,
78
-
&d.RepoAt,
78
+
&d.RepoDid,
79
79
&d.Branch,
80
80
&d.Dir,
81
81
&d.CommitSHA,
+27
-27
appview/db/sites.go
+27
-27
appview/db/sites.go
···
138
138
}
139
139
140
140
// GetRepoSiteConfig returns the site configuration for a repo, or nil if not configured.
141
-
func GetRepoSiteConfig(e Execer, repoAt string) (*models.RepoSite, error) {
141
+
func GetRepoSiteConfig(e Execer, repoDid string) (*models.RepoSite, error) {
142
142
row := e.QueryRow(`
143
-
select id, repo_at, branch, dir, is_index, created, updated
143
+
select id, repo_did, branch, dir, is_index, created, updated
144
144
from repo_sites
145
-
where repo_at = ?
146
-
`, repoAt)
145
+
where repo_did = ?
146
+
`, repoDid)
147
147
148
148
var s models.RepoSite
149
149
var isIndex int
150
150
var createdStr, updatedStr string
151
151
152
-
err := row.Scan(&s.ID, &s.RepoAt, &s.Branch, &s.Dir, &isIndex, &createdStr, &updatedStr)
152
+
err := row.Scan(&s.ID, &s.RepoDid, &s.Branch, &s.Dir, &isIndex, &createdStr, &updatedStr)
153
153
if errors.Is(err, sql.ErrNoRows) {
154
154
return nil, nil
155
155
}
···
173
173
}
174
174
175
175
// SetRepoSiteConfig inserts or replaces the site configuration for a repo.
176
-
func SetRepoSiteConfig(e Execer, repoAt, branch, dir string, isIndex bool) error {
176
+
func SetRepoSiteConfig(e Execer, repoDid, branch, dir string, isIndex bool) error {
177
177
isIndexInt := 0
178
178
if isIndex {
179
179
isIndexInt = 1
180
180
}
181
181
182
182
_, err := e.Exec(`
183
-
insert into repo_sites (repo_at, branch, dir, is_index, updated)
183
+
insert into repo_sites (repo_did, branch, dir, is_index, updated)
184
184
values (?, ?, ?, ?, strftime('%Y-%m-%dT%H:%M:%SZ', 'now'))
185
-
on conflict(repo_at) do update set
185
+
on conflict(repo_did) do update set
186
186
branch = excluded.branch,
187
187
dir = excluded.dir,
188
188
is_index = excluded.is_index,
189
189
updated = excluded.updated
190
-
`, repoAt, branch, dir, isIndexInt)
190
+
`, repoDid, branch, dir, isIndexInt)
191
191
return err
192
192
}
193
193
194
194
// DeleteRepoSiteConfig removes the site configuration for a repo.
195
-
func DeleteRepoSiteConfig(e Execer, repoAt string) error {
196
-
_, err := e.Exec(`delete from repo_sites where repo_at = ?`, repoAt)
195
+
func DeleteRepoSiteConfig(e Execer, repoDid string) error {
196
+
_, err := e.Exec(`delete from repo_sites where repo_did = ?`, repoDid)
197
197
return err
198
198
}
199
199
200
200
// GetRepoSiteConfigsForDid returns all site configurations for repos owned by a DID.
201
-
// RepoName is populated on each returned RepoSite.
201
+
// RepoRkey is populated on each returned RepoSite.
202
202
func GetRepoSiteConfigsForDid(e Execer, did string) ([]*models.RepoSite, error) {
203
203
rows, err := e.Query(`
204
-
select rs.id, rs.repo_at, r.name, rs.branch, rs.dir, rs.is_index, rs.created, rs.updated
204
+
select rs.id, rs.repo_did, r.rkey, rs.branch, rs.dir, rs.is_index, rs.created, rs.updated
205
205
from repo_sites rs
206
-
join repos r on r.at_uri = rs.repo_at
206
+
join repos r on r.repo_did = rs.repo_did
207
207
where r.did = ?
208
208
`, did)
209
209
if err != nil {
···
216
216
var s models.RepoSite
217
217
var isIndex int
218
218
var createdStr, updatedStr string
219
-
if err := rows.Scan(&s.ID, &s.RepoAt, &s.RepoName, &s.Branch, &s.Dir, &isIndex, &createdStr, &updatedStr); err != nil {
219
+
if err := rows.Scan(&s.ID, &s.RepoDid, &s.RepoRkey, &s.Branch, &s.Dir, &isIndex, &createdStr, &updatedStr); err != nil {
220
220
return nil, err
221
221
}
222
222
s.IsIndex = isIndex != 0
···
237
237
func DeleteRepoSiteConfigsForDid(e Execer, did string) error {
238
238
_, err := e.Exec(`
239
239
delete from repo_sites
240
-
where repo_at in (
241
-
select at_uri from repos where did = ?
240
+
where repo_did in (
241
+
select repo_did from repos where did = ?
242
242
)
243
243
`, did)
244
244
return err
245
245
}
246
246
247
-
// GetIndexRepoAtForDid returns the repo_at of the repo that currently holds
248
-
// is_index=1 for the given DID, excluding excludeRepoAt (the current repo).
247
+
// GetIndexRepoDidForDid returns the repo_did of the repo that currently holds
248
+
// is_index=1 for the given DID, excluding excludeRepoDid (the current repo).
249
249
// Returns "", nil if no other repo is the index site.
250
-
func GetIndexRepoAtForDid(e Execer, did, excludeRepoAt string) (string, error) {
250
+
func GetIndexRepoDidForDid(e Execer, did, excludeRepoDid string) (string, error) {
251
251
row := e.QueryRow(`
252
-
select rs.repo_at
252
+
select rs.repo_did
253
253
from repo_sites rs
254
-
join repos r on r.at_uri = rs.repo_at
254
+
join repos r on r.repo_did = rs.repo_did
255
255
where r.did = ?
256
256
and rs.is_index = 1
257
-
and rs.repo_at != ?
257
+
and rs.repo_did != ?
258
258
limit 1
259
-
`, did, excludeRepoAt)
259
+
`, did, excludeRepoDid)
260
260
261
-
var repoAt string
262
-
err := row.Scan(&repoAt)
261
+
var repoDid string
262
+
err := row.Scan(&repoDid)
263
263
if errors.Is(err, sql.ErrNoRows) {
264
264
return "", nil
265
265
}
266
266
if err != nil {
267
267
return "", err
268
268
}
269
-
return repoAt, nil
269
+
return repoDid, nil
270
270
}
+68
-70
appview/db/star.go
+68
-70
appview/db/star.go
···
1
1
package db
2
2
3
3
import (
4
-
"database/sql"
5
-
"errors"
6
4
"fmt"
7
5
"log"
8
6
"slices"
9
7
"strings"
10
8
"time"
11
9
12
-
"github.com/bluesky-social/indigo/atproto/syntax"
13
10
"tangled.org/core/appview/models"
14
11
"tangled.org/core/appview/pagination"
15
12
"tangled.org/core/orm"
16
13
)
17
14
18
15
func AddStar(e Execer, star *models.Star) error {
19
-
query := `insert or ignore into stars (did, subject_at, rkey) values (?, ?, ?)`
16
+
query := `insert or ignore into stars (did, subject_type, subject, rkey) values (?, ?, ?, ?)`
20
17
_, err := e.Exec(
21
18
query,
22
19
star.Did,
23
-
star.RepoAt.String(),
20
+
string(star.SubjectType),
21
+
star.Subject,
24
22
star.Rkey,
25
23
)
26
24
return err
27
25
}
28
26
29
27
// Get a star record
30
-
func GetStar(e Execer, did string, subjectAt syntax.ATURI) (*models.Star, error) {
28
+
func GetStar(e Execer, did string, subject string) (*models.Star, error) {
31
29
query := `
32
-
select did, subject_at, created, rkey
30
+
select did, subject_type, subject, created, rkey
33
31
from stars
34
-
where did = ? and subject_at = ?`
35
-
row := e.QueryRow(query, did, subjectAt)
32
+
where did = ? and subject = ?`
33
+
row := e.QueryRow(query, did, subject)
36
34
37
35
var star models.Star
38
36
var created string
39
-
err := row.Scan(&star.Did, &star.RepoAt, &created, &star.Rkey)
37
+
err := row.Scan(&star.Did, &star.SubjectType, &star.Subject, &created, &star.Rkey)
40
38
if err != nil {
41
39
return nil, err
42
40
}
···
52
50
return &star, nil
53
51
}
54
52
55
-
func GetStars(e Execer, subjectAt syntax.ATURI, page pagination.Page) ([]models.Star, error) {
53
+
func GetStars(e Execer, subject string, page pagination.Page) ([]models.Star, error) {
56
54
query := `
57
-
select did, subject_at, created, rkey
55
+
select did, subject_type, subject, created, rkey
58
56
from stars
59
-
where subject_at = ?
57
+
where subject = ?
60
58
order by created desc
61
59
limit ? offset ?
62
60
`
63
-
rows, err := e.Query(query, subjectAt, page.Limit, page.Offset)
61
+
rows, err := e.Query(query, subject, page.Limit, page.Offset)
64
62
if err != nil {
65
63
return nil, err
66
64
}
···
70
68
for rows.Next() {
71
69
var star models.Star
72
70
var created string
73
-
if err := rows.Scan(&star.Did, &star.RepoAt, &created, &star.Rkey); err != nil {
71
+
if err := rows.Scan(&star.Did, &star.SubjectType, &star.Subject, &created, &star.Rkey); err != nil {
74
72
return nil, err
75
73
}
76
74
···
85
83
}
86
84
87
85
// Remove a star
88
-
func DeleteStar(e Execer, did string, subjectAt syntax.ATURI) error {
89
-
_, err := e.Exec(`delete from stars where did = ? and subject_at = ?`, did, subjectAt)
86
+
func DeleteStar(e Execer, did string, subject string) error {
87
+
_, err := e.Exec(`delete from stars where did = ? and subject = ?`, did, subject)
90
88
return err
91
89
}
92
90
···
96
94
return err
97
95
}
98
96
99
-
func GetStarCount(e Execer, subjectAt syntax.ATURI) (int, error) {
97
+
func GetStarCount(e Execer, subjectType models.StarSubjectType, subject string) (int, error) {
100
98
stars := 0
101
99
err := e.QueryRow(
102
-
`select count(did) from stars where subject_at = ?`, subjectAt).Scan(&stars)
100
+
`select count(did) from stars where subject_type = ? and subject = ?`,
101
+
string(subjectType), subject,
102
+
).Scan(&stars)
103
103
if err != nil {
104
104
return 0, err
105
105
}
106
106
return stars, nil
107
107
}
108
108
109
-
// getStarStatuses returns a map of repo URIs to star status for a given user
109
+
// getStarStatuses returns a map of subjects to star status for a given user
110
110
// This is an internal helper function to avoid N+1 queries
111
-
func getStarStatuses(e Execer, userDid string, repoAts []syntax.ATURI) (map[string]bool, error) {
112
-
if len(repoAts) == 0 || userDid == "" {
111
+
func getStarStatuses(e Execer, userDid string, subjects []string) (map[string]bool, error) {
112
+
if len(subjects) == 0 || userDid == "" {
113
113
return make(map[string]bool), nil
114
114
}
115
115
116
-
placeholders := make([]string, len(repoAts))
117
-
args := make([]any, len(repoAts)+1)
116
+
placeholders := make([]string, len(subjects))
117
+
args := make([]any, len(subjects)+1)
118
118
args[0] = userDid
119
119
120
-
for i, repoAt := range repoAts {
120
+
for i, subj := range subjects {
121
121
placeholders[i] = "?"
122
-
args[i+1] = repoAt.String()
122
+
args[i+1] = subj
123
123
}
124
124
125
125
query := fmt.Sprintf(`
126
-
SELECT subject_at
126
+
SELECT subject
127
127
FROM stars
128
-
WHERE did = ? AND subject_at IN (%s)
128
+
WHERE did = ? AND subject IN (%s)
129
129
`, strings.Join(placeholders, ","))
130
130
131
131
rows, err := e.Query(query, args...)
···
135
135
defer rows.Close()
136
136
137
137
result := make(map[string]bool)
138
-
// Initialize all repos as not starred
139
-
for _, repoAt := range repoAts {
140
-
result[repoAt.String()] = false
138
+
// Initialize all subjects as not starred
139
+
for _, subj := range subjects {
140
+
result[subj] = false
141
141
}
142
142
143
-
// Mark starred repos as true
143
+
// Mark starred subjects as true
144
144
for rows.Next() {
145
-
var repoAt string
146
-
if err := rows.Scan(&repoAt); err != nil {
145
+
var subj string
146
+
if err := rows.Scan(&subj); err != nil {
147
147
return nil, err
148
148
}
149
-
result[repoAt] = true
149
+
result[subj] = true
150
150
}
151
151
152
152
return result, nil
153
153
}
154
154
155
-
func GetStarStatus(e Execer, userDid string, subjectAt syntax.ATURI) bool {
156
-
statuses, err := getStarStatuses(e, userDid, []syntax.ATURI{subjectAt})
155
+
func GetStarStatus(e Execer, userDid string, subject string) bool {
156
+
statuses, err := getStarStatuses(e, userDid, []string{subject})
157
157
if err != nil {
158
158
return false
159
159
}
160
-
return statuses[subjectAt.String()]
160
+
return statuses[subject]
161
161
}
162
162
163
-
// GetStarStatuses returns a map of repo URIs to star status for a given user
164
-
func GetStarStatuses(e Execer, userDid string, subjectAts []syntax.ATURI) (map[string]bool, error) {
165
-
return getStarStatuses(e, userDid, subjectAts)
163
+
// GetStarStatuses returns a map of subjects to star status for a given user
164
+
func GetStarStatuses(e Execer, userDid string, subjects []string) (map[string]bool, error) {
165
+
return getStarStatuses(e, userDid, subjects)
166
166
}
167
167
168
168
// GetRepoStars return a list of stars each holding target repository.
···
175
175
args = append(args, filter.Arg()...)
176
176
}
177
177
178
-
whereClause := ""
179
-
if conditions != nil {
180
-
whereClause = " where " + strings.Join(conditions, " and ")
181
-
}
178
+
conditions = append(conditions, "subject_type = 'repo'")
179
+
180
+
whereClause := " where " + strings.Join(conditions, " and ")
182
181
183
182
pageClause := ""
184
183
if page.Limit != 0 {
···
186
185
}
187
186
188
187
repoQuery := fmt.Sprintf(
189
-
`select did, subject_at, created, rkey
188
+
`select did, subject_type, subject, created, rkey
190
189
from stars
191
190
%s
192
191
order by created desc
···
204
203
for rows.Next() {
205
204
var star models.Star
206
205
var created string
207
-
err := rows.Scan(&star.Did, &star.RepoAt, &created, &star.Rkey)
206
+
err := rows.Scan(&star.Did, &star.SubjectType, &star.Subject, &created, &star.Rkey)
208
207
if err != nil {
209
208
return nil, err
210
209
}
···
214
213
star.Created = t
215
214
}
216
215
217
-
repoAt := string(star.RepoAt)
218
-
starMap[repoAt] = append(starMap[repoAt], star)
216
+
starMap[star.Subject] = append(starMap[star.Subject], star)
219
217
}
220
218
221
219
// populate *Repo in each star
···
230
228
return nil, nil
231
229
}
232
230
233
-
repos, err := GetRepos(e, orm.FilterIn("at_uri", args))
231
+
repos, err := GetRepos(e, orm.FilterIn("repo_did", args))
234
232
if err != nil {
235
233
return nil, err
236
234
}
237
235
238
236
var repoStars []models.RepoStar
239
237
for _, r := range repos {
240
-
if stars, ok := starMap[string(r.RepoAt())]; ok {
238
+
if stars, ok := starMap[r.RepoDid]; ok {
241
239
for _, star := range stars {
242
240
repoStars = append(repoStars, models.RepoStar{
243
241
Star: star,
···
275
273
276
274
repoQuery := fmt.Sprintf(`select count(1) from stars %s`, whereClause)
277
275
var count int64
278
-
err := e.QueryRow(repoQuery, args...).Scan(&count)
279
-
280
-
if !errors.Is(err, sql.ErrNoRows) && err != nil {
276
+
if err := e.QueryRow(repoQuery, args...).Scan(&count); err != nil {
281
277
return 0, err
282
278
}
283
279
···
286
282
287
283
// GetTopStarredReposLastWeek returns the top 8 most starred repositories from the last week
288
284
func GetTopStarredReposLastWeek(e Execer) ([]models.Repo, error) {
289
-
// first, get the top repo URIs by star count from the last week
285
+
// first, get the top repo DIDs by star count from the last week
290
286
query := `
291
287
with recent_starred_repos as (
292
-
select distinct subject_at
288
+
select distinct subject
293
289
from stars
294
290
where created >= datetime('now', '-7 days')
291
+
and subject_type = 'repo'
295
292
),
296
293
repo_star_counts as (
297
294
select
298
-
s.subject_at,
295
+
s.subject,
299
296
count(*) as stars_gained_last_week
300
297
from stars s
301
-
join recent_starred_repos rsr on s.subject_at = rsr.subject_at
298
+
join recent_starred_repos rsr on s.subject = rsr.subject
302
299
where s.created >= datetime('now', '-7 days')
303
-
group by s.subject_at
300
+
and s.subject_type = 'repo'
301
+
group by s.subject
304
302
)
305
-
select rsc.subject_at
303
+
select rsc.subject
306
304
from repo_star_counts rsc
307
305
order by rsc.stars_gained_last_week desc
308
306
limit 5
···
314
312
}
315
313
defer rows.Close()
316
314
317
-
var repoUris []string
315
+
var repoDids []string
318
316
for rows.Next() {
319
-
var repoUri string
320
-
err := rows.Scan(&repoUri)
317
+
var repoDid string
318
+
err := rows.Scan(&repoDid)
321
319
if err != nil {
322
320
return nil, err
323
321
}
324
-
repoUris = append(repoUris, repoUri)
322
+
repoDids = append(repoDids, repoDid)
325
323
}
326
324
327
325
if err := rows.Err(); err != nil {
328
326
return nil, err
329
327
}
330
328
331
-
if len(repoUris) == 0 {
329
+
if len(repoDids) == 0 {
332
330
return []models.Repo{}, nil
333
331
}
334
332
335
333
// get full repo data
336
-
repos, err := GetRepos(e, orm.FilterIn("at_uri", repoUris))
334
+
repos, err := GetRepos(e, orm.FilterIn("repo_did", repoDids))
337
335
if err != nil {
338
336
return nil, err
339
337
}
···
341
339
// sort repos by the original trending order
342
340
repoMap := make(map[string]models.Repo)
343
341
for _, repo := range repos {
344
-
repoMap[repo.RepoAt().String()] = repo
342
+
repoMap[repo.RepoDid] = repo
345
343
}
346
344
347
-
orderedRepos := make([]models.Repo, 0, len(repoUris))
348
-
for _, uri := range repoUris {
349
-
if repo, exists := repoMap[uri]; exists {
345
+
orderedRepos := make([]models.Repo, 0, len(repoDids))
346
+
for _, did := range repoDids {
347
+
if repo, exists := repoMap[did]; exists {
350
348
orderedRepos = append(orderedRepos, repo)
351
349
}
352
350
}
+4
-5
appview/db/timeline.go
+4
-5
appview/db/timeline.go
···
3
3
import (
4
4
"sort"
5
5
6
-
"github.com/bluesky-social/indigo/atproto/syntax"
7
6
"tangled.org/core/appview/models"
8
7
"tangled.org/core/appview/pagination"
9
8
"tangled.org/core/orm"
···
101
100
return nil, nil
102
101
}
103
102
104
-
var repoAts []syntax.ATURI
103
+
var repoDids []string
105
104
for _, r := range repos {
106
-
repoAts = append(repoAts, r.RepoAt())
105
+
repoDids = append(repoDids, r.RepoDid)
107
106
}
108
107
109
-
return GetStarStatuses(e, loggedInUserDid, repoAts)
108
+
return GetStarStatuses(e, loggedInUserDid, repoDids)
110
109
}
111
110
112
111
func getRepoStarInfo(repo *models.Repo, starStatuses map[string]bool) (bool, int64) {
113
112
var isStarred bool
114
113
if starStatuses != nil {
115
-
isStarred = starStatuses[repo.RepoAt().String()]
114
+
isStarred = starStatuses[repo.RepoDid]
116
115
}
117
116
118
117
var starCount int64
+8
-9
appview/db/webhooks.go
+8
-9
appview/db/webhooks.go
···
6
6
"strings"
7
7
"time"
8
8
9
-
"github.com/bluesky-social/indigo/atproto/syntax"
10
9
"tangled.org/core/appview/models"
11
10
"tangled.org/core/orm"
12
11
)
···
28
27
query := fmt.Sprintf(`
29
28
select
30
29
id,
31
-
repo_at,
30
+
repo_did,
32
31
url,
33
32
secret,
34
33
active,
···
55
54
56
55
err := rows.Scan(
57
56
&wh.Id,
58
-
&wh.RepoAt,
57
+
&wh.RepoDid,
59
58
&wh.Url,
60
59
&secret,
61
60
&active,
···
119
118
}
120
119
121
120
result, err := e.Exec(`
122
-
insert into webhooks (repo_at, url, secret, active, events)
121
+
insert into webhooks (repo_did, url, secret, active, events)
123
122
values (?, ?, ?, ?, ?)
124
-
`, webhook.RepoAt.String(), webhook.Url, webhook.Secret, active, eventsStr)
123
+
`, string(webhook.RepoDid), webhook.Url, webhook.Secret, active, eventsStr)
125
124
126
125
if err != nil {
127
126
return fmt.Errorf("failed to insert webhook: %w", err)
···
285
284
}
286
285
287
286
// GetWebhooksForRepo is a convenience function to get all webhooks for a repository
288
-
func GetWebhooksForRepo(e Execer, repoAt syntax.ATURI) ([]models.Webhook, error) {
289
-
return GetWebhooks(e, orm.FilterEq("repo_at", repoAt.String()))
287
+
func GetWebhooksForRepo(e Execer, repoDid string) ([]models.Webhook, error) {
288
+
return GetWebhooks(e, orm.FilterEq("repo_did", repoDid))
290
289
}
291
290
292
291
// GetActiveWebhooksForRepo returns only active webhooks for a repository
293
-
func GetActiveWebhooksForRepo(e Execer, repoAt syntax.ATURI) ([]models.Webhook, error) {
292
+
func GetActiveWebhooksForRepo(e Execer, repoDid string) ([]models.Webhook, error) {
294
293
return GetWebhooks(e,
295
-
orm.FilterEq("repo_at", repoAt.String()),
294
+
orm.FilterEq("repo_did", repoDid),
296
295
orm.FilterEq("active", 1),
297
296
)
298
297
}
+5
-6
appview/state/gfi.go
+5
-6
appview/state/gfi.go
···
4
4
"net/http"
5
5
"sort"
6
6
7
-
"github.com/bluesky-social/indigo/atproto/syntax"
8
7
"tangled.org/core/appview/db"
9
8
"tangled.org/core/appview/models"
10
9
"tangled.org/core/appview/pages"
···
48
47
49
48
repoUris := make([]string, 0, len(repoLabels))
50
49
for _, rl := range repoLabels {
51
-
repoUris = append(repoUris, rl.RepoAt.String())
50
+
repoUris = append(repoUris, string(rl.RepoDid))
52
51
}
53
52
54
53
allIssues, err := db.GetIssuesPaginated(
···
56
55
pagination.Page{
57
56
Limit: 500,
58
57
},
59
-
orm.FilterIn("repo_at", repoUris),
58
+
orm.FilterIn("repo_did", repoUris),
60
59
orm.FilterEq("open", 1),
61
60
)
62
61
if err != nil {
···
72
71
}
73
72
}
74
73
75
-
repoGroups := make(map[syntax.ATURI]*models.RepoGroup)
74
+
repoGroups := make(map[string]*models.RepoGroup)
76
75
for _, issue := range goodFirstIssues {
77
-
if group, exists := repoGroups[issue.Repo.RepoAt()]; exists {
76
+
if group, exists := repoGroups[issue.Repo.RepoDid]; exists {
78
77
group.Issues = append(group.Issues, issue)
79
78
} else {
80
-
repoGroups[issue.Repo.RepoAt()] = &models.RepoGroup{
79
+
repoGroups[issue.Repo.RepoDid] = &models.RepoGroup{
81
80
Repo: issue.Repo,
82
81
Issues: []models.Issue{issue},
83
82
}
+47
-16
appview/state/star.go
+47
-16
appview/state/star.go
···
1
1
package state
2
2
3
3
import (
4
+
"fmt"
5
+
"log"
4
6
"net/http"
5
7
"time"
6
8
···
11
13
"tangled.org/core/appview/db"
12
14
"tangled.org/core/appview/models"
13
15
"tangled.org/core/appview/pages"
14
-
"tangled.org/core/orm"
15
16
"tangled.org/core/tid"
16
17
)
17
18
19
+
func resolveStarSubject(d db.Execer, subjectUri syntax.ATURI) (models.StarSubjectType, string, *tangled.FeedStar_Subject, error) {
20
+
collection := subjectUri.Collection()
21
+
22
+
switch collection.String() {
23
+
case tangled.RepoNSID:
24
+
repo, err := db.GetRepoByAtUri(d, subjectUri.String())
25
+
if err != nil {
26
+
return "", "", nil, err
27
+
}
28
+
if repo.RepoDid == "" {
29
+
return "", "", nil, fmt.Errorf("repo has no DID: %s", subjectUri)
30
+
}
31
+
subject := &tangled.FeedStar_Subject{
32
+
FeedStar_Repo: &tangled.FeedStar_Repo{Did: repo.RepoDid},
33
+
}
34
+
return models.StarSubjectRepo, repo.RepoDid, subject, nil
35
+
36
+
case tangled.StringNSID:
37
+
uri := subjectUri.String()
38
+
subject := &tangled.FeedStar_Subject{
39
+
FeedStar_String: &tangled.FeedStar_String{Uri: uri},
40
+
}
41
+
return models.StarSubjectString, uri, subject, nil
42
+
43
+
default:
44
+
return "", "", nil, fmt.Errorf("unsupported star subject collection: %s", collection)
45
+
}
46
+
}
47
+
18
48
func (s *State) Star(w http.ResponseWriter, r *http.Request) {
19
49
l := s.logger.With("handler", "Star")
20
50
currentUser := s.oauth.GetMultiAccountUser(r)
···
31
61
return
32
62
}
33
63
64
+
subjectType, subjectKey, starSubject, err := resolveStarSubject(s.db, subjectUri)
65
+
if err != nil {
66
+
log.Println("failed to resolve star subject", err)
67
+
return
68
+
}
69
+
34
70
client, err := s.oauth.AuthorizedClient(r)
35
71
if err != nil {
36
72
l.Error("failed to authorize client", "err", err)
···
44
80
createdAt := time.Now().Format(time.RFC3339)
45
81
rkey := tid.TID()
46
82
47
-
subjectStr := subjectUri.String()
48
83
starRecord := &tangled.FeedStar{
49
84
CreatedAt: createdAt,
50
-
Subject: &subjectStr,
51
-
}
52
-
repo, err := db.GetRepo(s.db, orm.FilterEq("at_uri", subjectUri.String()))
53
-
if err == nil && repo.RepoDid != "" {
54
-
starRecord.SubjectDid = &repo.RepoDid
85
+
Subject: starSubject,
55
86
}
56
87
57
88
resp, err := comatproto.RepoPutRecord(r.Context(), client, &comatproto.RepoPutRecord_Input{
···
67
98
l.Info("created atproto record", "uri", resp.Uri)
68
99
69
100
star := &models.Star{
70
-
Did: currentUser.Did,
71
-
RepoAt: subjectUri,
72
-
Rkey: rkey,
101
+
Did: currentUser.Did,
102
+
SubjectType: subjectType,
103
+
Subject: subjectKey,
104
+
Rkey: rkey,
73
105
}
74
106
75
107
err = db.AddStar(s.db, star)
···
78
110
return
79
111
}
80
112
81
-
starCount, err := db.GetStarCount(s.db, subjectUri)
113
+
starCount, err := db.GetStarCount(s.db, subjectType, subjectKey)
82
114
if err != nil {
83
-
l.Error("failed to get star count", "subjectUri", subjectUri, "err", err)
115
+
l.Error("failed to get star count", "subject", subjectKey, "err", err)
84
116
}
85
117
86
118
s.notifier.NewStar(r.Context(), star)
···
95
127
return
96
128
case http.MethodDelete:
97
129
// find the record in the db
98
-
star, err := db.GetStar(s.db, currentUser.Did, subjectUri)
130
+
star, err := db.GetStar(s.db, currentUser.Did, subjectKey)
99
131
if err != nil {
100
132
l.Error("failed to get star relationship", "err", err)
101
133
return
···
118
150
// this is not an issue, the firehose event might have already done this
119
151
}
120
152
121
-
starCount, err := db.GetStarCount(s.db, subjectUri)
153
+
starCount, err := db.GetStarCount(s.db, subjectType, subjectKey)
122
154
if err != nil {
123
-
l.Error("failed to get star count", "subjectUri", subjectUri, "err", err)
155
+
l.Error("failed to get star count", "subject", subjectKey, "err", err)
124
156
return
125
157
}
126
158
···
135
167
136
168
return
137
169
}
138
-
139
170
}
History
9 rounds
0 comments
oyster.cafe
submitted
#8
1 commit
expand
collapse
appview/db: queries use repo_did lookups
Lewis: May this revision serve well! <lewis@tangled.org>
merge conflicts detected
expand
collapse
expand
collapse
- 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
oyster.cafe
submitted
#7
1 commit
expand
collapse
appview/db: queries use repo_did lookups
Lewis: May this revision serve well! <lewis@tangled.org>
expand 0 comments
oyster.cafe
submitted
#6
1 commit
expand
collapse
appview/db: queries use repo_did lookups
Lewis: May this revision serve well! <lewis@tangled.org>
expand 0 comments
oyster.cafe
submitted
#5
1 commit
expand
collapse
appview/db: queries use repo_did lookups
Lewis: May this revision serve well! <lewis@tangled.org>
expand 0 comments
oyster.cafe
submitted
#4
1 commit
expand
collapse
appview/db: queries use repo_did lookups
Lewis: May this revision serve well! <lewis@tangled.org>
expand 0 comments
oyster.cafe
submitted
#3
1 commit
expand
collapse
appview/db: queries use repo_did lookups
Lewis: May this revision serve well! <lewis@tangled.org>
expand 0 comments
oyster.cafe
submitted
#2
1 commit
expand
collapse
appview/db: queries use repo_did lookups
Lewis: May this revision serve well! <lewis@tangled.org>
expand 0 comments
oyster.cafe
submitted
#1
1 commit
expand
collapse
appview/db: queries use repo_did lookups
Lewis: May this revision serve well! <lewis@tangled.org>
expand 0 comments
oyster.cafe
submitted
#0
1 commit
expand
collapse
appview/db: queries use repo_did lookups
Lewis: May this revision serve well! <lewis@tangled.org>