Skip to content

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

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

import InstanceSnapshotStore from '@app/features/app/state/InstanceSnapshotStore';
import RuntimeConfig from '@app/features/app/state/RuntimeConfig';
import type {LimitContextInput} from '@app/features/app/utils/LimitContext';
import {LimitContext} from '@app/features/app/utils/LimitContext';
import type {LimitKey} from '@fluxer/constants/src/LimitConfigMetadata';
import {resolveLimit} from '@fluxer/limits/src/LimitResolver';
import type {LimitConfigSnapshot} from '@fluxer/limits/src/LimitTypes';

interface LimitResolveOptions {
	key: LimitKey;
	fallback: number;
	context?: LimitContextInput;
	instanceDomain?: string;
}

class LimitResolverClass {
	resolve(options: LimitResolveOptions): number {
		const {key, fallback, context, instanceDomain} = options;
		const snapshot = this.getSnapshotForInstance(instanceDomain);
		if (snapshot === null) {
			return fallback;
		}
		const ctx = context ? LimitContext.build(context) : LimitContext.current();
		const resolved = resolveLimit(snapshot, ctx, key);
		if (!Number.isFinite(resolved) || resolved < 0) {
			return fallback;
		}
		return Math.min(Math.floor(resolved), Number.MAX_SAFE_INTEGER);
	}

	private getSnapshotForInstance(instanceDomain?: string): LimitConfigSnapshot | null {
		if (instanceDomain !== undefined) {
			return InstanceSnapshotStore.getLimitsForInstance(instanceDomain);
		}
		return RuntimeConfig.getSnapshotOrNull()?.limits ?? null;
	}

	resolveStock(key: LimitKey, fallback: number): number {
		return this.resolve({
			key,
			fallback,
			context: LimitContext.stock(),
		});
	}

	resolveRestricted(key: LimitKey, fallback: number): number {
		return this.resolve({
			key,
			fallback,
			context: LimitContext.restricted(),
		});
	}
}

export const LimitResolver = new LimitResolverClass();