Unify city+cafe into one places page, gate join with on-page modal, and enrich ntfy with icons, tables, and action buttons across portal/API/admin. Co-authored-by: Cursor <cursoragent@cursor.com>
139 lines
3.7 KiB
TypeScript
139 lines
3.7 KiB
TypeScript
/**
|
|
* Publish to self-hosted ntfy (https://ntfy.nomadro.cn).
|
|
* Env: NTFY_BASE_URL, NTFY_TOPIC, NTFY_TOKEN (optional Bearer).
|
|
* Rich payload: markdown / icon / click / attach / actions.
|
|
* @see https://docs.ntfy.sh/publish/
|
|
*/
|
|
|
|
export type NtfyPriority =
|
|
| "min"
|
|
| "low"
|
|
| "default"
|
|
| "high"
|
|
| "urgent"
|
|
| number;
|
|
|
|
export type NtfyAction = {
|
|
action: "view" | "http" | "broadcast";
|
|
label: string;
|
|
url: string;
|
|
clear?: boolean;
|
|
method?: string;
|
|
headers?: Record<string, string>;
|
|
body?: string;
|
|
};
|
|
|
|
export type NtfyPublishInput = {
|
|
title: string;
|
|
message: string;
|
|
priority?: NtfyPriority;
|
|
tags?: string | string[];
|
|
topic?: string;
|
|
/** Render message as Markdown (lists, bold, links, images). */
|
|
markdown?: boolean;
|
|
/** URL opened when the notification is tapped. */
|
|
click?: string;
|
|
/** JPEG/PNG icon URL shown beside the notification. */
|
|
icon?: string;
|
|
/** External attachment URL (image preview when image). */
|
|
attach?: string;
|
|
filename?: string;
|
|
/** Up to 3 action buttons. */
|
|
actions?: NtfyAction[];
|
|
};
|
|
|
|
const DEFAULT_ICON =
|
|
process.env.NTFY_ICON_URL?.trim() ||
|
|
"https://nomadro.com/icon-192.png";
|
|
|
|
export function ntfyConfigured(): boolean {
|
|
const base = (process.env.NTFY_BASE_URL || "https://ntfy.nomadro.cn").trim();
|
|
const topic = (process.env.NTFY_TOPIC || "nomadro").trim();
|
|
return Boolean(base && topic);
|
|
}
|
|
|
|
function resolvePriority(raw: NtfyPriority | undefined): number {
|
|
if (typeof raw === "number") return Math.max(1, Math.min(5, Math.round(raw)));
|
|
return (
|
|
({ min: 1, low: 2, default: 3, high: 4, urgent: 5 } as const)[
|
|
raw || "default"
|
|
] ?? 3
|
|
);
|
|
}
|
|
|
|
function resolveTags(tags: string | string[] | undefined): string[] {
|
|
if (!tags) return [];
|
|
const list = Array.isArray(tags) ? tags : tags.split(",");
|
|
return list.map((t) => t.trim()).filter(Boolean).slice(0, 12);
|
|
}
|
|
|
|
export async function publishNtfy(input: NtfyPublishInput): Promise<{
|
|
ok: boolean;
|
|
skipped?: boolean;
|
|
status?: number;
|
|
error?: string;
|
|
}> {
|
|
const base = (process.env.NTFY_BASE_URL || "https://ntfy.nomadro.cn").replace(
|
|
/\/$/,
|
|
"",
|
|
);
|
|
const topic = (input.topic || process.env.NTFY_TOPIC || "nomadro").replace(
|
|
/^\/+|\/+$/g,
|
|
"",
|
|
);
|
|
if (!base || !topic) {
|
|
return { ok: false, skipped: true, error: "ntfy not configured" };
|
|
}
|
|
|
|
// JSON body: Node fetch rejects non-ASCII in Title/Tags headers (ByteString).
|
|
const headers: Record<string, string> = {
|
|
"Content-Type": "application/json",
|
|
};
|
|
const token = (process.env.NTFY_TOKEN || "").trim();
|
|
if (token) headers.Authorization = `Bearer ${token}`;
|
|
|
|
const tags = resolveTags(input.tags);
|
|
const actions = (input.actions || []).slice(0, 3);
|
|
const markdown = input.markdown !== false;
|
|
const icon = (input.icon || DEFAULT_ICON).trim();
|
|
|
|
const body = JSON.stringify({
|
|
topic,
|
|
title: input.title.slice(0, 200),
|
|
message: input.message.slice(0, 3900),
|
|
priority: resolvePriority(input.priority),
|
|
markdown,
|
|
...(tags.length ? { tags } : {}),
|
|
...(icon ? { icon } : {}),
|
|
...(input.click ? { click: input.click.slice(0, 512) } : {}),
|
|
...(input.attach
|
|
? {
|
|
attach: input.attach.slice(0, 512),
|
|
...(input.filename
|
|
? { filename: input.filename.slice(0, 120) }
|
|
: {}),
|
|
}
|
|
: {}),
|
|
...(actions.length ? { actions } : {}),
|
|
});
|
|
|
|
try {
|
|
const res = await fetch(`${base}/`, {
|
|
method: "POST",
|
|
headers,
|
|
body,
|
|
cache: "no-store",
|
|
});
|
|
if (!res.ok) {
|
|
return {
|
|
ok: false,
|
|
status: res.status,
|
|
error: await res.text().then((t) => t.slice(0, 200)),
|
|
};
|
|
}
|
|
return { ok: true, status: res.status };
|
|
} catch (e) {
|
|
return { ok: false, error: e instanceof Error ? e.message : String(e) };
|
|
}
|
|
}
|