peppa/packages/core/src/subscription-names.ts
2026-11-15 19:11:04 +05:00

27 lines
1.4 KiB
TypeScript

// PEP-121: a subscription's name usually carries the login of whoever connected it
// («Claude — tatyana.saifarova 5»). Only a superadmin and that person see it; everyone else
// gets a neutral name. Anything that shows a subscription to a user goes through here.
import type { Db } from "@peppa/db";
import { isSuperadmin } from "./monitor.ts";
import type { User } from "./users.ts";
const KIND: Record<string, string> = { claude: "Claude", codex: "ChatGPT", opencode: "OpenCode" };
/** «Подписка Claude» — the name for texts with no particular reader (messages to a project). */
export const publicSubscriptionName = (s: { provider: string }) => `Подписка ${KIND[s.provider] ?? "агента"}`;
export interface SubscriptionViewer {
admin: boolean;
userId: number;
}
export async function subscriptionViewer(db: Db, user: Pick<User, "id" | "login">): Promise<SubscriptionViewer> {
return { admin: await isSuperadmin(db, user), userId: user.id };
}
/** The real name for a superadmin and the one who connected the subscription, the neutral one otherwise. */
export function visibleSubscriptionName(s: { name: string; provider: string; createdBy?: number | null }, viewer: SubscriptionViewer | null | undefined): string {
if (viewer && (viewer.admin || (s.createdBy != null && s.createdBy === viewer.userId))) return s.name;
return publicSubscriptionName(s);
}