Line data Source code
1 4 : (ns matchscore.db.postgres.telegram-daily-reminder
2 : (:require
3 : [matchscore.utils.db :as utils.db]
4 : [next.jdbc :as jdbc]))
5 :
6 :
7 4 : (defn due-event-ids
8 : "Unfinished events whose newest tracked main post in some destination predates the
9 : boundary: upcoming ones with a still-linked destination (to repost), and ones past
10 : their scheduled end (to remove the post). An ended event is retried for three days
11 : only; after that Telegram no longer lets the bot delete or the chat is unreachable."
12 : [ds boundary limit]
13 1 : (->>
14 2 : (jdbc/execute!
15 1 : ds
16 2 : ["SELECT DISTINCT posts.event_id AS uuid
17 : FROM (SELECT m.event_id,
18 : m.chat_id,
19 : m.thread_id,
20 : max(m.created_datetime) AS newest
21 : FROM telegram_chat_messages m
22 : WHERE m.deleted_at IS NULL
23 : AND m.event_id IS NOT NULL
24 : AND m.kind IN ('participants_post', 'draws_post')
25 : GROUP BY m.event_id, m.chat_id, m.thread_id) posts
26 : JOIN events e ON e.uuid = posts.event_id
27 : WHERE posts.newest < ?::timestamptz
28 : AND e.state NOT IN ('completed', 'cancelled')
29 : AND ((e.datetime_end <= now() AND e.datetime_end > now() - interval '3 days')
30 : OR (e.datetime_end > now()
31 : AND (EXISTS (SELECT 1
32 : FROM telegram_event_links l
33 : WHERE l.event_id = e.uuid
34 : AND l.chat_id = posts.chat_id
35 : AND l.thread_id = posts.thread_id)
36 : OR EXISTS (SELECT 1
37 : FROM telegram_template_links l
38 : WHERE l.template_id = e.template_id
39 : AND l.chat_id = posts.chat_id
40 : AND l.thread_id = posts.thread_id))))
41 : ORDER BY posts.event_id
42 : LIMIT ?"
43 3 : (str boundary)
44 2 : (long limit)])
45 9 : (mapv #(str (:uuid (utils.db/unqualify-keys %))))))
|