Line data Source code
1 4 : (ns matchscore.api.updates
2 : "Public in-app feed backed by the exact revisions published to Telegram."
3 : (:require
4 : [clojure.string :as str]
5 : [matchscore.db.postgres.telegram-publications :as publications]
6 : [matchscore.telegram.admin-posts :as admin-posts]
7 : [ring.util.http-status :as status]
8 : [schemas.api.core :as api-schemas])
9 : (:import
10 : (java.sql Timestamp)))
11 :
12 :
13 56 : (def UpdatePostSchema [:map [:markdown :string] [:html :string] [:image-url [:maybe :string]]])
14 :
15 :
16 4 : (def UpdateSchema
17 8 : [:map
18 12 : [:id :string]
19 12 : [:title :string]
20 12 : [:published-datetime :string]
21 20 : [:posts [:vector UpdatePostSchema]]])
22 :
23 :
24 8 : (def ^:private default-telegram-channel-url "https://t.me/matchscore_bot_updates")
25 :
26 :
27 8 : (def ^:private telegram-channel-pattern #"^@([A-Za-z][A-Za-z0-9_]{4,})$")
28 :
29 :
30 4 : (def UpdatesResponseSchema
31 8 : [:map
32 20 : [:status [:= 200]]
33 8 : [:body
34 8 : [:map
35 12 : [:message :string]
36 8 : [:data
37 8 : [:map
38 20 : [:latest-revision-id [:maybe :string]]
39 12 : [:telegram-channel-url :string]
40 20 : [:updates [:vector UpdateSchema]]]]]]])
41 :
42 :
43 4 : (defn- telegram-channel-url
44 : [chat-id]
45 15 : (if-let [[_ username] (and (string? chat-id) (re-matches telegram-channel-pattern chat-id))]
46 4 : (str "https://t.me/" username)
47 0 : default-telegram-channel-url))
48 :
49 :
50 4 : (defn- markdown-title
51 : [markdown]
52 0 : (some->
53 0 : markdown
54 0 : str/split-lines
55 0 : first
56 0 : (str/replace #"^#{1,6}\s+" "")
57 0 : (str/replace "**" "")
58 0 : str/trim
59 0 : not-empty))
60 :
61 :
62 4 : (defn- published-datetime-response
63 : [published-datetime]
64 5 : (if (instance? Timestamp published-datetime)
65 3 : (str (.toInstant ^Timestamp published-datetime))
66 0 : (str published-datetime)))
67 :
68 :
69 4 : (defn- update-response
70 : [{:keys [revision-id title published-datetime content]}]
71 4 : (let [posts (:posts content)]
72 8 : {:id (str revision-id)
73 4 : :title (or
74 3 : (not-empty title)
75 0 : (some->
76 0 : posts
77 0 : first
78 : :markdown
79 0 : markdown-title)
80 : "MatchScore update")
81 3 : :published-datetime (published-datetime-response published-datetime)
82 2 : :posts (mapv
83 1 : (fn [{:keys [markdown image-url]}]
84 5 : {:markdown markdown
85 3 : :html (admin-posts/markdown->telegram-html markdown)
86 1 : :image-url image-url})
87 1 : posts)}))
88 :
89 :
90 4 : (defn updates-handler
91 : {:malli/schema [:=> [:cat api-schemas/RingRequest] UpdatesResponseSchema]
92 : :summary "List published MatchScore updates"
93 : :description
94 : "Returns the same immutable Rich Markdown revisions that were successfully published to Telegram."}
95 : [{:keys [pg]}]
96 4 : (let [published (publications/published-updates pg)
97 4 : updates (mapv update-response published)]
98 4 : {:status status/ok
99 4 : :body {:message "Published MatchScore updates loaded."
100 20 : :data {:latest-revision-id (some->
101 1 : updates
102 1 : first
103 : :id)
104 2 : :telegram-channel-url (telegram-channel-url
105 7 : (get-in published [0 :content :chat-id]))
106 1 : :updates updates}}}))
|