Skip to content

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

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

import {ChannelThreadsAssignmentResponse} from '@fluxer/schema/src/domains/admin/ChannelThreadsSchemas';
import {
	DomainMigrationAssignmentResponse,
	INERT_DOMAIN_MIGRATION_ASSIGNMENT,
} from '@fluxer/schema/src/domains/admin/DomainMigrationSchemas';
import {GuildVisitsAssignmentResponse} from '@fluxer/schema/src/domains/admin/GuildVisitsSchemas';
import {PlutoniumPageAssignmentResponse} from '@fluxer/schema/src/domains/admin/PlutoniumPageSchemas';
import {INERT_VOICE_P2P_ASSIGNMENT, VoiceP2pAssignmentResponse} from '@fluxer/schema/src/domains/admin/VoiceP2pSchemas';
import {z} from 'zod';

export const EXPERIMENT_MIN_POLL_INTERVAL_SECONDS = 60;
export const EXPERIMENT_MAX_POLL_INTERVAL_SECONDS = 86400;
export const EXPERIMENT_MAX_POLL_JITTER_PERCENT = 50;
export const DEFAULT_EXPERIMENT_POLL_INTERVAL_SECONDS = 300;
export const DEFAULT_EXPERIMENT_POLL_JITTER_PERCENT = 15;

const experimentDeliveryFields = {
	poll_interval_seconds: z
		.number()
		.int()
		.min(EXPERIMENT_MIN_POLL_INTERVAL_SECONDS)
		.max(EXPERIMENT_MAX_POLL_INTERVAL_SECONDS),
	poll_jitter_percent: z.number().int().min(0).max(EXPERIMENT_MAX_POLL_JITTER_PERCENT),
};

export const ExperimentDeliveryConfigSchema = z.object({
	poll_interval_seconds: experimentDeliveryFields.poll_interval_seconds.default(
		DEFAULT_EXPERIMENT_POLL_INTERVAL_SECONDS,
	),
	poll_jitter_percent: experimentDeliveryFields.poll_jitter_percent.default(DEFAULT_EXPERIMENT_POLL_JITTER_PERCENT),
});

export type ExperimentDeliveryConfig = z.infer<typeof ExperimentDeliveryConfigSchema>;

export const ExperimentDeliveryConfigUpdateRequest = z.object(experimentDeliveryFields).partial();

export type ExperimentDeliveryConfigUpdateRequest = z.infer<typeof ExperimentDeliveryConfigUpdateRequest>;

export const ExperimentDeliveryConfigResponse = ExperimentDeliveryConfigSchema;

export type ExperimentDeliveryConfigResponse = z.infer<typeof ExperimentDeliveryConfigResponse>;

const ExperimentAssignmentsSchema = z.object({
	domain_migration: DomainMigrationAssignmentResponse.optional(),
	channel_threads: ChannelThreadsAssignmentResponse.optional(),
	plutonium_page: PlutoniumPageAssignmentResponse.optional(),
	voice_p2p: VoiceP2pAssignmentResponse.optional(),
	guild_visits: GuildVisitsAssignmentResponse.optional(),
});

export const ExperimentAssignmentsResponse = z.object({
	poll_interval_seconds: z.number().int(),
	poll_jitter_percent: experimentDeliveryFields.poll_jitter_percent,
	assignments: ExperimentAssignmentsSchema,
});

export type ExperimentAssignmentsResponse = z.infer<typeof ExperimentAssignmentsResponse>;

export const INERT_EXPERIMENT_ASSIGNMENTS_RESPONSE: ExperimentAssignmentsResponse = {
	poll_interval_seconds: DEFAULT_EXPERIMENT_POLL_INTERVAL_SECONDS,
	poll_jitter_percent: DEFAULT_EXPERIMENT_POLL_JITTER_PERCENT,
	assignments: {},
};

export function readDomainMigrationAssignment(
	response: ExperimentAssignmentsResponse,
): DomainMigrationAssignmentResponse {
	return response.assignments.domain_migration ?? INERT_DOMAIN_MIGRATION_ASSIGNMENT;
}

export function readVoiceP2pAssignment(response: ExperimentAssignmentsResponse): VoiceP2pAssignmentResponse {
	return response.assignments.voice_p2p ?? INERT_VOICE_P2P_ASSIGNMENT;
}