Line data Source code
1 4 : (ns matchscore.application.telegram-links
2 : "Managing a Telegram link from the app instead of from the chat: disconnecting
3 : it and sending its main post again. The caller is authorized on the template
4 : or event the link belongs to, which replaces the chat-admin check the bot
5 : commands make."
6 : (:require
7 : [cats.core :as c]
8 : [cats.monad.either :as either]
9 : [matchscore.domain.event-telegram-link.repository.postgres :as event-telegram-link-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.template-telegram-link.repository.postgres
15 : :as
16 : template-telegram-link-postgres-repo]
17 : [matchscore.jobs.scheduler :as scheduler]
18 : [matchscore.jobs.telegram-commands :as telegram-commands-jobs]
19 : [matchscore.jobs.telegram-notifications :as telegram-notifications-jobs]
20 : [matchscore.utils.errors :as utils.errors]))
21 :
22 :
23 16 : (def ^:private main-post-kinds #{"participants_post" "draws_post"})
24 :
25 :
26 14 : (defn- open? [event] (not (contains? #{"completed" "cancelled"} (:state event))))
27 :
28 :
29 4 : (defn find-link
30 : "Finds a link of the given kind (:template or :event) by its UUID, or nil."
31 : [repos kind link-uuid]
32 3 : (case kind
33 2 : :template (template-telegram-link-postgres-repo/find-by-uuid
34 3 : (:template-telegram-link-postgres-repo repos)
35 1 : link-uuid)
36 2 : :event (event-telegram-link-postgres-repo/find-by-uuid
37 3 : (:event-telegram-link-postgres-repo repos)
38 1 : link-uuid)))
39 :
40 :
41 4 : (defn- unlink-template-link!
42 : [{:keys [repos goose]} {:keys [template-id chat-id thread-id]}]
43 9 : (c/mlet [_ (template-telegram-link-postgres-repo/delete!
44 3 : (:template-telegram-link-postgres-repo repos)
45 1 : template-id
46 1 : chat-id
47 1 : thread-id)]
48 : ;; Events that relied on this link lose the chat too.
49 27 : (doseq [event-id (telegram-chat-message-postgres-repo/event-ids-with-main-posts
50 3 : (:telegram-chat-message-postgres-repo repos)
51 1 : template-id
52 1 : chat-id
53 1 : thread-id)]
54 3 : (scheduler/schedule-job
55 1 : goose
56 : `telegram-notifications-jobs/remove-unlinked-posts-job
57 4 : {:args [event-id]}))
58 3 : (c/return nil)))
59 :
60 :
61 4 : (defn- unlink-event-link!
62 : [{:keys [repos goose]} {:keys [event-id chat-id thread-id]}]
63 9 : (c/mlet [_ (event-telegram-link-postgres-repo/delete!
64 3 : (:event-telegram-link-postgres-repo repos)
65 1 : event-id
66 1 : chat-id
67 1 : thread-id)]
68 : ;; Take the list out of the chat it no longer belongs to.
69 3 : (scheduler/schedule-job
70 1 : goose
71 : `telegram-notifications-jobs/remove-unlinked-posts-job
72 4 : {:args [event-id]})
73 3 : (c/return nil)))
74 :
75 :
76 4 : (defn unlink!
77 : "Deletes the link, queues removal of the main posts it left in the chat, and
78 : queues the bot's notice to that chat, so the chat sees the same as after
79 : /unlink_template and /unlink_event. `actor-id` is the user who disconnected it.
80 : Returns Either<Error, link>."
81 : [{:keys [goose]
82 : :as ctx}
83 : kind
84 : {:keys [chat-id thread-id]
85 : :as link}
86 : actor-id]
87 10 : (c/mlet [_ (case kind
88 4 : :template (unlink-template-link! ctx link)
89 4 : :event (unlink-event-link! ctx link))]
90 3 : (scheduler/schedule-job
91 1 : goose
92 : `telegram-commands-jobs/announce-unlinked-job
93 12 : {:args [{:kind (name kind)
94 3 : :resource-id (case kind
95 3 : :template (:template-id link)
96 3 : :event (:event-id link))
97 1 : :chat-id chat-id
98 1 : :thread-id thread-id
99 1 : :user-id actor-id}]})
100 3 : (c/return link)))
101 :
102 :
103 4 : (defn- posted-kind
104 : "Kind of the event's current main post in the link's chat, or nil without one."
105 : [repos event {:keys [chat-id thread-id]}]
106 3 : (->>
107 2 : (telegram-chat-message-postgres-repo/find-undeleted-for-event
108 3 : (:telegram-chat-message-postgres-repo repos)
109 3 : (:uuid event))
110 3 : (filter
111 21 : #(and (main-post-kinds (:kind %)) (= chat-id (:chat-id %)) (= thread-id (:thread-id %))))
112 3 : (sort-by :uuid)
113 1 : last
114 : :kind))
115 :
116 :
117 4 : (defn- repost-events
118 : "Open events whose main post the link feeds. A template link covers the events
119 : that already have a post in its chat; without any, its active event or else
120 : its next scheduled one."
121 : [repos kind {:keys [event-id template-id chat-id thread-id]}]
122 3 : (case kind
123 2 : :event (filterv
124 1 : open?
125 2 : (keep
126 1 : identity
127 8 : [(event-postgres-repo/find-by-id (:event-postgres-repo repos) event-id {})]))
128 :
129 3 : :template (let [events (filterv
130 1 : open?
131 2 : (event-postgres-repo/find-by-template-id
132 3 : (:event-postgres-repo repos)
133 1 : template-id
134 1 : {}))
135 2 : posted (set
136 2 : (telegram-chat-message-postgres-repo/event-ids-with-main-posts
137 3 : (:telegram-chat-message-postgres-repo repos)
138 1 : template-id
139 1 : chat-id
140 1 : thread-id))]
141 8 : (or
142 14 : (not-empty (filterv #(contains? posted (str (:uuid %))) events))
143 25 : (some->>
144 1 : events
145 12 : (filter #(contains? #{"active" "scheduled"} (:state %)))
146 20 : (sort-by (juxt #(if (= "active" (:state %)) 0 1) #(inst-ms (:datetime-start %))))
147 1 : first
148 1 : vector)
149 : []))))
150 :
151 :
152 4 : (defn repost!
153 : "Queues a fresh main post for the events the link feeds. An event whose current
154 : post in that chat shows the draws gets the draws again; any other gets the
155 : participant list. The post jobs refresh every chat linked to the event.
156 : Returns Either<Error, {:event-uuids [...]}>."
157 : [{:keys [repos goose]} kind link]
158 6 : (let [events (repost-events repos kind link)]
159 4 : (if (empty? events)
160 2 : (either/left
161 3 : (utils.errors/build-error
162 : :validation
163 5 : {:code :error.telegram-link/nothing-to-post
164 : :message "There is no open event to post for this chat."}))
165 1 : (do
166 26 : (doseq [event events]
167 2 : (scheduler/schedule-job
168 1 : goose
169 10 : (if (= "draws_post" (posted-kind repos event link))
170 : `telegram-notifications-jobs/post-draws-job
171 : `telegram-notifications-jobs/post-participants-job)
172 8 : {:args [(str (:uuid event))]}))
173 11 : (either/right {:event-uuids (mapv (comp str :uuid) events)})))))
|