Line data Source code
1 4 : (ns matchscore.api.telegram-links
2 : (:require
3 : [cats.core :as c]
4 : [cats.monad.either :as either]
5 : [matchscore.access.resolvers :as access-resolvers]
6 : [matchscore.access.resource :as access-resource]
7 : [matchscore.application.telegram-links :as app-telegram-links]
8 : [matchscore.domain.telegram-link.query :as telegram-link-query]
9 : [ring.util.http-status :as status]
10 : [schemas.api.core :as api-schemas]))
11 :
12 :
13 4 : (def TelegramLinkPathParamsSchema
14 20 : [:map {:swagger/title "TelegramLinkPathParamsSchema"}
15 28 : [:link-uuid {:description "Telegram link UUID in v7 format"
16 : :example "550e8400-e29b-41d4-a716-446655440000"}
17 12 : [:ref :types/UuidString]]])
18 :
19 :
20 4 : (def TelegramLinkUnlinkResponseSchema
21 28 : [:or {:swagger/title "TelegramLinkUnlinkResponseSchema"
22 : :swagger/description "Telegram link disconnect response"}
23 8 : [:map
24 20 : [:status [:= status/ok]]
25 64 : [:body [:map [:message string?] [:data [:map [:link-uuid [:ref :types/UuidString]]]]]]]
26 56 : [:map [:status [:= status/internal-server-error]] [:body [:map [:message string?]]]]])
27 :
28 :
29 4 : (def TelegramLinkRepostResponseSchema
30 28 : [:or {:swagger/title "TelegramLinkRepostResponseSchema"
31 : :swagger/description "Telegram link re-post scheduling response"}
32 8 : [:map
33 20 : [:status [:= status/accepted]]
34 8 : [:body
35 8 : [:map
36 12 : [:message string?]
37 8 : [:data
38 8 : [:map
39 20 : [:link-uuid [:ref :types/UuidString]]
40 28 : [:event-uuids [:vector [:ref :types/UuidString]]]]]]]]
41 8 : [:map
42 20 : [:status [:= status/unprocessable-entity]]
43 56 : [:body [:map [:message string?] [:error [:map [:code keyword?]]]]]]])
44 :
45 :
46 4 : (def AdminTelegramLinkListResponseSchema
47 28 : [:map {:swagger/title "AdminTelegramLinkListResponseSchema"
48 : :swagger/description "Every Telegram link on the platform"}
49 20 : [:status [:= status/ok]]
50 8 : [:body
51 8 : [:map
52 12 : [:message string?]
53 8 : [:data
54 8 : [:vector
55 8 : [:map
56 20 : [:uuid [:ref :types/UuidString]]
57 12 : [:chat-id string?]
58 20 : [:chat-title [:maybe string?]]
59 20 : [:thread-id [:maybe int?]]
60 24 : [:resource-type [:enum "template" "event"]]
61 20 : [:resource-uuid [:ref :types/UuidString]]
62 20 : [:resource-public-id [:maybe string?]]
63 20 : [:resource-name [:maybe string?]]
64 20 : [:resource-state [:maybe string?]]
65 20 : [:resource-owner-name [:maybe string?]]
66 20 : [:resource-owner-username [:maybe string?]]
67 20 : [:resource-datetime-start [:maybe inst?]]
68 20 : [:resource-next-event-start [:maybe inst?]]
69 20 : [:last-post-datetime [:maybe inst?]]
70 20 : [:last-post-message-id [:maybe string?]]
71 20 : [:last-error [:maybe string?]]
72 20 : [:last-error-code [:maybe int?]]
73 20 : [:last-error-datetime [:maybe inst?]]
74 20 : [:created-datetime [:maybe inst?]]]]]]]])
75 :
76 :
77 9 : (defn- request-ctx [{:keys [ctx auth-headers]}] (assoc ctx :auth-headers auth-headers))
78 :
79 :
80 4 : (defn- require-link!
81 : [request kind]
82 7 : (let [link-uuid (get-in request [:route-params :link-uuid])]
83 3 : (access-resolvers/require-or-404!
84 10 : (app-telegram-links/find-link (get-in request [:ctx :repos]) kind link-uuid)
85 : :telegram-link
86 1 : link-uuid)))
87 :
88 :
89 4 : (defn- template-link-authorization-object
90 : [request]
91 5 : (let [link (require-link! request :template)]
92 8 : (access-resource/template-obj (request-ctx request) (:template-id link))))
93 :
94 :
95 4 : (defn- event-link-authorization-object
96 : "Event links follow event management rights, which also cover the owner and
97 : admins of the event's template."
98 : [request]
99 5 : (let [link (require-link! request :event)]
100 8 : (access-resource/template-event-management-obj (request-ctx request) (:event-id link))))
101 :
102 :
103 4 : (defn- unlink-response
104 : [{:keys [ctx goose]
105 : :as request}
106 : kind]
107 5 : (let [link (require-link! request kind)
108 2 : result (app-telegram-links/unlink!
109 5 : (assoc ctx :goose goose)
110 1 : kind
111 1 : link
112 6 : (get-in request [:auth-headers :user/uuid]))]
113 4 : (if (either/right? result)
114 4 : {:status status/ok
115 4 : :body {:message "Telegram chat disconnected."
116 5 : :data {:link-uuid (:uuid link)}}}
117 0 : {:status status/internal-server-error
118 0 : :body {:message "Could not disconnect the Telegram chat."}})))
119 :
120 :
121 4 : (defn- repost-response
122 : [{:keys [ctx goose]
123 : :as request}
124 : kind]
125 5 : (let [link (require-link! request kind)
126 9 : result (app-telegram-links/repost! (assoc ctx :goose goose) kind link)]
127 4 : (if (either/right? result)
128 4 : {:status status/accepted
129 4 : :body {:message "Telegram post scheduled."
130 9 : :data (assoc (c/extract result) :link-uuid (:uuid link))}}
131 4 : (let [{:keys [code message]} (c/extract result)]
132 4 : {:status status/unprocessable-entity
133 4 : :body {:message message
134 3 : :error {:code code}}}))))
135 :
136 :
137 4 : (defn template-link-delete-handler
138 : {:malli/schema [:=> [:cat api-schemas/RingRequest] TelegramLinkUnlinkResponseSchema]
139 : :malli/parameters {:path TelegramLinkPathParamsSchema}
140 : :authz {:obj :template
141 : :act :update
142 : :obj-resolver #'template-link-authorization-object}
143 : :summary "Disconnect a Telegram chat from a template"
144 : :description
145 : "Deletes the template's Telegram link, queues removal of the main posts its events left in that chat, and has the bot tell the chat who disconnected it. Available to users who may update the template; no Telegram chat-admin check is made."}
146 : [request]
147 4 : (unlink-response request :template))
148 :
149 :
150 4 : (defn event-link-delete-handler
151 : {:malli/schema [:=> [:cat api-schemas/RingRequest] TelegramLinkUnlinkResponseSchema]
152 : :malli/parameters {:path TelegramLinkPathParamsSchema}
153 : :authz {:obj :event
154 : :act :update
155 : :obj-resolver #'event-link-authorization-object}
156 : :summary "Disconnect a Telegram chat from an event"
157 : :description
158 : "Deletes the event's Telegram link, queues removal of the main post it left in that chat, and has the bot tell the chat who disconnected it. Available to users who may manage the event; no Telegram chat-admin check is made."}
159 : [request]
160 4 : (unlink-response request :event))
161 :
162 :
163 4 : (defn template-link-repost-handler
164 : {:malli/schema [:=> [:cat api-schemas/RingRequest] TelegramLinkRepostResponseSchema]
165 : :malli/parameters {:path TelegramLinkPathParamsSchema}
166 : :authz {:obj :template
167 : :act :update
168 : :obj-resolver #'template-link-authorization-object}
169 : :summary "Re-post a template's events to its Telegram chat"
170 : :description
171 : "Schedules a fresh main post for the template's open events that already have a post in the linked chat, or for its active or next scheduled event when none has. Returns 422 when the template has no open event."}
172 : [request]
173 4 : (repost-response request :template))
174 :
175 :
176 4 : (defn event-link-repost-handler
177 : {:malli/schema [:=> [:cat api-schemas/RingRequest] TelegramLinkRepostResponseSchema]
178 : :malli/parameters {:path TelegramLinkPathParamsSchema}
179 : :authz {:obj :event
180 : :act :update
181 : :obj-resolver #'event-link-authorization-object}
182 : :summary "Re-post an event to its Telegram chat"
183 : :description
184 : "Schedules a fresh main post for the event: the draws when its current post shows them, otherwise the participant list. Returns 422 when the event is completed or cancelled."}
185 : [request]
186 4 : (repost-response request :event))
187 :
188 :
189 4 : (defn- link->admin-row
190 : [link]
191 2 : (->
192 1 : link
193 2 : (select-keys
194 20 : [:uuid
195 : :chat-id
196 : :chat-title
197 : :thread-id
198 : :resource-type
199 : :resource-uuid
200 : :resource-public-id
201 : :resource-name
202 : :resource-state
203 : :resource-owner-name
204 : :resource-owner-username
205 : :resource-datetime-start
206 : :resource-next-event-start
207 : :last-post-datetime
208 : :last-post-message-id
209 : :last-error
210 : :last-error-code
211 : :last-error-datetime
212 : :created-datetime])
213 4 : (update :chat-id str)
214 2 : (update
215 : :last-post-message-id
216 8 : #(some->
217 : %
218 0 : str))))
219 :
220 :
221 4 : (defn admin-telegram-links-handler
222 : {:malli/schema [:=> [:cat api-schemas/RingRequest] AdminTelegramLinkListResponseSchema]
223 : :authz {:obj :admin
224 : :act :manage
225 : :create? true}
226 : :summary "List all Telegram links"
227 : :description
228 : "Returns every Telegram link on the platform with its template or event, owner, latest bot post and last delivery failure. Events completed or cancelled more than 30 days ago are left out. Root only."}
229 : [{:keys [ctx]
230 : :as _request}]
231 4 : {:status status/ok
232 4 : :body {:message "OK"
233 8 : :data (mapv link->admin-row (telegram-link-query/list-all-links (:repos ctx)))}})
|