Line data Source code
1 4 : (ns matchscore.jobs.telegram-event-ended
2 : (:require
3 : [cats.core :as c]
4 : [cats.monad.either :as either]
5 : [cheshire.core :as json]
6 : [clojure.string :as str]
7 : [matchscore.application.scoreboard.get :as scoreboard-get]
8 : [matchscore.db.postgres.template-occurrence-exceptions :as occurrence-exception-db]
9 : [matchscore.domain.class.repository.postgres :as class-postgres-repo]
10 : [matchscore.domain.event.repository.postgres :as event-postgres-repo]
11 : [matchscore.domain.telegram-chat-message.repository.postgres
12 : :as
13 : telegram-chat-message-postgres-repo]
14 : [matchscore.domain.telegram-link.query :as telegram-link-query]
15 : [matchscore.domain.template-telegram-link.repository.postgres
16 : :as
17 : template-telegram-link-postgres-repo]
18 : [matchscore.domain.template.repository.postgres :as template-postgres-repo]
19 : [matchscore.jobs.telegram-cleanup :as telegram-cleanup]
20 : [matchscore.jobs.telegram-photo-reminder :as photo-reminder]
21 : [matchscore.telegram.event-lock :as event-lock]
22 : [matchscore.telegram.formatting :as formatting]
23 : [matchscore.telegram.localization :as l10n]
24 : [matchscore.telegram.payment-details-message :as payment-message]
25 : [next.jdbc :as jdbc]
26 : [next.jdbc.result-set :as rs]
27 : [taoensso.telemere :as t]
28 : [telegrambot-lib.core :as tbot])
29 : (:import
30 : (java.time Instant)))
31 :
32 :
33 4 : (defn- escape-html
34 : [s]
35 3 : (->
36 1 : s
37 4 : (str/replace "&" "&")
38 4 : (str/replace "<" "<")
39 4 : (str/replace ">" ">")
40 4 : (str/replace "\"" """)
41 1 : (str/replace "'" "'")))
42 :
43 :
44 4 : (defn- participant-name
45 : [participant]
46 10 : (or (get-in participant [:user :name]) (get-in participant [:team :name]) "?"))
47 :
48 :
49 4 : (def ^:private medals
50 28 : {0 "🥇"
51 : 1 "🥈"
52 : 2 "🥉"})
53 :
54 :
55 4 : (defn- format-scoreboard
56 : [draw-entries]
57 2 : (->>
58 1 : draw-entries
59 3 : (mapcat :results)
60 3 : (mapcat :results)
61 3 : (keep :participant)
62 2 : (map-indexed
63 1 : (fn [idx p]
64 9 : (let [prefix (get medals idx (str (inc idx) "."))]
65 9 : (str prefix " " (escape-html (participant-name p))))))
66 1 : (str/join "\n")))
67 :
68 :
69 4 : (defn- class-draw-entries
70 : [ctx event-uuid class-uuid]
71 1 : (try
72 8 : (let [result (scoreboard-get/execute! ctx event-uuid (str class-uuid))]
73 8 : (when (either/right? result) (c/extract result)))
74 : (catch Exception e
75 0 : (t/log!
76 : :warn
77 0 : {:message "Failed to build scoreboard for class"
78 0 : :class-uuid class-uuid
79 0 : :exception e})
80 : nil)))
81 :
82 :
83 4 : (defn- build-scoreboard-text
84 : [ctx event-uuid classes]
85 5 : (let [multi? (> (count classes) 1)
86 2 : blocks (keep
87 1 : (fn [class]
88 8 : (let [entries (class-draw-entries ctx event-uuid (:uuid class))
89 8 : board (when (seq entries) (format-scoreboard entries))]
90 5 : (when-not (str/blank? board)
91 13 : (if multi? (str "<b>" (escape-html (:name class)) "</b>\n" board) board))))
92 1 : classes)]
93 9 : (when (seq blocks) (str/join "\n\n" blocks))))
94 :
95 :
96 4 : (defn- safe-send!
97 : "Sends a Telegram message to the given chat/thread. Returns boolean indicating success."
98 : [bot chat-id thread-id {:keys [text reply-markup]}]
99 2 : (try
100 2 : (tbot/send-message
101 1 : bot
102 5 : (cond->
103 6 : {:chat_id chat-id
104 1 : :text text
105 : :parse_mode "HTML"}
106 1 : thread-id (assoc :message_thread_id thread-id)
107 1 : reply-markup (assoc :reply_markup (json/generate-string reply-markup))))
108 : true
109 : (catch Exception e
110 0 : (t/log!
111 : :warn
112 0 : {:message "Failed to send event-ended Telegram message (ignored)"
113 0 : :chat-id chat-id
114 0 : :thread-id thread-id
115 0 : :exception e})
116 : false)))
117 :
118 :
119 4 : (defn- option
120 : [options key default]
121 10 : (cond (contains? options key) (get options key)
122 6 : (contains? options (name key)) (get options (name key))
123 1 : :else default))
124 :
125 :
126 4 : (defn- note-line
127 : [locale note]
128 28 : (when-let [note (some->
129 1 : note
130 1 : str
131 1 : str/trim
132 1 : not-empty)]
133 7 : (l10n/t locale :telegram.notify/note (escape-html note))))
134 :
135 :
136 4 : (defn- notification-text
137 : [header datetime locale note]
138 13 : (str/join "\n" (remove str/blank? [header datetime (note-line locale note)])))
139 :
140 :
141 4 : (defn- participant-recipients
142 : [pg resource-column resource-id]
143 6 : (let [membership-table (if (= resource-column :event)
144 : "event_participants"
145 : "template_participants")
146 5 : id-column (if (= resource-column :event) "event_id" "template_id")]
147 2 : (jdbc/execute!
148 1 : pg
149 13 : [(str
150 : "SELECT DISTINCT u.uuid AS user_id, u.telegram_id, u.settings AS user_settings "
151 : "FROM "
152 1 : membership-table
153 : " membership "
154 : "JOIN participants p ON p.uuid = membership.participant_id "
155 : "JOIN users u ON u.uuid = p.user_id "
156 1 : "WHERE membership." id-column
157 : " = ?::uuid " "AND membership.status <> 'withdrawn' "
158 : "AND u.telegram_id IS NOT NULL " "AND u.telegram_bot_access_status = 'confirmed'")
159 3 : (str resource-id)]
160 3 : {:builder-fn rs/as-unqualified-kebab-maps})))
161 :
162 :
163 4 : (defn- recipient-locale
164 : [resource recipient]
165 2 : (formatting/normalize-locale
166 4 : (or
167 6 : (get-in recipient [:user-settings :language])
168 0 : (get-in recipient [:user-settings "language"])
169 0 : (:language resource))))
170 :
171 :
172 4 : (defn- notify-participants!
173 : [system-components resource-column resource build-message]
174 2 : (let [{:keys [pg telegram-bot]} system-components
175 7 : recipients (participant-recipients pg resource-column (:uuid resource))]
176 3 : (reduce
177 1 : (fn [sent recipient]
178 7 : (let [message (build-message (recipient-locale resource recipient))]
179 1 : (if
180 5 : (and
181 7 : (not (str/blank? (:text message)))
182 6 : (safe-send! telegram-bot (:telegram-id recipient) nil message))
183 2 : (inc sent)
184 0 : sent)))
185 : 0
186 1 : recipients)))
187 :
188 :
189 4 : (defn- notify-linked-groups!
190 : "Resolves the Telegram links for the event and sends the message produced by
191 : (build-message ctx event locale) to each linked group. A nil/blank :text skips
192 : sending. Does not record the message in telegram_chat_messages so it is not
193 : deleted by cleanup-event-messages-job. Returns {:status :ok :count <sent>}."
194 : [system-components event-uuid log-label build-message]
195 2 : (let [{:keys [repos telegram-bot]} system-components
196 3 : ctx {:repos repos}
197 3 : event-repo (:event-postgres-repo repos)
198 5 : event (event-postgres-repo/find-by-id event-repo event-uuid nil)
199 7 : links (when event (telegram-link-query/resolve-links-for-event ctx event))]
200 6 : (cond (not event)
201 0 : (do
202 0 : (t/log! :warn (str "TG " log-label ": event not found, skipping " event-uuid))
203 0 : {:status :ok
204 : :count 0})
205 :
206 3 : (empty? links)
207 1 : (do
208 98 : (t/log! :info (str "TG " log-label ": no linked groups for event " event-uuid))
209 5 : {:status :ok
210 : :count 0})
211 :
212 : :else
213 4 : (let [locale (l10n/resource-locale event)
214 5 : message (build-message ctx event locale)]
215 6 : (if (str/blank? (:text message))
216 0 : (do
217 0 : (t/log! :info (str "TG " log-label ": no text to send for event " event-uuid))
218 0 : {:status :ok
219 : :count 0})
220 4 : (let [sent (reduce
221 1 : (fn [acc {:keys [chat-id thread-id]}]
222 9 : (if (safe-send! telegram-bot chat-id thread-id message) (inc acc) acc))
223 : 0
224 1 : links)]
225 92 : (t/log!
226 : :info
227 6 : (str
228 1 : "TG " log-label
229 1 : ": sent to " sent
230 2 : "/" (count links)
231 1 : " group(s) for event " event-uuid))
232 4 : {:status :ok
233 1 : :count sent}))))))
234 :
235 :
236 8 : (declare send-results-message!)
237 :
238 :
239 4 : (defn- same-destination?
240 : [chat-id thread-id message]
241 21 : (and (= chat-id (:chat-id message)) (= (or thread-id -1) (or (:thread-id message) -1))))
242 :
243 :
244 4 : (defn- send-event-completed-under-lock!
245 : [{:keys [repos telegram-bot config]} event-uuid]
246 4 : (let [ctx {:repos repos}
247 7 : event (event-postgres-repo/find-by-id (:event-postgres-repo repos) event-uuid nil)
248 3 : msg-repo (:telegram-chat-message-postgres-repo repos)
249 7 : links (when event (telegram-link-query/resolve-links-for-event ctx event))]
250 4 : (cond
251 3 : (not event)
252 1 : (do
253 96 : (t/log! :warn (str "TG event-ended: event not found, skipping " event-uuid))
254 5 : {:status :ok
255 : :count 0})
256 :
257 6 : (not= "completed" (:state event))
258 0 : (do
259 0 : (t/log! :info (str "TG event-ended: event is no longer completed, skipping " event-uuid))
260 0 : {:status :skipped
261 : :count 0})
262 :
263 3 : (empty? links)
264 1 : (do
265 96 : (t/log! :info (str "TG event-ended: no linked groups for event " event-uuid))
266 5 : {:status :ok
267 : :count 0})
268 :
269 : :else
270 4 : (let [locale (l10n/resource-locale event)
271 6 : classes (class-postgres-repo/find-by-event-id (:class-postgres-repo repos) event-uuid)
272 5 : scoreboard (build-scoreboard-text ctx event-uuid classes)
273 5 : text (let [header (l10n/t locale :telegram.notify/event-ended)]
274 8 : (if scoreboard (str header "\n\n" scoreboard) header))
275 : reply-markup
276 11 : (formatting/build-keyboard event [] locale (get-in config [:telegram :bot-username]))
277 : previous
278 4 : (telegram-chat-message-postgres-repo/find-undeleted-for-event msg-repo event-uuid)
279 : outcome
280 2 : (reduce
281 1 : (fn
282 : [{:keys [sent failed]
283 : :as acc}
284 : {:keys [chat-id thread-id]}]
285 4 : (let [final-post (some
286 4 : #(and
287 8 : (contains? #{"event_completed" "results_post"} (:kind %))
288 4 : (same-destination? chat-id thread-id %))
289 1 : previous)]
290 2 : (cond
291 2 : final-post acc
292 : :else
293 5 : (if-let [message-id
294 7 : (send-results-message! telegram-bot chat-id thread-id text reply-markup)]
295 3 : (let [recorded (telegram-chat-message-postgres-repo/record!
296 1 : msg-repo
297 11 : {:chat-id chat-id
298 1 : :thread-id thread-id
299 1 : :message-id message-id
300 : :sender-kind "bot"
301 : :kind "event_completed"
302 1 : :text text
303 3 : :event-id (:uuid event)})]
304 4 : (when (either/left? recorded)
305 0 : (throw
306 0 : (ex-info
307 : "Failed to record completed-event Telegram message"
308 0 : {:event-id event-uuid
309 0 : :chat-id chat-id
310 0 : :error (c/extract recorded)})))
311 6 : (assoc acc :sent (inc sent)))
312 6 : (assoc acc :failed (inc failed))))))
313 5 : {:sent 0
314 : :failed 0}
315 1 : links)]
316 92 : (t/log!
317 : :info
318 5 : (str
319 3 : "TG event-ended: sent to " (:sent outcome)
320 2 : "/" (count links)
321 1 : " group(s) for event " event-uuid))
322 6 : (when (pos? (:failed outcome))
323 1 : (throw
324 3 : (ex-info
325 : "Completed-event Telegram delivery requires retry"
326 5 : {:event-id event-uuid
327 3 : :sent (:sent outcome)
328 3 : :failed (:failed outcome)})))
329 4 : {:status :ok
330 3 : :count (:sent outcome)}))))
331 :
332 :
333 4 : (defn send-event-completed-job
334 : "Goose job: idempotently sends an event-completed scoreboard to linked groups.
335 : A previously delivered completion or newer results post for the same destination
336 : suppresses duplicates."
337 : [{:keys [pg repos]
338 : :as system-components}
339 : event-uuid]
340 4 : (let [msg-repo (:telegram-chat-message-postgres-repo repos)]
341 3 : (event-lock/with-lock
342 7 : (or pg (:ds msg-repo))
343 1 : event-uuid
344 4 : #(send-event-completed-under-lock! system-components event-uuid))))
345 :
346 :
347 4 : (defn complete-event-notification-job
348 : "Immediate completion workflow: under one event lock, ignore stale work, remove
349 : transient event messages, then send the tracked final scoreboard. This avoids the
350 : delayed Redis scheduler dependency that previously separated cleanup from delivery."
351 : [{:keys [pg repos]
352 : :as system-components}
353 : event-uuid]
354 4 : (let [msg-repo (:telegram-chat-message-postgres-repo repos)]
355 2 : (event-lock/with-lock
356 7 : (or pg (:ds msg-repo))
357 1 : event-uuid
358 1 : (fn []
359 8 : (let [event (event-postgres-repo/find-by-id (:event-postgres-repo repos) event-uuid nil)
360 3 : previous (when event
361 2 : (telegram-chat-message-postgres-repo/find-undeleted-for-event
362 1 : msg-repo
363 1 : event-uuid))]
364 6 : (cond (not event)
365 0 : {:status :ok
366 : :count 0}
367 :
368 6 : (not= "completed" (:state event))
369 0 : {:status :skipped
370 : :count 0}
371 :
372 9 : (some #(= "results_post" (:kind %)) previous)
373 1 : (do
374 92 : (t/log!
375 : :info
376 3 : (str
377 : "TG event-ended: newer results already exist; skipping stale completion "
378 1 : event-uuid))
379 7 : {:status :skipped
380 : :reason :newer-results
381 : :count 0})
382 :
383 : :else
384 0 : (let [cleanup-results (telegram-cleanup/remove-messages!
385 0 : system-components
386 0 : event
387 : ;; Payment posts stay: players pay after the game. They
388 : ;; close themselves once everyone has paid.
389 0 : (remove
390 0 : #(contains?
391 0 : (into
392 0 : #{"event_summary" "event_completed" "results_post"
393 0 : photo-reminder/message-kind}
394 0 : payment-message/post-kinds)
395 0 : (:kind %))
396 0 : previous))
397 0 : failed (count (filter #(= :failed (:status %)) cleanup-results))]
398 0 : (when (pos? failed)
399 0 : (throw
400 0 : (ex-info
401 : "Telegram completion cleanup requires retry"
402 0 : {:event-id event-uuid
403 0 : :failed failed})))
404 0 : (send-event-completed-under-lock! system-components event-uuid))))))))
405 :
406 :
407 4 : (defn- safe-delete-message!
408 : "Deletes a Telegram message, ignoring failures. Returns true on success, false otherwise."
409 : [bot chat-id message-id]
410 2 : (try
411 2 : (tbot/delete-message
412 1 : bot
413 4 : {:chat_id chat-id
414 1 : :message_id message-id})
415 : true
416 : (catch Exception e
417 0 : (t/log!
418 : :warn
419 0 : {:message "Failed to delete previous results message (ignored)"
420 0 : :chat-id chat-id
421 0 : :msg-id message-id
422 0 : :exception e})
423 : false)))
424 :
425 :
426 4 : (defn- send-results-message!
427 : "Sends a results message to one chat/thread.
428 : Returns the new Telegram message-id on success, nil otherwise."
429 : [bot chat-id thread-id text reply-markup]
430 2 : (try
431 1 : (->
432 2 : (tbot/send-message
433 1 : bot
434 9 : (cond->
435 6 : {:chat_id chat-id
436 1 : :text text
437 : :parse_mode "HTML"}
438 5 : thread-id (assoc :message_thread_id thread-id)
439 5 : reply-markup (assoc :reply_markup (json/generate-string reply-markup))))
440 4 : (get-in [:result :message_id]))
441 : (catch Exception e
442 92 : (t/log!
443 : :warn
444 6 : {:message "Failed to send results Telegram message (ignored)"
445 1 : :chat-id chat-id
446 1 : :thread-id thread-id
447 1 : :exception e})
448 : nil)))
449 :
450 :
451 4 : (defn- delete-previous-results!
452 : "Deletes previously-posted results messages for one link's chat/thread and marks the
453 : audit rows of the messages that were actually deleted, so each group only ever shows the
454 : latest results and rows are not marked deleted for messages that failed to delete."
455 : [bot repo previous chat-id thread-id]
456 4 : (let [prior (filter
457 21 : #(and (= chat-id (:chat-id %)) (= (or thread-id -1) (or (:thread-id %) -1)))
458 1 : previous)
459 11 : deleted (filter #(safe-delete-message! bot chat-id (:message-id %)) prior)]
460 5 : (when (seq deleted)
461 7 : (telegram-chat-message-postgres-repo/mark-deleted! repo (mapv :uuid deleted)))))
462 :
463 :
464 4 : (defn- build-results-text
465 : "Builds the results message: a header with the event name and date, then the scoreboard."
466 : [locale event scoreboard]
467 16 : (let [name (some->
468 3 : (:name event)
469 1 : str
470 1 : str/trim)
471 4 : title (if (str/blank? name)
472 0 : (l10n/t locale :telegram.notify/results-header-no-event)
473 7 : (l10n/t locale :telegram.notify/results-header (escape-html name)))
474 3 : detail (formatting/event-datetime-detail event)
475 8 : date (when detail (l10n/t locale :telegram.notify/results-date detail))
476 9 : header (cond-> title date (str "\n" date))]
477 5 : (str header "\n\n" scoreboard)))
478 :
479 :
480 4 : (defn- send-event-results-updated-under-lock!
481 : "Goose job: re-posts the current scoreboard to every Telegram group linked to the event.
482 : Deletes the previously-posted results message (if any) in each group before sending a
483 : fresh one, so linked groups only ever see the latest results without a stream of
484 : 'results updated' notices. The message is headed with the event name and date. Intended
485 : for manual triggering after an event is completed but its results have since changed.
486 : Records each post in telegram_chat_messages (kind 'results_post') so the next run can
487 : delete it."
488 : [system-components event-uuid]
489 2 : (let [{:keys [repos telegram-bot config]} system-components
490 3 : ctx {:repos repos}
491 3 : event-repo (:event-postgres-repo repos)
492 3 : msg-repo (:telegram-chat-message-postgres-repo repos)
493 5 : event (event-postgres-repo/find-by-id event-repo event-uuid nil)
494 7 : links (when event (telegram-link-query/resolve-links-for-event ctx event))]
495 3 : (cond
496 3 : (not event)
497 0 : (do
498 0 : (t/log! :warn (str "TG results-updated: event not found, skipping " event-uuid))
499 0 : {:status :ok
500 : :count 0})
501 :
502 3 : (empty? links)
503 0 : (do
504 0 : (t/log! :info (str "TG results-updated: no linked groups for event " event-uuid))
505 0 : {:status :ok
506 : :count 0})
507 :
508 : :else
509 4 : (let [locale (l10n/resource-locale event)
510 2 : classes (class-postgres-repo/find-by-event-id
511 3 : (:class-postgres-repo repos)
512 1 : event-uuid)
513 5 : scoreboard (build-scoreboard-text ctx event-uuid classes)]
514 4 : (if (str/blank? scoreboard)
515 0 : (do
516 0 : (t/log! :info (str "TG results-updated: no scoreboard for event " event-uuid))
517 0 : {:status :ok
518 : :count 0})
519 6 : (let [text (build-results-text locale event scoreboard)
520 2 : reply-markup (formatting/build-keyboard
521 1 : event
522 1 : []
523 1 : locale
524 6 : (get-in config [:telegram :bot-username]))
525 : previous
526 1 : (->>
527 4 : (telegram-chat-message-postgres-repo/find-undeleted-for-event msg-repo event-uuid)
528 10 : (filter #(contains? #{"event_completed" "results_post"} (:kind %))))
529 : sent
530 3 : (reduce
531 1 : (fn [acc {:keys [chat-id thread-id]}]
532 5 : (if-let [msg-id
533 7 : (send-results-message! telegram-bot chat-id thread-id text reply-markup)]
534 3 : (let [recorded (telegram-chat-message-postgres-repo/record!
535 1 : msg-repo
536 11 : {:chat-id chat-id
537 1 : :thread-id thread-id
538 1 : :message-id msg-id
539 : :sender-kind "bot"
540 : :kind "results_post"
541 1 : :text text
542 3 : :event-id (:uuid event)})]
543 4 : (when (either/left? recorded)
544 0 : (throw
545 0 : (ex-info
546 : "Failed to record results Telegram message"
547 0 : {:event-id event-uuid
548 0 : :chat-id chat-id
549 0 : :error (c/extract recorded)})))
550 : ;; Only remove the prior results once the
551 : ;; replacement exists in both Telegram and the
552 : ;; DB.
553 7 : (delete-previous-results! telegram-bot msg-repo previous chat-id thread-id)
554 2 : (inc acc))
555 0 : acc))
556 : 0
557 1 : links)]
558 92 : (t/log!
559 : :info
560 5 : (str
561 1 : "TG results-updated: sent to " sent
562 2 : "/" (count links)
563 1 : " group(s) for event " event-uuid))
564 4 : {:status :ok
565 1 : :count sent}))))))
566 :
567 :
568 4 : (defn send-event-results-updated-job
569 : "Re-posts final results while serialized with completion delivery. A successful
570 : results post replaces both the previous results post and the automatic completion
571 : post for the same Telegram destination."
572 : [{:keys [pg repos]
573 : :as system-components}
574 : event-uuid]
575 4 : (let [msg-repo (:telegram-chat-message-postgres-repo repos)]
576 3 : (event-lock/with-lock
577 7 : (or pg (:ds msg-repo))
578 1 : event-uuid
579 4 : #(send-event-results-updated-under-lock! system-components event-uuid))))
580 :
581 :
582 4 : (defn send-event-cancelled-job
583 : "Goose job: sends an 'event cancelled' notification to every Telegram group linked to the
584 : event, and optionally to its participants. Like send-event-completed-job but without
585 : the scoreboard. Does not record the message in telegram_chat_messages so it is not
586 : deleted by cleanup-event-messages-job."
587 5 : ([system-components event-uuid] (send-event-cancelled-job system-components event-uuid {}))
588 : ([system-components event-uuid options]
589 2 : (let [{:keys [repos]} system-components
590 7 : event (event-postgres-repo/find-by-id (:event-postgres-repo repos) event-uuid nil)
591 5 : note (option options :note nil)
592 6 : notify-linked-chats? (boolean (option options :notify-linked-chats true))
593 6 : notify-participants? (boolean (option options :notify-participants false))
594 1 : build-message (fn [locale]
595 7 : (let [name (if (str/blank? (:name event))
596 0 : (l10n/t locale :telegram.notify/default-event)
597 3 : (:name event))
598 3 : header (l10n/t
599 1 : locale
600 : :telegram.notify/event-cancelled
601 3 : (escape-html name))
602 4 : datetime (formatting/format-event-datetime locale event)]
603 8 : {:text (notification-text header datetime locale note)}))
604 7 : group-result (if (and event notify-linked-chats?)
605 3 : (notify-linked-groups!
606 1 : system-components
607 1 : event-uuid
608 : "event-cancelled"
609 1 : (fn [_ctx _event locale]
610 3 : (build-message locale)))
611 5 : {:status :ok
612 : :count 0})
613 8 : participant-count (if (and event notify-participants?)
614 6 : (notify-participants! system-components :event event build-message)
615 : 0)]
616 4 : {:status :ok
617 5 : :count (+ (:count group-result) participant-count)})))
618 :
619 :
620 4 : (defn send-template-occurrence-skipped-job
621 : "Goose job: announces a persisted skipped recurrence occurrence to the selected
622 : template audiences. If the skip has been undone before the job runs, it is ignored."
623 : [system-components template-id exception-id]
624 2 : (let [{:keys [pg repos telegram-bot]} system-components
625 5 : exception (occurrence-exception-db/find-by-id pg template-id exception-id)
626 3 : template (template-postgres-repo/find-by-id
627 3 : (:template-postgres-repo repos)
628 1 : template-id
629 : nil)]
630 8 : (if-not (and exception template)
631 0 : {:status :ok
632 : :count 0}
633 6 : (let [start (Instant/parse (:occurrence-start exception))
634 5 : resource (assoc template
635 1 : :datetime-start start
636 1 : :datetime-end (.plusMillis
637 : start
638 8 : (long (or (:duration template) 0))))
639 1 : build-message (fn [locale]
640 7 : (let [name (if (str/blank? (:name template))
641 0 : (l10n/t locale :telegram.notify/default-event)
642 3 : (:name template))
643 3 : header (l10n/t
644 1 : locale
645 : :telegram.notify/occurrence-skipped
646 3 : (escape-html name))
647 4 : datetime (formatting/format-event-datetime locale resource)]
648 2 : {:text
649 8 : (notification-text header datetime locale (:note exception))}))
650 5 : links (when (:notify-linked-chats exception)
651 2 : (template-telegram-link-postgres-repo/find-by-template-id
652 3 : (:template-telegram-link-postgres-repo repos)
653 1 : template-id))
654 5 : group-message (build-message (l10n/resource-locale template))
655 3 : group-count (reduce
656 1 : (fn [sent {:keys [chat-id thread-id]}]
657 7 : (if (safe-send! telegram-bot chat-id thread-id group-message)
658 2 : (inc sent)
659 0 : sent))
660 : 0
661 1 : links)
662 5 : participant-count (if (:notify-participants exception)
663 0 : (notify-participants!
664 0 : system-components
665 : :template
666 0 : template
667 0 : build-message)
668 : 0)]
669 6 : {:status :ok
670 3 : :count (+ group-count participant-count)
671 1 : :group-count group-count
672 1 : :participant-count participant-count}))))
|