Go · 5280 bytes Raw Blame History
1 // Code generated by sqlc. DO NOT EDIT.
2 // versions:
3 // sqlc v1.31.1
4 // source: watches.sql
5
6 package socialdb
7
8 import (
9 "context"
10
11 "github.com/jackc/pgx/v5/pgtype"
12 )
13
14 const countWatchersForRepo = `-- name: CountWatchersForRepo :one
15 SELECT COUNT(*) FROM watches w
16 JOIN users u ON u.id = w.user_id
17 WHERE w.repo_id = $1
18 AND w.level <> 'ignore'
19 AND u.suspended_at IS NULL
20 `
21
22 func (q *Queries) CountWatchersForRepo(ctx context.Context, db DBTX, repoID int64) (int64, error) {
23 row := db.QueryRow(ctx, countWatchersForRepo, repoID)
24 var count int64
25 err := row.Scan(&count)
26 return count, err
27 }
28
29 const deleteWatch = `-- name: DeleteWatch :exec
30 DELETE FROM watches WHERE user_id = $1 AND repo_id = $2
31 `
32
33 type DeleteWatchParams struct {
34 UserID int64
35 RepoID int64
36 }
37
38 // Used when a user unsets their explicit preference (returning to the
39 // implicit `participating` default). Trigger drops the watcher_count
40 // when the prior level wasn't 'ignore'.
41 func (q *Queries) DeleteWatch(ctx context.Context, db DBTX, arg DeleteWatchParams) error {
42 _, err := db.Exec(ctx, deleteWatch, arg.UserID, arg.RepoID)
43 return err
44 }
45
46 const getWatch = `-- name: GetWatch :one
47
48 SELECT user_id, repo_id, level, updated_at
49 FROM watches WHERE user_id = $1 AND repo_id = $2
50 `
51
52 type GetWatchParams struct {
53 UserID int64
54 RepoID int64
55 }
56
57 // ─── watches ───────────────────────────────────────────────────────
58 func (q *Queries) GetWatch(ctx context.Context, db DBTX, arg GetWatchParams) (Watch, error) {
59 row := db.QueryRow(ctx, getWatch, arg.UserID, arg.RepoID)
60 var i Watch
61 err := row.Scan(
62 &i.UserID,
63 &i.RepoID,
64 &i.Level,
65 &i.UpdatedAt,
66 )
67 return i, err
68 }
69
70 const insertWatchIfAbsent = `-- name: InsertWatchIfAbsent :exec
71 INSERT INTO watches (user_id, repo_id, level)
72 VALUES ($1, $2, $3)
73 ON CONFLICT (user_id, repo_id) DO NOTHING
74 `
75
76 type InsertWatchIfAbsentParams struct {
77 UserID int64
78 RepoID int64
79 Level WatchLevel
80 }
81
82 // Auto-watch flow: only insert if the user doesn't already have a
83 // preference. ON CONFLICT DO NOTHING preserves the user's chosen
84 // level when the trigger fires repeatedly.
85 func (q *Queries) InsertWatchIfAbsent(ctx context.Context, db DBTX, arg InsertWatchIfAbsentParams) error {
86 _, err := db.Exec(ctx, insertWatchIfAbsent, arg.UserID, arg.RepoID, arg.Level)
87 return err
88 }
89
90 const listRepoWatchersByLevel = `-- name: ListRepoWatchersByLevel :many
91 SELECT user_id FROM watches
92 WHERE repo_id = $1 AND level = $2
93 `
94
95 type ListRepoWatchersByLevelParams struct {
96 RepoID int64
97 Level WatchLevel
98 }
99
100 // S29 notification-routing consumer: for fan-out, get every watcher
101 // of a repo at the requested level (e.g. `level='all'` for new-issue
102 // events). This is the cross-package read; expose the user_ids
103 // without joining users — fan-out adds the user join itself.
104 func (q *Queries) ListRepoWatchersByLevel(ctx context.Context, db DBTX, arg ListRepoWatchersByLevelParams) ([]int64, error) {
105 rows, err := db.Query(ctx, listRepoWatchersByLevel, arg.RepoID, arg.Level)
106 if err != nil {
107 return nil, err
108 }
109 defer rows.Close()
110 items := []int64{}
111 for rows.Next() {
112 var user_id int64
113 if err := rows.Scan(&user_id); err != nil {
114 return nil, err
115 }
116 items = append(items, user_id)
117 }
118 if err := rows.Err(); err != nil {
119 return nil, err
120 }
121 return items, nil
122 }
123
124 const listWatchersForRepo = `-- name: ListWatchersForRepo :many
125 SELECT w.user_id, w.level, w.updated_at, u.username, u.display_name
126 FROM watches w
127 JOIN users u ON u.id = w.user_id
128 WHERE w.repo_id = $1
129 AND w.level <> 'ignore'
130 AND u.suspended_at IS NULL
131 ORDER BY w.updated_at DESC
132 LIMIT $2 OFFSET $3
133 `
134
135 type ListWatchersForRepoParams struct {
136 RepoID int64
137 Limit int32
138 Offset int32
139 }
140
141 type ListWatchersForRepoRow struct {
142 UserID int64
143 Level WatchLevel
144 UpdatedAt pgtype.Timestamptz
145 Username string
146 DisplayName string
147 }
148
149 // Watchers list. `level <> 'ignore'` excludes users who have actively
150 // muted the repo. Excludes suspended users from public surfaces.
151 func (q *Queries) ListWatchersForRepo(ctx context.Context, db DBTX, arg ListWatchersForRepoParams) ([]ListWatchersForRepoRow, error) {
152 rows, err := db.Query(ctx, listWatchersForRepo, arg.RepoID, arg.Limit, arg.Offset)
153 if err != nil {
154 return nil, err
155 }
156 defer rows.Close()
157 items := []ListWatchersForRepoRow{}
158 for rows.Next() {
159 var i ListWatchersForRepoRow
160 if err := rows.Scan(
161 &i.UserID,
162 &i.Level,
163 &i.UpdatedAt,
164 &i.Username,
165 &i.DisplayName,
166 ); err != nil {
167 return nil, err
168 }
169 items = append(items, i)
170 }
171 if err := rows.Err(); err != nil {
172 return nil, err
173 }
174 return items, nil
175 }
176
177 const upsertWatch = `-- name: UpsertWatch :exec
178 INSERT INTO watches (user_id, repo_id, level)
179 VALUES ($1, $2, $3)
180 ON CONFLICT (user_id, repo_id) DO UPDATE
181 SET level = EXCLUDED.level,
182 updated_at = now()
183 `
184
185 type UpsertWatchParams struct {
186 UserID int64
187 RepoID int64
188 Level WatchLevel
189 }
190
191 // Always-write upsert. The AFTER trigger handles the watcher_count
192 // delta on transition into / out of `ignore`.
193 func (q *Queries) UpsertWatch(ctx context.Context, db DBTX, arg UpsertWatchParams) error {
194 _, err := db.Exec(ctx, upsertWatch, arg.UserID, arg.RepoID, arg.Level)
195 return err
196 }
197