Line data Source code
1 4 : (ns matchscore.api.admin-telegram
2 : "Root-only preview and publish endpoints for Telegram channel updates."
3 : (:require
4 : [matchscore.db.postgres.telegram-publications :as publications]
5 : [matchscore.telegram.admin-posts :as admin-posts]
6 : [matchscore.telegram.changelog-candidates :as candidates]
7 : [matchscore.utils.uuid :as uuid-utils]
8 : [ring.util.http-status :as status]
9 : [schemas.api.core :as api-schemas]))
10 :
11 :
12 4 : (def ^:private authz
13 28 : {:obj :admin
14 : :act :manage
15 : :create? true})
16 :
17 :
18 4 : (defn- validation-response
19 : [exception]
20 4 : {:status status/bad-request
21 7 : :body {:message (:message (ex-data exception))}})
22 :
23 :
24 4 : (defn- candidate-response
25 : [candidate]
26 4 : (let [content (:content candidate)
27 2 : posts (mapv
28 1 : (fn [post]
29 8 : (cond-> {:text (:markdown post)}
30 3 : (:image-url post) (assoc :image-url (:image-url post))))
31 3 : (:posts content))
32 3 : preview (mapv
33 8 : #(select-keys % [:html :markdown :length :image-url])
34 2 : (:posts
35 2 : (admin-posts/prepare
36 5 : {:chat-id "@candidate_preview"
37 3 : :disable-notification (:disable-notification content)
38 1 : :posts posts})))]
39 13 : {:candidate-id (:uuid candidate)
40 3 : :source-id (:source-id candidate)
41 3 : :title (:title candidate)
42 3 : :source (:source-metadata candidate)
43 3 : :status (:status candidate)
44 3 : :publication-id (:publication-id candidate)
45 5 : :disable-notification (true? (:disable-notification content))
46 1 : :posts posts
47 1 : :preview-posts preview}))
48 :
49 :
50 4 : (defn admin-telegram-candidates-handler
51 : {:malli/schema
52 : [:=> [:cat api-schemas/RingRequest] api-schemas/AdminTelegramCandidatesResponseSchema]
53 : :authz authz
54 : :summary "List code-generated Telegram changelogs"
55 : :description "Imports bundled Codex drafts idempotently and returns the review queue. Root only."}
56 : [{:keys [pg]}]
57 3 : (candidates/sync-bundled! pg)
58 4 : {:status status/ok
59 4 : :body {:message "Generated Telegram changelogs loaded."
60 6 : :data (mapv candidate-response (publications/changelog-candidates pg))}})
61 :
62 :
63 4 : (defn admin-telegram-archive-candidate-handler
64 : {:malli/schema
65 : [:=> [:cat api-schemas/RingRequest] api-schemas/AdminTelegramCandidateArchiveResponseSchema]
66 : :malli/parameters {:path api-schemas/AdminTelegramCandidatePathParamsSchema}
67 : :authz authz
68 : :summary "Archive a generated Telegram changelog"
69 : :description
70 : "Marks an awaiting-review candidate as rejected without sending it to Telegram. Root only."}
71 : [{:keys [pg auth-headers route-params]}]
72 4 : (let [candidate-id (:candidate-uuid route-params)]
73 6 : (if-not (uuid-utils/uuid-string? candidate-id)
74 0 : {:status status/bad-request
75 0 : :body {:message "Invalid changelog candidate ID."}}
76 1 : (try
77 1 : (let [candidate
78 7 : (publications/reject-changelog-candidate! pg candidate-id (:user/uuid auth-headers))]
79 4 : {:status status/ok
80 4 : :body {:message "Generated changelog archived without publishing."
81 6 : :data {:candidate-id (:uuid candidate)
82 3 : :status (:status candidate)}}})
83 : (catch clojure.lang.ExceptionInfo e
84 0 : (if (= :admin-telegram-validation (:type (ex-data e)))
85 0 : (validation-response e)
86 0 : (throw e)))))))
87 :
88 :
89 4 : (defn admin-telegram-preview-handler
90 : {:malli/schema [:=>
91 : [:cat api-schemas/RingRequest api-schemas/AdminTelegramPublicationSchema]
92 : api-schemas/AdminTelegramPreviewResponseSchema]
93 : :authz authz
94 : :summary "Preview Telegram channel posts"
95 : :description
96 : "Validates Rich Markdown and saves the exact immutable revision being previewed. Root only."}
97 : [{:keys [pg auth-headers]} publication]
98 1 : (try
99 4 : (let [prepared (admin-posts/prepare publication)
100 2 : saved (publications/save-preview!
101 1 : pg
102 3 : (:user/uuid auth-headers)
103 3 : (:publication-id publication)
104 3 : (:candidate-id publication)
105 1 : prepared)]
106 4 : {:status status/ok
107 4 : :body {:message "Preview saved."
108 4 : :data (merge prepared saved)}})
109 : (catch clojure.lang.ExceptionInfo e
110 11 : (if (= :admin-telegram-validation (:type (ex-data e))) (validation-response e) (throw e)))))
111 :
112 :
113 4 : (defn admin-telegram-publish-handler
114 : {:malli/schema [:=>
115 : [:cat api-schemas/RingRequest api-schemas/AdminTelegramPublishRequestSchema]
116 : api-schemas/AdminTelegramPublishResponseSchema]
117 : :authz authz
118 : :summary "Publish Telegram channel posts"
119 : :description "Idempotently publishes a saved revision using Telegram Rich Messages. Root only."}
120 : [{:keys [bot pg auth-headers]
121 : :as _request}
122 : {:keys [revision-id idempotency-key]}]
123 1 : (try
124 5 : (let [revision (or
125 4 : (publications/revision pg revision-id)
126 : (throw
127 0 : (ex-info "Telegram publication revision not found." {:type :api-not-found})))
128 9 : result (admin-posts/publish! pg bot (:user/uuid auth-headers) revision idempotency-key)
129 12 : data {:publication-id (:publication-id revision)
130 1 : :revision-id revision-id
131 3 : :delivery-id (:delivery-id result)
132 3 : :delivery-status (:delivery-status result)
133 3 : :deduplicated (:already-existed? result)
134 6 : :chat-id (get-in revision [:content :chat-id])
135 7 : :published-post-count (count (get-in revision [:content :posts]))
136 3 : :sent-message-ids (:sent-message-ids result)}]
137 4 : (if (:ok? result)
138 4 : {:status status/ok
139 9 : :body {:message (if (:already-existed? result)
140 : "This revision was already published; nothing was sent again."
141 : "Telegram rich messages published.")
142 1 : :data data}}
143 9 : {:status (if (:already-existed? result) status/conflict status/bad-gateway)
144 4 : :body {:message
145 7 : (if (= "delivery_unknown" (:delivery-status result))
146 : "Telegram delivery could not be confirmed. Review the channel before any retry."
147 4 : (str
148 : "Telegram publication stopped after a delivery error. "
149 4 : (count (:sent-message-ids result))
150 : " message(s) were sent; this revision will not be sent automatically again."))
151 1 : :data data
152 8 : :error (or (:error result) {})}}))
153 : (catch clojure.lang.ExceptionInfo e
154 11 : (if (= :admin-telegram-validation (:type (ex-data e))) (validation-response e) (throw e)))))
|