Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Cleanup orphan extensions' settings #646

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions api/src/channel/channel.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import { SubscriberService } from '@/chat/services/subscriber.service';
import { CONSOLE_CHANNEL_NAME } from '@/extensions/channels/console/settings';
import { WEB_CHANNEL_NAME } from '@/extensions/channels/web/settings';
import { LoggerService } from '@/logger/logger.service';
import { SettingService } from '@/setting/services/setting.service';
import { getSessionStore } from '@/utils/constants/session-store';
import { ExtensionService } from '@/utils/generics/extension-service';
import {
SocketGet,
SocketPost,
Expand All @@ -27,13 +29,27 @@ import ChannelHandler from './lib/Handler';
import { ChannelName } from './types';

@Injectable()
export class ChannelService {
export class ChannelService extends ExtensionService<
ChannelHandler<ChannelName>
> {
private registry: Map<string, ChannelHandler<ChannelName>> = new Map();

constructor(
private readonly logger: LoggerService,
protected readonly logger: LoggerService,
protected readonly settingService: SettingService,
private readonly subscriberService: SubscriberService,
) {}
) {
super(settingService, logger);
}

/**
* Retrieves the type of extension this service manages.
*
* @returns The type of extension this service manages.
*/
public getExtensionType(): 'channel' {
return 'channel';
}

/**
* Registers a channel with a specific handler.
Expand Down
18 changes: 15 additions & 3 deletions api/src/helper/helper.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,36 @@ import { Injectable, InternalServerErrorException } from '@nestjs/common';

import { LoggerService } from '@/logger/logger.service';
import { SettingService } from '@/setting/services/setting.service';
import { ExtensionService } from '@/utils/generics/extension-service';

import BaseHelper from './lib/base-helper';
import { HelperName, HelperRegistry, HelperType, TypeOfHelper } from './types';

@Injectable()
export class HelperService {
export class HelperService extends ExtensionService<BaseHelper> {
private registry: HelperRegistry = new Map();

constructor(
private readonly settingService: SettingService,
private readonly logger: LoggerService,
protected readonly settingService: SettingService,
protected readonly logger: LoggerService,
) {
super(settingService, logger);

// Init empty registry
Object.values(HelperType).forEach((type: HelperType) => {
this.registry.set(type, new Map());
});
}

/**
* Retrieves the type of extension this service manages.
*
* @returns The type of extension this service manages.
*/
protected getExtensionType(): 'helper' {
return 'helper';
}

/**
* Registers a helper.
*
Expand Down
12 changes: 11 additions & 1 deletion api/src/plugins/plugins.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,27 @@
import { Test } from '@nestjs/testing';

import { LoggerModule } from '@/logger/logger.module';
import { SettingService } from '@/setting/services/setting.service';
import { DummyPlugin } from '@/utils/test/dummy/dummy.plugin';

import { BaseBlockPlugin } from './base-block-plugin';
import { PluginService } from './plugins.service';
import { PluginType } from './types';

// Mock services
const mockSettingService = {
get: jest.fn(),
} as unknown as SettingService;

describe('PluginsService', () => {
let pluginsService: PluginService;
beforeAll(async () => {
const module = await Test.createTestingModule({
providers: [PluginService, DummyPlugin],
providers: [
PluginService,
DummyPlugin,
{ provide: SettingService, useValue: mockSettingService },
],
imports: [LoggerModule],
}).compile();
pluginsService = module.get<PluginService>(PluginService);
Expand Down
26 changes: 23 additions & 3 deletions api/src/plugins/plugins.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
Expand All @@ -8,6 +8,10 @@

import { Injectable, InternalServerErrorException } from '@nestjs/common';

import { LoggerService } from '@/logger/logger.service';
import { SettingService } from '@/setting/services/setting.service';
import { ExtensionService } from '@/utils/generics/extension-service';

import { BasePlugin } from './base-plugin.service';
import { PluginInstance } from './map-types';
import { PluginName, PluginType } from './types';
Expand All @@ -26,7 +30,9 @@ import { PluginName, PluginType } from './types';
* @typeparam T - The plugin type, which extends from `BasePlugin`. By default, it uses `BaseBlockPlugin`.
*/
@Injectable()
export class PluginService<T extends BasePlugin = BasePlugin> {
export class PluginService<
T extends BasePlugin = BasePlugin,
> extends ExtensionService<T> {
/**
* The registry of plugins, stored as a map where the first key is the type of plugin,
* the second key is the name of the plugin and the value is a plugin of type `T`.
Expand All @@ -35,7 +41,21 @@ export class PluginService<T extends BasePlugin = BasePlugin> {
Object.keys(PluginType).map((t) => [t as PluginType, new Map()]),
);

constructor() {}
constructor(
protected readonly settingService: SettingService,
protected readonly logger: LoggerService,
) {
super(settingService, logger);
}

/**
* Retrieves the type of extension this service manages.
*
* @returns The type of extension this service manages.
*/
public getExtensionType(): 'plugin' {
return 'plugin';
}

/**
* Registers a plugin with a given name.
Expand Down
33 changes: 33 additions & 0 deletions api/src/setting/services/setting.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
} from '@/utils/constants/cache';
import { Cacheable } from '@/utils/decorators/cacheable.decorator';
import { BaseService } from '@/utils/generics/base-service';
import { ExtensionName } from '@/utils/types/extension';

import { SettingCreateDto } from '../dto/setting.dto';
import { SettingRepository } from '../repositories/setting.repository';
Expand Down Expand Up @@ -51,6 +52,15 @@ export class SettingService extends BaseService<Setting> {
}
}

/**
* Removes all settings that belong to the provided group.
*
* @param group - The group of settings to remove.
*/
async deleteGroup(group: string) {
await this.repository.deleteMany({ group });
}

/**
* Loads all settings and returns them grouped in ascending order by weight.
*
Expand Down Expand Up @@ -100,6 +110,29 @@ export class SettingService extends BaseService<Setting> {
);
}

/**
*
* Retrieves a list of all setting groups for a specific extension type.
*
* @param extensionType - The extension type to filter by.
*
* @returns A promise that resolves to a list of setting groups.
*/
async getExtensionSettings(
extensionType: 'channel' | 'plugin' | 'helper',
): Promise<HyphenToUnderscore<ExtensionName>[]> {
const settings = await this.find({
group: { $regex: `_${extensionType}$` },
});
const groups = settings.reduce<string[]>((acc, setting) => {
if (!acc.includes(setting.group)) {
acc.push(setting.group);
}
return acc;
}, []) as HyphenToUnderscore<ExtensionName>[];
return groups;
}

/**
* Retrieves the application configuration object.
*
Expand Down
62 changes: 62 additions & 0 deletions api/src/utils/generics/extension-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
*/

import { OnApplicationBootstrap } from '@nestjs/common';

import { LoggerService } from '@/logger/logger.service';
import { SettingService } from '@/setting/services/setting.service';

export abstract class ExtensionService<
T extends { getNamespace(): string; getName(): string },
> implements OnApplicationBootstrap
{
constructor(
protected readonly settingService: SettingService,
protected readonly logger: LoggerService,
) {}

async onApplicationBootstrap(): Promise<void> {
await this.cleanup(this.getExtensionType());
}

/**
* Cleanups the unregistered extensions from settings.
*
* @param extensionType - The type of extension (e.g., 'plugin', 'helper', 'channel').
*/
async cleanup(extensionType: 'channel' | 'plugin' | 'helper'): Promise<void> {
const activeExtensions = this.getAll().map((handler) =>
handler.getNamespace(),
);

const orphanSettings = (
await this.settingService.getExtensionSettings(extensionType)
).filter((group) => !activeExtensions.includes(group));

await Promise.all(
orphanSettings.map(async (group) => {
this.logger.log(`Deleting orphaned settings for ${group}...`);
return this.settingService.deleteGroup(group);
}),
);
}

/**
* Retrieves all registered extensions as an array.
*
* @returns An array containing all the registered extensions.
*/
public abstract getAll(): T[];

/**
* Abstract method to get the type of extension this service manages.
*
* @returns The type of extension (e.g., 'plugin', 'helper', 'channel').
*/
protected abstract getExtensionType(): 'channel' | 'plugin' | 'helper';
}