Skip to content

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

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

import type {ChannelID, GuildID, InviteCode, UserID} from '@app/api/BrandedTypes';
import {mapChannelToPartialResponse} from '@app/api/channel/ChannelMappers';
import type {ChannelService} from '@app/api/channel/services/ChannelService';
import type {ThreadViewer} from '@app/api/experiment/ChannelThreadsGate';
import {guildVisitsOffer} from '@app/api/experiment/GuildVisitsGate';
import type {GuildService} from '@app/api/guild/services/GuildService';
import type {IGatewayService} from '@app/api/infrastructure/IGatewayService';
import type {UserCacheService} from '@app/api/infrastructure/UserCacheService';
import {
	mapInviteToGroupDmInviteMetadataResponse,
	mapInviteToGroupDmInviteResponse,
	mapInviteToGuildInviteMetadataResponse,
	mapInviteToGuildInviteResponse,
} from '@app/api/invite/InviteModel';
import type {InviteService} from '@app/api/invite/InviteService';
import type {RequestCache} from '@app/api/middleware/RequestCacheMiddleware';
import type {Channel} from '@app/api/models/Channel';
import type {Invite} from '@app/api/models/Invite';
import type {TierContext} from '@app/api/user/AccountTier';
import {InviteTypes} from '@fluxer/constants/src/ChannelConstants';
import type {ChannelPartialResponse} from '@fluxer/schema/src/domains/channel/ChannelSchemas';
import type {GuildPartialResponse} from '@fluxer/schema/src/domains/guild/GuildResponseSchemas';
import type {
	ChannelInviteCreateRequest,
	InviteMetadataResponseSchema,
	InviteResponseSchema,
} from '@fluxer/schema/src/domains/invite/InviteSchemas';

interface MappingHelpers {
	userCacheService: UserCacheService;
	requestCache: RequestCache;
	getChannelResponse: (channelId: ChannelID) => Promise<ChannelPartialResponse>;
	getChannelSystem: (channelId: ChannelID) => Promise<Channel | null>;
	getChannelMemberCount: (channelId: ChannelID) => Promise<number>;
	getGuildResponse: (guildId: GuildID) => Promise<GuildPartialResponse>;
	getGuildCounts: (guildId: GuildID) => Promise<{
		memberCount: number;
		presenceCount: number;
	}>;
	gatewayService: IGatewayService;
}

function withVisitsOffer(response: InviteResponseSchema, userId: UserID | null): InviteResponseSchema {
	if (!('guild' in response)) return response;
	return {...response, guild: {...response.guild, ...guildVisitsOffer(response.guild.features, userId)}};
}

export class InviteRequestService {
	constructor(
		private readonly inviteService: InviteService,
		private readonly channelService: ChannelService,
		private readonly guildService: GuildService,
		private readonly gatewayService: IGatewayService,
		private readonly userCacheService: UserCacheService,
	) {}

	async getInvite(params: {
		inviteCode: InviteCode;
		requestCache: RequestCache;
		userId?: UserID | null;
	}): Promise<InviteResponseSchema> {
		const invite = await this.inviteService.getInvite(params.inviteCode);
		return withVisitsOffer(await this.mapInviteResponse(invite, params.requestCache), params.userId ?? null);
	}

	async acceptInvite(params: {
		userId: UserID;
		inviteCode: InviteCode;
		requestCache: RequestCache;
		tierContext: TierContext;
		ip?: string | null;
	}): Promise<InviteResponseSchema> {
		const invite = await this.inviteService.acceptInvite(params);
		return this.mapInviteResponse(invite, params.requestCache);
	}

	async deleteInvite(params: {
		userId: UserID;
		viewer: ThreadViewer;
		inviteCode: InviteCode;
		auditLogReason?: string | null;
	}): Promise<void> {
		const invite = await this.inviteService.getInvite(params.inviteCode);
		await this.inviteService.deleteInvite(
			{userId: params.userId, viewer: params.viewer, inviteCode: params.inviteCode},
			params.auditLogReason,
		);
		await this.inviteService.dispatchInviteDelete(invite);
	}

	async createChannelInvite(params: {
		inviterId: UserID;
		viewer: ThreadViewer;
		channelId: ChannelID;
		requestCache: RequestCache;
		data: ChannelInviteCreateRequest;
		auditLogReason?: string | null;
	}): Promise<InviteMetadataResponseSchema> {
		const {invite, isNew} = await this.inviteService.createInvite(
			{
				inviterId: params.inviterId,
				viewer: params.viewer,
				channelId: params.channelId,
				maxUses: params.data.max_uses ?? 0,
				maxAge: params.data.max_age ?? 0,
				unique: params.data.unique ?? false,
				temporary: params.data.temporary ?? false,
			},
			params.auditLogReason,
		);
		const inviteData = await this.mapInviteMetadataResponse(invite, params.requestCache);
		if (isNew) {
			await this.inviteService.dispatchInviteCreate(invite, inviteData);
		}
		return inviteData;
	}

	async listChannelInvites(params: {
		userId: UserID;
		viewer: ThreadViewer;
		channelId: ChannelID;
		requestCache: RequestCache;
	}): Promise<Array<InviteMetadataResponseSchema>> {
		const invites = await this.inviteService.getChannelInvites({
			userId: params.userId,
			viewer: params.viewer,
			channelId: params.channelId,
		});
		return this.mapInviteList(invites, params.requestCache);
	}

	async listGuildInvites(params: {
		userId: UserID;
		guildId: GuildID;
		requestCache: RequestCache;
	}): Promise<Array<InviteMetadataResponseSchema>> {
		const invites = await this.inviteService.getGuildInvites({
			userId: params.userId,
			guildId: params.guildId,
		});
		return this.mapInviteList(invites, params.requestCache);
	}

	private createMappingHelpers(requestCache: RequestCache): MappingHelpers {
		return {
			userCacheService: this.userCacheService,
			requestCache,
			getChannelResponse: async (channelId: ChannelID) =>
				mapChannelToPartialResponse(await this.channelService.channelData.operations.getPublicChannelData(channelId)),
			getChannelSystem: async (channelId: ChannelID) =>
				await this.channelService.channelData.operations.getChannelSystem(channelId),
			getChannelMemberCount: async (channelId: ChannelID) =>
				await this.channelService.channelData.operations.getChannelMemberCount(channelId),
			getGuildResponse: async (guildId: GuildID) => await this.guildService.data.operations.getPublicGuildData(guildId),
			getGuildCounts: async (guildId: GuildID) => await this.gatewayService.getGuildCounts(guildId),
			gatewayService: this.gatewayService,
		};
	}

	private async mapInviteResponse(invite: Invite, requestCache: RequestCache): Promise<InviteResponseSchema> {
		const helpers = this.createMappingHelpers(requestCache);
		if (invite.type === InviteTypes.GROUP_DM) {
			return mapInviteToGroupDmInviteResponse({invite, ...helpers});
		}
		return mapInviteToGuildInviteResponse({invite, ...helpers});
	}

	private async mapInviteMetadataResponse(
		invite: Invite,
		requestCache: RequestCache,
	): Promise<InviteMetadataResponseSchema> {
		const helpers = this.createMappingHelpers(requestCache);
		if (invite.type === InviteTypes.GROUP_DM) {
			return mapInviteToGroupDmInviteMetadataResponse({invite, ...helpers});
		}
		return mapInviteToGuildInviteMetadataResponse({invite, ...helpers});
	}

	private async mapInviteList(
		invites: Array<Invite>,
		requestCache: RequestCache,
	): Promise<Array<InviteMetadataResponseSchema>> {
		return Promise.all(invites.map((invite) => this.mapInviteMetadataResponse(invite, requestCache)));
	}
}