Skip to content

Commit

Permalink
fix: cleanup method per extension type
Browse files Browse the repository at this point in the history
  • Loading branch information
yassine-sallemi committed Jan 30, 2025
1 parent 5eb15cc commit a372529
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 89 deletions.
37 changes: 35 additions & 2 deletions api/src/channel/channel.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@
* 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 { Injectable, UnauthorizedException } from '@nestjs/common';
import {
Injectable,
OnApplicationBootstrap,
UnauthorizedException,
} from '@nestjs/common';
import { Request, Response } from 'express';

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 {
SocketGet,
Expand All @@ -27,14 +32,42 @@ import ChannelHandler from './lib/Handler';
import { ChannelName } from './types';

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

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

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

/**
* Cleanups the unregisterd channels from settings.
*
*/
async cleanup(): Promise<void> {
const activePlugins = this.getAll().map((handler) =>
handler.getNamespace(),
);

const channelSettings = (await this.settingService.getAllGroups()).filter(
(group) => group.split('_').pop() === 'channel',
) as HyphenToUnderscore<ChannelName>[];

const orphanSettings = channelSettings.filter(
(group) => !activePlugins.includes(group),
);

for (const group of orphanSettings) {
this.logger.log(`Deleting orphaned settings for ${group}...`);
await this.settingService.deleteGroup(group);
}
}

/**
* Registers a channel with a specific handler.
*
Expand Down
70 changes: 0 additions & 70 deletions api/src/extra/cleanup.service.ts

This file was deleted.

13 changes: 2 additions & 11 deletions api/src/extra/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
/*
* Copyright © 2025 Hexastack. All rights reserved.
* Copyright © 2024 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 { Module } from '@nestjs/common';

import { CleanupService } from './cleanup.service';

@Module({
providers: [CleanupService],
})
class CleanupModule {}

export default [CleanupModule];
export default [];
35 changes: 33 additions & 2 deletions api/src/helper/helper.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
* 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 { Injectable, InternalServerErrorException } from '@nestjs/common';
import {
Injectable,
InternalServerErrorException,
OnApplicationBootstrap,
} from '@nestjs/common';

import { LoggerService } from '@/logger/logger.service';
import { SettingService } from '@/setting/services/setting.service';
Expand All @@ -15,7 +19,7 @@ import BaseHelper from './lib/base-helper';
import { HelperName, HelperRegistry, HelperType, TypeOfHelper } from './types';

@Injectable()
export class HelperService {
export class HelperService implements OnApplicationBootstrap {
private registry: HelperRegistry = new Map();

constructor(
Expand All @@ -28,6 +32,33 @@ export class HelperService {
});
}

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

/**
* Cleanups the unregisterd helpers from settings.
*
*/
async cleanup(): Promise<void> {
const activeHelpers = this.getAll().map((handler) =>
handler.getNamespace(),
);

const helperSettings = (await this.settingService.getAllGroups()).filter(
(group) => group.split('_').pop() === 'helper',
) as HyphenToUnderscore<HelperName>[];

const orphanSettings = helperSettings.filter(
(group) => !activeHelpers.includes(group),
);

for (const group of orphanSettings) {
this.logger.log(`Deleting orphaned settings for ${group}...`);
await this.settingService.deleteGroup(group);
}
}

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

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

import { BaseBlockPlugin } from './base-block-plugin';
Expand All @@ -20,7 +21,7 @@ describe('PluginsService', () => {
beforeAll(async () => {
const module = await Test.createTestingModule({
providers: [PluginService, DummyPlugin],
imports: [LoggerModule],
imports: [LoggerModule, SettingModule],
}).compile();
pluginsService = module.get<PluginService>(PluginService);
await module.get<DummyPlugin>(DummyPlugin).onModuleInit();
Expand Down
45 changes: 42 additions & 3 deletions api/src/plugins/plugins.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@
* 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 { Injectable, InternalServerErrorException } from '@nestjs/common';
import {
Injectable,
InternalServerErrorException,
OnApplicationBootstrap,
} from '@nestjs/common';

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

import { BasePlugin } from './base-plugin.service';
import { PluginInstance } from './map-types';
Expand All @@ -26,7 +33,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>
implements OnApplicationBootstrap
{
/**
* 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 +44,37 @@ export class PluginService<T extends BasePlugin = BasePlugin> {
Object.keys(PluginType).map((t) => [t as PluginType, new Map()]),
);

constructor() {}
constructor(
private readonly settingService: SettingService,
private readonly logger: LoggerService,
) {}

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

/**
* Cleanups the unregisterd plugins from settings.
*
*/
async cleanup(): Promise<void> {
const activePlugins = this.getAll().map((handler) =>
handler.getNamespace(),
);

const pluginSettings = (await this.settingService.getAllGroups()).filter(
(group) => group.split('_').pop() === 'plugin',
) as HyphenToUnderscore<PluginName>[];

const orphanSettings = pluginSettings.filter(
(group) => !activePlugins.includes(group),
);

for (const group of orphanSettings) {
this.logger.log(`Deleting orphaned settings for ${group}...`);
await this.settingService.deleteGroup(group);
}
}

/**
* Registers a plugin with a given name.
Expand Down

0 comments on commit a372529

Please sign in to comment.