mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-01 17:03:15 +00:00
feat(notifications): let a plugin register a notification channel
TREK's four channels (in-app, email, webhook, ntfy) were a closed set: notificationService.send() dispatched with four copy-pasted `if` blocks and no provider abstraction, so a fifth channel meant editing eight files by hand. A plugin could produce a notification via ctx.notify.send(), but never deliver one. A plugin now registers a channel with `hooks.notificationChannel` + `hook:notification-channel` on a plain `type: 'integration'` — not a new manifest type, so the TREK-Plugins registry schema and both its CI gates are untouched. Core refactor - New channel registry (services/notifications/): email/webhook/ntfy become ExternalChannel providers wrapping the EXISTING send functions — no delivery logic is rewritten, only relocated. In-app deliberately stays out: it writes typed rows with scope/target/callbacks, not a rendered title+body, the same line shared/ already draws with i18n/externalNotifications. - The event text is now rendered once per recipient instead of once per channel. - The channel set is open: NotifChannel becomes a string, the matrix is registry-derived, and the UI columns are server-driven. The DB column was already bare TEXT and the Zod contract already a string record — only the TypeScript and the two UIs were ever closed. The hook runs USERLESS. Every other hook is user-initiated, so actingUserId falls out of the request; a notification is host-initiated for an ARBITRARY recipient, so ctx.settings.get() would return undefined. The host resolves the recipient's decrypted scope:'user' settings itself and passes them as an argument. That is what lets a channel plugin be handed someone's push token WITHOUT being handed the right to read their trips as them. Enabling the plugin is the opt-in: a plugin channel is not gated on the admin's `notification_channels` list. A built-in always exists in code and needs an explicit switch; a plugin channel only exists because an admin enabled that plugin. (Nothing could write a `plugin:` id into that CSV anyway, and the admin toggle rebuilt it from three booleans, silently dropping anything else — so requiring a second opt-in meant the channel could never be turned on at all.) Also fixed, found while building this: - Plugin settings keys were unvalidated, so a field named `__proto__` or `constructor` resolved off Object.prototype: a REQUIRED field with such a name reported as configured for every user who had configured nothing — enough, for a channel, to be dispatched to everyone with no credentials. Keys are now constrained at install and the config blob is parsed null-prototype, so it is impossible even for an already-installed plugin. - A `select` field's options were cast straight through, so the obvious `["1","5"]` form rendered every dropdown entry BLANK (the client reads value/label). Now coerced, and malformed options are rejected. Also adds: operator-supplied egress hosts (a plugin talking to a self-hosted service can't name the operator's host at publish time, so an admin adds it post-install and the runtime re-spawns the child with the widened allow-list — only for a plugin that DECLARED operatorEgress, and only an admin, never a user); settings-page actions (a "Test connection" button, user-initiated so ctx.settings.get() returns the clicking user's own value); and a Gotify-shaped notification-channel template in the SDK. Verified end to end against a real Gotify container, not just in tests.
This commit is contained in:
@@ -40,7 +40,7 @@ describe('permission catalog', () => {
|
||||
'db:write:places', 'db:write:days', 'db:write:itinerary', 'db:write:trips',
|
||||
'db:meta',
|
||||
'ws:broadcast:trip', 'ws:broadcast:user',
|
||||
'hook:photo-provider', 'hook:calendar-source', 'hook:place-detail-provider', 'hook:trip-warning-provider', 'http:outbound',
|
||||
'hook:photo-provider', 'hook:calendar-source', 'hook:notification-channel', 'hook:place-detail-provider', 'hook:trip-warning-provider', 'http:outbound',
|
||||
]);
|
||||
for (const p of PERMISSION_CATALOG) expect(p.hint.length).toBeGreaterThan(0); // every option is described
|
||||
});
|
||||
@@ -120,3 +120,92 @@ describe('validateManifest dependency rules', () => {
|
||||
expect(validateManifest({ ...base, pluginDependencies: [{ id: 'koffi', version: '*' }, { id: 'koffi', version: '^1' }] }).ok).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('notification-channel template', () => {
|
||||
let tmp: string;
|
||||
beforeEach(() => { tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'chan-')); });
|
||||
afterEach(() => { fs.rmSync(tmp, { recursive: true, force: true }); });
|
||||
|
||||
function make(over: Parameters<typeof scaffold>[3] = {}) {
|
||||
scaffold('my-gotify', 'integration', tmp, { template: 'notification-channel', ...over });
|
||||
return JSON.parse(fs.readFileSync(path.join(tmp, 'my-gotify', 'trek-plugin.json'), 'utf8'));
|
||||
}
|
||||
|
||||
it('scaffolds a valid manifest with the hook grant, matching egress, and the capability', () => {
|
||||
const m = make({ egress: ['gotify.example.com'] });
|
||||
expect(m.type).toBe('integration'); // a channel is NOT a new plugin type
|
||||
expect(m.permissions).toContain('hook:notification-channel');
|
||||
expect(m.permissions).toContain('http:outbound:gotify.example.com');
|
||||
expect(m.egress).toEqual(['gotify.example.com']);
|
||||
expect(m.capabilities.notificationChannel.title).toBe('My Gotify');
|
||||
expect(validateManifest(m).ok).toBe(true);
|
||||
});
|
||||
|
||||
it('declares the per-user credential as a secret, required, user-scoped field', () => {
|
||||
const m = make();
|
||||
const token = m.settings.find((s: { key: string }) => s.key === 'appToken');
|
||||
// These three flags are exactly what makes the host hand it to the hook as `config`.
|
||||
expect(token.scope).toBe('user');
|
||||
expect(token.secret).toBe(true);
|
||||
expect(token.required).toBe(true);
|
||||
});
|
||||
|
||||
it('generates a server entry that implements the hook', () => {
|
||||
make();
|
||||
const js = fs.readFileSync(path.join(tmp, 'my-gotify', 'server', 'index.js'), 'utf8');
|
||||
expect(js).toContain('notificationChannel');
|
||||
expect(js).toContain('async send(msg, config, ctx)');
|
||||
expect(js).toContain('config.appToken');
|
||||
|
||||
// The invariant is about WHERE ctx.settings.get() may be used, not whether:
|
||||
// - the HOOK is userless, so it must read credentials from its `config` argument;
|
||||
// - an ACTION is user-initiated, so it SHOULD read them via ctx.settings.get().
|
||||
const hookBody = js.slice(js.indexOf('notificationChannel:'));
|
||||
expect(hookBody).not.toContain('ctx.settings.get');
|
||||
const actionBody = js.slice(js.indexOf('actions:'), js.indexOf('hooks:'));
|
||||
expect(actionBody).toContain('await ctx.settings.get');
|
||||
});
|
||||
|
||||
it('scaffolds a Test connection action so the user can verify credentials', () => {
|
||||
const m = make();
|
||||
expect(m.actions).toEqual([{ key: 'testConnection', label: 'Test connection' }]);
|
||||
expect(validateManifest(m).ok).toBe(true);
|
||||
});
|
||||
|
||||
it('rejects a malformed action key', () => {
|
||||
const m = make();
|
||||
expect(validateManifest({ ...m, actions: [{ key: '__proto__', label: 'x' }] }).ok).toBe(false);
|
||||
expect(validateManifest({ ...m, actions: [{ key: 'a', label: 'x' }, { key: 'a', label: 'y' }] }).ok).toBe(false);
|
||||
});
|
||||
|
||||
it('is server-only: no client/ dir, and a UI type is refused', () => {
|
||||
make();
|
||||
expect(fs.existsSync(path.join(tmp, 'my-gotify', 'client'))).toBe(false);
|
||||
expect(() => scaffold('my-widget', 'widget', tmp, { template: 'notification-channel' })).toThrow(/requires type "integration"/);
|
||||
});
|
||||
});
|
||||
|
||||
describe('capabilities.notificationChannel validation', () => {
|
||||
const base = {
|
||||
id: 'chan', name: 'Chan', version: '1.0.0', apiVersion: 1, type: 'integration',
|
||||
nativeModules: false, permissions: ['hook:notification-channel'],
|
||||
};
|
||||
|
||||
it('accepts a narrowed event list', () => {
|
||||
const r = validateManifest({ ...base, capabilities: { notificationChannel: { events: ['trip_invite', 'booking_change'] } } });
|
||||
expect(r.ok).toBe(true);
|
||||
});
|
||||
|
||||
it('rejects an event a plugin channel can never carry', () => {
|
||||
// version_available is admin-scoped — it goes out over the admin's own credentials.
|
||||
const r = validateManifest({ ...base, capabilities: { notificationChannel: { events: ['version_available'] } } });
|
||||
expect(r.ok).toBe(false);
|
||||
expect(r.errors!.join(' ')).toMatch(/not a plugin-deliverable event/);
|
||||
});
|
||||
|
||||
it('rejects the capability without the matching grant', () => {
|
||||
const r = validateManifest({ ...base, permissions: ['db:own'], capabilities: { notificationChannel: { title: 'X' } } });
|
||||
expect(r.ok).toBe(false);
|
||||
expect(r.errors!.join(' ')).toMatch(/requires the "hook:notification-channel" permission/);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user