Line data Source code
1 4 : (ns matchscore.application.telegram-access
2 : "Write-through cache of Telegram chat membership.
3 :
4 : When a user requests access to a template linked to a Telegram chat, we call
5 : Telegram getChatMember for each linked chat and upsert a membership row for
6 : the chats where the user is a current member. Subsequent template-readable
7 : checks join against the cached rows, so members can view the template without
8 : another Telegram round-trip.
9 :
10 : A Telegram user who taps through from a chat but has no MatchScore account yet
11 : is auto-provisioned (from the getChatMember profile) the first time their
12 : membership of a linked chat is confirmed, so the membership row has a real
13 : user to hang off."
14 : (:require
15 : [cats.core :as c]
16 : [cats.monad.either :as either]
17 : [matchscore.domain.event.query :as event-query]
18 : [matchscore.domain.telegram-chat-member.repository.postgres :as telegram-chat-member-repo]
19 : [matchscore.domain.telegram-link.query :as telegram-link-query]
20 : [matchscore.domain.template-telegram-link.repository.postgres :as template-telegram-link-repo]
21 : [matchscore.domain.template.query :as template-query]
22 : [matchscore.domain.user.command :as user-command]
23 : [matchscore.domain.user.query :as user-query]
24 : [matchscore.telegram.commands :as telegram-commands]
25 : [matchscore.utils.uuid :as utils.uuid]
26 : [taoensso.telemere :as t]))
27 :
28 :
29 : ;; Telegram chat member statuses that grant view access to a linked template.
30 : ;; "restricted" is intentionally excluded: a "restricted" member may or may not
31 : ;; still be in the chat (distinguished only by Telegram's is_member flag, which
32 : ;; we do not track), so we conservatively deny rather than risk granting access
33 : ;; to a user who has actually left.
34 20 : (def ^:private member-statuses #{"creator" "administrator" "member"})
35 :
36 :
37 4 : (defn- ensure-user!
38 : "Returns the MatchScore user UUID for the Telegram identity, auto-creating a
39 : minimal account from the getChatMember profile when none exists yet. Handles
40 : the unique-telegram_id race by re-resolving on a failed insert. Returns nil
41 : only when creation fails for a non-conflict reason."
42 : [ctx telegram-id tg-user]
43 0 : (let [repos (:repos ctx)
44 0 : repo (:user-postgres-repo repos)
45 0 : redis (:user-redis-repo repos)]
46 0 : (or
47 0 : (user-query/find-uuid-by-telegram-id repo redis telegram-id)
48 0 : (let [result (user-command/create-from-telegram!
49 0 : ctx
50 0 : {:telegram-id telegram-id
51 0 : :first-name (:first_name tg-user)
52 0 : :last-name (:last_name tg-user)
53 0 : :username (:username tg-user)})]
54 0 : (if (either/right? result)
55 0 : (let [uuid (:uuid (c/extract result))]
56 0 : (t/log!
57 : :info
58 0 : {:message "Auto-provisioned Telegram user (chat-member grant)"
59 0 : :telegram-id telegram-id
60 0 : :user-uuid uuid})
61 0 : uuid)
62 : ;; Likely a concurrent insert won the unique telegram_id — re-resolve.
63 0 : (or
64 0 : (user-query/find-uuid-by-telegram-id repo redis telegram-id)
65 : (do
66 0 : (t/log!
67 : :warn
68 0 : {:message "Failed to auto-provision Telegram user"
69 0 : :telegram-id telegram-id
70 0 : :error (c/extract result)})
71 : nil)))))))
72 :
73 :
74 4 : (defn- sync-chat-membership!
75 : "Verifies the user's membership of a single chat via Telegram and reconciles
76 : the cached row. Auto-provisions the MatchScore account on the first confirmed
77 : membership when `user-id` is still nil. Returns the (possibly newly created)
78 : user-id when the caller is a current member, otherwise nil."
79 : [ctx repo bot chat-id user-id telegram-id]
80 12 : (let [member (telegram-commands/chat-member bot chat-id telegram-id)
81 6 : status (:status member)]
82 11 : (cond (contains? member-statuses status)
83 22 : (when-let [uid (or user-id (ensure-user! ctx telegram-id (:user member)))]
84 4 : (telegram-chat-member-repo/upsert!
85 2 : repo
86 12 : {:chat-id chat-id
87 2 : :telegram-id telegram-id
88 2 : :user-id uid
89 2 : :status status})
90 2 : uid)
91 :
92 : ;; A definitive non-member status ("left"/"kicked") — drop any stale grant.
93 3 : (some? status)
94 2 : (do
95 8 : (when user-id (telegram-chat-member-repo/delete-by-chat-and-user! repo chat-id user-id))
96 : nil)
97 :
98 : ;; nil status — the Telegram call failed; leave existing rows untouched.
99 : :else nil)))
100 :
101 :
102 4 : (defn- grant-access!
103 : "Reconciles membership across all `chat-ids`, threading a lazily-created
104 : user-id so the account is provisioned at most once. Returns
105 : Either<Error, {:granted bool}>."
106 : [ctx bot chat-ids user-id telegram-id]
107 8 : (if-not telegram-id
108 0 : (either/left {:message "Authentication required."})
109 14 : (let [repo (get-in ctx [:repos :telegram-chat-member-postgres-repo])]
110 10 : (loop [chats (distinct chat-ids)
111 2 : uid user-id
112 : granted false]
113 8 : (if (empty? chats)
114 10 : (either/right {:granted granted})
115 22 : (let [member-uid (sync-chat-membership! ctx repo bot (first chats) uid telegram-id)]
116 2 : (recur (rest chats) (or uid member-uid) (or granted (boolean member-uid)))))))))
117 :
118 :
119 4 : (defn- resolve-event
120 : "Resolves an event by public-id or UUID for the membership grant decision."
121 : [ctx id]
122 0 : (let [repo (get-in ctx [:repos :event-postgres-repo])]
123 0 : (or
124 0 : (event-query/get-event-by-public-id repo id {:joins []})
125 0 : (when (utils.uuid/uuid-string? id) (event-query/get-event repo id {:joins []})))))
126 :
127 :
128 4 : (defn- resolve-template
129 : [ctx id]
130 14 : (let [repo (get-in ctx [:repos :template-postgres-repo])]
131 10 : (or
132 14 : (template-query/get-template-by-public-id repo id {:joins []})
133 20 : (when (utils.uuid/uuid-string? id) (template-query/get-template repo id {:joins []})))))
134 :
135 :
136 4 : (defn grant-event-access!
137 : "Grants event view access when the Telegram caller belongs to a linked chat."
138 : [ctx bot event-id user-id telegram-id]
139 0 : (if-not telegram-id
140 0 : (either/left {:message "Authentication required."})
141 0 : (if-let [event (resolve-event ctx event-id)]
142 0 : (let [chat-ids (keep :chat-id (telegram-link-query/resolve-links-for-event ctx event))]
143 0 : (grant-access! ctx bot chat-ids user-id telegram-id))
144 0 : (either/right {:granted false}))))
145 :
146 :
147 4 : (defn grant-template-access!
148 : "Grants the user view access to the template when they are a member of any
149 : Telegram chat the template is linked to. `template-id` may be a public-id or
150 : a UUID. `user-id` may be nil (auto-provisioned on first confirmed membership).
151 : Returns Either<Error, {:granted bool}>."
152 : [ctx bot template-id user-id telegram-id]
153 8 : (if-not telegram-id
154 5 : (either/left {:message "Authentication required."})
155 18 : (if-let [template (resolve-template ctx template-id)]
156 8 : (let [chat-ids (keep
157 : :chat-id
158 4 : (template-telegram-link-repo/find-by-template-id
159 12 : (get-in ctx [:repos :template-telegram-link-postgres-repo])
160 6 : (:uuid template)))]
161 14 : (grant-access! ctx bot chat-ids user-id telegram-id))
162 0 : (either/right {:granted false}))))
|