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

            Line data    Source code
       1            4 : (ns matchscore.telegram.commands
       2              :   (:require
       3              :    [clojure.string :as str]
       4              :    [matchscore.domain.class.repository.postgres :as class-postgres-repo]
       5              :    [matchscore.domain.event.repository.postgres :as event-postgres-repo]
       6              :    [matchscore.domain.telegram-chat-message.repository.postgres
       7              :     :as
       8              :     telegram-chat-message-postgres-repo]
       9              :    [matchscore.domain.template.repository.postgres :as template-postgres-repo]
      10              :    [matchscore.domain.user.repository.postgres :as user-postgres-repo]
      11              :    [matchscore.telegram.notifications-trigger :as notifications-trigger]
      12              :    [taoensso.telemere :as t]
      13              :    [telegrambot-lib.core :as tbot]))
      14              : 
      15              : 
      16            4 : (def ^:private known-commands
      17           28 :   #{:link_event :unlink_event :link_template :unlink_template #_:join #_:leave :bot_health :start})
      18              : 
      19              : 
      20            4 : (defn parse-start-payload
      21              :  "Parses the payload Telegram attaches to /start when the bot is added to a group
      22              :    through a connect link (t.me/<bot>?startgroup=<payload>): `lt_<public-id>`
      23              :    links a template, `le_<public-id>` an event. Returns
      24              :    {:cmd :link_template|:link_event :public-id \"...\"}, or nil for any other /start."
      25              :  [payload]
      26            9 :  (when-let [[_ kind public-id]
      27           22 :             (and (string? payload) (re-matches #"(lt|le)_([A-Za-z0-9_-]{1,61})" payload))]
      28            9 :    {:cmd       (if (= "lt" kind) :link_template :link_event)
      29            1 :     :public-id public-id}))
      30              : 
      31              : 
      32            4 : (defn parse-command
      33              :  "Parses a bot command text (e.g. '/link_event abc123' or '/join@BotName cls15').
      34              :    Returns {:cmd <keyword> :args [rest-tokens...]} or nil when not a known command.
      35              :    When bot-username is non-nil and the command has an @BotName suffix, the
      36              :    suffix must match bot-username (case-insensitive); a mismatch returns nil."
      37              :  [text bot-username]
      38           37 :  (when (and (string? text) (str/starts-with? text "/"))
      39           21 :    (let [tokens      (str/split (str/trim text) #"\s+")
      40            9 :          first-token (first tokens)
      41           15 :          parts       (str/split first-token #"@" 2)]
      42            6 :      (when
      43           20 :        (or
      44           12 :         (= 1 (count parts))
      45            8 :         (nil? bot-username)
      46           16 :         (= (str/lower-case (second parts)) (str/lower-case bot-username)))
      47           27 :        (let [cmd (keyword (subs (first parts) 1))]
      48           15 :          (when (known-commands cmd)
      49           12 :            {:cmd  cmd
      50           15 :             :args (vec (rest tokens))}))))))
      51              : 
      52              : 
      53            4 : (defn resolve-event-by-public-id
      54              :  "Finds an event by its 12-char public-id. Returns event map or nil."
      55              :  [{:keys [repos]} public-id]
      56           15 :  (when (string? public-id)
      57           18 :    (event-postgres-repo/find-by-public-id (:event-postgres-repo repos) public-id)))
      58              : 
      59              : 
      60            4 : (defn resolve-template-by-public-id
      61              :  "Finds a template by its 12-char public-id. Returns template map or nil."
      62              :  [{:keys [repos]} public-id]
      63           15 :  (when (string? public-id)
      64           18 :    (template-postgres-repo/find-by-public-id (:template-postgres-repo repos) public-id)))
      65              : 
      66              : 
      67            4 : (defn resolve-class-by-public-id
      68              :  "Finds a class by its public-id, scoped to the given event-uuid.
      69              :    Returns class map or nil."
      70              :  [{:keys [repos]} event-uuid public-id]
      71           10 :  (when (and (string? public-id) event-uuid)
      72            2 :    (class-postgres-repo/find-by-event-and-public-id
      73            3 :     (:class-postgres-repo repos)
      74            1 :     event-uuid
      75            1 :     public-id)))
      76              : 
      77              : 
      78            4 : (defn chat-member
      79              :  "Calls Telegram getChatMember and returns the result map
      80              :    {:status \"member\"|... :user {:id .. :first_name .. :last_name .. :username ..}},
      81              :    or nil when the call fails."
      82              :  [bot chat-id telegram-user-id]
      83            4 :  (try
      84            6 :    (:result
      85            6 :     (tbot/get-chat-member
      86            3 :      bot
      87           12 :      {:chat_id chat-id
      88            3 :       :user_id telegram-user-id}))
      89              :    (catch Exception e
      90           92 :      (t/log!
      91              :       :warn
      92            4 :       {:message   "chat-member failed"
      93            1 :        :exception e})
      94              :      nil)))
      95              : 
      96              : 
      97            4 : (defn- chat-member-status
      98              :   "Calls Telegram getChatMember and returns the membership status string
      99              :    (e.g. \"creator\", \"administrator\", \"member\", \"restricted\", \"left\",
     100              :    \"kicked\"), or nil when the call fails."
     101              :   [bot chat-id telegram-user-id]
     102           21 :   (:status (chat-member bot chat-id telegram-user-id)))
     103              : 
     104              : 
     105            4 : (defn check-chat-admin?
     106              :  "Calls Telegram getChatMember for the given user and checks if they are
     107              :    a creator or administrator of the chat. Returns boolean."
     108              :  [bot chat-id telegram-user-id]
     109           30 :  (contains? #{"creator" "administrator"} (chat-member-status bot chat-id telegram-user-id)))
     110              : 
     111              : 
     112            4 : (defn reply!
     113              :  "Sends a text reply in the same chat (and thread if present).
     114              :    Returns the resulting Telegram message_id on success, nil on failure."
     115           14 :  ([bot chat-id thread-id text] (reply! bot chat-id thread-id text nil))
     116           16 :  ([bot chat-id thread-id text parse-mode] (reply! bot chat-id thread-id text parse-mode nil))
     117              :  ([bot chat-id thread-id text parse-mode reply-to-message-id]
     118            3 :   (try
     119            6 :     (let [resp (tbot/send-message
     120            2 :                 bot
     121           19 :                 (cond->
     122            8 :                   {:chat_id chat-id
     123            2 :                    :text    text}
     124            6 :                   parse-mode (assoc :parse_mode parse-mode)
     125            6 :                   thread-id  (assoc :message_thread_id thread-id)
     126            3 :                   reply-to-message-id (assoc :reply_parameters
     127            5 :                                        {:message_id reply-to-message-id
     128              :                                         :allow_sending_without_reply true})))]
     129           12 :       (get-in resp [:result :message_id]))
     130              :     (catch Exception e
     131           92 :       (t/log!
     132              :        :warn
     133            4 :        {:message   "reply! failed"
     134            1 :         :exception e})
     135              :       nil))))
     136              : 
     137              : 
     138            4 : (defn record-msg!
     139              :  "Records a Telegram chat message in the audit table. Idempotent.
     140              :    `:repos` ctx is used to look up the repo. Returns nil on bad data."
     141              :  [{:keys [repos]} msg]
     142           47 :  (when (and msg (:chat-id msg) (:message-id msg))
     143           18 :    (telegram-chat-message-postgres-repo/record! (:telegram-chat-message-postgres-repo repos) msg)))
     144              : 
     145              : 
     146            4 : (defn deep-link-url
     147              :  "Builds a Telegram deep link to open the bot's mini app."
     148              :  [bot-username startapp?]
     149            6 :  (when bot-username
     150            4 :    (if startapp?
     151           10 :      (str "https://t.me/" bot-username "?startapp")
     152            4 :      (str "https://t.me/" bot-username))))
     153              : 
     154              : 
     155            4 : (defn resolve-user-name
     156              :  "Returns the matchscore user's display name (users.name) by uuid, or nil."
     157              :  [{:keys [repos]} user-uuid]
     158            3 :  (when user-uuid
     159            9 :    (some->
     160            6 :      (user-postgres-repo/find-by-id (:user-postgres-repo repos) user-uuid)
     161              :      :name)))
     162              : 
     163              : 
     164            4 : (defn- escape-html
     165              :   [s]
     166            9 :   (->
     167            3 :     s
     168           12 :     (str/replace "&" "&amp;")
     169           12 :     (str/replace "<" "&lt;")
     170           12 :     (str/replace ">" "&gt;")
     171           12 :     (str/replace "\"" "&quot;")
     172            3 :     (str/replace "'" "&#39;")))
     173              : 
     174              : 
     175            4 : (defn sender-mention
     176              :  [{:keys [sender-tg-id sender-username sender-first-name sender-last-name]}]
     177           33 :  (cond (not (str/blank? sender-username)) {:text (str "@" sender-username)}
     178              : 
     179            3 :        sender-tg-id
     180           36 :        (let [name (str/trim (str/join " " (filter seq [sender-first-name sender-last-name])))]
     181           27 :          {:text       (str
     182              :                        "<a href=\"tg://user?id="
     183            3 :                        sender-tg-id
     184              :                        "\">"
     185           21 :                        (escape-html (if (str/blank? name) "User" name))
     186              :                        "</a>")
     187              :           :parse-mode "HTML"})))
     188              : 
     189              : 
     190            4 : (defn mention-prefixed-text
     191              :  [text sender]
     192           12 :  (let [mention (sender-mention sender)]
     193           18 :    (cond-> mention
     194           18 :      (:text mention) (assoc :text
     195           41 :                       (str (:text mention) "\n" (if (:parse-mode mention) (escape-html text) text)))
     196           18 :      (nil? (:text mention)) (assoc :text text))))
     197              : 
     198              : 
     199            4 : (defn notify-action!
     200              :  "Refreshes the main event message after a membership change. History is captured
     201              :    by the database transaction; no standalone join/leave message is sent."
     202              :  [ctx {:keys [event]}]
     203           12 :  (notifications-trigger/trigger-debounced! ctx (:uuid event)))
        

Generated by: LCOV version 2.0-1