Go · 4705 bytes Raw Blame History
1 // Code generated by sqlc. DO NOT EDIT.
2 // versions:
3 // sqlc v1.31.1
4 // source: threads.sql
5
6 package notifdb
7
8 import (
9 "context"
10 )
11
12 const deleteNotificationThread = `-- name: DeleteNotificationThread :exec
13 DELETE FROM notification_threads
14 WHERE recipient_user_id = $1 AND thread_kind = $2 AND thread_id = $3
15 `
16
17 type DeleteNotificationThreadParams struct {
18 RecipientUserID int64
19 ThreadKind NotificationThreadKind
20 ThreadID int64
21 }
22
23 func (q *Queries) DeleteNotificationThread(ctx context.Context, db DBTX, arg DeleteNotificationThreadParams) error {
24 _, err := db.Exec(ctx, deleteNotificationThread, arg.RecipientUserID, arg.ThreadKind, arg.ThreadID)
25 return err
26 }
27
28 const getNotificationThread = `-- name: GetNotificationThread :one
29
30 SELECT recipient_user_id, thread_kind, thread_id, subscribed, reason, updated_at
31 FROM notification_threads
32 WHERE recipient_user_id = $1 AND thread_kind = $2 AND thread_id = $3
33 `
34
35 type GetNotificationThreadParams struct {
36 RecipientUserID int64
37 ThreadKind NotificationThreadKind
38 ThreadID int64
39 }
40
41 // ─── notification_threads ──────────────────────────────────────────
42 func (q *Queries) GetNotificationThread(ctx context.Context, db DBTX, arg GetNotificationThreadParams) (NotificationThread, error) {
43 row := db.QueryRow(ctx, getNotificationThread, arg.RecipientUserID, arg.ThreadKind, arg.ThreadID)
44 var i NotificationThread
45 err := row.Scan(
46 &i.RecipientUserID,
47 &i.ThreadKind,
48 &i.ThreadID,
49 &i.Subscribed,
50 &i.Reason,
51 &i.UpdatedAt,
52 )
53 return i, err
54 }
55
56 const insertNotificationThreadIfAbsent = `-- name: InsertNotificationThreadIfAbsent :exec
57 INSERT INTO notification_threads (recipient_user_id, thread_kind, thread_id, subscribed, reason)
58 VALUES ($1, $2, $3, $4, $5)
59 ON CONFLICT (recipient_user_id, thread_kind, thread_id) DO NOTHING
60 `
61
62 type InsertNotificationThreadIfAbsentParams struct {
63 RecipientUserID int64
64 ThreadKind NotificationThreadKind
65 ThreadID int64
66 Subscribed bool
67 Reason string
68 }
69
70 // Auto-subscription path: only insert if the user has no explicit
71 // preference yet. Preserves user choices (e.g. an explicit
72 // `subscribed=false` from clicking "Unsubscribe").
73 func (q *Queries) InsertNotificationThreadIfAbsent(ctx context.Context, db DBTX, arg InsertNotificationThreadIfAbsentParams) error {
74 _, err := db.Exec(ctx, insertNotificationThreadIfAbsent,
75 arg.RecipientUserID,
76 arg.ThreadKind,
77 arg.ThreadID,
78 arg.Subscribed,
79 arg.Reason,
80 )
81 return err
82 }
83
84 const listSubscribersForThread = `-- name: ListSubscribersForThread :many
85 SELECT recipient_user_id, reason
86 FROM notification_threads
87 WHERE thread_kind = $1 AND thread_id = $2 AND subscribed = true
88 `
89
90 type ListSubscribersForThreadParams struct {
91 ThreadKind NotificationThreadKind
92 ThreadID int64
93 }
94
95 type ListSubscribersForThreadRow struct {
96 RecipientUserID int64
97 Reason string
98 }
99
100 // Fan-out helper: returns recipients who explicitly subscribed to a
101 // thread. The fan-out worker unions this with the per-repo `watches`
102 // result + author/assignee/reviewer rules.
103 func (q *Queries) ListSubscribersForThread(ctx context.Context, db DBTX, arg ListSubscribersForThreadParams) ([]ListSubscribersForThreadRow, error) {
104 rows, err := db.Query(ctx, listSubscribersForThread, arg.ThreadKind, arg.ThreadID)
105 if err != nil {
106 return nil, err
107 }
108 defer rows.Close()
109 items := []ListSubscribersForThreadRow{}
110 for rows.Next() {
111 var i ListSubscribersForThreadRow
112 if err := rows.Scan(&i.RecipientUserID, &i.Reason); err != nil {
113 return nil, err
114 }
115 items = append(items, i)
116 }
117 if err := rows.Err(); err != nil {
118 return nil, err
119 }
120 return items, nil
121 }
122
123 const upsertNotificationThread = `-- name: UpsertNotificationThread :exec
124 INSERT INTO notification_threads (recipient_user_id, thread_kind, thread_id, subscribed, reason)
125 VALUES ($1, $2, $3, $4, $5)
126 ON CONFLICT (recipient_user_id, thread_kind, thread_id)
127 DO UPDATE SET subscribed = EXCLUDED.subscribed,
128 reason = EXCLUDED.reason,
129 updated_at = now()
130 `
131
132 type UpsertNotificationThreadParams struct {
133 RecipientUserID int64
134 ThreadKind NotificationThreadKind
135 ThreadID int64
136 Subscribed bool
137 Reason string
138 }
139
140 // Always-write upsert. Used by Subscribe / Unsubscribe / Ignore
141 // handlers and by the auto-subscription rules in the fan-out
142 // worker.
143 func (q *Queries) UpsertNotificationThread(ctx context.Context, db DBTX, arg UpsertNotificationThreadParams) error {
144 _, err := db.Exec(ctx, upsertNotificationThread,
145 arg.RecipientUserID,
146 arg.ThreadKind,
147 arg.ThreadID,
148 arg.Subscribed,
149 arg.Reason,
150 )
151 return err
152 }
153