feat(threads): ship threads and the Plutonium page to everyone (#3277)

This commit is contained in:
Hampus authored and GitHub committed 2026-10-08 16:30:39 +02:00
1 parent 1515a8487f
commit f8505d19f7
53 files changed
+296 -2008

No files matched your search

@@ -12,10 +12,6 @@ import {AdminArchiveResponseSchema} from '@fluxer/schema/src/domains/admin/Admin
import {GuildAdminResponse} from '@fluxer/schema/src/domains/admin/AdminGuildSchemas';
import {UserAdminResponseSchema} from '@fluxer/schema/src/domains/admin/AdminUserSchemas';
import {CaptchaConfigResponse, CaptchaConfigUpdateRequest} from '@fluxer/schema/src/domains/admin/CaptchaSchemas';
import {
ChannelThreadsConfigResponse,
ChannelThreadsConfigUpdateRequest,
} from '@fluxer/schema/src/domains/admin/ChannelThreadsSchemas';
import {
DomainMigrationConfigResponse,
DomainMigrationConfigUpdateRequest,
@@ -28,10 +24,6 @@ import {
InstanceBillingResponse,
InstanceBillingUpdateRequest,
} from '@fluxer/schema/src/domains/admin/InstanceBillingSchemas';
import {
PlutoniumPageConfigResponse,
PlutoniumPageConfigUpdateRequest,
} from '@fluxer/schema/src/domains/admin/PlutoniumPageSchemas';
import {PushRelayConfigResponse, PushRelayConfigUpdateRequest} from '@fluxer/schema/src/domains/admin/PushRelaySchemas';
import {
ExperimentDeliveryConfigResponse,
@@ -143,7 +135,7 @@ const SearchIndexTypeEnum = createNamedStringLiteralUnion(
['guild_members', 'guild_members', 'Guild member search index'],
['favorite_memes', 'favorite_memes', 'Favourite meme search index'],
['discovery', 'discovery', 'Discovery guild search index'],
['threads', 'threads', 'Thread search index (channel_threads experiment)'],
['threads', 'threads', 'Thread search index'],
],
'Type of search index to refresh',
);
@@ -676,9 +668,7 @@ export const InstanceConfigResponse = z.object({
gateway_rollout: GatewayRolloutConfigResponse,
push_relay: PushRelayConfigResponse,
domain_migration: DomainMigrationConfigResponse,
plutonium_page: PlutoniumPageConfigResponse,
captcha: CaptchaConfigResponse,
channel_threads: ChannelThreadsConfigResponse,
experiment_delivery: ExperimentDeliveryConfigResponse,
registration: InstanceRegistrationResponse,
self_hosted: z.boolean(),
@@ -712,9 +702,7 @@ export const InstanceConfigUpdateRequest = z.object({
gateway_rollout: GatewayRolloutConfigUpdateRequest.nullish(),
push_relay: PushRelayConfigUpdateRequest.nullish(),
domain_migration: DomainMigrationConfigUpdateRequest.nullish(),
plutonium_page: PlutoniumPageConfigUpdateRequest.nullish(),
captcha: CaptchaConfigUpdateRequest.nullish(),
channel_threads: ChannelThreadsConfigUpdateRequest.nullish(),
experiment_delivery: ExperimentDeliveryConfigUpdateRequest.nullish(),
registration: z
.object({
@@ -1,18 +1,14 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import {
applyChannelThreadsConfigUpdate,
type ChannelThreadsConfig,
ChannelThreadsConfigSchema,
ChannelThreadsConfigUpdateRequest,
channelThreadsGuildActive,
channelThreadsGuildFieldsChanged,
channelThreadsUserActive,
channelThreadsUserExcluded,
channelThreadsUserFieldsChanged,
compileChannelThreadsConfig,
DEFAULT_CHANNEL_THREADS_CONFIG,
DEFAULT_COMPILED_CHANNEL_THREADS_CONFIG,
everyoneChannelThreadsConfig,
resolveChannelThreadsAssignment,
} from '@fluxer/schema/src/domains/admin/ChannelThreadsSchemas';
import {experimentBucket} from '@fluxer/schema/src/domains/experiment/ExperimentBucket';
@@ -51,13 +47,8 @@ describe('channel threads configuration', () => {
included_user_ids: [],
excluded_user_ids: [],
});
expect(channelThreadsGuildActive(DEFAULT_COMPILED_CHANNEL_THREADS_CONFIG, GUILD_ID)).toBe(false);
expect(channelThreadsUserActive(DEFAULT_COMPILED_CHANNEL_THREADS_CONFIG, USER_ID)).toBe(false);
});
test('keeps partial updates free of defaults and server-maintained fields', () => {
expect(ChannelThreadsConfigUpdateRequest.parse({enabled: true})).toEqual({enabled: true});
expect(ChannelThreadsConfigUpdateRequest.parse({config_version: 9, ever_enabled: false})).toEqual({});
expect(channelThreadsGuildActive(compiled(), GUILD_ID)).toBe(false);
expect(channelThreadsUserActive(compiled(), USER_ID)).toBe(false);
});
test.each([
@@ -69,9 +60,8 @@ describe('channel threads configuration', () => {
{guild_salt: 'x'.repeat(65)},
{enabled_guild_ids: ['guild']},
{excluded_user_ids: ['123456789012345678901']},
])('rejects invalid values in stored config and updates: %j', (value) => {
])('rejects invalid values: %j', (value) => {
expect(ChannelThreadsConfigSchema.safeParse(value).success).toBe(false);
expect(ChannelThreadsConfigUpdateRequest.safeParse(value).success).toBe(false);
});
test('caps every targeted id list at a thousand', () => {
@@ -82,17 +72,23 @@ describe('channel threads configuration', () => {
}
});
test('bumps the version on every update and keeps ever_enabled sticky', () => {
const first = applyChannelThreadsConfigUpdate(DEFAULT_CHANNEL_THREADS_CONFIG, {user_basis_points: 100});
expect(first.config_version).toBe(1);
expect(first.ever_enabled).toBe(false);
const enabled = applyChannelThreadsConfigUpdate(first, {enabled: true});
expect(enabled.config_version).toBe(2);
expect(enabled.ever_enabled).toBe(true);
const disabled = applyChannelThreadsConfigUpdate(enabled, {enabled: false});
expect(disabled.config_version).toBe(3);
expect(disabled.enabled).toBe(false);
expect(disabled.ever_enabled).toBe(true);
test('the everyone config serves every guild and user and keeps the given version', () => {
const everyone = everyoneChannelThreadsConfig(42);
expect(everyone).toEqual({
...DEFAULT_CHANNEL_THREADS_CONFIG,
enabled: true,
config_version: 42,
ever_enabled: true,
guild_basis_points: 10000,
user_basis_points: 10000,
});
expect(ChannelThreadsConfigSchema.parse(everyone)).toEqual(everyone);
const cfg = compileChannelThreadsConfig(everyone);
for (const id of syntheticIds(200)) {
expect(channelThreadsGuildActive(cfg, id)).toBe(true);
expect(channelThreadsUserActive(cfg, id)).toBe(true);
}
expect(resolveChannelThreadsAssignment(cfg, USER_ID)).toEqual({active: true, config_version: 42});
});
});
@@ -144,24 +140,11 @@ describe('channel threads predicates', () => {
}
});
test('the assignment is present only for active users and carries the version', () => {
test('the assignment is present only for active users and reports the version', () => {
const cfg = compiled({enabled: true, config_version: 7, included_user_ids: [USER_ID]});
expect(resolveChannelThreadsAssignment(cfg, USER_ID)).toEqual({active: true, config_version: 7});
expect(resolveChannelThreadsAssignment(cfg, OTHER_USER_ID)).toBeUndefined();
});
test('field change detection separates the guild and user dimensions', () => {
const base = config({enabled: true, enabled_guild_ids: [GUILD_ID], included_user_ids: [USER_ID]});
expect(channelThreadsGuildFieldsChanged(base, {...base, config_version: 5})).toBe(false);
expect(channelThreadsUserFieldsChanged(base, {...base, config_version: 5})).toBe(false);
expect(channelThreadsGuildFieldsChanged(base, {...base, enabled_guild_ids: [OTHER_GUILD_ID]})).toBe(true);
expect(channelThreadsUserFieldsChanged(base, {...base, enabled_guild_ids: [OTHER_GUILD_ID]})).toBe(false);
expect(channelThreadsGuildFieldsChanged(base, {...base, excluded_user_ids: [USER_ID]})).toBe(false);
expect(channelThreadsUserFieldsChanged(base, {...base, excluded_user_ids: [USER_ID]})).toBe(true);
expect(channelThreadsGuildFieldsChanged(base, {...base, enabled_guild_ids: [GUILD_ID, GUILD_ID]})).toBe(false);
expect(channelThreadsGuildFieldsChanged(base, {...base, enabled: false})).toBe(true);
expect(channelThreadsUserFieldsChanged(base, {...base, enabled: false})).toBe(true);
});
});
describe('shared experiment bucket vectors', () => {
@@ -47,27 +47,23 @@ export type ChannelThreadsConfig = z.infer<typeof ChannelThreadsConfigSchema>;
export const DEFAULT_CHANNEL_THREADS_CONFIG: ChannelThreadsConfig = ChannelThreadsConfigSchema.parse({});
export const ChannelThreadsConfigUpdateRequest = z
.object(channelThreadsConfigFields)
.omit({config_version: true, ever_enabled: true})
.partial();
export type ChannelThreadsConfigUpdateRequest = z.infer<typeof ChannelThreadsConfigUpdateRequest>;
export const ChannelThreadsConfigResponse = ChannelThreadsConfigSchema;
export type ChannelThreadsConfigResponse = z.infer<typeof ChannelThreadsConfigResponse>;
export function applyChannelThreadsConfigUpdate(
current: ChannelThreadsConfig,
update: ChannelThreadsConfigUpdateRequest,
): ChannelThreadsConfig {
const next = ChannelThreadsConfigSchema.parse({
...current,
...update,
config_version: current.config_version + 1,
});
return {...next, ever_enabled: current.ever_enabled || next.enabled};
export function everyoneChannelThreadsConfig(configVersion: number): ChannelThreadsConfig {
return {
...DEFAULT_CHANNEL_THREADS_CONFIG,
enabled: true,
config_version: configVersion,
ever_enabled: true,
guild_basis_points: CHANNEL_THREADS_BASIS_POINTS_MAX,
user_basis_points: CHANNEL_THREADS_BASIS_POINTS_MAX,
enabled_guild_ids: [],
disabled_guild_ids: [],
included_user_ids: [],
excluded_user_ids: [],
};
}
export interface CompiledChannelThreadsConfig {
@@ -88,9 +84,6 @@ export function compileChannelThreadsConfig(config: ChannelThreadsConfig): Compi
};
}
export const DEFAULT_COMPILED_CHANNEL_THREADS_CONFIG: CompiledChannelThreadsConfig =
compileChannelThreadsConfig(DEFAULT_CHANNEL_THREADS_CONFIG);
export function channelThreadsGuildActive(compiled: CompiledChannelThreadsConfig, guildId: string): boolean {
const {config} = compiled;
if (!config.enabled) return false;
@@ -111,32 +104,6 @@ export function channelThreadsUserActive(compiled: CompiledChannelThreadsConfig,
return config.user_basis_points > 0 && experimentBucket(userId, config.user_salt) < config.user_basis_points;
}
function sameIds(a: ReadonlyArray<string>, b: ReadonlyArray<string>): boolean {
const left = new Set(a);
const right = new Set(b);
return left.size === right.size && [...right].every((id) => left.has(id));
}
export function channelThreadsGuildFieldsChanged(a: ChannelThreadsConfig, b: ChannelThreadsConfig): boolean {
return (
a.enabled !== b.enabled ||
a.guild_basis_points !== b.guild_basis_points ||
a.guild_salt !== b.guild_salt ||
!sameIds(a.enabled_guild_ids, b.enabled_guild_ids) ||
!sameIds(a.disabled_guild_ids, b.disabled_guild_ids)
);
}
export function channelThreadsUserFieldsChanged(a: ChannelThreadsConfig, b: ChannelThreadsConfig): boolean {
return (
a.enabled !== b.enabled ||
a.user_basis_points !== b.user_basis_points ||
a.user_salt !== b.user_salt ||
!sameIds(a.included_user_ids, b.included_user_ids) ||
!sameIds(a.excluded_user_ids, b.excluded_user_ids)
);
}
export const ChannelThreadsAssignmentResponse = z.object({
active: z.literal(true),
config_version: z.number().int().min(0),
@@ -1,123 +0,0 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import {
DEFAULT_PLUTONIUM_PAGE_CONFIG,
type PlutoniumPageConfig,
PlutoniumPageConfigSchema,
PlutoniumPageConfigUpdateRequest,
resolvePlutoniumPageAssignment,
} from '@fluxer/schema/src/domains/admin/PlutoniumPageSchemas';
import {type ExperimentTargeting, experimentBucket} from '@fluxer/schema/src/domains/experiment/ExperimentBucket';
import {describe, expect, test} from 'vitest';
const NO_TARGETING: ExperimentTargeting = {memberGuildIds: new Set(), premium: false};
const TARGETED_USER_ID = '1000000000000000001';
function createConfig(overrides: Partial<PlutoniumPageConfig> = {}): PlutoniumPageConfig {
return {...DEFAULT_PLUTONIUM_PAGE_CONFIG, included_user_ids: [], excluded_user_ids: [], ...overrides};
}
function syntheticUserIds(count: number): Array<string> {
return Array.from({length: count}, (_, index) => (1400000000000000000n + BigInt(index)).toString());
}
describe('plutonium page configuration', () => {
test('defaults to disabled', () => {
expect(PlutoniumPageConfigSchema.parse({})).toEqual({
enabled: false,
config_version: 0,
rollout_basis_points: 0,
rollout_salt: 'plutonium-page-v1',
included_user_ids: [],
excluded_user_ids: [],
included_guild_ids: [],
include_premium_users: false,
});
});
test('strips config_version from admin updates', () => {
expect(PlutoniumPageConfigUpdateRequest.safeParse({config_version: 3}).data).toEqual({});
expect(PlutoniumPageConfigUpdateRequest.safeParse({rollout_basis_points: 10001}).success).toBe(false);
});
});
describe('resolvePlutoniumPageAssignment', () => {
test('serves nobody while disabled, even included users', () => {
const config = createConfig({rollout_basis_points: 10000, included_user_ids: [TARGETED_USER_ID]});
expect(resolvePlutoniumPageAssignment(config, TARGETED_USER_ID, NO_TARGETING)).toEqual({enabled: false});
});
test('applies exclusions before inclusions', () => {
const config = createConfig({
enabled: true,
included_user_ids: [TARGETED_USER_ID],
excluded_user_ids: [TARGETED_USER_ID],
});
expect(resolvePlutoniumPageAssignment(config, TARGETED_USER_ID, NO_TARGETING)).toEqual({enabled: false});
});
test('serves included users at zero rollout', () => {
const config = createConfig({enabled: true, included_user_ids: [TARGETED_USER_ID]});
expect(resolvePlutoniumPageAssignment(config, TARGETED_USER_ID, NO_TARGETING)).toEqual({enabled: true});
});
test('buckets the rollout by salt and user id', () => {
const config = createConfig({enabled: true, rollout_basis_points: 2500});
for (const userId of syntheticUserIds(200)) {
expect(resolvePlutoniumPageAssignment(config, userId, NO_TARGETING).enabled).toBe(
experimentBucket(userId, config.rollout_salt) < 2500,
);
}
});
});
describe('resolvePlutoniumPageAssignment guild targeting', () => {
const INCLUDED_GUILD_ID = '3000000000000000001';
const MEMBER_GUILDS: ExperimentTargeting = {
memberGuildIds: new Set(['3000000000000000009', INCLUDED_GUILD_ID]),
premium: false,
};
test('serves members of an included guild at zero rollout', () => {
const config = createConfig({enabled: true, included_guild_ids: [INCLUDED_GUILD_ID]});
expect(resolvePlutoniumPageAssignment(config, TARGETED_USER_ID, MEMBER_GUILDS)).toEqual({enabled: true});
expect(resolvePlutoniumPageAssignment(config, TARGETED_USER_ID, NO_TARGETING)).toEqual({enabled: false});
});
test('keeps user exclusions ahead of guild membership', () => {
const config = createConfig({
enabled: true,
included_guild_ids: [INCLUDED_GUILD_ID],
excluded_user_ids: [TARGETED_USER_ID],
});
expect(resolvePlutoniumPageAssignment(config, TARGETED_USER_ID, MEMBER_GUILDS)).toEqual({enabled: false});
});
test('serves no guild members while disabled', () => {
const config = createConfig({included_guild_ids: [INCLUDED_GUILD_ID]});
expect(resolvePlutoniumPageAssignment(config, TARGETED_USER_ID, MEMBER_GUILDS)).toEqual({enabled: false});
});
});
describe('resolvePlutoniumPageAssignment premium targeting', () => {
const PREMIUM: ExperimentTargeting = {memberGuildIds: new Set(), premium: true};
test('serves premium users only when the switch is on', () => {
const on = createConfig({enabled: true, include_premium_users: true});
expect(resolvePlutoniumPageAssignment(on, TARGETED_USER_ID, PREMIUM)).toEqual({enabled: true});
expect(resolvePlutoniumPageAssignment(on, TARGETED_USER_ID, NO_TARGETING)).toEqual({enabled: false});
expect(resolvePlutoniumPageAssignment(createConfig({enabled: true}), TARGETED_USER_ID, PREMIUM)).toEqual({
enabled: false,
});
});
test('keeps user exclusions ahead of the premium switch', () => {
const config = createConfig({
enabled: true,
include_premium_users: true,
excluded_user_ids: [TARGETED_USER_ID],
});
expect(resolvePlutoniumPageAssignment(config, TARGETED_USER_ID, PREMIUM)).toEqual({enabled: false});
});
});
@@ -1,61 +1,9 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import {
EXPERIMENT_BUCKET_RESOLUTION,
type ExperimentTargeting,
experimentAudienceIncludes,
experimentBucket,
} from '@fluxer/schema/src/domains/experiment/ExperimentBucket';
import {z} from 'zod';
const PLUTONIUM_PAGE_ROLLOUT_BASIS_POINTS_MAX = EXPERIMENT_BUCKET_RESOLUTION;
const PLUTONIUM_PAGE_MAX_TARGETED_USERS = 1000;
const DEFAULT_PLUTONIUM_PAGE_SALT = 'plutonium-page-v1';
const PLUTONIUM_PAGE_SALT_PATTERN = /^[\x20-\x7e]+$/u;
const PlutoniumPageTargetIdSchema = z.string().regex(/^\d{1,20}$/u);
const PlutoniumPageTargetedUserIdsSchema = z.array(PlutoniumPageTargetIdSchema).max(PLUTONIUM_PAGE_MAX_TARGETED_USERS);
const plutoniumPageConfigFields = {
enabled: z.boolean(),
config_version: z.number().int().min(0),
rollout_basis_points: z.number().int().min(0).max(PLUTONIUM_PAGE_ROLLOUT_BASIS_POINTS_MAX),
rollout_salt: z.string().trim().min(1).max(64).regex(PLUTONIUM_PAGE_SALT_PATTERN),
included_user_ids: PlutoniumPageTargetedUserIdsSchema,
included_guild_ids: PlutoniumPageTargetedUserIdsSchema,
include_premium_users: z.boolean(),
excluded_user_ids: PlutoniumPageTargetedUserIdsSchema,
};
export const PlutoniumPageConfigSchema = z.object({
enabled: plutoniumPageConfigFields.enabled.default(false),
config_version: plutoniumPageConfigFields.config_version.default(0),
rollout_basis_points: plutoniumPageConfigFields.rollout_basis_points.default(0),
rollout_salt: plutoniumPageConfigFields.rollout_salt.default(DEFAULT_PLUTONIUM_PAGE_SALT),
included_user_ids: plutoniumPageConfigFields.included_user_ids.default([]),
included_guild_ids: plutoniumPageConfigFields.included_guild_ids.default([]),
include_premium_users: plutoniumPageConfigFields.include_premium_users.default(false),
excluded_user_ids: plutoniumPageConfigFields.excluded_user_ids.default([]),
});
export type PlutoniumPageConfig = z.infer<typeof PlutoniumPageConfigSchema>;
export const DEFAULT_PLUTONIUM_PAGE_CONFIG: PlutoniumPageConfig = PlutoniumPageConfigSchema.parse({});
export const PlutoniumPageConfigUpdateRequest = z
.object(plutoniumPageConfigFields)
.omit({config_version: true})
.partial();
export type PlutoniumPageConfigUpdateRequest = z.infer<typeof PlutoniumPageConfigUpdateRequest>;
export const PlutoniumPageConfigResponse = PlutoniumPageConfigSchema;
export type PlutoniumPageConfigResponse = z.infer<typeof PlutoniumPageConfigResponse>;
export const PlutoniumPageAssignmentResponse = z.object({
enabled: plutoniumPageConfigFields.enabled,
enabled: z.boolean(),
});
export type PlutoniumPageAssignmentResponse = z.infer<typeof PlutoniumPageAssignmentResponse>;
@@ -63,15 +11,3 @@ export type PlutoniumPageAssignmentResponse = z.infer<typeof PlutoniumPageAssign
export const INERT_PLUTONIUM_PAGE_ASSIGNMENT: PlutoniumPageAssignmentResponse = {
enabled: false,
};
export function resolvePlutoniumPageAssignment(
config: PlutoniumPageConfig,
userId: string,
targeting: ExperimentTargeting,
): PlutoniumPageAssignmentResponse {
if (!config.enabled) return {...INERT_PLUTONIUM_PAGE_ASSIGNMENT};
if (config.excluded_user_ids.includes(userId)) return {...INERT_PLUTONIUM_PAGE_ASSIGNMENT};
if (config.included_user_ids.includes(userId)) return {enabled: true};
if (experimentAudienceIncludes(config, targeting)) return {enabled: true};
return {enabled: experimentBucket(userId, config.rollout_salt) < config.rollout_basis_points};
}