Line data Source code
1 4 : (ns matchscore.domain.event-telegram-link.repository.postgres
2 : (:require
3 : [cats.monad.either :as either]
4 : [honey.sql :as sql]
5 : [matchscore.utils.db :as utils.db]
6 : [matchscore.utils.errors :as utils.errors]
7 : [next.jdbc :as jdbc]))
8 :
9 :
10 4 : (defn- row->link
11 : [row]
12 12 : (when row
13 16 : (let [r (utils.db/unqualify-keys row)]
14 72 : {:uuid (str (:uuid r))
15 48 : :event-id (str (or (:event-id r) (:event_id r)))
16 40 : :chat-id (or (:chat-id r) (:chat_id r))
17 39 : :chat-title (or (:chat-title r) (:chat_title r))
18 70 : :thread-id (let [t (or (:thread-id r) (:thread_id r))] (when (not= t -1) t))
19 48 : :linked-by-user-id (str (or (:linked-by-user-id r) (:linked_by_user_id r)))
20 39 : :last-message-id (or (:last-message-id r) (:last_message_id r))
21 40 : :created-datetime (or (:created-datetime r) (:created_datetime r))
22 40 : :updated-datetime (or (:updated-datetime r) (:updated_datetime r))
23 39 : :last-error (or (:last-error r) (:last_error r))
24 39 : :last-error-code (or (:last-error-code r) (:last_error_code r))
25 39 : :last-error-at (or (:last-error-at r) (:last_error_at r))})))
26 :
27 :
28 4 : (defn- same-chat-thread?
29 : [link chat-id thread-id]
30 22 : (and (= (:chat-id link) chat-id) (= (or (:thread-id link) -1) (or thread-id -1))))
31 :
32 :
33 4 : (defn- already-linked-error
34 : [event-id existing-link]
35 3 : (utils.errors/build-error
36 : :validation
37 6 : {:code :error.event-telegram-link/already-linked
38 : :message "Event is already linked to a Telegram chat or forum topic"
39 4 : :context {:event-id event-id
40 1 : :existing-link existing-link}}))
41 :
42 :
43 4 : (defprotocol
44 : EventTelegramLinkPostgresRepository
45 : (find-by-uuid
46 : [this link-uuid]
47 : "Find one link by its own UUID. Returns link map or nil.")
48 : (find-by-event-id
49 : [this event-id]
50 : "Find all links for an event UUID. Returns vec of link maps.")
51 : (find-by-chat-and-thread
52 : [this event-id chat-id thread-id]
53 : "Find a specific link row. Returns link map or nil.")
54 : (upsert!
55 : [this link]
56 : "Insert or update link. Returns Either<Error, link>.")
57 : (delete!
58 : [this event-id chat-id thread-id]
59 : "Delete the link for this (event, chat, thread). Returns Either<Error, nil>.")
60 : (update-last-message-id!
61 : [this link-uuid message-id]
62 : "Update last_message_id after a successful post and clear the recorded
63 : delivery failure. Returns Either<Error, link>.")
64 : (record-delivery-failure!
65 : [this link-uuid failure]
66 : "Store the last failed attempt to post to this link. `failure` is
67 : {:code <telegram error code or nil> :description <text>}.
68 : Returns Either<Error, link>.")
69 : (record-delivery-failure-by-chat-id!
70 : [this chat-id failure]
71 : "Store a delivery failure on every link of the chat, whatever its topic, for
72 : when the bot loses the whole chat. `failure` is {:code :description}.
73 : Returns Either<Error, nil>.")
74 : (clear-access-failure-by-chat-id!
75 : [this chat-id]
76 : "Clear failures that say the bot had no access to the chat (removed, or not
77 : allowed to post) on every link of the chat, for when the bot gets access
78 : back. Other failures stay. Returns Either<Error, nil>.")
79 : (update-chat-title-by-chat-id!
80 : [this chat-id new-title]
81 : "Update chat_title for all links with this chat-id. Returns Either<Error, nil>."))
82 :
83 :
84 4 : (defrecord
85 : PostgresEventTelegramLinkRepository
86 : [ds]
87 : EventTelegramLinkPostgresRepository
88 :
89 : (find-by-uuid
90 : [_ link-uuid]
91 2 : (->>
92 2 : (sql/format
93 6 : {:select [:*]
94 2 : :from [:telegram_event_links]
95 7 : :where [:= :uuid [:cast link-uuid :uuid]]})
96 3 : (jdbc/execute! ds)
97 1 : first
98 1 : row->link))
99 :
100 : (find-by-event-id
101 : [_ event-id]
102 4 : (->>
103 8 : (sql/format
104 24 : {:select [:*]
105 8 : :from [:telegram_event_links]
106 28 : :where [:= :event-id [:cast event-id :uuid]]})
107 12 : (jdbc/execute! ds)
108 8 : (mapv row->link)))
109 :
110 : (find-by-chat-and-thread
111 : [_ event-id chat-id thread-id]
112 27 : (let [effective-thread (or thread-id -1)]
113 8 : (->>
114 8 : (sql/format
115 24 : {:select [:*]
116 8 : :from [:telegram_event_links]
117 8 : :where [:and
118 28 : [:= :event-id [:cast event-id :uuid]]
119 16 : [:= :chat-id chat-id]
120 16 : [:= :thread-id effective-thread]]})
121 12 : (jdbc/execute! ds)
122 4 : first
123 4 : row->link)))
124 :
125 : (upsert!
126 : [_this {:keys [event-id chat-id thread-id linked-by-user-id chat-title]}]
127 4 : (try
128 16 : (jdbc/with-transaction
129 4 : [tx ds]
130 16 : (let [tx-repo (->PostgresEventTelegramLinkRepository tx)
131 23 : effective-thread (or thread-id -1)
132 24 : existing-link (find-by-chat-and-thread tx-repo event-id chat-id thread-id)
133 4 : existing-conflict (->>
134 16 : (find-by-event-id tx-repo event-id)
135 17 : (remove #(same-chat-thread? % chat-id thread-id))
136 4 : first)]
137 22 : (cond existing-link (either/left (already-linked-error event-id existing-link))
138 :
139 10 : existing-conflict (either/left (already-linked-error event-id existing-conflict))
140 :
141 : :else
142 32 : (let [query {:insert-into :telegram_event_links
143 28 : :values [(cond->
144 36 : {:event_id [:cast event-id :uuid]
145 4 : :chat_id chat-id
146 4 : :thread_id effective-thread
147 16 : :linked_by_user_id [:cast linked-by-user-id :uuid]}
148 12 : chat-title (assoc :chat_title chat-title))]
149 16 : :on-conflict [:event-id :chat-id :thread-id]
150 24 : :do-update-set {:fields [:linked-by-user-id
151 : :chat-title
152 : :updated-datetime]}
153 8 : :returning [:*]}
154 4 : result (->>
155 12 : (sql/format query)
156 12 : (jdbc/execute! tx)
157 4 : first)]
158 20 : (either/right (row->link result))))))
159 : (catch Exception e
160 0 : (either/left
161 0 : (utils.errors/build-error
162 : :postgres
163 0 : {:message (.getMessage e)
164 0 : :context {:table :telegram_event_links
165 : :op :upsert}})))))
166 :
167 : (delete!
168 : [_ event-id chat-id thread-id]
169 2 : (try
170 12 : (let [effective-thread (or thread-id -1)]
171 4 : (jdbc/execute!
172 2 : ds
173 4 : (sql/format
174 8 : {:delete-from :telegram_event_links
175 4 : :where [:and
176 14 : [:= :event-id [:cast event-id :uuid]]
177 8 : [:= :chat-id chat-id]
178 8 : [:= :thread-id effective-thread]]}))
179 6 : (either/right nil))
180 : (catch Exception e
181 0 : (either/left
182 0 : (utils.errors/build-error
183 : :postgres
184 0 : {:message (.getMessage e)
185 0 : :context {:table :telegram_event_links
186 : :op :delete}})))))
187 :
188 : (update-last-message-id!
189 : [_ link-uuid message-id]
190 3 : (try
191 6 : (let [result (->>
192 6 : (sql/format
193 18 : {:update :telegram_event_links
194 27 : :set {:last_message_id message-id
195 : :last_error nil
196 : :last_error_code nil
197 : :last_error_at nil}
198 21 : :where [:= :uuid [:cast link-uuid :uuid]]
199 6 : :returning [:*]})
200 9 : (jdbc/execute! ds)
201 3 : first)]
202 15 : (either/right (row->link result)))
203 : (catch Exception e
204 0 : (either/left
205 0 : (utils.errors/build-error
206 : :postgres
207 0 : {:message (.getMessage e)
208 0 : :context {:table :telegram_event_links
209 : :op :update-last-message-id}})))))
210 :
211 : (record-delivery-failure!
212 : [_ link-uuid {:keys [code description]}]
213 3 : (try
214 6 : (let [result (->>
215 6 : (sql/format
216 18 : {:update :telegram_event_links
217 15 : :set {:last_error description
218 3 : :last_error_code code
219 6 : :last_error_at [:now]}
220 21 : :where [:= :uuid [:cast link-uuid :uuid]]
221 6 : :returning [:*]})
222 9 : (jdbc/execute! ds)
223 3 : first)]
224 15 : (either/right (row->link result)))
225 : (catch Exception e
226 0 : (either/left
227 0 : (utils.errors/build-error
228 : :postgres
229 0 : {:message (.getMessage e)
230 0 : :context {:table :telegram_event_links
231 : :op :record-delivery-failure}})))))
232 :
233 : (record-delivery-failure-by-chat-id!
234 : [_ chat-id {:keys [code description]}]
235 1 : (try
236 2 : (jdbc/execute!
237 1 : ds
238 2 : (sql/format
239 5 : {:update :telegram_event_links
240 5 : :set {:last_error description
241 1 : :last_error_code code
242 2 : :last_error_at [:now]}
243 4 : :where [:= :chat-id chat-id]}))
244 3 : (either/right nil)
245 : (catch Exception e
246 0 : (either/left
247 0 : (utils.errors/build-error
248 : :postgres
249 0 : {:message (.getMessage e)
250 0 : :context {:table :telegram_event_links
251 : :op :record-delivery-failure-by-chat-id}})))))
252 :
253 : (clear-access-failure-by-chat-id!
254 : [_ chat-id]
255 1 : (try
256 2 : (jdbc/execute!
257 1 : ds
258 2 : (sql/format
259 5 : {:update :telegram_event_links
260 7 : :set {:last_error nil
261 : :last_error_code nil
262 : :last_error_at nil}
263 2 : :where [:and
264 4 : [:= :chat-id chat-id]
265 10 : [:or [:= :last_error_code 403] [:ilike :last_error "%not enough rights%"]]]}))
266 3 : (either/right nil)
267 : (catch Exception e
268 0 : (either/left
269 0 : (utils.errors/build-error
270 : :postgres
271 0 : {:message (.getMessage e)
272 0 : :context {:table :telegram_event_links
273 : :op :clear-access-failure-by-chat-id}})))))
274 :
275 : (update-chat-title-by-chat-id!
276 : [_ chat-id new-title]
277 1 : (try
278 2 : (jdbc/execute!
279 1 : ds
280 2 : (sql/format
281 5 : {:update :telegram_event_links
282 3 : :set {:chat_title new-title}
283 4 : :where [:= :chat-id chat-id]}))
284 3 : (either/right nil)
285 : (catch Exception e
286 0 : (either/left
287 0 : (utils.errors/build-error
288 : :postgres
289 0 : {:message (.getMessage e)
290 0 : :context {:table :telegram_event_links
291 : :op :update-chat-title}}))))))
292 :
293 :
294 4 : (defn make-repository
295 : "Creates a PostgresEventTelegramLinkRepository."
296 : [ds]
297 12 : (->PostgresEventTelegramLinkRepository ds))
|