From 0a7185d38769e4fdbb6a91bcd0da4f9a4d294330 Mon Sep 17 00:00:00 2001 From: Bianca Date: Mon, 26 Oct 2026 20:59:01 +0100 Subject: [PATCH] feat(people): read Telegram group admins from the bot, without storing them People needs who administers each Telegram group (admins' groups, the Capo Admin area, the census), and Telegram is the only source (RFC v3 decision 2). The backend now asks the bot over the socket (getGroupAdmins) for the groups it stores; the bot answers from its cache of Telegram, and nothing is stored here. A group the bot has not read yet, or every group while the bot does not answer within 3 seconds, stays unknown (null), so People keeps what it last knew instead of reading "no admins". --- src/people/index.ts | 7 ++++--- src/people/model.ts | 26 +++++++++++++++++++++++++- src/people/types.ts | 2 +- src/websocket/index.ts | 19 +++++++++++++++++++ src/websocket/telegram.ts | 5 +++++ tests/people.test.ts | 24 +++++++++++++++++++++++- 6 files changed, 77 insertions(+), 6 deletions(-) diff --git a/src/people/index.ts b/src/people/index.ts index 9d363a2..fe5f9c6 100644 --- a/src/people/index.ts +++ b/src/people/index.ts @@ -5,7 +5,7 @@ import { decryptUser } from "@/utils/users" import { provisionMember, setMemberNumber } from "./azure-members" import { peopleConfig } from "./config" import { transferType } from "./files" -import { fail, receiptEligible } from "./model" +import { fail, receiptEligible, telegramGroupsWithAdmins } from "./model" import { createPeopleService } from "./service" import { createPeoplePgStore } from "./store-pg" @@ -26,10 +26,11 @@ export const people = createPeopleService({ telegramId: idp?.snapshot?.current()?.bySub(a.sub)?.telegramId ?? null, })) }, - // Group admins are native Telegram status (RFC v3 decision 2), which the backend does not store. + // Group admins are native Telegram status (RFC v3 decision 2): the bot reads them, the backend never stores them. async telegramGroups() { const groups = await DB.select().from(SCHEMA.TG.groups) - return groups.map((g) => ({ chatId: String(g.telegramId), title: g.title, admins: null })) + const { WSS } = await import("@/server") + return telegramGroupsWithAdmins(groups, await WSS.getGroupAdmins(groups.map((g) => g.telegramId))) }, async telegramUsers() { const users = await DB.select().from(SCHEMA.TG.users) diff --git a/src/people/model.ts b/src/people/model.ts index 6f4456e..e53fd78 100644 --- a/src/people/model.ts +++ b/src/people/model.ts @@ -1,7 +1,16 @@ import { createHash, randomUUID } from "node:crypto" import { TRPCError } from "@trpc/server" import * as C from "./contract" -import type { Officer, PeopleConfig, PeopleState, Person, StoredReceipt, UnitOfWork, Viewer } from "./types" +import type { + Officer, + PeopleConfig, + PeopleState, + Person, + StoredReceipt, + TelegramGroup, + UnitOfWork, + Viewer, +} from "./types" export function fail( code: "BAD_REQUEST" | "FORBIDDEN" | "NOT_FOUND" | "CONFLICT" | "PRECONDITION_FAILED", @@ -252,3 +261,18 @@ export function validateFields(team: C.TeamId, fields: C.TeamFields) { export function newFileId() { return randomUUID() } + +/** + * Telegram groups with their native admins (RFC v3 decision 2), which only Telegram owns: the bot reads them and the + * backend asks it, without storing them. A group the bot has not read yet, or every group while the bot does not + * answer, has `admins: null`, so People keeps what it last knew instead of reading "no admins". + */ +export function telegramGroupsWithAdmins( + groups: { telegramId: number; title: string }[], + admins: ReadonlyMap | null +): TelegramGroup[] { + return groups.map((g) => { + const userIds = admins?.get(g.telegramId) + return { chatId: String(g.telegramId), title: g.title, admins: userIds ? userIds.map(String) : null } + }) +} diff --git a/src/people/types.ts b/src/people/types.ts index 6ac15ab..a25243b 100644 --- a/src/people/types.ts +++ b/src/people/types.ts @@ -139,5 +139,5 @@ export type Account = { emailVerified?: boolean } export type TelegramUser = { id: string; name: string; username: string | null } -/** `admins` is null while the group's admin list is unknown: People then keeps the groups it last knew. */ +/** `admins` is null while the bot cannot tell the group's Telegram admins: People then keeps the groups it last knew. */ export type TelegramGroup = { chatId: string; title: string; admins: string[] | null } diff --git a/src/websocket/index.ts b/src/websocket/index.ts index 8e27d75..e65b78d 100644 --- a/src/websocket/index.ts +++ b/src/websocket/index.ts @@ -21,6 +21,8 @@ export type WebSocketServerOptions = { /** A missing acknowledgement must not hang the request that triggered the emit. */ const ACK_TIMEOUT_MS = 10_000 +/** People pages wait on this read: without the bot, they show groups without admins instead of hanging. */ +const READ_TIMEOUT_MS = 3_000 export const engine = new Engine() @@ -123,6 +125,23 @@ export class WebSocketServer { }) } + /** Each group's Telegram admins as the bot knows them, or null when the bot does not answer. */ + async getGroupAdmins(chatIds: number[]): Promise | null> { + const tgSocket = this.telegramSocket("getGroupAdmins") + if (!tgSocket) return null + + return new Promise((res) => { + tgSocket.timeout(READ_TIMEOUT_MS).emit("getGroupAdmins", { chatIds }, (timeout, groups) => { + if (timeout) { + logger.error({ err: timeout }, "[WS] Cannot read the groups' admins from the bot") + res(null) + } else { + res(new Map(groups.map((group) => [group.chatId, group.userIds]))) + } + }) + }) + } + async leaveChat(data: Parameters[0]): Promise { const tgSocket = this.telegramSocket("leaveChat") if (!tgSocket) return false diff --git a/src/websocket/telegram.ts b/src/websocket/telegram.ts index 1edd53e..b147e76 100644 --- a/src/websocket/telegram.ts +++ b/src/websocket/telegram.ts @@ -31,6 +31,11 @@ export interface ToClient { }, cb: (error: string | null) => void ) => void + /** + * The native Telegram admins of the given groups (RFC v3 decision 2), from the bot's cache of Telegram. Groups the + * bot has not read yet are left out. + */ + getGroupAdmins: (data: { chatIds: number[] }, cb: (groups: { chatId: number; userIds: number[] }[]) => void) => void leaveChat: ( data: { chatId: number diff --git a/tests/people.test.ts b/tests/people.test.ts index df779a5..f97e22b 100644 --- a/tests/people.test.ts +++ b/tests/people.test.ts @@ -1,7 +1,7 @@ import { describe, expect, it, vi } from "vitest" import { myAreaSchema } from "@/people/contract" import { isValidIban } from "@/people/iban" -import { academicYear, csvCell, today } from "@/people/model" +import { academicYear, csvCell, telegramGroupsWithAdmins, today } from "@/people/model" import { samplePdf } from "./esign-fixtures" import { admit, harness, issue, master, permissions, vice, viewer } from "./people-fixtures" @@ -489,3 +489,25 @@ it("keeps groups and pending removals while Telegram group admins are unknown", expect(person.discrepancies).toContainEqual(expect.objectContaining({ kind: "admin-removal" })) expect(h.state().persons["P-0003"].pendingGroupRemovals).toHaveLength(1) }) + +it("reads Telegram group admins from the bot, and none while it does not answer", () => { + const groups = [ + { telegramId: -1001, title: "Read" }, + { telegramId: -1002, title: "Read, no admins" }, + { telegramId: -1003, title: "Not read yet" }, + ] + expect( + telegramGroupsWithAdmins( + groups, + new Map([ + [-1001, [103, 104]], + [-1002, []], + ]) + ) + ).toEqual([ + { chatId: "-1001", title: "Read", admins: ["103", "104"] }, + { chatId: "-1002", title: "Read, no admins", admins: [] }, + { chatId: "-1003", title: "Not read yet", admins: null }, + ]) + expect(telegramGroupsWithAdmins(groups, null).map((g) => g.admins)).toEqual([null, null, null]) +})