103 lines
6.6 KiB
TypeScript
103 lines
6.6 KiB
TypeScript
import { createHmac, timingSafeEqual } from "node:crypto";
|
||
import { OFFICES, PLACEMENT_SOURCES, type Application } from "./applications.js";
|
||
import type { DigestRun, Recipient, SourceStatus } from "./digest.js";
|
||
import { BOT_TEXTS, LANGS, TEAM_DIGEST_TEXTS, TEAM_TEXTS, type Lang } from "./i18n.js";
|
||
import { SOURCE_MAP } from "./resume-sources.js";
|
||
|
||
export const COOKIE = "team_session";
|
||
const esc = (s: string) => s.replace(/[&<>"']/g, (c) => `&#${c.charCodeAt(0)};`);
|
||
|
||
/** Значение cookie команды: HMAC от ключа доступа (сам ключ в cookie не кладём). */
|
||
export const sessionValue = (apiToken: string) => createHmac("sha256", apiToken).update("team-session-v1").digest("hex");
|
||
|
||
export function sameSecret(a: string, b: string): boolean {
|
||
if (!a || !b) return false;
|
||
const x = Buffer.from(a);
|
||
const y = Buffer.from(b);
|
||
return x.length === y.length && timingSafeEqual(x, y);
|
||
}
|
||
|
||
export function readCookie(header: string | undefined, name: string): string | undefined {
|
||
for (const part of (header ?? "").split(";")) {
|
||
const [k, ...v] = part.trim().split("=");
|
||
if (k === name) return v.join("=");
|
||
}
|
||
return undefined;
|
||
}
|
||
|
||
function page(lang: Lang, title: string, body: string): string {
|
||
const nav = LANGS.map((l) => `<a href="/team?lang=${l}">${l}</a>`).join(" ");
|
||
return `<!doctype html><html lang="${lang}"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><meta name="robots" content="noindex"><title>${esc(title)}</title>
|
||
<style>body{font:16px/1.5 system-ui,sans-serif;max-width:64rem;margin:0 auto;padding:1rem 16px;color:#222}a{color:#0a58ca}table{border-collapse:collapse;width:100%}th,td{border-bottom:1px solid #ddd;padding:.4rem;text-align:left;vertical-align:top}.err{color:#b00020}input,select,button{font:inherit;padding:.3rem .5rem}code{word-break:break-all}</style></head><body>
|
||
<p>${nav}</p><h1>${esc(title)}</h1>${body}</body></html>`;
|
||
}
|
||
|
||
export function renderLogin(lang: Lang, state: "form" | "bad" | "nokey"): string {
|
||
const t = TEAM_TEXTS[lang];
|
||
if (state === "nokey") return page(lang, t.loginTitle, `<p>${esc(t.noKey)}</p>`);
|
||
const err = state === "bad" ? `<p class="err">${esc(t.badKey)}</p>` : "";
|
||
return page(lang, t.loginTitle, `<p>${esc(t.loginHint)}</p>${err}
|
||
<form method="post" action="/team/login?lang=${lang}"><label>${esc(t.keyLabel)}<br><input type="password" name="key" autocomplete="current-password" required></label> <button type="submit">${esc(t.btnLogin)}</button></form>`);
|
||
}
|
||
|
||
export interface DigestView {
|
||
recipient?: Recipient; lastRun?: DigestRun; pending: number; statuses: SourceStatus[];
|
||
/** Источники с автоматическим поиском, для которых задан ключ. */
|
||
configured: string[];
|
||
/** Только что созданный код подключения получателя. */
|
||
linkCode?: string;
|
||
}
|
||
|
||
export function renderDigest(lang: Lang, v: DigestView, botUsername: string | undefined): string {
|
||
const t = TEAM_DIGEST_TEXTS[lang];
|
||
const btn = (action: string, label: string) =>
|
||
`<form method="post" action="/team/digest/${action}?lang=${lang}" style="display:inline"><button type="submit">${esc(label)}</button></form>`;
|
||
const who = v.recipient
|
||
? `<p>${esc(t.recipient(v.recipient.name + (v.recipient.username ? ` (@${v.recipient.username})` : ""), v.recipient.linkedAt.slice(0, 10)))}</p>`
|
||
: `<p>${esc(t.noRecipient)}</p>`;
|
||
const link = !botUsername
|
||
? `<p>${esc(t.linkNoBot)}</p>`
|
||
: v.linkCode
|
||
? `<p><code>https://t.me/${esc(botUsername)}?start=dg_${esc(v.linkCode)}</code></p><p>${esc(t.linkHint)}</p>`
|
||
: btn("link", t.btnLink);
|
||
const run = v.lastRun
|
||
? `<p>${esc(t.lastRun(v.lastRun.at, t.delivery[v.lastRun.state] + (v.lastRun.state === "delivered" ? t.sentCount(v.lastRun.sent) : "")))}</p>`
|
||
: `<p>${esc(t.noRuns)}</p>`;
|
||
const rows = SOURCE_MAP.map((src) => {
|
||
let state = "—";
|
||
if (src.mode === "api") {
|
||
const st = v.statuses.find((x) => x.source === src.id);
|
||
state = !v.configured.includes(src.id) ? t.states.needs_access
|
||
: st ? t.states[st.state] + t.checked(st.checkedAt.slice(0, 16)) : t.states.unchecked;
|
||
} else if (src.mode === "manual") {
|
||
state = `<a href="${esc(src.url)}" rel="noreferrer">${esc(t.manualLink)}</a>`;
|
||
return `<tr><td>${esc(src.name)}</td><td>${esc(t.modes[src.id] ?? "")}</td><td>${state}</td></tr>`;
|
||
}
|
||
return `<tr><td>${esc(src.name)}</td><td>${esc(t.modes[src.id] ?? "")}</td><td>${esc(state)}</td></tr>`;
|
||
}).join("");
|
||
return `<h2>${esc(t.title)}</h2><p>${esc(t.intro)}</p>${who}${link}${run}<p>${esc(t.pending(v.pending))}</p>
|
||
<p>${btn("run", t.btnRunNow)} ${v.recipient ? btn("test", t.btnTest) : ""}</p>
|
||
<h3>${esc(t.sourcesTitle)}</h3><table><thead><tr><th>${esc(t.cols.source)}</th><th>${esc(t.cols.how)}</th><th>${esc(t.cols.state)}</th></tr></thead><tbody>${rows}</tbody></table>`;
|
||
}
|
||
|
||
export function renderTeam(lang: Lang, apps: Application[], office: string | undefined, botUsername: string | undefined, digest?: DigestView): string {
|
||
const t = TEAM_TEXTS[lang];
|
||
const b = BOT_TEXTS[lang];
|
||
const opts = [`<option value="">${esc(t.allOffices)}</option>`, ...OFFICES.map((o) =>
|
||
`<option value="${o}"${o === office ? " selected" : ""}>${esc(b.offices[o])}</option>`)].join("");
|
||
const filter = `<form method="get" action="/team"><input type="hidden" name="lang" value="${lang}"><select name="office">${opts}</select> <button type="submit">${esc(t.btnFilter)}</button></form>`;
|
||
const c = t.cols;
|
||
const rows = apps.map((a) => `<tr><td>${esc(a.createdAt)}</td><td>${esc(a.fullName)}</td><td><a href="tel:+${esc(a.phone)}">+${esc(a.phone)}</a></td>
|
||
<td>${a.tgUsername ? `<a href="https://t.me/${esc(a.tgUsername)}">@${esc(a.tgUsername)}</a>` : "—"}</td><td>${esc(b.cities[a.city] ?? a.city)}</td><td>${esc(b.offices[a.office] ?? a.office)}</td><td>${esc(a.source === "direct" ? t.direct : a.source)}</td></tr>`).join("");
|
||
const table = apps.length
|
||
? `<table><thead><tr><th>${c.date}</th><th>${c.name}</th><th>${c.phone}</th><th>${c.telegram}</th><th>${c.city}</th><th>${c.office}</th><th>${c.source}</th></tr></thead><tbody>${rows}</tbody></table>`
|
||
: `<p>${esc(t.empty)}</p>`;
|
||
const links = botUsername
|
||
? `<ul>${PLACEMENT_SOURCES.map((s) => `<li>${esc(s)}: <code>https://t.me/${esc(botUsername)}?start=${s}</code></li>`).join("")}</ul><p>${esc(t.linksHint)}</p>`
|
||
: `<p>${esc(t.noBot)}</p>`;
|
||
return page(lang, t.title, `<p>${esc(t.intro)}</p>
|
||
<form method="post" action="/team/logout"><button type="submit">${esc(t.logout)}</button></form>
|
||
${digest ? renderDigest(lang, digest, botUsername) : ""}
|
||
${filter}${table}<h2>${esc(t.linksTitle)}</h2>${links}`);
|
||
}
|