1package db
2
3import (
4 "fmt"
5 "strings"
6 "time"
7
8 "github.com/bluesky-social/indigo/atproto/syntax"
9)
10
11type Collaborator struct {
12 // identifiers for the record
13 Id int64
14 Did syntax.DID
15 Rkey string
16
17 // content
18 SubjectDid syntax.DID
19 RepoAt syntax.ATURI
20
21 // meta
22 Created time.Time
23}
24
25func AddCollaborator(e Execer, c Collaborator) error {
26 _, err := e.Exec(
27 `insert into collaborators (did, rkey, subject_did, repo_at) values (?, ?, ?, ?);`,
28 c.Did, c.Rkey, c.SubjectDid, c.RepoAt,
29 )
30 return err
31}
32
33func DeleteCollaborator(e Execer, filters ...filter) error {
34 var conditions []string
35 var args []any
36 for _, filter := range filters {
37 conditions = append(conditions, filter.Condition())
38 args = append(args, filter.Arg()...)
39 }
40
41 whereClause := ""
42 if conditions != nil {
43 whereClause = " where " + strings.Join(conditions, " and ")
44 }
45
46 query := fmt.Sprintf(`delete from collaborators %s`, whereClause)
47
48 _, err := e.Exec(query, args...)
49 return err
50}
51
52func CollaboratingIn(e Execer, collaborator string) ([]Repo, error) {
53 rows, err := e.Query(`select repo_at from collaborators where subject_did = ?`, collaborator)
54 if err != nil {
55 return nil, err
56 }
57 defer rows.Close()
58
59 var repoAts []string
60 for rows.Next() {
61 var aturi string
62 err := rows.Scan(&aturi)
63 if err != nil {
64 return nil, err
65 }
66 repoAts = append(repoAts, aturi)
67 }
68 if err := rows.Err(); err != nil {
69 return nil, err
70 }
71 if repoAts == nil {
72 return nil, nil
73 }
74
75 return GetRepos(e, 0, FilterIn("at_uri", repoAts))
76}