Signed-off-by: Anirudh Oppiliappan anirudh@tangled.sh
-7
appview/db/db.go
-7
appview/db/db.go
+12
-1
appview/db/pulls.go
+12
-1
appview/db/pulls.go
···
55
parentChangeId = &pull.ParentChangeId
56
}
57
58
-
_, err = tx.Exec(
59
`
60
insert into pulls (
61
repo_at, owner_did, pull_id, title, target_branch, body, rkey, state, source_branch, source_repo_at, stack_id, change_id, parent_change_id
···
79
return err
80
}
81
82
_, err = tx.Exec(`
83
insert into pull_submissions (pull_id, repo_at, round_number, patch, source_rev)
84
values (?, ?, ?, ?, ?)
···
121
122
query := fmt.Sprintf(`
123
select
124
owner_did,
125
repo_at,
126
pull_id,
···
154
var createdAt string
155
var sourceBranch, sourceRepoAt, stackId, changeId, parentChangeId sql.NullString
156
err := rows.Scan(
157
&pull.OwnerDid,
158
&pull.RepoAt,
159
&pull.PullId,
···
325
func GetPull(e Execer, repoAt syntax.ATURI, pullId int) (*models.Pull, error) {
326
query := `
327
select
328
owner_did,
329
pull_id,
330
created,
···
350
var createdAt string
351
var sourceBranch, sourceRepoAt, stackId, changeId, parentChangeId sql.NullString
352
err := row.Scan(
353
&pull.OwnerDid,
354
&pull.PullId,
355
&createdAt,
···
55
parentChangeId = &pull.ParentChangeId
56
}
57
58
+
result, err := tx.Exec(
59
`
60
insert into pulls (
61
repo_at, owner_did, pull_id, title, target_branch, body, rkey, state, source_branch, source_repo_at, stack_id, change_id, parent_change_id
···
79
return err
80
}
81
82
+
// Set the database primary key ID
83
+
id, err := result.LastInsertId()
84
+
if err != nil {
85
+
return err
86
+
}
87
+
pull.ID = int(id)
88
+
89
_, err = tx.Exec(`
90
insert into pull_submissions (pull_id, repo_at, round_number, patch, source_rev)
91
values (?, ?, ?, ?, ?)
···
128
129
query := fmt.Sprintf(`
130
select
131
+
id,
132
owner_did,
133
repo_at,
134
pull_id,
···
162
var createdAt string
163
var sourceBranch, sourceRepoAt, stackId, changeId, parentChangeId sql.NullString
164
err := rows.Scan(
165
+
&pull.ID,
166
&pull.OwnerDid,
167
&pull.RepoAt,
168
&pull.PullId,
···
334
func GetPull(e Execer, repoAt syntax.ATURI, pullId int) (*models.Pull, error) {
335
query := `
336
select
337
+
id,
338
owner_did,
339
pull_id,
340
created,
···
360
var createdAt string
361
var sourceBranch, sourceRepoAt, stackId, changeId, parentChangeId sql.NullString
362
err := row.Scan(
363
+
&pull.ID,
364
&pull.OwnerDid,
365
&pull.PullId,
366
&createdAt,
+36
-6
appview/db/repos.go
+36
-6
appview/db/repos.go
···
10
"time"
11
12
"github.com/bluesky-social/indigo/atproto/syntax"
13
"tangled.org/core/appview/models"
14
)
15
16
func GetRepos(e Execer, limit int, filters ...filter) ([]models.Repo, error) {
17
repoMap := make(map[syntax.ATURI]*models.Repo)
18
···
35
36
repoQuery := fmt.Sprintf(
37
`select
38
did,
39
name,
40
knot,
···
63
var description, source, spindle sql.NullString
64
65
err := rows.Scan(
66
&repo.Did,
67
&repo.Name,
68
&repo.Knot,
···
327
var repo models.Repo
328
var nullableDescription sql.NullString
329
330
-
row := e.QueryRow(`select did, name, knot, created, rkey, description from repos where at_uri = ?`, atUri)
331
332
var createdAt string
333
-
if err := row.Scan(&repo.Did, &repo.Name, &repo.Knot, &createdAt, &repo.Rkey, &nullableDescription); err != nil {
334
return nil, err
335
}
336
createdAtTime, _ := time.Parse(time.RFC3339, createdAt)
···
386
var repos []models.Repo
387
388
rows, err := e.Query(
389
-
`select distinct r.did, r.name, r.knot, r.rkey, r.description, r.created, r.source
390
from repos r
391
left join collaborators c on r.at_uri = c.repo_at
392
where (r.did = ? or c.subject_did = ?)
···
406
var nullableDescription sql.NullString
407
var nullableSource sql.NullString
408
409
-
err := rows.Scan(&repo.Did, &repo.Name, &repo.Knot, &repo.Rkey, &nullableDescription, &createdAt, &nullableSource)
410
if err != nil {
411
return nil, err
412
}
···
443
var nullableSource sql.NullString
444
445
row := e.QueryRow(
446
-
`select did, name, knot, rkey, description, created, source
447
from repos
448
where did = ? and name = ? and source is not null and source != ''`,
449
did, name,
450
)
451
452
-
err := row.Scan(&repo.Did, &repo.Name, &repo.Knot, &repo.Rkey, &nullableDescription, &createdAt, &nullableSource)
453
if err != nil {
454
return nil, err
455
}
···
10
"time"
11
12
"github.com/bluesky-social/indigo/atproto/syntax"
13
+
securejoin "github.com/cyphar/filepath-securejoin"
14
+
"tangled.org/core/api/tangled"
15
"tangled.org/core/appview/models"
16
)
17
18
+
type Repo struct {
19
+
Id int64
20
+
Did string
21
+
Name string
22
+
Knot string
23
+
Rkey string
24
+
Created time.Time
25
+
Description string
26
+
Spindle string
27
+
28
+
// optionally, populate this when querying for reverse mappings
29
+
RepoStats *models.RepoStats
30
+
31
+
// optional
32
+
Source string
33
+
}
34
+
35
+
func (r Repo) RepoAt() syntax.ATURI {
36
+
return syntax.ATURI(fmt.Sprintf("at://%s/%s/%s", r.Did, tangled.RepoNSID, r.Rkey))
37
+
}
38
+
39
+
func (r Repo) DidSlashRepo() string {
40
+
p, _ := securejoin.SecureJoin(r.Did, r.Name)
41
+
return p
42
+
}
43
+
44
func GetRepos(e Execer, limit int, filters ...filter) ([]models.Repo, error) {
45
repoMap := make(map[syntax.ATURI]*models.Repo)
46
···
63
64
repoQuery := fmt.Sprintf(
65
`select
66
+
id,
67
did,
68
name,
69
knot,
···
92
var description, source, spindle sql.NullString
93
94
err := rows.Scan(
95
+
&repo.Id,
96
&repo.Did,
97
&repo.Name,
98
&repo.Knot,
···
357
var repo models.Repo
358
var nullableDescription sql.NullString
359
360
+
row := e.QueryRow(`select id, did, name, knot, created, rkey, description from repos where at_uri = ?`, atUri)
361
362
var createdAt string
363
+
if err := row.Scan(&repo.Id, &repo.Did, &repo.Name, &repo.Knot, &createdAt, &repo.Rkey, &nullableDescription); err != nil {
364
return nil, err
365
}
366
createdAtTime, _ := time.Parse(time.RFC3339, createdAt)
···
416
var repos []models.Repo
417
418
rows, err := e.Query(
419
+
`select distinct r.id, r.did, r.name, r.knot, r.rkey, r.description, r.created, r.source
420
from repos r
421
left join collaborators c on r.at_uri = c.repo_at
422
where (r.did = ? or c.subject_did = ?)
···
436
var nullableDescription sql.NullString
437
var nullableSource sql.NullString
438
439
+
err := rows.Scan(&repo.Id, &repo.Did, &repo.Name, &repo.Knot, &repo.Rkey, &nullableDescription, &createdAt, &nullableSource)
440
if err != nil {
441
return nil, err
442
}
···
473
var nullableSource sql.NullString
474
475
row := e.QueryRow(
476
+
`select id, did, name, knot, rkey, description, created, source
477
from repos
478
where did = ? and name = ? and source is not null and source != ''`,
479
did, name,
480
)
481
482
+
err := row.Scan(&repo.Id, &repo.Did, &repo.Name, &repo.Knot, &repo.Rkey, &nullableDescription, &createdAt, &nullableSource)
483
if err != nil {
484
return nil, err
485
}
+27
-27
appview/notify/db/db.go
+27
-27
appview/notify/db/db.go
···
4
"context"
5
"log"
6
7
-
"tangled.sh/tangled.sh/core/appview/db"
8
-
"tangled.sh/tangled.sh/core/appview/notify"
9
-
"tangled.sh/tangled.sh/core/idresolver"
10
)
11
12
type databaseNotifier struct {
···
23
24
var _ notify.Notifier = &databaseNotifier{}
25
26
-
func (n *databaseNotifier) NewRepo(ctx context.Context, repo *db.Repo) {
27
// no-op for now
28
}
29
30
-
func (n *databaseNotifier) NewStar(ctx context.Context, star *db.Star) {
31
var err error
32
repos, err := db.GetRepos(n.db, 1, db.FilterEq("at_uri", string(star.RepoAt)))
33
if err != nil {
···
61
Type: models.NotificationTypeRepoStarred,
62
EntityType: "repo",
63
EntityId: string(star.RepoAt),
64
-
RepoId: &repo.ID,
65
}
66
-
67
err = n.db.CreateNotification(ctx, notification)
68
if err != nil {
69
log.Printf("NewStar: failed to create notification: %v", err)
···
71
}
72
}
73
74
-
func (n *databaseNotifier) DeleteStar(ctx context.Context, star *db.Star) {
75
// no-op
76
}
77
78
-
func (n *databaseNotifier) NewIssue(ctx context.Context, issue *db.Issue) {
79
repos, err := db.GetRepos(n.db, 1, db.FilterEq("at_uri", string(issue.RepoAt)))
80
if err != nil {
81
log.Printf("NewIssue: failed to get repos: %v", err)
···
106
Type: models.NotificationTypeIssueCreated,
107
EntityType: "issue",
108
EntityId: string(issue.AtUri()),
109
-
RepoId: &repo.ID,
110
IssueId: &issue.Id,
111
}
112
···
117
}
118
}
119
120
-
func (n *databaseNotifier) NewIssueComment(ctx context.Context, comment *db.IssueComment) {
121
issues, err := db.GetIssues(n.db, db.FilterEq("at_uri", comment.IssueAt))
122
if err != nil {
123
log.Printf("NewIssueComment: failed to get issues: %v", err)
···
170
Type: models.NotificationTypeIssueCommented,
171
EntityType: "issue",
172
EntityId: string(issue.AtUri()),
173
-
RepoId: &repo.ID,
174
IssueId: &issue.Id,
175
}
176
···
181
}
182
}
183
184
-
func (n *databaseNotifier) NewFollow(ctx context.Context, follow *db.Follow) {
185
prefs, err := n.db.GetNotificationPreferences(ctx, follow.SubjectDid)
186
if err != nil {
187
log.Printf("NewFollow: failed to get notification preferences for %s: %v", follow.SubjectDid, err)
···
206
}
207
}
208
209
-
func (n *databaseNotifier) DeleteFollow(ctx context.Context, follow *db.Follow) {
210
// no-op
211
}
212
213
-
func (n *databaseNotifier) NewPull(ctx context.Context, pull *db.Pull) {
214
repos, err := db.GetRepos(n.db, 1, db.FilterEq("at_uri", string(pull.RepoAt)))
215
if err != nil {
216
log.Printf("NewPull: failed to get repos: %v", err)
···
241
Type: models.NotificationTypePullCreated,
242
EntityType: "pull",
243
EntityId: string(pull.RepoAt),
244
-
RepoId: &repo.ID,
245
PullId: func() *int64 { id := int64(pull.ID); return &id }(),
246
}
247
···
252
}
253
}
254
255
-
func (n *databaseNotifier) NewPullComment(ctx context.Context, comment *db.PullComment) {
256
pulls, err := db.GetPulls(n.db,
257
db.FilterEq("repo_at", comment.RepoAt),
258
db.FilterEq("pull_id", comment.PullId))
···
306
Type: models.NotificationTypePullCommented,
307
EntityType: "pull",
308
EntityId: comment.RepoAt,
309
-
RepoId: &repo.ID,
310
PullId: func() *int64 { id := int64(pull.ID); return &id }(),
311
}
312
···
317
}
318
}
319
320
-
func (n *databaseNotifier) UpdateProfile(ctx context.Context, profile *db.Profile) {
321
// no-op
322
}
323
···
325
// no-op
326
}
327
328
-
func (n *databaseNotifier) EditString(ctx context.Context, string *db.String) {
329
// no-op
330
}
331
332
-
func (n *databaseNotifier) NewString(ctx context.Context, string *db.String) {
333
// no-op
334
}
335
336
-
func (n *databaseNotifier) NewIssueClosed(ctx context.Context, issue *db.Issue) {
337
// Get repo details
338
repos, err := db.GetRepos(n.db, 1, db.FilterEq("at_uri", string(issue.RepoAt)))
339
if err != nil {
···
367
Type: models.NotificationTypeIssueClosed,
368
EntityType: "issue",
369
EntityId: string(issue.AtUri()),
370
-
RepoId: &repo.ID,
371
IssueId: &issue.Id,
372
}
373
···
378
}
379
}
380
381
-
func (n *databaseNotifier) NewPullMerged(ctx context.Context, pull *db.Pull) {
382
// Get repo details
383
repos, err := db.GetRepos(n.db, 1, db.FilterEq("at_uri", string(pull.RepoAt)))
384
if err != nil {
···
412
Type: models.NotificationTypePullMerged,
413
EntityType: "pull",
414
EntityId: string(pull.RepoAt),
415
-
RepoId: &repo.ID,
416
PullId: func() *int64 { id := int64(pull.ID); return &id }(),
417
}
418
···
423
}
424
}
425
426
-
func (n *databaseNotifier) NewPullClosed(ctx context.Context, pull *db.Pull) {
427
// Get repo details
428
repos, err := db.GetRepos(n.db, 1, db.FilterEq("at_uri", string(pull.RepoAt)))
429
if err != nil {
···
457
Type: models.NotificationTypePullClosed,
458
EntityType: "pull",
459
EntityId: string(pull.RepoAt),
460
-
RepoId: &repo.ID,
461
PullId: func() *int64 { id := int64(pull.ID); return &id }(),
462
}
463
···
4
"context"
5
"log"
6
7
+
"tangled.org/core/appview/db"
8
+
"tangled.org/core/appview/models"
9
+
"tangled.org/core/appview/notify"
10
+
"tangled.org/core/idresolver"
11
)
12
13
type databaseNotifier struct {
···
24
25
var _ notify.Notifier = &databaseNotifier{}
26
27
+
func (n *databaseNotifier) NewRepo(ctx context.Context, repo *models.Repo) {
28
// no-op for now
29
}
30
31
+
func (n *databaseNotifier) NewStar(ctx context.Context, star *models.Star) {
32
var err error
33
repos, err := db.GetRepos(n.db, 1, db.FilterEq("at_uri", string(star.RepoAt)))
34
if err != nil {
···
62
Type: models.NotificationTypeRepoStarred,
63
EntityType: "repo",
64
EntityId: string(star.RepoAt),
65
+
RepoId: &repo.Id,
66
}
67
err = n.db.CreateNotification(ctx, notification)
68
if err != nil {
69
log.Printf("NewStar: failed to create notification: %v", err)
···
71
}
72
}
73
74
+
func (n *databaseNotifier) DeleteStar(ctx context.Context, star *models.Star) {
75
// no-op
76
}
77
78
+
func (n *databaseNotifier) NewIssue(ctx context.Context, issue *models.Issue) {
79
repos, err := db.GetRepos(n.db, 1, db.FilterEq("at_uri", string(issue.RepoAt)))
80
if err != nil {
81
log.Printf("NewIssue: failed to get repos: %v", err)
···
106
Type: models.NotificationTypeIssueCreated,
107
EntityType: "issue",
108
EntityId: string(issue.AtUri()),
109
+
RepoId: &repo.Id,
110
IssueId: &issue.Id,
111
}
112
···
117
}
118
}
119
120
+
func (n *databaseNotifier) NewIssueComment(ctx context.Context, comment *models.IssueComment) {
121
issues, err := db.GetIssues(n.db, db.FilterEq("at_uri", comment.IssueAt))
122
if err != nil {
123
log.Printf("NewIssueComment: failed to get issues: %v", err)
···
170
Type: models.NotificationTypeIssueCommented,
171
EntityType: "issue",
172
EntityId: string(issue.AtUri()),
173
+
RepoId: &repo.Id,
174
IssueId: &issue.Id,
175
}
176
···
181
}
182
}
183
184
+
func (n *databaseNotifier) NewFollow(ctx context.Context, follow *models.Follow) {
185
prefs, err := n.db.GetNotificationPreferences(ctx, follow.SubjectDid)
186
if err != nil {
187
log.Printf("NewFollow: failed to get notification preferences for %s: %v", follow.SubjectDid, err)
···
206
}
207
}
208
209
+
func (n *databaseNotifier) DeleteFollow(ctx context.Context, follow *models.Follow) {
210
// no-op
211
}
212
213
+
func (n *databaseNotifier) NewPull(ctx context.Context, pull *models.Pull) {
214
repos, err := db.GetRepos(n.db, 1, db.FilterEq("at_uri", string(pull.RepoAt)))
215
if err != nil {
216
log.Printf("NewPull: failed to get repos: %v", err)
···
241
Type: models.NotificationTypePullCreated,
242
EntityType: "pull",
243
EntityId: string(pull.RepoAt),
244
+
RepoId: &repo.Id,
245
PullId: func() *int64 { id := int64(pull.ID); return &id }(),
246
}
247
···
252
}
253
}
254
255
+
func (n *databaseNotifier) NewPullComment(ctx context.Context, comment *models.PullComment) {
256
pulls, err := db.GetPulls(n.db,
257
db.FilterEq("repo_at", comment.RepoAt),
258
db.FilterEq("pull_id", comment.PullId))
···
306
Type: models.NotificationTypePullCommented,
307
EntityType: "pull",
308
EntityId: comment.RepoAt,
309
+
RepoId: &repo.Id,
310
PullId: func() *int64 { id := int64(pull.ID); return &id }(),
311
}
312
···
317
}
318
}
319
320
+
func (n *databaseNotifier) UpdateProfile(ctx context.Context, profile *models.Profile) {
321
// no-op
322
}
323
···
325
// no-op
326
}
327
328
+
func (n *databaseNotifier) EditString(ctx context.Context, string *models.String) {
329
// no-op
330
}
331
332
+
func (n *databaseNotifier) NewString(ctx context.Context, string *models.String) {
333
// no-op
334
}
335
336
+
func (n *databaseNotifier) NewIssueClosed(ctx context.Context, issue *models.Issue) {
337
// Get repo details
338
repos, err := db.GetRepos(n.db, 1, db.FilterEq("at_uri", string(issue.RepoAt)))
339
if err != nil {
···
367
Type: models.NotificationTypeIssueClosed,
368
EntityType: "issue",
369
EntityId: string(issue.AtUri()),
370
+
RepoId: &repo.Id,
371
IssueId: &issue.Id,
372
}
373
···
378
}
379
}
380
381
+
func (n *databaseNotifier) NewPullMerged(ctx context.Context, pull *models.Pull) {
382
// Get repo details
383
repos, err := db.GetRepos(n.db, 1, db.FilterEq("at_uri", string(pull.RepoAt)))
384
if err != nil {
···
412
Type: models.NotificationTypePullMerged,
413
EntityType: "pull",
414
EntityId: string(pull.RepoAt),
415
+
RepoId: &repo.Id,
416
PullId: func() *int64 { id := int64(pull.ID); return &id }(),
417
}
418
···
423
}
424
}
425
426
+
func (n *databaseNotifier) NewPullClosed(ctx context.Context, pull *models.Pull) {
427
// Get repo details
428
repos, err := db.GetRepos(n.db, 1, db.FilterEq("at_uri", string(pull.RepoAt)))
429
if err != nil {
···
457
Type: models.NotificationTypePullClosed,
458
EntityType: "pull",
459
EntityId: string(pull.RepoAt),
460
+
RepoId: &repo.Id,
461
PullId: func() *int64 { id := int64(pull.ID); return &id }(),
462
}
463
+1
appview/state/state.go
+1
appview/state/state.go