A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
// SPDX-License-Identifier: AGPL-3.0-or-later
import {createChannelID, createGuildID, type UserID} from '@app/api/BrandedTypes';
import type {ChannelRepository} from '@app/api/channel/ChannelRepository';
import type {ChannelService} from '@app/api/channel/services/ChannelService';
import type {ThreadViewer} from '@app/api/experiment/ChannelThreadsGate';
import type {GuildService} from '@app/api/guild/services/GuildService';
import type {UserCacheService} from '@app/api/infrastructure/UserCacheService';
import type {RequestCache} from '@app/api/middleware/RequestCacheMiddleware';
import {GlobalSearchService} from '@app/api/search/GlobalSearchService';
import {normalizeQuotedPhrases} from '@app/api/search/SearchQuotedPhrases';
import type {UserRepository} from '@app/api/user/repositories/UserRepository';
import type {WorkerTaskName} from '@app/api/worker/WorkerLaneConfig';
import {ValidationErrorCodes} from '@fluxer/constants/src/ValidationErrorCodes';
import {InputValidationError} from '@fluxer/errors/src/domains/core/InputValidationError';
import type {GlobalSearchMessagesRequest} from '@fluxer/schema/src/domains/message/MessageRequestSchemas';
import type {MessageSearchResponse} from '@fluxer/schema/src/domains/message/MessageResponseSchemas';
import type {IWorkerService} from '@pkgs/worker/src/contracts/IWorkerService';
export class SearchService {
private readonly globalSearch: GlobalSearchService;
private readonly channelService: ChannelService;
private readonly guildService: GuildService;
constructor(params: {
channelRepository: ChannelRepository;
channelService: ChannelService;
guildService: GuildService;
userRepository: UserRepository;
userCacheService: UserCacheService;
workerService: IWorkerService<WorkerTaskName>;
}) {
this.channelService = params.channelService;
this.guildService = params.guildService;
this.globalSearch = new GlobalSearchService(
params.channelRepository,
params.guildService,
params.userRepository,
params.userCacheService,
params.workerService,
);
}
async searchMessages(params: {
userId: UserID;
viewer: ThreadViewer;
iosApp: boolean;
requestCache: RequestCache;
data: GlobalSearchMessagesRequest;
}): Promise<MessageSearchResponse> {
const {userId, viewer, iosApp, requestCache, data} = params;
const {channel_id, channel_ids, context_channel_id, context_guild_id, ...rawSearchParams} = data;
const searchParams = normalizeQuotedPhrases(rawSearchParams);
const contextChannelId = context_channel_id ? createChannelID(context_channel_id) : null;
const contextGuildId = context_guild_id ? createGuildID(context_guild_id) : null;
const channelIds = (channel_ids ?? channel_id)?.map((id) => createChannelID(id)) ?? [];
const scope = searchParams.scope ?? 'current';
let result: MessageSearchResponse;
switch (scope) {
case 'all_guilds':
result = await this.guildService.search.searchAllGuilds({
userId,
viewer,
channelIds,
iosApp,
searchParams,
requestCache,
});
break;
case 'all_dms':
case 'open_dms':
result = await this.globalSearch.searchAcrossDms({
userId,
viewer,
scope,
searchParams,
requestCache,
includeChannelId: contextChannelId,
requestedChannelIds: channelIds,
});
break;
case 'all':
result = await this.globalSearch.searchAcrossGuildsAndDms({
userId,
viewer,
dmScope: 'all_dms',
iosApp,
searchParams,
requestCache,
includeChannelId: contextChannelId,
requestedChannelIds: channelIds,
});
break;
case 'open_dms_and_all_guilds':
result = await this.globalSearch.searchAcrossGuildsAndDms({
userId,
viewer,
dmScope: 'open_dms',
iosApp,
searchParams,
requestCache,
includeChannelId: contextChannelId,
requestedChannelIds: channelIds,
});
break;
default:
if (contextGuildId) {
result = await this.guildService.search.searchMessages({
userId,
viewer,
guildId: contextGuildId,
channelIds,
iosApp,
searchParams,
requestCache,
});
} else if (contextChannelId) {
result = await this.channelService.messages.retrieval.searchMessages({
userId,
viewer,
channelId: contextChannelId,
searchParams,
requestCache,
});
} else {
throw InputValidationError.fromCode('context', ValidationErrorCodes.CONTEXT_CHANNEL_OR_GUILD_ID_REQUIRED);
}
break;
}
return result;
}
}