LCOV - code coverage report
Current view: top level - matchscore/telegram - name_reminder.clj Coverage Total Hit
Test: lcov.info Lines: 94.7 % 75 71
Test Date: 2026-11-23 18:11:10 Functions: - 0 0

            Line data    Source code
       1            4 : (ns matchscore.telegram.name-reminder
       2              :   "Telegram texts and rules for nudging players to confirm their name. Players
       3              :    who join from a group chat get their Telegram name (often a nickname or emoji)
       4              :    and may never open the app, so the nudges reach them in Telegram:
       5              : 
       6              :    - the Join button answers privately and opens the bot chat (`join-alert`);
       7              :    - the bot's private chat explains and links to the profile (`private-message`);
       8              :    - organizers can post one reminder in the event's group (`group-message`)."
       9              :   (:require
      10              :    [clojure.string :as str]
      11              :    [matchscore.i18n :as i18n]
      12              :    [matchscore.telegram.formatting :as formatting]
      13              :    [taoensso.carmine :as car]
      14              :    [taoensso.telemere :as t])
      15              :   (:import
      16              :    (java.nio.charset StandardCharsets)
      17              :    (java.time        Instant LocalTime ZoneId)
      18              :    (java.util        Base64)))
      19              : 
      20              : 
      21            8 : (def start-payload
      22              :   "Payload of the bot link (`t.me/<bot>?start=name`) that asks about the name."
      23              :   "name")
      24              : 
      25              : 
      26            8 : (def profile-path "/my/account/edit")
      27              : 
      28              : 
      29            4 : (def join-nudge-ttl-seconds
      30              :   "The Join button nudges a player at most once per this period."
      31           28 :   (* 7 24 60 60))
      32              : 
      33              : 
      34            4 : (def group-reminder-ttl-seconds
      35              :   "Organizers can post the group reminder for an event at most once per this period."
      36           20 :   (* 24 60 60))
      37              : 
      38              : 
      39            4 : (defn group-key
      40              :  "Redis key of the per-event group reminder cooldown."
      41              :  [event-id]
      42            4 :  (str "tg:name-reminder:group:" event-id))
      43              : 
      44              : 
      45            4 : (defn reserve-group-cooldown!
      46              :  "Atomically claims the event's group reminder cooldown for `token` (a per-request id).
      47              :   True when this request now holds it; false when another request does."
      48              :  [redis event-id token]
      49           47 :  (= "OK" (car/wcar redis (car/set (group-key event-id) token :nx :ex group-reminder-ttl-seconds))))
      50              : 
      51              : 
      52            4 : (defn release-group-cooldown!
      53              :  "Deletes the cooldown only while it still holds `token`, so a request never clears a
      54              :   newer request's reservation. Best effort: returns true when deleted, false otherwise
      55              :   (including a Redis failure, which is logged; the cooldown then simply expires)."
      56              :  [redis event-id token]
      57            1 :  (try
      58            7 :    (when (and redis token)
      59            2 :      (=
      60              :       1
      61           35 :       (car/wcar
      62            1 :        redis
      63            3 :        (car/lua
      64              :         "if redis.call('get', _:key) == _:token then return redis.call('del', _:key) else return 0 end"
      65            5 :         {:key (group-key event-id)}
      66            3 :         {:token token}))))
      67              :    (catch Exception e
      68            0 :      (t/log!
      69              :       :warn
      70            0 :       {:message   "Failed to release name reminder cooldown"
      71            0 :        :event-id  (str event-id)
      72            0 :        :exception e})
      73              :      false)))
      74              : 
      75              : 
      76            8 : (def ^:private max-group-mentions 30)
      77              : 
      78              : 
      79            8 : (def ^:private alert-limit 200)
      80              : 
      81              : 
      82            8 : (def ^:private alert-name-limit 40)
      83              : 
      84              : 
      85           16 : (def ^:private send-zone (ZoneId/of "Europe/Kyiv"))
      86              : 
      87              : 
      88           20 : (def ^:private send-from (LocalTime/of 10 0))
      89              : 
      90              : 
      91           20 : (def ^:private send-until (LocalTime/of 20 0))
      92              : 
      93              : 
      94           20 : (defn- t [locale k & args] (apply i18n/translate (formatting/normalize-locale locale) k args))
      95              : 
      96              : 
      97            4 : (defn send-window?
      98              :  "Private reminders go out only in the daytime, Kyiv time."
      99              :  [^Instant now]
     100            4 :  (let [time (.toLocalTime (.atZone now send-zone))]
     101           10 :    (and (not (.isBefore time send-from)) (.isBefore time send-until))))
     102              : 
     103              : 
     104            4 : (defn bot-start-url
     105              :  "Opens the bot's private chat with the name payload, or nil without a bot username."
     106              :  [bot-username]
     107           23 :  (when-not (str/blank? bot-username) (str "https://t.me/" bot-username "?start=" start-payload)))
     108              : 
     109              : 
     110            4 : (defn app-url
     111              :  "Opens the mini app on `path` (see the frontend's startapp handling)."
     112              :  [bot-username path]
     113           10 :  (when-not (str/blank? bot-username)
     114            8 :    (str
     115            2 :     "https://t.me/" bot-username
     116            2 :     "?startapp="    (.encodeToString
     117            6 :                      (.withoutPadding (Base64/getUrlEncoder))
     118            4 :                      (.getBytes ^String path StandardCharsets/UTF_8)))))
     119              : 
     120              : 
     121            4 : (defn- profile-keyboard
     122              :   [locale bot-username]
     123           20 :   (when-let [url (app-url bot-username profile-path)]
     124           22 :     {:inline_keyboard [[{:text (t locale :telegram.name/button)
     125            2 :                          :url  url}]]}))
     126              : 
     127              : 
     128            4 : (defn- truncate
     129              :   [s limit]
     130           19 :   (let [s (str s)] (if (<= (count s) limit) s (str (subs s 0 (dec limit)) "…"))))
     131              : 
     132              : 
     133            4 : (defn user-locale
     134              :  "Locale for a private message: the user's app language when they chose one other
     135              :   than the English default, else the language of their latest event."
     136              :  [settings latest-event-language]
     137           19 :  (let [app-language (or (get settings :language) (get settings "language"))]
     138            4 :    (formatting/normalize-locale
     139           16 :     (or
     140           21 :      (when (and app-language (not= "en" app-language)) app-language)
     141            2 :      latest-event-language
     142              :      app-language))))
     143              : 
     144              : 
     145            4 : (defn private-message
     146              :  "The bot's private message about the user's name: {:text :reply-markup}."
     147              :  [locale user bot-username]
     148           24 :  {:text         (t locale :telegram.name/private-text (formatting/escape-html (:name user)))
     149            8 :   :reply-markup (profile-keyboard locale bot-username)})
     150              : 
     151              : 
     152            4 : (defn confirmed-message
     153              :  "The answer to the name link once the name is already confirmed."
     154              :  [locale user bot-username]
     155           12 :  {:text         (t locale :telegram.name/already-confirmed (formatting/escape-html (:name user)))
     156            4 :   :reply-markup (profile-keyboard locale bot-username)})
     157              : 
     158              : 
     159            4 : (defn join-alert
     160              :  "Text of the private alert the Join button shows (plain text, at most 200 chars).
     161              :   `joined-text` is the usual answer, for example \"Joined A\"."
     162              :  [locale joined-text user-name]
     163            2 :  (truncate
     164            9 :   (t locale :telegram.name/join-alert joined-text (truncate user-name alert-name-limit))
     165            1 :   alert-limit))
     166              : 
     167              : 
     168            4 : (defn- mention
     169              :   [{:keys [telegram-id name]}]
     170            9 :   (str "<a href=\"tg://user?id=" telegram-id "\">" (formatting/escape-html name) "</a>"))
     171              : 
     172              : 
     173            4 : (defn group-message
     174              :  "Group post that mentions the players whose name needs confirming: {:text
     175              :   :reply-markup}, or nil when there is nobody to mention. Mentions notify the players."
     176              :  [locale players bot-username]
     177            5 :  (when (seq players)
     178            5 :    (let [shown    (take max-group-mentions players)
     179            5 :          rest-n   (- (count players) (count shown))
     180           13 :          mentions (cond-> (str/join ", " (map mention shown))
     181            8 :                     (pos? rest-n) (str " " (t locale :telegram.name/group-more rest-n)))]
     182            8 :      {:text         (t locale :telegram.name/group-text mentions)
     183            4 :       :reply-markup (profile-keyboard locale bot-username)})))
        

Generated by: LCOV version 2.0-1