mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-21 19:40:34 +00:00
6680bc7c74
PluginsModule was 21 controllers, 24 domain imports and 10 providers in one class, which made it the hub most of the Nest graph ran through: AdminModule imported it for a single cascade-disable call and inherited all of it. It is now PluginsRuntimeModule (supervisor, capability router, hook contracts), PluginContributionsModule (the 14 read-only hook controllers, moved to contributions/), PluginOAuthModule (the credential leaf, moved to oauth/) and a composition root that keeps the install and delivery controllers. AdminModule imports the runtime alone. The three plugin files that reached for the db singleton alongside the injected DatabaseService no longer do. plugin-host-state and signature-status take the connection from the caller, and the three per-user settings functions that were bare exports 'so the RPC host wiring can call them directly' are now PluginUserSettingsService - the host wiring is providers, so it injects them. Coverage moves from one repo-wide 80% to a per-domain ratchet. The single number let a well-covered domain subsidise a thin one: booking-import sits at 50% and integrations at 20%, and the aggregate still cleared the bar, so either could have lost another ten points unnoticed. Each domain now carries its own measured floor, regenerated by scripts/coverage-thresholds.mjs. Two test-side findings on the way: expect(undefined).toContain(SomeClass) PASSES in this vitest version - it only throws for a string needle. Every '<X> is listed in its module providers' guard was written that way, so all twelve of them silently stopped guarding the moment their class moved. They go through expectRegisteredProvider now, which asserts the array shape first. The atlas region test paid for the one-off decompression of the multi-MB admin1 bundle inside its own 5s budget, which is why it failed under load and passed alone. The bundle is warmed in beforeAll instead.
85 lines
3.6 KiB
TypeScript
85 lines
3.6 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';
|
|
|
|
/**
|
|
* 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 undefined;
|
|
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);
|
|
for (const field of fields) {
|
|
const value = stored[field.field_key];
|
|
if (value == null) 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).
|
|
*/
|
|
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")
|
|
.all(pluginId) as Array<{ field_key: string }>;
|
|
if (required.length === 0) return true;
|
|
const stored = this.storedFor(pluginId, userId);
|
|
return required.every((field) => {
|
|
const value = stored[field.field_key];
|
|
return value != null && String(value) !== '';
|
|
});
|
|
}
|
|
|
|
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 ?? '{}');
|
|
}
|
|
}
|