This repository has no description
1package db
2
3import "database/sql"
4
5// UpsertIssueSubscription inserts or updates a user's subscription for an issue.
6// subscribed=true means they want notifications; subscribed=false means explicitly unsubscribed.
7func UpsertIssueSubscription(e Execer, userDid string, issueId int64, subscribed bool) error {
8 sub := 0
9 if subscribed {
10 sub = 1
11 }
12 _, err := e.Exec(`
13 INSERT INTO issue_subscriptions (user_did, issue_id, subscribed)
14 VALUES (?, ?, ?)
15 ON CONFLICT(user_did, issue_id) DO UPDATE SET subscribed = excluded.subscribed
16 `, userDid, issueId, sub)
17 return err
18}
19
20// UpsertPullSubscription inserts or updates a user's subscription for a pull.
21func UpsertPullSubscription(e Execer, userDid string, pullId int64, subscribed bool) error {
22 sub := 0
23 if subscribed {
24 sub = 1
25 }
26 _, err := e.Exec(`
27 INSERT INTO pull_subscriptions (user_did, pull_id, subscribed)
28 VALUES (?, ?, ?)
29 ON CONFLICT(user_did, pull_id) DO UPDATE SET subscribed = excluded.subscribed
30 `, userDid, pullId, sub)
31 return err
32}
33
34// GetIssueSubscription returns (subscribed, found, err).
35// If no row exists, found=false (meaning no explicit subscription).
36func GetIssueSubscription(e Execer, userDid string, issueId int64) (subscribed bool, found bool, err error) {
37 var sub int
38 err = e.QueryRow(`
39 SELECT subscribed FROM issue_subscriptions
40 WHERE user_did = ? AND issue_id = ?
41 `, userDid, issueId).Scan(&sub)
42 if err == sql.ErrNoRows {
43 return false, false, nil
44 }
45 if err != nil {
46 return false, false, err
47 }
48 return sub == 1, true, nil
49}
50
51// GetPullSubscription returns (subscribed, found, err).
52func GetPullSubscription(e Execer, userDid string, pullId int64) (subscribed bool, found bool, err error) {
53 var sub int
54 err = e.QueryRow(`
55 SELECT subscribed FROM pull_subscriptions
56 WHERE user_did = ? AND pull_id = ?
57 `, userDid, pullId).Scan(&sub)
58 if err == sql.ErrNoRows {
59 return false, false, nil
60 }
61 if err != nil {
62 return false, false, err
63 }
64 return sub == 1, true, nil
65}
66
67// GetIssueSubscribers returns DIDs with subscribed=1 for an issue.
68func GetIssueSubscribers(e Execer, issueId int64) ([]string, error) {
69 rows, err := e.Query(`
70 SELECT user_did FROM issue_subscriptions
71 WHERE issue_id = ? AND subscribed = 1
72 `, issueId)
73 if err != nil {
74 return nil, err
75 }
76 defer rows.Close()
77 var dids []string
78 for rows.Next() {
79 var did string
80 if err := rows.Scan(&did); err != nil {
81 return nil, err
82 }
83 dids = append(dids, did)
84 }
85 return dids, rows.Err()
86}
87
88// GetIssueUnsubscribers returns DIDs with subscribed=0 for an issue (opted out).
89func GetIssueUnsubscribers(e Execer, issueId int64) ([]string, error) {
90 rows, err := e.Query(`
91 SELECT user_did FROM issue_subscriptions
92 WHERE issue_id = ? AND subscribed = 0
93 `, issueId)
94 if err != nil {
95 return nil, err
96 }
97 defer rows.Close()
98 var dids []string
99 for rows.Next() {
100 var did string
101 if err := rows.Scan(&did); err != nil {
102 return nil, err
103 }
104 dids = append(dids, did)
105 }
106 return dids, rows.Err()
107}
108
109// GetPullSubscribers returns DIDs with subscribed=1 for a pull.
110func GetPullSubscribers(e Execer, pullId int64) ([]string, error) {
111 rows, err := e.Query(`
112 SELECT user_did FROM pull_subscriptions
113 WHERE pull_id = ? AND subscribed = 1
114 `, pullId)
115 if err != nil {
116 return nil, err
117 }
118 defer rows.Close()
119 var dids []string
120 for rows.Next() {
121 var did string
122 if err := rows.Scan(&did); err != nil {
123 return nil, err
124 }
125 dids = append(dids, did)
126 }
127 return dids, rows.Err()
128}
129
130// GetPullUnsubscribers returns DIDs with subscribed=0 for a pull (opted out).
131func GetPullUnsubscribers(e Execer, pullId int64) ([]string, error) {
132 rows, err := e.Query(`
133 SELECT user_did FROM pull_subscriptions
134 WHERE pull_id = ? AND subscribed = 0
135 `, pullId)
136 if err != nil {
137 return nil, err
138 }
139 defer rows.Close()
140 var dids []string
141 for rows.Next() {
142 var did string
143 if err := rows.Scan(&did); err != nil {
144 return nil, err
145 }
146 dids = append(dids, did)
147 }
148 return dids, rows.Err()
149}