mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-20 05:56:53 +00:00
1e6ab069cd
* feat(plugin-sdk): settings-field default + unknown-attribute warning
* feat(shared): default on the plugin settings-field contract
* feat(server): persist and serve plugin settings-field defaults
* feat(client): pre-fill plugin settings defaults in the admin and user forms
* feat(server): enforce required plugin settings on both save paths
* feat(client): block plugin-settings save on an empty required field
* feat(server): resolve plugin settings defaults at runtime
A settings-field `default` was stored and served to the form but never
reached the plugin: the child's init envelope, `ctx.settings.get()`, the
channel dispatch's readAll and the OAuth provider config all read the
stored blob only. Fold `stored ?? default` in on every path that hands
config to a plugin (settings-defaults.ts), and let a default satisfy
`required` on both save gates — refusing a save the runtime would have
accepted was a contradiction.
Also: one `isFilled` rule shared by the save gate and `hasRequired`
(checkbox exempt, whitespace empty); the install parser drops a
non-boolean checkbox default or a select default outside its options,
matching what the SDK already refuses; export SETTING_FIELD_KEYS so the
SDK's mirror can be parity-tested.
* feat(client): share the plugin settings form rules across all three forms
The mobile user settings screen was a third consumer of the fields/config
contract that never got `default` pre-fill or the required check, and
the seed/patch loops lived in five hand copies. Extract
seedSettingsValues / findMissingRequired / settingsPatch into one module
and use it from the admin instance form, the desktop user tab and the
mobile screen. The user forms now surface a 4xx `{ error }` from the
server (the required-field refusal) instead of the generic toast.
* feat(plugin-sdk): seed dev fixtures from manifest defaults
`trek-plugin dev` handed the plugin only the fixture values, so a plugin
relying on its manifest defaults read undefined in dev and only in dev.
Seed ctx.config / ctx.settings from the declared defaults (a fixture
value still wins) via a new exported settingDefaults(manifest, scope).
SETTING_FIELD_KEYS is now parity-tested against the host's list.
* docs(plugins): document settings defaults as runtime fallbacks
A `default` is the field's effective value wherever nobody set one, not
just a form pre-fill; `required` is enforced on both ends; unknown
attributes are dropped at install and warned on by validate. Add a
Settings section with the attribute table to the SDK README and update
the ctx type comments, the shared schema, and the wiki guide.
* add missing exports
95 lines
4.3 KiB
TypeScript
95 lines
4.3 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { DatabaseService } from '../database/database.service';
|
|
import { decrypt_api_key } from '../common/crypto/apiKeyCrypto';
|
|
import { safeParseConfig } from './plugin-config-parse';
|
|
import { isFilled, settingDefaults } from './settings-defaults';
|
|
|
|
/**
|
|
* A plugin's per-user settings, decrypted host-side.
|
|
*
|
|
* These three reads used to be bare module-level functions in plugins.service.ts that
|
|
* pulled the `db` singleton, with the comment "standalone (no Nest DI) so the RPC host
|
|
* wiring can call it directly". That reason expired: the RPC host wiring is a set of
|
|
* providers now, so they get injected like everything else, and there is no second
|
|
* route to the database that a test cannot substitute.
|
|
*
|
|
* Nothing here is safe to send to a client — every method returns plaintext secrets.
|
|
*/
|
|
@Injectable()
|
|
export class PluginUserSettingsService {
|
|
constructor(private readonly dbs: DatabaseService) {}
|
|
|
|
private get db() {
|
|
return this.dbs.connection;
|
|
}
|
|
|
|
/** One decrypted value for the acting user, for the runtime's `ctx.settings.get()`. */
|
|
readOne(pluginId: string, userId: number, key: string): unknown {
|
|
const isSecret =
|
|
(
|
|
this.db
|
|
.prepare("SELECT secret FROM plugin_settings_fields WHERE plugin_id = ? AND field_key = ? AND scope = 'user'")
|
|
.get(pluginId, key) as { secret: number } | undefined
|
|
)?.secret === 1;
|
|
const value = this.storedFor(pluginId, userId)[key];
|
|
if (value == null) return settingDefaults(this.db, pluginId, 'user')[key]; // unset → the manifest default, if any
|
|
return isSecret ? decrypt_api_key(value as string) : value;
|
|
}
|
|
|
|
/**
|
|
* ALL of a plugin's per-user settings for one user, decrypted.
|
|
*
|
|
* This is how a notification-channel hook reaches the recipient's credentials: that
|
|
* dispatch is host-initiated with no acting user, so `ctx.settings.get()` (which
|
|
* resolves against the acting user) would return undefined there.
|
|
*/
|
|
readAll(pluginId: string, userId: number): Record<string, unknown> {
|
|
const fields = this.db
|
|
.prepare("SELECT field_key, secret FROM plugin_settings_fields WHERE plugin_id = ? AND scope = 'user'")
|
|
.all(pluginId) as Array<{ field_key: string; secret: number }>;
|
|
const stored = this.storedFor(pluginId, userId);
|
|
// Null-prototype for the same reason as safeParseConfig: never let a field key write
|
|
// through to Object.prototype on the way out to the plugin.
|
|
const out: Record<string, unknown> = Object.create(null);
|
|
const defaults = settingDefaults(this.db, pluginId, 'user');
|
|
for (const field of fields) {
|
|
const value = stored[field.field_key];
|
|
if (value == null) {
|
|
if (field.field_key in defaults) out[field.field_key] = defaults[field.field_key];
|
|
continue;
|
|
}
|
|
out[field.field_key] = field.secret === 1 ? decrypt_api_key(value as string) : value;
|
|
}
|
|
return out;
|
|
}
|
|
|
|
/**
|
|
* Has this user filled in every `required`, `scope:'user'` field the plugin declares?
|
|
* A plugin with no required user fields is configured for everyone (an instance-wide
|
|
* channel, e.g. a shared workspace webhook).
|
|
*
|
|
* Same "filled" rule as the save gate (PluginsService.assertRequiredFilled) — a
|
|
* checkbox is exempt, whitespace is empty, a manifest default counts — because this is
|
|
* what decides whether a channel dispatches to the user, and a save the form accepted
|
|
* must not leave them "not configured".
|
|
*/
|
|
hasRequired(pluginId: string, userId: number): boolean {
|
|
const required = this.db
|
|
.prepare(
|
|
"SELECT field_key FROM plugin_settings_fields WHERE plugin_id = ? AND scope = 'user' AND required = 1 AND input_type != 'checkbox'",
|
|
)
|
|
.all(pluginId) as Array<{ field_key: string }>;
|
|
if (required.length === 0) return true;
|
|
const stored = this.storedFor(pluginId, userId);
|
|
const defaults = settingDefaults(this.db, pluginId, 'user');
|
|
return required.every((field) => isFilled(stored[field.field_key] ?? defaults[field.field_key]));
|
|
}
|
|
|
|
private storedFor(pluginId: string, userId: number): Record<string, unknown> {
|
|
const row = this.db
|
|
.prepare('SELECT config FROM plugin_user_config WHERE plugin_id = ? AND user_id = ?')
|
|
.get(pluginId, userId) as { config: string } | undefined;
|
|
return safeParseConfig(row?.config ?? '{}');
|
|
}
|
|
}
|