Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

perf(api): load elasticsearch only when it is the search engine

Hampus4d027684e7c4
diff --git a/fluxer_api/src/api/SearchFactory.ts b/fluxer_api/src/api/SearchFactory.ts
index 70a30654d..cab38252d 100644
--- a/fluxer_api/src/api/SearchFactory.ts
+++ b/fluxer_api/src/api/SearchFactory.ts
@@ -32,14 +32,7 @@ import {
import {ThreadSearchIndex} from '@app/api/search/thread/ThreadSearchIndex';
import {DEFAULT_SEARCH_CLIENT_TIMEOUT_MS} from '@fluxer/constants/src/Timeouts';
import type {ElasticsearchDistributedLock} from '@pkgs/elasticsearch_search/src/adapters/ElasticsearchIndexAdapter';
-import {ElasticsearchMessageAdapter} from '@pkgs/elasticsearch_search/src/adapters/ElasticsearchMessageAdapter';
-import {ElasticsearchReportAdapter} from '@pkgs/elasticsearch_search/src/adapters/ElasticsearchReportAdapter';
-import {ElasticsearchThreadAdapter} from '@pkgs/elasticsearch_search/src/adapters/ElasticsearchThreadAdapter';
-import {ElasticsearchUserAdapter} from '@pkgs/elasticsearch_search/src/adapters/ElasticsearchUserAdapter';
-import {
- createElasticsearchClient,
- type ElasticsearchClientConfig,
-} from '@pkgs/elasticsearch_search/src/ElasticsearchClient';
+import type {ElasticsearchClientConfig} from '@pkgs/elasticsearch_search/src/ElasticsearchClient';
let searchProvider: ISearchProvider | null = null;
let injectedSearchProvider: ISearchProvider | undefined;
@@ -60,19 +53,10 @@ function createElasticsearchSearchProvider(lock?: ElasticsearchDistributedLock):
};
return new SearchProvider(
async () => {
- const {GuildDiscoveryRepository} = await import('@app/api/guild/repositories/GuildDiscoveryRepository');
- const client = createElasticsearchClient(config);
- const message = new MessageSearchService(new ElasticsearchMessageAdapter({client, lock}));
- const guild = new GuildSearchService(
- new ElasticsearchGuildAdapter({client, lock}),
- new GuildDiscoveryRepository(),
+ const {createElasticsearchSearchServices} = await import(
+ '@app/api/search/elasticsearch/ElasticsearchSearchServices'
);
- const user = new UserSearchService(new ElasticsearchUserAdapter({client, lock}));
- const report = new ReportSearchService(new ElasticsearchReportAdapter({client, lock}));
- const auditLog = new AuditLogSearchService(new ElasticsearchAuditLogAdapter({client, lock}));
- const guildMember = new GuildMemberSearchService(new ElasticsearchGuildMemberAdapter({client, lock}));
- const thread = new ThreadSearchIndex(new ElasticsearchThreadAdapter({client, lock}));
- return {message, guild, user, report, auditLog, guildMember, thread};
+ return createElasticsearchSearchServices(config, lock);
},
() => Logger.info({node: config.node}, 'ElasticsearchSearchProvider initialised'),
);
diff --git a/fluxer_api/src/api/search/elasticsearch/ElasticsearchSearchServices.ts b/fluxer_api/src/api/search/elasticsearch/ElasticsearchSearchServices.ts
new file mode 100644
index 000000000..f43e3d957
--- /dev/null
+++ b/fluxer_api/src/api/search/elasticsearch/ElasticsearchSearchServices.ts
@@ -0,0 +1,40 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+import {GuildDiscoveryRepository} from '@app/api/guild/repositories/GuildDiscoveryRepository';
+import type {SearchServices} from '@app/api/infrastructure/SearchProvider';
+import {
+ AuditLogSearchService,
+ GuildMemberSearchService,
+ GuildSearchService,
+ MessageSearchService,
+ ReportSearchService,
+ UserSearchService,
+} from '@app/api/search/SearchServices';
+import {ThreadSearchIndex} from '@app/api/search/thread/ThreadSearchIndex';
+import {ElasticsearchAuditLogAdapter} from '@pkgs/elasticsearch_search/src/adapters/ElasticsearchAuditLogAdapter';
+import {ElasticsearchGuildAdapter} from '@pkgs/elasticsearch_search/src/adapters/ElasticsearchGuildAdapter';
+import {ElasticsearchGuildMemberAdapter} from '@pkgs/elasticsearch_search/src/adapters/ElasticsearchGuildMemberAdapter';
+import type {ElasticsearchDistributedLock} from '@pkgs/elasticsearch_search/src/adapters/ElasticsearchIndexAdapter';
+import {ElasticsearchMessageAdapter} from '@pkgs/elasticsearch_search/src/adapters/ElasticsearchMessageAdapter';
+import {ElasticsearchReportAdapter} from '@pkgs/elasticsearch_search/src/adapters/ElasticsearchReportAdapter';
+import {ElasticsearchThreadAdapter} from '@pkgs/elasticsearch_search/src/adapters/ElasticsearchThreadAdapter';
+import {ElasticsearchUserAdapter} from '@pkgs/elasticsearch_search/src/adapters/ElasticsearchUserAdapter';
+import {
+ createElasticsearchClient,
+ type ElasticsearchClientConfig,
+} from '@pkgs/elasticsearch_search/src/ElasticsearchClient';
+
+export function createElasticsearchSearchServices(
+ config: ElasticsearchClientConfig,
+ lock?: ElasticsearchDistributedLock,
+): SearchServices {
+ const client = createElasticsearchClient(config);
+ const message = new MessageSearchService(new ElasticsearchMessageAdapter({client, lock}));
+ const guild = new GuildSearchService(new ElasticsearchGuildAdapter({client, lock}), new GuildDiscoveryRepository());
+ const user = new UserSearchService(new ElasticsearchUserAdapter({client, lock}));
+ const report = new ReportSearchService(new ElasticsearchReportAdapter({client, lock}));
+ const auditLog = new AuditLogSearchService(new ElasticsearchAuditLogAdapter({client, lock}));
+ const guildMember = new GuildMemberSearchService(new ElasticsearchGuildMemberAdapter({client, lock}));
+ const thread = new ThreadSearchIndex(new ElasticsearchThreadAdapter({client, lock}));
+ return {message, guild, user, report, auditLog, guildMember, thread};
+}