refactor progress

This commit is contained in:
Hampus Kraft
2026-02-17 12:22:36 +00:00
parent cb31608523
commit d5abd1a7e4
8257 changed files with 1190207 additions and 761040 deletions

View File

@@ -0,0 +1,181 @@
/*
* Copyright (C) 2026 Fluxer Contributors
*
* This file is part of Fluxer.
*
* Fluxer is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Fluxer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Fluxer. If not, see <https://www.gnu.org/licenses/>.
*/
import {Config} from '@fluxer/api/src/Config';
import type {CachedLimitConfig} from '@fluxer/api/src/constants/LimitConfig';
import {
createDefaultLimitConfig,
getLimitConfigKvKey,
LIMIT_CONFIG_REFRESH_CHANNEL,
LIMIT_CONFIG_REFRESH_LOCK_KEY,
mergeWithCurrentDefaults,
sanitizeLimitConfigForInstance,
} from '@fluxer/api/src/constants/LimitConfig';
import type {InstanceConfigRepository} from '@fluxer/api/src/instance/InstanceConfigRepository';
import {Logger} from '@fluxer/api/src/Logger';
import type {ICacheService} from '@fluxer/cache/src/ICacheService';
import type {IKVProvider, IKVSubscription} from '@fluxer/kv_client/src/IKVProvider';
import {computeWireFormat} from '@fluxer/limits/src/LimitDiffer';
import {computeDefaultsHash} from '@fluxer/limits/src/LimitHashing';
import type {LimitConfigSnapshot, LimitConfigWireFormat} from '@fluxer/limits/src/LimitTypes';
let globalLimitConfigService: LimitConfigService | null = null;
export class LimitConfigService {
private config: LimitConfigSnapshot = createDefaultLimitConfig({selfHosted: Config.instance.selfHosted});
private repository: InstanceConfigRepository;
private cacheService: ICacheService;
private kvClient: IKVProvider | null;
private kvSubscription: IKVSubscription | null = null;
private subscriberInitialized = false;
private readonly cacheKey: string;
constructor(repository: InstanceConfigRepository, cacheService: ICacheService, kvClient: IKVProvider | null = null) {
this.repository = repository;
this.cacheService = cacheService;
this.kvClient = kvClient;
this.cacheKey = getLimitConfigKvKey(Config.instance.selfHosted);
}
setAsGlobalInstance(): void {
globalLimitConfigService = this;
}
async initialize(): Promise<void> {
await this.refreshCache();
this.initializeSubscriber();
Logger.info('LimitConfigService initialized');
}
getConfigSnapshot(): LimitConfigSnapshot {
return this.config;
}
getConfigWireFormat(): LimitConfigWireFormat {
return computeWireFormat(this.config);
}
async refreshCache(): Promise<void> {
const currentHash = computeDefaultsHash();
const lockToken = await this.cacheService.acquireLock(LIMIT_CONFIG_REFRESH_LOCK_KEY, 10);
if (!lockToken) {
Logger.debug('Limit config refresh already in progress, waiting for cache update');
await this.sleep(50);
const cached = await this.cacheService.get<CachedLimitConfig>(this.cacheKey);
if (cached && cached.defaultsHash === currentHash) {
this.config = sanitizeLimitConfigForInstance(cached.config, {selfHosted: Config.instance.selfHosted});
return;
}
this.config = createDefaultLimitConfig({selfHosted: Config.instance.selfHosted});
return;
}
try {
const cached = await this.cacheService.get<CachedLimitConfig>(this.cacheKey);
if (cached && cached.defaultsHash === currentHash) {
this.config = sanitizeLimitConfigForInstance(cached.config, {selfHosted: Config.instance.selfHosted});
return;
}
const dbConfig = await this.repository.getLimitConfig();
if (dbConfig === null) {
this.config = createDefaultLimitConfig({selfHosted: Config.instance.selfHosted});
await this.cacheService.delete(this.cacheKey);
Logger.debug('No database limit config, using fresh defaults');
return;
}
Logger.info(
{hashMismatch: cached?.defaultsHash !== currentHash},
'Merging database config with current defaults',
);
const merged = mergeWithCurrentDefaults(dbConfig, {selfHosted: Config.instance.selfHosted});
await this.repository.setLimitConfig(merged);
await this.cacheService.set(this.cacheKey, {
config: merged,
defaultsHash: currentHash,
});
this.config = sanitizeLimitConfigForInstance(merged, {selfHosted: Config.instance.selfHosted});
} finally {
await this.cacheService.releaseLock(LIMIT_CONFIG_REFRESH_LOCK_KEY, lockToken);
}
}
private sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async updateConfig(config: LimitConfigSnapshot): Promise<void> {
const normalized = sanitizeLimitConfigForInstance(config, {selfHosted: Config.instance.selfHosted});
await this.repository.setLimitConfig(normalized);
await this.cacheService.delete(this.cacheKey);
await this.refreshCache();
await this.cacheService.publish(LIMIT_CONFIG_REFRESH_CHANNEL, 'refresh');
Logger.info({ruleCount: normalized.rules.length}, 'Limit config updated');
}
private initializeSubscriber(): void {
if (this.subscriberInitialized || !this.kvClient) {
return;
}
const subscription = this.kvClient.duplicate();
this.kvSubscription = subscription;
subscription
.connect()
.then(() => subscription.subscribe(LIMIT_CONFIG_REFRESH_CHANNEL))
.then(() => {
subscription.on('message', (channel) => {
if (channel === LIMIT_CONFIG_REFRESH_CHANNEL) {
this.refreshCache().catch((err) => {
Logger.error({err}, 'Failed to refresh limit config from pubsub');
});
}
});
})
.catch((error) => {
Logger.error({error}, 'Failed to subscribe to limit config refresh channel');
});
this.subscriberInitialized = true;
}
shutdown(): void {
if (this.kvSubscription) {
this.kvSubscription.quit().catch((err) => {
Logger.error({err}, 'Failed to close KV subscription');
});
this.kvSubscription = null;
}
}
}
export function getGlobalLimitConfigSnapshot(): LimitConfigSnapshot {
if (!globalLimitConfigService) {
throw new Error('LimitConfigService global instance has not been initialized');
}
return globalLimitConfigService.getConfigSnapshot();
}

View File

@@ -0,0 +1,40 @@
/*
* Copyright (C) 2026 Fluxer Contributors
*
* This file is part of Fluxer.
*
* Fluxer is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Fluxer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Fluxer. If not, see <https://www.gnu.org/licenses/>.
*/
import type {LimitKey} from '@fluxer/constants/src/LimitConfigMetadata';
import {resolveLimit} from '@fluxer/limits/src/LimitResolver';
import type {EvaluationContext, LimitConfigSnapshot, LimitMatchContext} from '@fluxer/limits/src/LimitTypes';
export function resolveLimitSafe(
snapshot: LimitConfigSnapshot | null | undefined,
ctx: LimitMatchContext,
key: LimitKey,
fallback: number,
evaluationContext: EvaluationContext = 'user',
): number {
if (!snapshot) {
return fallback;
}
const resolved = resolveLimit(snapshot, ctx, key, {evaluationContext});
if (Number.isFinite(resolved) && resolved >= 0) {
return resolved;
}
return fallback;
}

View File

@@ -0,0 +1,46 @@
/*
* Copyright (C) 2026 Fluxer Contributors
*
* This file is part of Fluxer.
*
* Fluxer is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Fluxer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Fluxer. If not, see <https://www.gnu.org/licenses/>.
*/
import type {User} from '@fluxer/api/src/models/User';
import {checkIsPremium} from '@fluxer/api/src/user/UserHelpers';
import type {LimitMatchContext} from '@fluxer/limits/src/LimitTypes';
export function createLimitMatchContext({
user,
guildFeatures,
}: {
user?: User | null;
guildFeatures?: Iterable<string> | null;
}): LimitMatchContext {
const traits = new Set<string>();
const traitValues = user?.traits ? Array.from(user.traits) : [];
for (const trait of traitValues) {
if (trait && trait !== 'premium') traits.add(trait);
}
if (user && checkIsPremium(user)) {
traits.add('premium');
}
const guildFeatureSet = new Set<string>();
if (guildFeatures) {
for (const feature of guildFeatures) {
if (feature) guildFeatureSet.add(feature);
}
}
return {traits, guildFeatures: guildFeatureSet};
}

View File

@@ -0,0 +1,66 @@
/*
* Copyright (C) 2026 Fluxer Contributors
*
* This file is part of Fluxer.
*
* Fluxer is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Fluxer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Fluxer. If not, see <https://www.gnu.org/licenses/>.
*/
import {createDefaultLimitConfig, mergeWithCurrentDefaults} from '@fluxer/api/src/constants/LimitConfig';
import type {LimitConfigSnapshot} from '@fluxer/limits/src/LimitTypes';
import {describe, expect, test} from 'vitest';
describe('Limit config defaults', () => {
test('hosted defaults include only premium and default tier limit rules', () => {
const config = createDefaultLimitConfig({selfHosted: false});
const premiumRule = config.rules.find((rule) => rule.id === 'premium');
const defaultRule = config.rules.find((rule) => rule.id === 'default');
expect(premiumRule).toBeDefined();
expect(defaultRule).toBeDefined();
expect(config.rules.map((rule) => rule.id)).toEqual(['premium', 'default']);
});
test('self-hosted defaults include only default tier limit rule', () => {
const config = createDefaultLimitConfig({selfHosted: true});
expect(config.rules.map((rule) => rule.id)).toEqual(['default']);
});
});
describe('Limit config default merge', () => {
test('legacy unlocked features on known rules are dropped during merge', () => {
const legacyConfig = {
traitDefinitions: ['premium'],
rules: [
{
id: 'premium',
filters: {traits: ['premium']},
limits: {},
unlockedFeatures: ['MORE_EMOJI', 'UNLIMITED_EMOJI'],
},
{
id: 'default',
limits: {},
},
],
} as unknown as LimitConfigSnapshot;
const merged = mergeWithCurrentDefaults(legacyConfig, {selfHosted: false});
const premiumRule = merged.rules.find((rule) => rule.id === 'premium') as Record<string, unknown> | undefined;
expect(premiumRule?.unlockedFeatures).toBeUndefined();
});
});

View File

@@ -0,0 +1,285 @@
/*
* Copyright (C) 2026 Fluxer Contributors
*
* This file is part of Fluxer.
*
* Fluxer is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Fluxer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Fluxer. If not, see <https://www.gnu.org/licenses/>.
*/
import {type ApiTestHarness, createApiTestHarness} from '@fluxer/api/src/test/ApiTestHarness';
import {HTTP_STATUS} from '@fluxer/api/src/test/TestConstants';
import {createBuilderWithoutAuth} from '@fluxer/api/src/test/TestRequestBuilder';
import type {LimitKey} from '@fluxer/constants/src/LimitConfigMetadata';
import {DEFAULT_FREE_LIMITS, DEFAULT_PREMIUM_LIMITS} from '@fluxer/limits/src/LimitDefaults';
import {expandWireFormat} from '@fluxer/limits/src/LimitDiffer';
import {computeDefaultsHash} from '@fluxer/limits/src/LimitHashing';
import {resolveLimits} from '@fluxer/limits/src/LimitResolver';
import type {LimitConfigWireFormat, LimitMatchContext} from '@fluxer/limits/src/LimitTypes';
import {afterEach, beforeEach, describe, expect, test} from 'vitest';
interface WellKnownResponse {
limits: LimitConfigWireFormat;
}
describe('Limit Wire Format', () => {
let harness: ApiTestHarness;
beforeEach(async () => {
harness = await createApiTestHarness();
});
afterEach(async () => {
await harness?.shutdown();
});
test('well-known endpoint returns version 2 format', async () => {
const response = (await createBuilderWithoutAuth(harness)
.get('/.well-known/fluxer')
.expect(HTTP_STATUS.OK)
.execute()) as WellKnownResponse;
expect(response.limits).toBeDefined();
expect(response.limits.version).toBe(2);
expect(response.limits.traitDefinitions).toBeDefined();
expect(Array.isArray(response.limits.traitDefinitions)).toBe(true);
expect(response.limits.rules).toBeDefined();
expect(Array.isArray(response.limits.rules)).toBe(true);
expect(response.limits.defaultsHash).toBeDefined();
expect(typeof response.limits.defaultsHash).toBe('string');
});
test('rules have overrides field not limits field in well-known response', async () => {
const response = (await createBuilderWithoutAuth(harness)
.get('/.well-known/fluxer')
.expect(HTTP_STATUS.OK)
.execute()) as WellKnownResponse;
expect(response.limits.rules.length).toBeGreaterThan(0);
for (const rule of response.limits.rules) {
expect(rule.id).toBeDefined();
expect(rule.overrides).toBeDefined();
expect(typeof rule.overrides).toBe('object');
// @ts-expect-error - checking that limits field does not exist in wire format
expect(rule.limits).toBeUndefined();
}
});
test('wire format already has overrides field', async () => {
const response = (await createBuilderWithoutAuth(harness)
.get('/.well-known/fluxer')
.expect(HTTP_STATUS.OK)
.execute()) as WellKnownResponse;
expect(response.limits.version).toBe(2);
expect(response.limits.defaultsHash).toBeDefined();
expect(typeof response.limits.defaultsHash).toBe('string');
expect(response.limits.defaultsHash.length).toBeGreaterThan(0);
for (const rule of response.limits.rules) {
expect(typeof rule.overrides).toBe('object');
// @ts-expect-error - checking that limits field does not exist in wire format
expect(rule.limits).toBeUndefined();
}
});
test('defaultsHash matches computed hash', async () => {
const response = (await createBuilderWithoutAuth(harness)
.get('/.well-known/fluxer')
.expect(HTTP_STATUS.OK)
.execute()) as WellKnownResponse;
const expectedHash = computeDefaultsHash();
expect(response.limits.defaultsHash).toBe(expectedHash);
});
test('wire format can be expanded back to full format', async () => {
const response = (await createBuilderWithoutAuth(harness)
.get('/.well-known/fluxer')
.expect(HTTP_STATUS.OK)
.execute()) as WellKnownResponse;
const expanded = expandWireFormat(response.limits);
expect(expanded.traitDefinitions).toEqual(response.limits.traitDefinitions);
expect(expanded.rules.length).toBe(response.limits.rules.length);
for (let i = 0; i < expanded.rules.length; i++) {
const expandedRule = expanded.rules[i];
const wireRule = response.limits.rules[i];
expect(expandedRule.id).toBe(wireRule.id);
expect(expandedRule.filters).toEqual(wireRule.filters);
expect(expandedRule.limits).toBeDefined();
expect(typeof expandedRule.limits).toBe('object');
for (const key of Object.keys(DEFAULT_FREE_LIMITS) as Array<LimitKey>) {
expect(expandedRule.limits[key]).toBeDefined();
}
}
});
test('premium rules have correct overrides compared to free defaults', async () => {
const response = (await createBuilderWithoutAuth(harness)
.get('/.well-known/fluxer')
.expect(HTTP_STATUS.OK)
.execute()) as WellKnownResponse;
const premiumRule = response.limits.rules.find((rule) => rule.id === 'premium');
if (!premiumRule) {
return;
}
expect(premiumRule.overrides).toBeDefined();
const overrideKeys = Object.keys(premiumRule.overrides) as Array<LimitKey>;
expect(overrideKeys.length).toBeGreaterThan(0);
for (const key of overrideKeys) {
const overrideValue = premiumRule.overrides[key];
const freeValue = DEFAULT_FREE_LIMITS[key];
expect(overrideValue).not.toBe(freeValue);
expect(DEFAULT_PREMIUM_LIMITS[key]).toBe(overrideValue);
}
});
test('default rule has minimal or no overrides', async () => {
const response = (await createBuilderWithoutAuth(harness)
.get('/.well-known/fluxer')
.expect(HTTP_STATUS.OK)
.execute()) as WellKnownResponse;
const defaultRule = response.limits.rules.find((rule) => rule.id === 'default');
if (!defaultRule) {
return;
}
expect(defaultRule.overrides).toBeDefined();
const overrideCount = Object.keys(defaultRule.overrides).length;
expect(overrideCount).toBe(0);
});
test('wire format roundtrip preserves limit resolution behavior', async () => {
const response = (await createBuilderWithoutAuth(harness)
.get('/.well-known/fluxer')
.expect(HTTP_STATUS.OK)
.execute()) as WellKnownResponse;
const expanded = expandWireFormat(response.limits);
const freeContext: LimitMatchContext = {
traits: new Set(),
guildFeatures: new Set(),
};
const premiumContext: LimitMatchContext = {
traits: new Set(['premium']),
guildFeatures: new Set(),
};
const expandedFree = resolveLimits(expanded, freeContext);
const expandedPremium = resolveLimits(expanded, premiumContext);
for (const key of Object.keys(DEFAULT_FREE_LIMITS) as Array<LimitKey>) {
expect(expandedFree.limits[key]).toBe(DEFAULT_FREE_LIMITS[key]);
}
for (const key of Object.keys(DEFAULT_PREMIUM_LIMITS) as Array<LimitKey>) {
expect(expandedPremium.limits[key]).toBe(DEFAULT_PREMIUM_LIMITS[key]);
}
});
test('wire format correctly identifies premium feature overrides', async () => {
const response = (await createBuilderWithoutAuth(harness)
.get('/.well-known/fluxer')
.expect(HTTP_STATUS.OK)
.execute()) as WellKnownResponse;
const premiumRule = response.limits.rules.find((rule) => rule.id === 'premium');
if (!premiumRule) {
return;
}
const featureKeys = Object.keys(premiumRule.overrides).filter((key) =>
key.startsWith('feature_'),
) as Array<LimitKey>;
for (const key of featureKeys) {
expect(DEFAULT_FREE_LIMITS[key]).toBe(0);
expect(DEFAULT_PREMIUM_LIMITS[key]).toBe(1);
expect(premiumRule.overrides[key]).toBe(1);
}
});
test('wire format preserves trait definitions', async () => {
const response = (await createBuilderWithoutAuth(harness)
.get('/.well-known/fluxer')
.expect(HTTP_STATUS.OK)
.execute()) as WellKnownResponse;
expect(response.limits.traitDefinitions).toBeDefined();
expect(Array.isArray(response.limits.traitDefinitions)).toBe(true);
});
test('wire format preserves rule filters', async () => {
const response = (await createBuilderWithoutAuth(harness)
.get('/.well-known/fluxer')
.expect(HTTP_STATUS.OK)
.execute()) as WellKnownResponse;
for (const rule of response.limits.rules) {
if (rule.filters) {
expect(typeof rule.filters).toBe('object');
}
}
});
test('defaultsHash is stable across conversions', async () => {
const hash1 = computeDefaultsHash();
const hash2 = computeDefaultsHash();
expect(hash1).toBe(hash2);
expect(typeof hash1).toBe('string');
expect(hash1.length).toBeGreaterThan(0);
});
test('wire format type definition matches runtime structure', async () => {
const response = (await createBuilderWithoutAuth(harness)
.get('/.well-known/fluxer')
.expect(HTTP_STATUS.OK)
.execute()) as WellKnownResponse;
const wireFormat: LimitConfigWireFormat = response.limits;
expect(wireFormat.version).toBe(2);
expect(typeof wireFormat.defaultsHash).toBe('string');
expect(Array.isArray(wireFormat.traitDefinitions)).toBe(true);
expect(Array.isArray(wireFormat.rules)).toBe(true);
for (const rule of wireFormat.rules) {
expect(typeof rule.id).toBe('string');
expect(typeof rule.overrides).toBe('object');
if (rule.filters) {
expect(typeof rule.filters).toBe('object');
}
}
});
});