66 lines
3.1 KiB
TypeScript
66 lines
3.1 KiB
TypeScript
// PEP-113: superadmins learn about every new account — a message by their notification settings
|
||
// (event `users`: Telegram and browser push), once per account.
|
||
import { users, type Db } from "@peppa/db";
|
||
import { inArray, sql } from "drizzle-orm";
|
||
import { currentActor } from "./actor.ts";
|
||
import { platformAdmins } from "./monitor.ts";
|
||
import { deliverToUser } from "./notifications.ts";
|
||
import type { User } from "./users.ts";
|
||
import { m as tr } from "@peppa/shared/i18n";
|
||
|
||
/** How the account appeared: first eGov / Google login, or created by an administrator (web, CLI). */
|
||
export type NewUserVia = "egov" | "google" | "admin";
|
||
|
||
/** «Anna Petrova (anna.petrova)» or just the login. */
|
||
const who = (u: Pick<User, "login" | "name">) => (u.name && u.name.toLowerCase() !== u.login.toLowerCase() ? `${u.name} (${u.login})` : u.login);
|
||
|
||
export function newUserText(user: User, via: NewUserVia, o: { total: number; creator?: string | null; webUrl?: string }): string {
|
||
const how =
|
||
via === "egov"
|
||
? tr("вход через eGov")
|
||
: via === "google"
|
||
? user.googleEmail
|
||
? tr("вход через Google, {email}", { email: user.googleEmail })
|
||
: tr("вход через Google")
|
||
: o.creator
|
||
? tr("создан администратором {login}", { login: o.creator })
|
||
: tr("создан администратором");
|
||
const lines = [tr("👤 Новый пользователь: {who} — {how}", { who: who(user), how }), tr("Всего пользователей: {n}", { n: o.total })];
|
||
if (o.webUrl) lines.push(`${o.webUrl}/users`);
|
||
return lines.join("\n");
|
||
}
|
||
|
||
/**
|
||
* Tells the superadmins about a new account. Skips the newcomer and whoever created the account;
|
||
* never throws — a failed notification must not break a login. Returns how many admins got it.
|
||
*/
|
||
export async function announceNewUser(db: Db, user: User, via: NewUserVia): Promise<number> {
|
||
try {
|
||
const logins = await platformAdmins(db);
|
||
if (!logins.length) return 0;
|
||
const creatorId = via === "admin" ? (currentActor()?.userId ?? null) : null;
|
||
const admins = (await db.select().from(users).where(inArray(users.login, logins))).filter((u) => !u.disabledAt);
|
||
const [{ total } = { total: 0 }] = await db.select({ total: sql<number>`count(*)` }).from(users);
|
||
const text = newUserText(user, via, {
|
||
total,
|
||
creator: creatorId == null ? null : (admins.find((a) => a.id === creatorId)?.login ?? null),
|
||
webUrl: (process.env.PEPPA_WEB_URL ?? "").replace(/\/+$/, ""),
|
||
});
|
||
let sent = 0;
|
||
for (const a of admins) {
|
||
if (a.id === user.id || a.id === creatorId) continue;
|
||
const r = await deliverToUser(db, a, {
|
||
event: "users",
|
||
dedupeKey: `newuser:${user.id}:${a.telegramChatId}`,
|
||
pushKey: `newuser:${user.id}:u${a.id}`,
|
||
telegram: { params: { text, disable_web_page_preview: true } },
|
||
push: { title: tr("Новый пользователь Peppa FACTORY"), body: text.split("\n")[0]!, url: "/users", tag: `newuser:${user.id}` },
|
||
});
|
||
if (r.telegram || r.push) sent++;
|
||
}
|
||
return sent;
|
||
} catch {
|
||
return 0;
|
||
}
|
||
}
|