This repository has no description
0

Configure Feed

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

core / spindle / db / collaborators.go
3.1 kB 115 lines
1package db 2 3import ( 4 "database/sql" 5 "fmt" 6 7 "github.com/bluesky-social/indigo/atproto/syntax" 8) 9 10type RepoCollaborator struct { 11 OwnerDid syntax.DID 12 Rkey syntax.RecordKey 13 Subject syntax.DID 14 RepoDid syntax.DID 15} 16 17func (d *DB) AddRepoCollaborator(c RepoCollaborator) error { 18 _, err := d.Exec( 19 `insert into repo_collaborators (owner_did, rkey, subject, repo_did) 20 values (?, ?, ?, ?) 21 on conflict(owner_did, rkey) do update set 22 subject = excluded.subject, 23 repo_did = excluded.repo_did`, 24 c.OwnerDid.String(), c.Rkey.String(), c.Subject.String(), c.RepoDid.String(), 25 ) 26 return err 27} 28 29func (d *DB) AddKnotCollaborator(repoDid, subject syntax.DID) error { 30 _, err := d.Exec( 31 `insert into repo_collaborators (owner_did, rkey, subject, repo_did) 32 values (?, ?, ?, ?) 33 on conflict(owner_did, rkey) do nothing`, 34 repoDid.String(), subject.String(), subject.String(), repoDid.String(), 35 ) 36 return err 37} 38 39func scanCollab(row interface{ Scan(...any) error }) (*RepoCollaborator, error) { 40 var owner, rkey, subject, repoDid string 41 if err := row.Scan(&owner, &rkey, &subject, &repoDid); err != nil { 42 return nil, err 43 } 44 return &RepoCollaborator{ 45 OwnerDid: syntax.DID(owner), 46 Rkey: syntax.RecordKey(rkey), 47 Subject: syntax.DID(subject), 48 RepoDid: syntax.DID(repoDid), 49 }, nil 50} 51 52func (d *DB) GetRepoCollaborator(ownerDid syntax.DID, rkey syntax.RecordKey) (*RepoCollaborator, error) { 53 return scanCollab(d.QueryRow( 54 `select owner_did, rkey, subject, repo_did from repo_collaborators where owner_did = ? and rkey = ?`, 55 ownerDid.String(), rkey.String(), 56 )) 57} 58 59func (d *DB) DeleteRepoCollaborator(ownerDid syntax.DID, rkey syntax.RecordKey) error { 60 res, err := d.Exec(`delete from repo_collaborators where owner_did = ? and rkey = ?`, ownerDid.String(), rkey.String()) 61 if err != nil { 62 return err 63 } 64 n, err := res.RowsAffected() 65 if err != nil { 66 return err 67 } 68 if n == 0 { 69 return sql.ErrNoRows 70 } 71 return nil 72} 73 74func (d *DB) DeleteRepoCollaboratorBySubjectRepo(subject, repoDid syntax.DID) error { 75 _, err := d.Exec( 76 `delete from repo_collaborators where repo_did = ? and subject = ?`, 77 repoDid.String(), subject.String(), 78 ) 79 if err != nil { 80 return fmt.Errorf("delete collaborator %s on %s: %w", subject, repoDid, err) 81 } 82 return nil 83} 84 85func (d *DB) DeleteRepoCollaboratorsByRepoDid(repoDid syntax.DID) error { 86 _, err := d.Exec(`delete from repo_collaborators where repo_did = ?`, repoDid.String()) 87 if err != nil { 88 return fmt.Errorf("delete collaborators for %s: %w", repoDid, err) 89 } 90 return nil 91} 92 93func (d *DB) ListCollaboratorsByRepoDid(repoDid syntax.DID) ([]RepoCollaborator, error) { 94 rows, err := d.Query( 95 `select owner_did, rkey, subject, repo_did from repo_collaborators where repo_did = ?`, 96 repoDid.String(), 97 ) 98 if err != nil { 99 return nil, fmt.Errorf("list collaborators for %s: %w", repoDid, err) 100 } 101 defer rows.Close() 102 103 var out []RepoCollaborator 104 for rows.Next() { 105 c, err := scanCollab(rows) 106 if err != nil { 107 return nil, err 108 } 109 out = append(out, *c) 110 } 111 if err := rows.Err(); err != nil { 112 return nil, err 113 } 114 return out, nil 115}