Skip to content

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

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

import type {UserID} from '@app/api/BrandedTypes';
import {
	channelToContentWarningView,
	computeEffectiveChannelNsfw,
	resolveEffectiveThreadNsfw,
} from '@app/api/channel/utils/EffectiveContentWarning';
import type {Channel} from '@app/api/models/Channel';
import type {UserRepository} from '@app/api/user/repositories/UserRepository';
import {canUserAccessNsfwContent} from '@app/api/utils/AgeUtils';
import {ContentWarningLevel} from '@fluxer/constants/src/GuildConstants';
import {THREAD_CHANNEL_TYPES} from '@fluxer/constants/src/ThreadConstants';
import {UnknownUserError} from '@fluxer/errors/src/domains/user/UnknownUserError';

export function channelRequiresAgeVerification(
	channel: Channel,
	channelsById: ReadonlyMap<string, Channel>,
	guildNsfw: boolean,
): boolean {
	const guild = {nsfw: guildNsfw, contentWarningLevel: ContentWarningLevel.INHERIT, contentWarningText: null};
	const parent = channel.parentId != null ? (channelsById.get(channel.parentId.toString()) ?? null) : null;
	if (THREAD_CHANNEL_TYPES.has(channel.type)) {
		if (!parent) return guildNsfw;
		const category = parent.parentId != null ? (channelsById.get(parent.parentId.toString()) ?? null) : null;
		return resolveEffectiveThreadNsfw(
			channelToContentWarningView(parent),
			category ? channelToContentWarningView(category) : null,
			guild,
		);
	}
	return computeEffectiveChannelNsfw(
		channelToContentWarningView(channel),
		parent ? channelToContentWarningView(parent) : null,
		guild,
	);
}

export interface SearchNsfwAccess {
	adult: boolean;
	allowed: boolean;
}

export async function getSearchNsfwAccess(
	userRepository: UserRepository,
	userId: UserID,
	iosApp: boolean,
): Promise<SearchNsfwAccess> {
	const user = await userRepository.findUnique(userId);
	if (!user) {
		throw new UnknownUserError();
	}
	const adult = canUserAccessNsfwContent(user);
	if (!adult || !iosApp) {
		return {adult, allowed: adult};
	}
	const settings = await userRepository.findSettings(userId);
	return {adult, allowed: settings?.allowMatureContentOnIos ?? false};
}