LCOV - code coverage report
Current view: top level - matchscore/jobs - telegram_chat.clj Coverage Total Hit
Test: lcov.info Lines: 86.7 % 75 65
Test Date: 2026-11-23 18:11:10 Functions: - 0 0

            Line data    Source code
       1            4 : (ns matchscore.jobs.telegram-chat
       2              :   (:require
       3              :    [cats.core :as c]
       4              :    [cats.monad.either :as either]
       5              :    [matchscore.domain.event-telegram-link.repository.postgres :as event-telegram-link-postgres-repo]
       6              :    [matchscore.domain.template-telegram-link.repository.postgres
       7              :     :as
       8              :     template-telegram-link-postgres-repo]
       9              :    [taoensso.telemere :as t]))
      10              : 
      11              : 
      12            4 : (defn sync-chat-title-job
      13              :  "Goose job: updates chat_title in all link rows for the given chat-id."
      14              :  [system-components {:keys [chat-id new-title]}]
      15            2 :  (let [{:keys [repos]} system-components
      16            2 :        tmpl-result     (template-telegram-link-postgres-repo/update-chat-title-by-chat-id!
      17            3 :                         (:template-telegram-link-postgres-repo repos)
      18            1 :                         chat-id
      19            1 :                         new-title)
      20            2 :        event-result    (event-telegram-link-postgres-repo/update-chat-title-by-chat-id!
      21            3 :                         (:event-telegram-link-postgres-repo repos)
      22            1 :                         chat-id
      23            1 :                         new-title)
      24            1 :        errors          (->>
      25            3 :                          [tmpl-result event-result]
      26            3 :                          (filter either/left?)
      27            2 :                          (mapv c/extract))]
      28            4 :    (if (empty? errors)
      29           92 :      (t/log!
      30              :       :info
      31            5 :       {:message   "Synced Telegram chat title"
      32            1 :        :chat-id   chat-id
      33            1 :        :new-title new-title})
      34            1 :      (do
      35           92 :        (t/log!
      36              :         :error
      37            6 :         {:message   "Failed to sync Telegram chat title"
      38            1 :          :chat-id   chat-id
      39            1 :          :new-title new-title
      40            1 :          :errors    errors})
      41            1 :        (throw
      42            3 :         (ex-info
      43              :          "Failed to sync Telegram chat title"
      44            4 :          {:chat-id chat-id
      45            1 :           :errors  errors}))))))
      46              : 
      47              : 
      48            4 : (defn- access-failure
      49              :   "The delivery failure a bot membership status implies, in the words Telegram
      50              :    uses when a post is refused for that reason, or nil when the bot can post."
      51              :   [status can-send-messages]
      52            4 :   (case status
      53            5 :     "kicked"     {:code        403
      54              :                   :description "Forbidden: bot was kicked from the group chat"}
      55            5 :     "left"       {:code        403
      56              :                   :description "Forbidden: bot is not a member of the group chat"}
      57            5 :     "restricted" (when (false? can-send-messages)
      58            5 :                    {:code 400
      59              :                     :description
      60              :                     "Bad Request: not enough rights to send text messages to the chat"})
      61              :     nil))
      62              : 
      63              : 
      64            4 : (defn sync-bot-membership-job
      65              :  "Goose job: keeps link delivery status in step with the bot's membership of a
      66              :    chat. When the bot is removed or muted, every link of that chat is marked as
      67              :    not delivering right away, without waiting for the next post to fail; when it
      68              :    is let back in, that mark is cleared.
      69              : 
      70              :    payload keys:
      71              :      :chat-id           — the chat whose membership changed
      72              :      :status            — the bot's new status (member, administrator, restricted,
      73              :                           left, kicked)
      74              :      :can-send-messages — for a restricted bot, whether it may still post"
      75              :  [system-components {:keys [chat-id status can-send-messages]}]
      76            2 :  (let [{:keys [repos]} system-components
      77            3 :        tmpl-repo       (:template-telegram-link-postgres-repo repos)
      78            3 :        event-repo      (:event-telegram-link-postgres-repo repos)
      79            4 :        failure         (access-failure status can-send-messages)
      80            2 :        results         (if failure
      81            3 :                          [(template-telegram-link-postgres-repo/record-delivery-failure-by-chat-id!
      82            1 :                            tmpl-repo
      83            1 :                            chat-id
      84            1 :                            failure)
      85            2 :                           (event-telegram-link-postgres-repo/record-delivery-failure-by-chat-id!
      86            1 :                            event-repo
      87            1 :                            chat-id
      88            1 :                            failure)]
      89            3 :                          [(template-telegram-link-postgres-repo/clear-access-failure-by-chat-id!
      90            1 :                            tmpl-repo
      91            1 :                            chat-id)
      92            2 :                           (event-telegram-link-postgres-repo/clear-access-failure-by-chat-id!
      93            1 :                            event-repo
      94            1 :                            chat-id)])
      95            1 :        errors          (->>
      96            1 :                          results
      97            3 :                          (filter either/left?)
      98            2 :                          (mapv c/extract))]
      99            4 :    (if (empty? errors)
     100           92 :      (t/log!
     101              :       :info
     102            5 :       {:message "Synced Telegram bot membership"
     103            1 :        :chat-id chat-id
     104            1 :        :status  status})
     105            0 :      (do
     106            0 :        (t/log!
     107              :         :error
     108            0 :         {:message "Failed to sync Telegram bot membership"
     109            0 :          :chat-id chat-id
     110            0 :          :status  status
     111            0 :          :errors  errors})
     112            0 :        (throw
     113            0 :         (ex-info
     114              :          "Failed to sync Telegram bot membership"
     115            0 :          {:chat-id chat-id
     116            0 :           :errors  errors}))))))
        

Generated by: LCOV version 2.0-1