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