mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-23 05:21:15 +00:00
2e1dca9fe9
Settings, Integrations, wanderer gains an optional background check of the trails you imported, off until you switch it on. While on, TREK reads the short record of each such trail at most once a day (never its GPX file) and leaves a hint when the trail may have moved on: "may have changed" when only wanderer's change time moved, "changed" when its name or description proves it, and "not checkable" when the trail is gone, no longer yours or carries no change time. Nothing is ever said to be unchanged. A new hint arrives as a notification that opens the tour (a new ?place= link in the planner); the tour's provenance shows it with the source check right beside it, and the card lists all open hints in a dialog, each leading to its tour. The check reads the source in full as before and spends the hint; taking a change in stays the confirmed reimport. No tour is ever changed or deleted. The check only asks through your own connection, only about trails your account imported into trips you can still open, and stops when you switch it off, remove the connection, wanderer refuses the token (until a test passes or a new token is saved) or the admin switches wanderer off; hints and snapshots stay. A run asks at most 25 connections for at most 10 records each, one after another, and an instance that fails (an outage, a rate limit) is left alone for an hour, doubling up to two days. Server: migration 046120 adds the switch and the run bookkeeping to wanderer_connections and the tour_source_hints table (one row per snapshot, for its importer, gone with it). WandererWatchService and an hourly wanderer-watch job on the CronRegistrar live in the tours domain and read through WandererService; GET and PUT /api/integrations/wanderer/watch and GET /api/trips/:tripId/tours/wanderer/sources/:placeId/hint (place_edit, like the check) answer with @trek/shared contracts. A new user notification event, wanderer_source_hint, can be muted per channel. Saving a new token now clears what the last check said about the old one. There is no MCP tool, as for the rest of the personal wanderer connection. All 27 locales carry the strings, and the admin wiki page says what the check does.
174 lines
7.0 KiB
TypeScript
174 lines
7.0 KiB
TypeScript
import type { NotificationEventKey } from '@trek/shared/i18n/externalNotifications';
|
|
|
|
/**
|
|
* The notification vocabulary: event names, channel ids, and the contract a
|
|
* channel implements. No database, no DI, and — deliberately — no imports from
|
|
* anywhere else in the domain.
|
|
*
|
|
* This module exists to keep the notification cluster a DAG. The preferences
|
|
* service, the channel registry and the transports all need these names, and
|
|
* before they lived here the cluster was only acyclic because two of those
|
|
* edges happened to be `import type` and compiled away. The first person to put
|
|
* `NotifEventType` or `ALL_EVENT_TYPES` in a value position would have turned
|
|
* that into a real runtime cycle, with the built-in channels as a third ring.
|
|
*/
|
|
|
|
// ── Channels ───────────────────────────────────────────────────────────────
|
|
|
|
/**
|
|
* A channel id. Open by design: the built-ins plus `plugin:<id>` for any plugin
|
|
* implementing the `notificationChannel` hook. The DB column is a bare TEXT with
|
|
* no CHECK constraint and the Zod contract is already a string record, so the set
|
|
* was only ever closed in TypeScript.
|
|
*/
|
|
export type NotifChannel = string;
|
|
|
|
export const INAPP_CHANNEL = 'inapp';
|
|
|
|
// ── Events ─────────────────────────────────────────────────────────────────
|
|
|
|
export type NotifEventType =
|
|
| 'trip_invite'
|
|
| 'booking_change'
|
|
| 'trip_reminder'
|
|
| 'todo_due'
|
|
| 'vacay_invite'
|
|
| 'vacay_share'
|
|
| 'collection_invite'
|
|
| 'photos_shared'
|
|
| 'collab_message'
|
|
| 'packing_tagged'
|
|
| 'version_available'
|
|
| 'replica_failure'
|
|
| 'paddle_region_requested'
|
|
| 'synology_session_cleared'
|
|
| 'wanderer_source_hint'
|
|
| 'plugin_notification';
|
|
|
|
/** Every event, in the order the preferences UI lists them. */
|
|
export const ALL_EVENT_TYPES: NotifEventType[] = [
|
|
'trip_invite',
|
|
'booking_change',
|
|
'trip_reminder',
|
|
'todo_due',
|
|
'vacay_invite',
|
|
'vacay_share',
|
|
'collection_invite',
|
|
'photos_shared',
|
|
'collab_message',
|
|
'packing_tagged',
|
|
'version_available',
|
|
'replica_failure',
|
|
'paddle_region_requested',
|
|
'synology_session_cleared',
|
|
'wanderer_source_hint',
|
|
'plugin_notification',
|
|
];
|
|
|
|
/** Events that target admins only (shown in admin panel, not in user settings). */
|
|
export const ADMIN_SCOPED_EVENTS = new Set<NotifEventType>([
|
|
'version_available',
|
|
'replica_failure',
|
|
'paddle_region_requested',
|
|
]);
|
|
|
|
// Compile-time guard: shared NotificationEventKey and server NotifEventType must
|
|
// stay in sync. It sits next to the union it guards — it used to live in the
|
|
// transports module, three files away from the type it was protecting.
|
|
type _EvtFwd = NotifEventType extends NotificationEventKey ? true : never;
|
|
type _EvtBwd = NotificationEventKey extends NotifEventType ? true : never;
|
|
const _eventKeyDriftGuard: [_EvtFwd, _EvtBwd] = [true, true];
|
|
void _eventKeyDriftGuard;
|
|
|
|
/**
|
|
* Channels whose admin-scoped preference is global (app_settings) rather than
|
|
* per-user. Built-ins only: plugin channels are user-scoped and never carry
|
|
* admin-scoped events.
|
|
*/
|
|
const ADMIN_GLOBAL_CHANNELS = ['email', 'webhook', 'ntfy'] as const;
|
|
export type AdminGlobalChannel = (typeof ADMIN_GLOBAL_CHANNELS)[number];
|
|
|
|
export function isAdminGlobalChannel(channel: string): channel is AdminGlobalChannel {
|
|
return (ADMIN_GLOBAL_CHANNELS as readonly string[]).includes(channel);
|
|
}
|
|
|
|
// ── The channel contract ───────────────────────────────────────────────────
|
|
//
|
|
// A channel is anything that delivers a *rendered* notification out of TREK:
|
|
// email, webhook, ntfy, and any plugin that implements the `notificationChannel`
|
|
// hook. In-app is deliberately NOT a channel — it writes typed rows with
|
|
// scope/target/callback payloads rather than a title+body, so it keeps its own
|
|
// path in NotificationsService.send(). The split mirrors the one shared/ already
|
|
// draws in i18n/<locale>/externalNotifications.ts.
|
|
//
|
|
// Channels are declarative: they say what they support and whether a given user
|
|
// has credentials, and they send. They do NOT consult preferences — the admin
|
|
// toggle (`notification_channels`) and the per-user event opt-outs are applied by
|
|
// the dispatch loop.
|
|
|
|
/** A notification already rendered into the recipient's language. */
|
|
export interface ChannelMessage {
|
|
event: NotifEventType;
|
|
title: string;
|
|
body: string;
|
|
/** Relative navigate target (e.g. `/trips/12`) — what the email CTA builder takes. */
|
|
navigateTarget?: string;
|
|
/** Absolute link (appUrl + navigateTarget) — what webhook/ntfy/plugin payloads carry. */
|
|
url?: string;
|
|
tripName?: string;
|
|
}
|
|
|
|
export interface ExternalChannel {
|
|
readonly id: string;
|
|
readonly source: 'builtin' | 'plugin';
|
|
/** Built-ins: an i18n key the client resolves. */
|
|
readonly labelKey?: string;
|
|
/** Plugin channels: a literal display name (the host has no i18n for it). */
|
|
readonly label?: string;
|
|
/** Where the user configures their credentials, if anywhere. */
|
|
readonly settingsPath?: string;
|
|
/**
|
|
* Admin-scoped events (`version_available`) bypass the `notification_channels`
|
|
* toggle for this channel and are gated by the admin global pref instead.
|
|
* Only email does this today; preserved verbatim from the pre-registry dispatch.
|
|
*/
|
|
readonly bypassesActiveToggleForAdminEvents?: boolean;
|
|
/** Delivers the one admin-scoped global copy (not per-recipient). */
|
|
readonly supportsAdminGlobal?: boolean;
|
|
/**
|
|
* The user's matrix marks this channel inactive (no column, no settings card)
|
|
* while isInstanceConfigured() says no. Web Push sets it: without a usable key
|
|
* pair a browser could not even subscribe. Email does not, and keeps its
|
|
* column while SMTP is not set up, as it always has.
|
|
*/
|
|
readonly hiddenWhileInstanceUnconfigured?: boolean;
|
|
|
|
supportsEvent(event: NotifEventType): boolean;
|
|
/**
|
|
* Instance-level readiness, independent of any one recipient (email: is SMTP set
|
|
* up at all?). Absent means "always ready".
|
|
*/
|
|
isInstanceConfigured?(): boolean | Promise<boolean>;
|
|
/** Does this recipient have credentials for this channel? */
|
|
isConfiguredFor(userId: number): boolean | Promise<boolean>;
|
|
sendToUser(userId: number, msg: ChannelMessage): Promise<unknown>;
|
|
sendGlobal?(msg: ChannelMessage): Promise<unknown>;
|
|
test?(userId: number, override?: Record<string, unknown>): Promise<{ success: boolean; error?: string }>;
|
|
}
|
|
|
|
/** One channel column in the preferences matrix. */
|
|
export interface ChannelDescriptor {
|
|
id: string;
|
|
source: 'builtin' | 'plugin';
|
|
/** Built-ins: an i18n key. */
|
|
labelKey?: string;
|
|
/** Plugin channels: a literal display name. */
|
|
label?: string;
|
|
/** Where the user sets their credentials for this channel, if anywhere. */
|
|
settingsPath?: string;
|
|
/** The admin has enabled this channel (in-app is always on). */
|
|
active: boolean;
|
|
/** This user has credentials for it. */
|
|
configured: boolean;
|
|
}
|