appview/db: return ids in a few places #592

merged
opened by anirudh.fi targeting master from push-xwotmtuuvokm
Changed files
+76 -41
appview
db
notify
db
state
-7
appview/db/db.go
···
_, err := tx.Exec(`
alter table spindles add column needs_upgrade integer not null default 0;
`)
-
if err != nil {
-
return err
-
}
-
-
_, err = tx.Exec(`
-
update spindles set needs_upgrade = 1;
-
`)
return err
})
+12 -1
appview/db/pulls.go
···
parentChangeId = &pull.ParentChangeId
}
-
_, err = tx.Exec(
+
result, err := tx.Exec(
`
insert into pulls (
repo_at, owner_did, pull_id, title, target_branch, body, rkey, state, source_branch, source_repo_at, stack_id, change_id, parent_change_id
···
return err
}
+
// Set the database primary key ID
+
id, err := result.LastInsertId()
+
if err != nil {
+
return err
+
}
+
pull.ID = int(id)
+
_, err = tx.Exec(`
insert into pull_submissions (pull_id, repo_at, round_number, patch, source_rev)
values (?, ?, ?, ?, ?)
···
query := fmt.Sprintf(`
select
+
id,
owner_did,
repo_at,
pull_id,
···
var createdAt string
var sourceBranch, sourceRepoAt, stackId, changeId, parentChangeId sql.NullString
err := rows.Scan(
+
&pull.ID,
&pull.OwnerDid,
&pull.RepoAt,
&pull.PullId,
···
func GetPull(e Execer, repoAt syntax.ATURI, pullId int) (*models.Pull, error) {
query := `
select
+
id,
owner_did,
pull_id,
created,
···
var createdAt string
var sourceBranch, sourceRepoAt, stackId, changeId, parentChangeId sql.NullString
err := row.Scan(
+
&pull.ID,
&pull.OwnerDid,
&pull.PullId,
&createdAt,
+36 -6
appview/db/repos.go
···
"time"
"github.com/bluesky-social/indigo/atproto/syntax"
+
securejoin "github.com/cyphar/filepath-securejoin"
+
"tangled.org/core/api/tangled"
"tangled.org/core/appview/models"
)
+
type Repo struct {
+
Id int64
+
Did string
+
Name string
+
Knot string
+
Rkey string
+
Created time.Time
+
Description string
+
Spindle string
+
+
// optionally, populate this when querying for reverse mappings
+
RepoStats *models.RepoStats
+
+
// optional
+
Source string
+
}
+
+
func (r Repo) RepoAt() syntax.ATURI {
+
return syntax.ATURI(fmt.Sprintf("at://%s/%s/%s", r.Did, tangled.RepoNSID, r.Rkey))
+
}
+
+
func (r Repo) DidSlashRepo() string {
+
p, _ := securejoin.SecureJoin(r.Did, r.Name)
+
return p
+
}
+
func GetRepos(e Execer, limit int, filters ...filter) ([]models.Repo, error) {
repoMap := make(map[syntax.ATURI]*models.Repo)
···
repoQuery := fmt.Sprintf(
`select
+
id,
did,
name,
knot,
···
var description, source, spindle sql.NullString
err := rows.Scan(
+
&repo.Id,
&repo.Did,
&repo.Name,
&repo.Knot,
···
var repo models.Repo
var nullableDescription sql.NullString
-
row := e.QueryRow(`select did, name, knot, created, rkey, description from repos where at_uri = ?`, atUri)
+
row := e.QueryRow(`select id, did, name, knot, created, rkey, description from repos where at_uri = ?`, atUri)
var createdAt string
-
if err := row.Scan(&repo.Did, &repo.Name, &repo.Knot, &createdAt, &repo.Rkey, &nullableDescription); err != nil {
+
if err := row.Scan(&repo.Id, &repo.Did, &repo.Name, &repo.Knot, &createdAt, &repo.Rkey, &nullableDescription); err != nil {
return nil, err
}
createdAtTime, _ := time.Parse(time.RFC3339, createdAt)
···
var repos []models.Repo
rows, err := e.Query(
-
`select distinct r.did, r.name, r.knot, r.rkey, r.description, r.created, r.source
+
`select distinct r.id, r.did, r.name, r.knot, r.rkey, r.description, r.created, r.source
from repos r
left join collaborators c on r.at_uri = c.repo_at
where (r.did = ? or c.subject_did = ?)
···
var nullableDescription sql.NullString
var nullableSource sql.NullString
-
err := rows.Scan(&repo.Did, &repo.Name, &repo.Knot, &repo.Rkey, &nullableDescription, &createdAt, &nullableSource)
+
err := rows.Scan(&repo.Id, &repo.Did, &repo.Name, &repo.Knot, &repo.Rkey, &nullableDescription, &createdAt, &nullableSource)
if err != nil {
return nil, err
}
···
var nullableSource sql.NullString
row := e.QueryRow(
-
`select did, name, knot, rkey, description, created, source
+
`select id, did, name, knot, rkey, description, created, source
from repos
where did = ? and name = ? and source is not null and source != ''`,
did, name,
)
-
err := row.Scan(&repo.Did, &repo.Name, &repo.Knot, &repo.Rkey, &nullableDescription, &createdAt, &nullableSource)
+
err := row.Scan(&repo.Id, &repo.Did, &repo.Name, &repo.Knot, &repo.Rkey, &nullableDescription, &createdAt, &nullableSource)
if err != nil {
return nil, err
}
+27 -27
appview/notify/db/db.go
···
"context"
"log"
-
"tangled.sh/tangled.sh/core/appview/db"
-
"tangled.sh/tangled.sh/core/appview/notify"
-
"tangled.sh/tangled.sh/core/idresolver"
+
"tangled.org/core/appview/db"
+
"tangled.org/core/appview/models"
+
"tangled.org/core/appview/notify"
+
"tangled.org/core/idresolver"
)
type databaseNotifier struct {
···
var _ notify.Notifier = &databaseNotifier{}
-
func (n *databaseNotifier) NewRepo(ctx context.Context, repo *db.Repo) {
+
func (n *databaseNotifier) NewRepo(ctx context.Context, repo *models.Repo) {
// no-op for now
}
-
func (n *databaseNotifier) NewStar(ctx context.Context, star *db.Star) {
+
func (n *databaseNotifier) NewStar(ctx context.Context, star *models.Star) {
var err error
repos, err := db.GetRepos(n.db, 1, db.FilterEq("at_uri", string(star.RepoAt)))
if err != nil {
···
Type: models.NotificationTypeRepoStarred,
EntityType: "repo",
EntityId: string(star.RepoAt),
-
RepoId: &repo.ID,
+
RepoId: &repo.Id,
}
-
err = n.db.CreateNotification(ctx, notification)
if err != nil {
log.Printf("NewStar: failed to create notification: %v", err)
···
}
}
-
func (n *databaseNotifier) DeleteStar(ctx context.Context, star *db.Star) {
+
func (n *databaseNotifier) DeleteStar(ctx context.Context, star *models.Star) {
// no-op
}
-
func (n *databaseNotifier) NewIssue(ctx context.Context, issue *db.Issue) {
+
func (n *databaseNotifier) NewIssue(ctx context.Context, issue *models.Issue) {
repos, err := db.GetRepos(n.db, 1, db.FilterEq("at_uri", string(issue.RepoAt)))
if err != nil {
log.Printf("NewIssue: failed to get repos: %v", err)
···
Type: models.NotificationTypeIssueCreated,
EntityType: "issue",
EntityId: string(issue.AtUri()),
-
RepoId: &repo.ID,
+
RepoId: &repo.Id,
IssueId: &issue.Id,
}
···
}
}
-
func (n *databaseNotifier) NewIssueComment(ctx context.Context, comment *db.IssueComment) {
+
func (n *databaseNotifier) NewIssueComment(ctx context.Context, comment *models.IssueComment) {
issues, err := db.GetIssues(n.db, db.FilterEq("at_uri", comment.IssueAt))
if err != nil {
log.Printf("NewIssueComment: failed to get issues: %v", err)
···
Type: models.NotificationTypeIssueCommented,
EntityType: "issue",
EntityId: string(issue.AtUri()),
-
RepoId: &repo.ID,
+
RepoId: &repo.Id,
IssueId: &issue.Id,
}
···
}
}
-
func (n *databaseNotifier) NewFollow(ctx context.Context, follow *db.Follow) {
+
func (n *databaseNotifier) NewFollow(ctx context.Context, follow *models.Follow) {
prefs, err := n.db.GetNotificationPreferences(ctx, follow.SubjectDid)
if err != nil {
log.Printf("NewFollow: failed to get notification preferences for %s: %v", follow.SubjectDid, err)
···
}
}
-
func (n *databaseNotifier) DeleteFollow(ctx context.Context, follow *db.Follow) {
+
func (n *databaseNotifier) DeleteFollow(ctx context.Context, follow *models.Follow) {
// no-op
}
-
func (n *databaseNotifier) NewPull(ctx context.Context, pull *db.Pull) {
+
func (n *databaseNotifier) NewPull(ctx context.Context, pull *models.Pull) {
repos, err := db.GetRepos(n.db, 1, db.FilterEq("at_uri", string(pull.RepoAt)))
if err != nil {
log.Printf("NewPull: failed to get repos: %v", err)
···
Type: models.NotificationTypePullCreated,
EntityType: "pull",
EntityId: string(pull.RepoAt),
-
RepoId: &repo.ID,
+
RepoId: &repo.Id,
PullId: func() *int64 { id := int64(pull.ID); return &id }(),
}
···
}
}
-
func (n *databaseNotifier) NewPullComment(ctx context.Context, comment *db.PullComment) {
+
func (n *databaseNotifier) NewPullComment(ctx context.Context, comment *models.PullComment) {
pulls, err := db.GetPulls(n.db,
db.FilterEq("repo_at", comment.RepoAt),
db.FilterEq("pull_id", comment.PullId))
···
Type: models.NotificationTypePullCommented,
EntityType: "pull",
EntityId: comment.RepoAt,
-
RepoId: &repo.ID,
+
RepoId: &repo.Id,
PullId: func() *int64 { id := int64(pull.ID); return &id }(),
}
···
}
}
-
func (n *databaseNotifier) UpdateProfile(ctx context.Context, profile *db.Profile) {
+
func (n *databaseNotifier) UpdateProfile(ctx context.Context, profile *models.Profile) {
// no-op
}
···
// no-op
}
-
func (n *databaseNotifier) EditString(ctx context.Context, string *db.String) {
+
func (n *databaseNotifier) EditString(ctx context.Context, string *models.String) {
// no-op
}
-
func (n *databaseNotifier) NewString(ctx context.Context, string *db.String) {
+
func (n *databaseNotifier) NewString(ctx context.Context, string *models.String) {
// no-op
}
-
func (n *databaseNotifier) NewIssueClosed(ctx context.Context, issue *db.Issue) {
+
func (n *databaseNotifier) NewIssueClosed(ctx context.Context, issue *models.Issue) {
// Get repo details
repos, err := db.GetRepos(n.db, 1, db.FilterEq("at_uri", string(issue.RepoAt)))
if err != nil {
···
Type: models.NotificationTypeIssueClosed,
EntityType: "issue",
EntityId: string(issue.AtUri()),
-
RepoId: &repo.ID,
+
RepoId: &repo.Id,
IssueId: &issue.Id,
}
···
}
}
-
func (n *databaseNotifier) NewPullMerged(ctx context.Context, pull *db.Pull) {
+
func (n *databaseNotifier) NewPullMerged(ctx context.Context, pull *models.Pull) {
// Get repo details
repos, err := db.GetRepos(n.db, 1, db.FilterEq("at_uri", string(pull.RepoAt)))
if err != nil {
···
Type: models.NotificationTypePullMerged,
EntityType: "pull",
EntityId: string(pull.RepoAt),
-
RepoId: &repo.ID,
+
RepoId: &repo.Id,
PullId: func() *int64 { id := int64(pull.ID); return &id }(),
}
···
}
}
-
func (n *databaseNotifier) NewPullClosed(ctx context.Context, pull *db.Pull) {
+
func (n *databaseNotifier) NewPullClosed(ctx context.Context, pull *models.Pull) {
// Get repo details
repos, err := db.GetRepos(n.db, 1, db.FilterEq("at_uri", string(pull.RepoAt)))
if err != nil {
···
Type: models.NotificationTypePullClosed,
EntityType: "pull",
EntityId: string(pull.RepoAt),
-
RepoId: &repo.ID,
+
RepoId: &repo.Id,
PullId: func() *int64 { id := int64(pull.ID); return &id }(),
}
+1
appview/state/state.go
···
"tangled.org/core/appview/db"
"tangled.org/core/appview/models"
"tangled.org/core/appview/notify"
+
dbnotify "tangled.org/core/appview/notify/db"
phnotify "tangled.org/core/appview/notify/posthog"
"tangled.org/core/appview/oauth"
"tangled.org/core/appview/pages"