Line data Source code
1 4 : (ns matchscore.db.postgres.telegram-links
2 : "Read model behind the Telegram chats overview: links joined with the template
3 : or event they belong to, the latest bot post, and the last delivery failure."
4 : (:require
5 : [next.jdbc :as jdbc]
6 : [next.jdbc.result-set :as rs]))
7 :
8 :
9 8 : (def past-event-days "How long a completed or cancelled event keeps appearing in the overview." 30)
10 :
11 :
12 8 : (def ^:private unlinked-limit 30)
13 :
14 :
15 8 : (def ^:private template-manager-sql
16 : "(me.id IS NULL
17 : OR t.created_by = me.id
18 : OR EXISTS (SELECT 1
19 : FROM template_participants tp
20 : JOIN participants tpp ON tpp.uuid = tp.participant_id
21 : WHERE tp.template_id = t.uuid
22 : AND tpp.user_id = me.id
23 : AND tp.roles @> '[\"admin\"]'::jsonb))")
24 :
25 :
26 8 : (def ^:private event-manager-sql
27 : "(me.id IS NULL
28 : OR e.created_by = me.id
29 : OR EXISTS (SELECT 1
30 : FROM event_participants ep
31 : JOIN participants epp ON epp.uuid = ep.participant_id
32 : WHERE ep.event_id = e.uuid
33 : AND epp.user_id = me.id
34 : AND ep.roles @> '[\"admin\"]'::jsonb)
35 : OR EXISTS (SELECT 1
36 : FROM templates mt
37 : WHERE mt.uuid = e.template_id
38 : AND (mt.created_by = me.id
39 : OR EXISTS (SELECT 1
40 : FROM template_participants mtp
41 : JOIN participants mtpp ON mtpp.uuid = mtp.participant_id
42 : WHERE mtp.template_id = mt.uuid
43 : AND mtpp.user_id = me.id
44 : AND mtp.roles @> '[\"admin\"]'::jsonb))))")
45 :
46 :
47 8 : (def ^:private link-columns-sql
48 : "l.uuid, l.chat_id, l.chat_title, l.thread_id, l.linked_by_user_id, l.created_datetime,
49 : l.updated_datetime, l.last_message_id,
50 : l.last_error, l.last_error_code, l.last_error_at AS last_error_datetime")
51 :
52 :
53 8 : (def ^:private next-template-event-sql
54 : "LEFT JOIN LATERAL (SELECT ne.datetime_start
55 : FROM events ne
56 : WHERE ne.template_id = t.uuid
57 : AND ne.source NOT IN ('psa', 'fsu', 'esf')
58 : AND ne.state NOT IN ('completed', 'cancelled')
59 : AND ne.datetime_start > now()
60 : ORDER BY ne.datetime_start
61 : LIMIT 1) ne ON true")
62 :
63 :
64 4 : (def ^:private template-links-sql
65 24 : (str
66 : "WITH me AS (SELECT ?::uuid AS id, ?::uuid AS resource_id)
67 : SELECT "
68 4 : link-columns-sql
69 : ",
70 : 'template' AS resource_type,
71 : t.uuid AS resource_uuid,
72 : t.public_id AS resource_public_id,
73 : t.name AS resource_name,
74 : t.state AS resource_state,
75 : t.sport_code AS resource_sport_code,
76 : c.name AS resource_club_name,
77 : NULL::timestamptz AS resource_datetime_start,
78 : ne.datetime_start AS resource_next_event_start,
79 : CASE WHEN me.id IS NULL THEN NULL
80 : WHEN t.created_by = me.id THEN 'owner'
81 : ELSE 'admin' END AS resource_role,
82 : u.name AS resource_owner_name,
83 : u.username AS resource_owner_username,
84 : lp.created_datetime AS last_post_datetime,
85 : lp.message_id AS last_post_message_id
86 : FROM telegram_template_links l
87 : CROSS JOIN me
88 : JOIN templates t ON t.uuid = l.template_id
89 : LEFT JOIN clubs c ON c.uuid = t.club_id
90 : LEFT JOIN users u ON u.uuid = t.created_by
91 : "
92 4 : next-template-event-sql
93 : "
94 : LEFT JOIN LATERAL (SELECT m.created_datetime, m.message_id
95 : FROM telegram_chat_messages m
96 : JOIN events pe ON pe.uuid = m.event_id
97 : WHERE pe.template_id = l.template_id
98 : AND pe.source NOT IN ('psa', 'fsu', 'esf')
99 : AND m.chat_id = l.chat_id
100 : AND m.thread_id = l.thread_id
101 : AND m.sender_kind = 'bot'
102 : AND m.kind <> 'reply'
103 : AND m.deleted_at IS NULL
104 : ORDER BY m.created_datetime DESC
105 : LIMIT 1) lp ON true
106 : WHERE (me.resource_id IS NULL OR l.template_id = me.resource_id)
107 : AND "
108 4 : template-manager-sql
109 : "
110 : ORDER BY l.created_datetime"))
111 :
112 :
113 4 : (def ^:private event-links-sql
114 20 : (str
115 : "WITH me AS (SELECT ?::uuid AS id, ?::uuid AS resource_id)
116 : SELECT "
117 4 : link-columns-sql
118 : ",
119 : 'event' AS resource_type,
120 : e.uuid AS resource_uuid,
121 : e.public_id AS resource_public_id,
122 : e.name AS resource_name,
123 : e.state AS resource_state,
124 : e.sport_code AS resource_sport_code,
125 : c.name AS resource_club_name,
126 : e.datetime_start AS resource_datetime_start,
127 : NULL::timestamptz AS resource_next_event_start,
128 : CASE WHEN me.id IS NULL THEN NULL
129 : WHEN e.created_by = me.id THEN 'owner'
130 : ELSE 'admin' END AS resource_role,
131 : u.name AS resource_owner_name,
132 : u.username AS resource_owner_username,
133 : lp.created_datetime AS last_post_datetime,
134 : lp.message_id AS last_post_message_id
135 : FROM telegram_event_links l
136 : CROSS JOIN me
137 : JOIN events e ON e.uuid = l.event_id
138 : LEFT JOIN clubs c ON c.uuid = e.club_id
139 : LEFT JOIN users u ON u.uuid = e.created_by
140 : LEFT JOIN LATERAL (SELECT m.created_datetime, m.message_id
141 : FROM telegram_chat_messages m
142 : WHERE m.event_id = l.event_id
143 : AND m.chat_id = l.chat_id
144 : AND m.thread_id = l.thread_id
145 : AND m.sender_kind = 'bot'
146 : AND m.kind <> 'reply'
147 : AND m.deleted_at IS NULL
148 : ORDER BY m.created_datetime DESC
149 : LIMIT 1) lp ON true
150 : WHERE (me.resource_id IS NULL OR l.event_id = me.resource_id)
151 : AND e.source NOT IN ('psa', 'fsu', 'esf')
152 : AND (me.resource_id IS NOT NULL
153 : OR e.state NOT IN ('completed', 'cancelled')
154 : OR e.datetime_end >= now() - make_interval(days => ?::int))
155 : AND "
156 4 : event-manager-sql
157 : "
158 : ORDER BY e.datetime_start, l.created_datetime"))
159 :
160 :
161 4 : (def ^:private unlinked-templates-sql
162 16 : (str
163 : "SELECT 'template' AS resource_type,
164 : t.uuid AS resource_uuid,
165 : t.public_id AS resource_public_id,
166 : t.name AS resource_name,
167 : t.state AS resource_state,
168 : t.sport_code AS resource_sport_code,
169 : c.name AS resource_club_name,
170 : NULL::timestamptz AS resource_datetime_start,
171 : ne.datetime_start AS resource_next_event_start
172 : FROM templates t
173 : LEFT JOIN clubs c ON c.uuid = t.club_id
174 : "
175 4 : next-template-event-sql
176 : "
177 : WHERE t.created_by = ?::uuid
178 : AND t.state = 'active'
179 : AND NOT EXISTS (SELECT 1 FROM telegram_template_links l WHERE l.template_id = t.uuid)
180 : ORDER BY t.created_datetime DESC
181 : LIMIT ?"))
182 :
183 :
184 8 : (def ^:private unlinked-events-sql
185 : "SELECT 'event' AS resource_type,
186 : e.uuid AS resource_uuid,
187 : e.public_id AS resource_public_id,
188 : e.name AS resource_name,
189 : e.state AS resource_state,
190 : e.sport_code AS resource_sport_code,
191 : c.name AS resource_club_name,
192 : e.datetime_start AS resource_datetime_start,
193 : NULL::timestamptz AS resource_next_event_start
194 : FROM events e
195 : LEFT JOIN clubs c ON c.uuid = e.club_id
196 : WHERE e.created_by = ?::uuid
197 : AND e.source NOT IN ('psa', 'fsu', 'esf')
198 : AND e.template_id IS NULL
199 : AND e.state NOT IN ('completed', 'cancelled')
200 : AND e.datetime_end > now()
201 : AND NOT EXISTS (SELECT 1 FROM telegram_event_links l WHERE l.event_id = e.uuid)
202 : ORDER BY e.datetime_start
203 : LIMIT ?")
204 :
205 :
206 4 : (defn- query
207 : [ds sql-params]
208 21 : (jdbc/execute! ds sql-params {:builder-fn rs/as-unqualified-kebab-maps}))
209 :
210 :
211 4 : (defn- row->resource
212 : [row]
213 6 : (->
214 3 : row
215 6 : (update :resource-uuid str)))
216 :
217 :
218 4 : (defn- row->link
219 : [row]
220 6 : (->
221 9 : (row->resource row)
222 12 : (update :uuid str)
223 12 : (update
224 : :linked-by-user-id
225 27 : #(some->
226 : %
227 3 : str))
228 23 : (update :thread-id #(when (not= -1 %) %))))
229 :
230 :
231 4 : (defn links
232 : "Every Telegram link the user manages, template links first. A user manages a
233 : template or event they created or hold the admin participant role in, and an
234 : event whose template they manage. Without `manager-id` the result covers every
235 : link on the platform. Completed and cancelled events are kept for
236 : `past-event-days` after their end.
237 :
238 : Each link carries the resource summary under :resource-* keys, the caller's
239 : :resource-role (\"owner\" or \"admin\"), the latest undeleted bot post in that
240 : chat, and the last delivery failure."
241 8 : ([ds] (links ds nil))
242 : ([ds manager-id]
243 6 : (into
244 30 : (mapv row->link (query ds [template-links-sql manager-id nil]))
245 9 : (map row->link)
246 24 : (query ds [event-links-sql manager-id nil past-event-days]))))
247 :
248 :
249 4 : (defn resource-links
250 : "Links of one template or event (`kind` is :template or :event), in the same
251 : shape as `links` but without a caller role. An event's links are returned
252 : whatever its state."
253 : [ds kind resource-id]
254 4 : (mapv
255 2 : row->link
256 6 : (case kind
257 7 : :template (query ds [template-links-sql nil resource-id])
258 8 : :event (query ds [event-links-sql nil resource-id past-event-days]))))
259 :
260 :
261 4 : (defn unlinked-resources
262 : "Active templates and upcoming standalone events the user created that have no
263 : Telegram link yet, templates first. Only the creator can link a resource, and an
264 : event of a template follows its template's links, so neither admins' resources
265 : nor template events are listed."
266 : [ds creator-id]
267 4 : (into
268 20 : (mapv row->resource (query ds [unlinked-templates-sql creator-id unlinked-limit]))
269 6 : (map row->resource)
270 14 : (query ds [unlinked-events-sql creator-id unlinked-limit])))
|