Skip to content

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

TypeScript fluxerfluxerapp
3.1 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-or-later

import type {ChannelID, UserID} from '@app/api/BrandedTypes';
import {mapChannelToResponse} from '@app/api/channel/ChannelMappers';
import type {ThreadViewer} from '@app/api/experiment/ChannelThreadsGate';
import type {UserCacheService} from '@app/api/infrastructure/UserCacheService';
import type {RequestCache} from '@app/api/middleware/RequestCacheMiddleware';
import type {Channel} from '@app/api/models/Channel';
import type {UserChannelService} from '@app/api/user/services/UserChannelService';

import {ChannelTypes} from '@fluxer/constants/src/ChannelConstants';
import type {ChannelResponse} from '@fluxer/schema/src/domains/channel/ChannelSchemas';
import type {CreatePrivateChannelRequest} from '@fluxer/schema/src/domains/user/UserRequestSchemas';

interface UserChannelListParams {
	userId: UserID;
	requestCache: RequestCache;
}

interface UserChannelCreateParams {
	userId: UserID;
	data: CreatePrivateChannelRequest;
	requestCache: RequestCache;
}

interface UserChannelPinParams {
	userId: UserID;
	viewer: ThreadViewer;
	channelId: ChannelID;
}

function collectDMRecipientIds(channels: Array<Channel>, currentUserId: UserID): Array<UserID> {
	const recipientIds = new Set<UserID>();
	for (const channel of channels) {
		if (channel.guildId != null || channel.type === ChannelTypes.DM_PERSONAL_NOTES) {
			continue;
		}
		for (const recipientId of channel.recipientIds) {
			if (recipientId !== currentUserId) {
				recipientIds.add(recipientId);
			}
		}
	}
	return Array.from(recipientIds);
}

export class UserChannelRequestService {
	constructor(
		private readonly userChannelService: UserChannelService,
		private readonly userCacheService: UserCacheService,
	) {}

	async listPrivateChannels(params: UserChannelListParams): Promise<Array<ChannelResponse>> {
		const channels = await this.userChannelService.getPrivateChannels(params.userId);
		await this.userCacheService.getUserPartialResponses(
			collectDMRecipientIds(channels, params.userId),
			params.requestCache,
		);
		return Promise.all(
			channels.map((channel) =>
				mapChannelToResponse({
					channel,
					currentUserId: params.userId,
					userCacheService: this.userCacheService,
					requestCache: params.requestCache,
				}),
			),
		);
	}

	async createPrivateChannel(params: UserChannelCreateParams): Promise<ChannelResponse> {
		const channel = await this.userChannelService.createOrOpenDMChannel({
			userId: params.userId,
			data: params.data,
			userCacheService: this.userCacheService,
			requestCache: params.requestCache,
		});
		return mapChannelToResponse({
			channel,
			currentUserId: params.userId,
			userCacheService: this.userCacheService,
			requestCache: params.requestCache,
		});
	}

	async pinChannel(params: UserChannelPinParams): Promise<void> {
		await this.userChannelService.setDmChannelPinned({
			userId: params.userId,
			viewer: params.viewer,
			channelId: params.channelId,
			pinned: true,
		});
	}

	async unpinChannel(params: UserChannelPinParams): Promise<void> {
		await this.userChannelService.setDmChannelPinned({
			userId: params.userId,
			viewer: params.viewer,
			channelId: params.channelId,
			pinned: false,
		});
	}
}