Skip to content

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

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

import type {ChannelID, GuildID, UserID} from '@app/api/BrandedTypes';
import {Config} from '@app/api/Config';
import type {ChannelRepository} from '@app/api/channel/ChannelRepository';
import {resolveExperimentTargeting} from '@app/api/experiment/ExperimentTargeting';
import type {GuildRepository} from '@app/api/guild/repositories/GuildRepository';
import type {LiveKitService} from '@app/api/infrastructure/LiveKitService';
import type {PinnedRoomServer, VoiceRoomStore} from '@app/api/infrastructure/VoiceRoomStore';
import type {InstanceConfigRepository} from '@app/api/instance/InstanceConfigRepository';
import {Logger} from '@app/api/Logger';
import type {User} from '@app/api/models/User';
import {assertTierAllows, canReachOthers, NO_TIER_CONTEXT} from '@app/api/user/AccountTier';
import type {UserRepository} from '@app/api/user/repositories/UserRepository';
import type {VoiceAccessContext, VoiceAvailabilityService} from '@app/api/voice/VoiceAvailabilityService';
import type {VoiceRegionAvailability, VoiceServerRecord} from '@app/api/voice/VoiceModel';
import {
	resolveVoiceRegionPreference,
	selectClosestPseudoRegionServer,
	selectVoiceRegionId,
} from '@app/api/voice/VoiceRegionSelection';
import {generateConnectionId} from '@app/api/words/Words';
import {TierGatedActions} from '@fluxer/constants/src/AccountTierConstants';
import {ChannelTypes} from '@fluxer/constants/src/ChannelConstants';
import {UnknownChannelError} from '@fluxer/errors/src/domains/channel/UnknownChannelError';
import {FeatureTemporarilyDisabledError} from '@fluxer/errors/src/domains/core/FeatureTemporarilyDisabledError';
import {UnknownGuildMemberError} from '@fluxer/errors/src/domains/guild/UnknownGuildMemberError';
import {UnknownUserError} from '@fluxer/errors/src/domains/user/UnknownUserError';
import {resolveVoiceP2pAssignment} from '@fluxer/schema/src/domains/admin/VoiceP2pSchemas';

interface GetVoiceTokenParams {
	guildId?: GuildID;
	channelId: ChannelID;
	userId: UserID;
	connectionId?: string;
	region?: string;
	latitude?: string;
	longitude?: string;
	canSpeak?: boolean;
	canStream?: boolean;
	canVideo?: boolean;
	tokenNonce?: string;
	p2p?: boolean;
	p2pInitiator?: boolean;
	p2pParticipantCount?: number;
	countryCode?: string;
}

interface VoiceTokenGrant {
	token: string;
	endpoint: string;
	connectionId: string;
	tokenNonce: string;
	regionId: string;
	serverId: string;
}

interface VoiceP2pGrant {
	p2p: true;
	connectionId: string;
	iceServers: Array<{urls: Array<string>}>;
	maxParticipants: number;
}

interface VoiceP2pDecline {
	p2p: false;
	declineReason: 'channel_full';
}

type VoiceP2pDecision = {kind: 'granted'; maxParticipants: number} | {kind: 'declined'} | {kind: 'channel_full'};

interface VoicePermissions {
	canSpeak: boolean;
	canStream: boolean;
	canVideo: boolean;
}

export class VoiceService {
	constructor(
		private liveKitService: LiveKitService,
		private guildRepository: GuildRepository,
		private userRepository: UserRepository,
		private channelRepository: ChannelRepository,
		private voiceRoomStore: VoiceRoomStore,
		private voiceAvailabilityService: VoiceAvailabilityService,
		private instanceConfigRepository: InstanceConfigRepository,
	) {}

	async getVoiceToken(params: GetVoiceTokenParams): Promise<VoiceTokenGrant | VoiceP2pGrant | VoiceP2pDecline> {
		const {guildId, channelId, userId, connectionId: providedConnectionId} = params;
		const selectionKey = this.createVoiceRoutingSelectionKey(guildId, channelId);
		let connectionId = providedConnectionId;
		let regionId: string | null = null;
		let serverId: string | null = null;
		const user = await this.userRepository.findUnique(userId);
		if (!user) {
			throw new UnknownUserError();
		}
		const channel = await this.channelRepository.channelData.findUnique(channelId);
		if (!channel) {
			throw new UnknownChannelError();
		}
		if (!canReachOthers(user)) {
			await this.assertVoiceReach(user, channel.type, guildId);
		}
		let mute = false;
		let deaf = false;
		let guildFeatures: Set<string> | undefined;
		const voicePermissions: VoicePermissions = {
			canSpeak: params.canSpeak ?? true,
			canStream: params.canStream ?? true,
			canVideo: params.canVideo ?? true,
		};
		if (guildId !== undefined) {
			const member = await this.guildRepository.getMember(guildId, userId);
			if (!member) {
				throw new UnknownGuildMemberError();
			}
			mute = member.isMute;
			deaf = member.isDeaf;
			const guild = await this.guildRepository.findUnique(guildId);
			if (guild) {
				guildFeatures = guild.features;
			}
		}
		const p2pDecision: VoiceP2pDecision = params.p2p ? await this.decideVoiceP2p(user, params) : {kind: 'declined'};
		if (p2pDecision.kind === 'channel_full') {
			return {p2p: false, declineReason: 'channel_full'};
		}
		if (p2pDecision.kind === 'granted') {
			const stunUrls = Config.voice.p2pStunUrls;
			return {
				p2p: true,
				connectionId: providedConnectionId || generateConnectionId(),
				iceServers: stunUrls?.length ? [{urls: stunUrls}] : [],
				maxParticipants: p2pDecision.maxParticipants,
			};
		}
		const context: VoiceAccessContext = {
			requestingUserId: userId,
			guildId,
			guildFeatures,
		};
		const accessibleRegions = this.voiceAvailabilityService
			.getAvailableRegions(context)
			.filter((region) => region.isAccessible);
		const defaultRegionId = this.liveKitService.getDefaultRegionId();
		const preferredRegionId = params.region ?? channel.rtcRegion ?? null;
		const regionPreference = resolveVoiceRegionPreference({
			preferredRegionId,
			accessibleRegions,
			defaultRegionId,
		});
		const accessibleServers = this.getAccessibleServersForRegions(accessibleRegions, context);
		let serverEndpoint: string | null = null;
		const pinnedServer = await this.voiceRoomStore.getPinnedRoomServer(guildId, channelId);
		const resolvedPinnedServer = await this.resolvePinnedServer({
			pinnedServer,
			guildId,
			channelId,
			context,
			accessibleRegions,
			preferredRegionId: regionPreference.regionId,
			mode: regionPreference.mode,
		});
		if (resolvedPinnedServer) {
			regionId = resolvedPinnedServer.regionId;
			serverId = resolvedPinnedServer.serverId;
			serverEndpoint = resolvedPinnedServer.endpoint;
		}
		if (!serverId) {
			const pseudoRegionServer = selectClosestPseudoRegionServer({
				mode: regionPreference.mode,
				accessibleServers,
				connectionCounts: this.voiceAvailabilityService.getServerConnectionCounts(),
				latitude: params.latitude,
				longitude: params.longitude,
				selectionKey,
			});
			if (pseudoRegionServer) {
				regionId = pseudoRegionServer.regionId;
				serverId = pseudoRegionServer.serverId;
				serverEndpoint = pseudoRegionServer.endpoint;
			} else {
				regionId = selectVoiceRegionId({
					preferredRegionId: regionPreference.regionId,
					mode: regionPreference.mode,
					accessibleRegions,
					latitude: params.latitude,
					longitude: params.longitude,
					selectionKey,
				});
				if (!regionId) {
					throw new FeatureTemporarilyDisabledError();
				}
				const serverSelection = this.voiceAvailabilityService.selectServerOrFallback(
					regionId,
					context,
					accessibleRegions,
				);
				if (!serverSelection) {
					throw new FeatureTemporarilyDisabledError();
				}
				regionId = serverSelection.regionId;
				serverId = serverSelection.server.serverId;
				serverEndpoint = serverSelection.server.endpoint;
			}
			await this.voiceRoomStore.pinRoomServer(guildId, channelId, regionId, serverId, serverEndpoint);
		}
		if (!serverId || !regionId || !serverEndpoint) {
			throw new FeatureTemporarilyDisabledError();
		}
		const serverRecord = this.liveKitService.getServer(regionId, serverId);
		if (!serverRecord) {
			throw new FeatureTemporarilyDisabledError();
		}
		connectionId = providedConnectionId || generateConnectionId();
		Logger.debug(
			{
				guildId: guildId?.toString(),
				channelId: channelId.toString(),
				userId: userId.toString(),
				providedConnectionId,
				generatedConnectionId: connectionId,
				wasGenerated: !providedConnectionId,
			},
			'Voice token connection ID selection',
		);
		const tokenNonce = params.tokenNonce ?? crypto.randomUUID();
		const {token, endpoint} = await this.liveKitService.createToken({
			userId,
			guildId,
			channelId,
			connectionId,
			tokenNonce,
			regionId,
			serverId,
			mute,
			deaf,
			canSpeak: voicePermissions.canSpeak,
			canStream: voicePermissions.canStream,
			canVideo: voicePermissions.canVideo,
		});
		if (mute || deaf) {
			this.liveKitService
				.updateParticipant({
					userId,
					guildId,
					channelId,
					connectionId,
					regionId,
					serverId,
					mute,
					deaf,
				})
				.catch((error) => {
					Logger.error(
						{
							userId,
							guildId,
							channelId,
							connectionId,
							regionId,
							serverId,
							mute,
							deaf,
							error,
						},
						'Failed to update LiveKit participant after token creation',
					);
				});
		}
		return {token, endpoint, connectionId, tokenNonce, regionId, serverId};
	}

	private async decideVoiceP2p(user: User, params: GetVoiceTokenParams): Promise<VoiceP2pDecision> {
		const config = await this.instanceConfigRepository.getVoiceP2pConfig();
		if (!config.enabled) {
			return {kind: 'declined'};
		}
		if (params.p2pParticipantCount !== undefined && params.p2pParticipantCount > config.max_participants) {
			return {kind: 'channel_full'};
		}
		const granted: VoiceP2pDecision = {kind: 'granted', maxParticipants: config.max_participants};
		if (!params.p2pInitiator) {
			return granted;
		}
		const targeting = await resolveExperimentTargeting(user, params.countryCode ?? null, [config]);
		return resolveVoiceP2pAssignment(config, user.id.toString(), targeting).enabled ? granted : {kind: 'declined'};
	}

	private createVoiceRoutingSelectionKey(guildId: GuildID | undefined, channelId: ChannelID): string {
		const guildKey = guildId?.toString() ?? 'dm';
		return `${guildKey}:${channelId.toString()}`;
	}

	private async resolvePinnedServer({
		pinnedServer,
		guildId,
		channelId,
		context,
		accessibleRegions,
		preferredRegionId,
		mode,
	}: {
		pinnedServer: PinnedRoomServer | null;
		guildId?: GuildID;
		channelId: ChannelID;
		context: VoiceAccessContext;
		accessibleRegions: Array<VoiceRegionAvailability>;
		preferredRegionId: string | null;
		mode: 'explicit' | 'automatic';
	}): Promise<{
		regionId: string;
		serverId: string;
		endpoint: string;
	} | null> {
		if (!pinnedServer) {
			return null;
		}
		if (mode === 'explicit' && preferredRegionId && pinnedServer.regionId !== preferredRegionId) {
			await this.voiceRoomStore.deleteRoomServer(guildId, channelId);
			return null;
		}
		const serverRecord = this.liveKitService.getServer(pinnedServer.regionId, pinnedServer.serverId);
		if (
			serverRecord &&
			accessibleRegions.some((region) => region.id === pinnedServer.regionId) &&
			this.voiceAvailabilityService.isServerAccessible(serverRecord, context) &&
			!this.voiceAvailabilityService.isServerDown(serverRecord)
		) {
			return {
				regionId: pinnedServer.regionId,
				serverId: pinnedServer.serverId,
				endpoint: serverRecord.endpoint,
			};
		}
		await this.voiceRoomStore.deleteRoomServer(guildId, channelId);
		return null;
	}

	private getAccessibleServersForRegions(
		accessibleRegions: Array<VoiceRegionAvailability>,
		context: VoiceAccessContext,
	): Array<VoiceServerRecord> {
		const servers: Array<VoiceServerRecord> = [];
		for (const region of accessibleRegions) {
			servers.push(...this.voiceAvailabilityService.getAccessibleServersForRegion(region.id, context));
		}
		return this.voiceAvailabilityService.preferPlaceableServers(servers);
	}

	async updateParticipant(params: {
		guildId?: GuildID;
		channelId: ChannelID;
		userId: UserID;
		mute: boolean;
		deaf: boolean;
		canSpeak?: boolean;
		canStream?: boolean;
		canVideo?: boolean;
	}): Promise<void> {
		const {guildId, channelId, userId, mute, deaf, canSpeak, canStream, canVideo} = params;
		const pinnedServer = await this.voiceRoomStore.getPinnedRoomServer(guildId, channelId);
		if (!pinnedServer) {
			return;
		}
		const result = await this.liveKitService.listParticipants({
			guildId,
			channelId,
			regionId: pinnedServer.regionId,
			serverId: pinnedServer.serverId,
		});
		if (result.status === 'error') {
			Logger.error(
				{errorCode: result.errorCode, guildId, channelId},
				'Failed to list participants for self mute/deaf update',
			);
			if (result.serverMissing) return;
			throw new Error(`Failed to list voice participants: ${result.errorCode}`);
		}
		let failures = 0;
		for (const participant of result.participants) {
			const parts = participant.identity.split('_');
			if (parts.length >= 2 && parts[0] === 'user') {
				const participantUserIdStr = parts[1];
				if (participantUserIdStr === userId.toString()) {
					const connectionId = parts.slice(2).join('_');
					try {
						await this.liveKitService.updateParticipant({
							userId,
							guildId,
							channelId,
							connectionId,
							regionId: pinnedServer.regionId,
							serverId: pinnedServer.serverId,
							mute,
							deaf,
							canSpeak,
							canStream,
							canVideo,
						});
					} catch (error) {
						Logger.error(
							{
								identity: participant.identity,
								userId,
								guildId,
								channelId,
								connectionId,
								regionId: pinnedServer.regionId,
								serverId: pinnedServer.serverId,
								mute,
								deaf,
								error,
							},
							'Failed to update participant',
						);
						failures += 1;
					}
				}
			}
		}
		if (failures > 0) {
			throw new Error(`Failed to update ${failures} voice participant connection(s)`);
		}
	}

	async disconnectParticipant(params: {
		guildId?: GuildID;
		channelId: ChannelID;
		userId: UserID;
		connectionId: string;
	}): Promise<void> {
		const {guildId, channelId, userId, connectionId} = params;
		const pinnedServer = await this.voiceRoomStore.getPinnedRoomServer(guildId, channelId);
		if (!pinnedServer) {
			await Promise.allSettled(
				this.liveKitService.listServers().map((server) =>
					this.liveKitService.disconnectParticipant({
						userId,
						guildId,
						channelId,
						connectionId,
						regionId: server.regionId,
						serverId: server.serverId,
					}),
				),
			);
			return;
		}
		await this.liveKitService.disconnectParticipant({
			userId,
			guildId,
			channelId,
			connectionId,
			regionId: pinnedServer.regionId,
			serverId: pinnedServer.serverId,
		});
	}

	private async assertVoiceReach(user: User, channelType: number, guildId: GuildID | null | undefined): Promise<void> {
		if (channelType === ChannelTypes.GUILD_VOICE) {
			const guild = guildId ? await this.guildRepository.findUnique(guildId) : null;
			assertTierAllows(user, TierGatedActions.VOICE, {...NO_TIER_CONTEXT, isGuildOwner: guild?.ownerId === user.id});
			return;
		}
		if (channelType === ChannelTypes.DM) {
			assertTierAllows(user, TierGatedActions.CALL, NO_TIER_CONTEXT);
		} else if (channelType === ChannelTypes.GROUP_DM) {
			assertTierAllows(user, TierGatedActions.GROUP_CALL, NO_TIER_CONTEXT);
		}
	}
}