Add server settings model and service.
This commit is contained in:
parent
903c215867
commit
3d0b7a0ea2
|
@ -3,6 +3,7 @@ import { SemVer } from "semver";
|
|||
|
||||
import { FeatureFlag, FeatureFlagValueType } from "../../../enums/feature-flag.enum";
|
||||
import { UserId } from "../../../types/guid";
|
||||
import { ServerSettings } from "../../models/domain/server-settings";
|
||||
import { Region } from "../environment.service";
|
||||
|
||||
import { ServerConfig } from "./server-config";
|
||||
|
@ -10,6 +11,8 @@ import { ServerConfig } from "./server-config";
|
|||
export abstract class ConfigService {
|
||||
/** The server config of the currently active user */
|
||||
serverConfig$: Observable<ServerConfig | null>;
|
||||
/** The server settings of the currently active user */
|
||||
serverSettings$: Observable<ServerSettings | null>;
|
||||
/** The cloud region of the currently active user */
|
||||
cloudRegion$: Observable<Region>;
|
||||
/**
|
||||
|
|
|
@ -6,6 +6,7 @@ import {
|
|||
ThirdPartyServerConfigData,
|
||||
EnvironmentServerConfigData,
|
||||
} from "../../models/data/server-config.data";
|
||||
import { ServerSettings } from "../../models/domain/server-settings";
|
||||
|
||||
const dayInMilliseconds = 24 * 3600 * 1000;
|
||||
|
||||
|
@ -16,6 +17,7 @@ export class ServerConfig {
|
|||
environment?: EnvironmentServerConfigData;
|
||||
utcDate: Date;
|
||||
featureStates: { [key: string]: AllowedFeatureFlagTypes } = {};
|
||||
settings: ServerSettings;
|
||||
|
||||
constructor(serverConfigData: ServerConfigData) {
|
||||
this.version = serverConfigData.version;
|
||||
|
@ -24,6 +26,7 @@ export class ServerConfig {
|
|||
this.utcDate = new Date(serverConfigData.utcDate);
|
||||
this.environment = serverConfigData.environment;
|
||||
this.featureStates = serverConfigData.featureStates;
|
||||
this.settings = serverConfigData.settings;
|
||||
|
||||
if (this.server?.name == null && this.server?.url == null) {
|
||||
this.server = null;
|
||||
|
|
|
@ -2,6 +2,7 @@ import { Jsonify } from "type-fest";
|
|||
|
||||
import { AllowedFeatureFlagTypes } from "../../../enums/feature-flag.enum";
|
||||
import { Region } from "../../abstractions/environment.service";
|
||||
import { ServerSettings } from "../domain/server-settings";
|
||||
import {
|
||||
ServerConfigResponse,
|
||||
ThirdPartyServerConfigResponse,
|
||||
|
@ -15,6 +16,7 @@ export class ServerConfigData {
|
|||
environment?: EnvironmentServerConfigData;
|
||||
utcDate: string;
|
||||
featureStates: { [key: string]: AllowedFeatureFlagTypes } = {};
|
||||
settings: ServerSettings;
|
||||
|
||||
constructor(serverConfigResponse: Partial<ServerConfigResponse>) {
|
||||
this.version = serverConfigResponse?.version;
|
||||
|
@ -27,6 +29,7 @@ export class ServerConfigData {
|
|||
? new EnvironmentServerConfigData(serverConfigResponse.environment)
|
||||
: null;
|
||||
this.featureStates = serverConfigResponse?.featureStates;
|
||||
this.settings = new ServerSettings(serverConfigResponse.settings);
|
||||
}
|
||||
|
||||
static fromJSON(obj: Jsonify<ServerConfigData>): ServerConfigData {
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
export interface Settings {
|
||||
disableUserRegistration: boolean;
|
||||
}
|
||||
|
||||
export class ServerSettings implements Settings {
|
||||
disableUserRegistration: boolean;
|
||||
|
||||
constructor(data?: Settings) {
|
||||
this.disableUserRegistration = data?.disableUserRegistration ?? false;
|
||||
}
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
import { AllowedFeatureFlagTypes } from "../../../enums/feature-flag.enum";
|
||||
import { BaseResponse } from "../../../models/response/base.response";
|
||||
import { Region } from "../../abstractions/environment.service";
|
||||
import { ServerSettings } from "../domain/server-settings";
|
||||
|
||||
export class ServerConfigResponse extends BaseResponse {
|
||||
version: string;
|
||||
|
@ -8,6 +9,7 @@ export class ServerConfigResponse extends BaseResponse {
|
|||
server: ThirdPartyServerConfigResponse;
|
||||
environment: EnvironmentServerConfigResponse;
|
||||
featureStates: { [key: string]: AllowedFeatureFlagTypes } = {};
|
||||
settings: ServerSettings;
|
||||
|
||||
constructor(response: any) {
|
||||
super(response);
|
||||
|
@ -21,6 +23,7 @@ export class ServerConfigResponse extends BaseResponse {
|
|||
this.server = new ThirdPartyServerConfigResponse(this.getResponseProperty("Server"));
|
||||
this.environment = new EnvironmentServerConfigResponse(this.getResponseProperty("Environment"));
|
||||
this.featureStates = this.getResponseProperty("FeatureStates");
|
||||
this.settings = new ServerSettings(this.getResponseProperty("Settings"));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -28,11 +28,12 @@ import { Environment, EnvironmentService, Region } from "../../abstractions/envi
|
|||
import { LogService } from "../../abstractions/log.service";
|
||||
import { devFlagEnabled, devFlagValue } from "../../misc/flags";
|
||||
import { ServerConfigData } from "../../models/data/server-config.data";
|
||||
import { ServerSettings } from "../../models/domain/server-settings";
|
||||
import { CONFIG_DISK, KeyDefinition, StateProvider, UserKeyDefinition } from "../../state";
|
||||
|
||||
export const RETRIEVAL_INTERVAL = devFlagEnabled("configRetrievalIntervalMs")
|
||||
? (devFlagValue("configRetrievalIntervalMs") as number)
|
||||
: 3_600_000; // 1 hour
|
||||
: 60_000; // 1 minute
|
||||
|
||||
export const SLOW_EMISSION_GUARD = 800;
|
||||
|
||||
|
@ -57,6 +58,8 @@ export class DefaultConfigService implements ConfigService {
|
|||
|
||||
serverConfig$: Observable<ServerConfig>;
|
||||
|
||||
serverSettings$: Observable<ServerSettings>;
|
||||
|
||||
cloudRegion$: Observable<Region>;
|
||||
|
||||
constructor(
|
||||
|
@ -79,7 +82,7 @@ export class DefaultConfigService implements ConfigService {
|
|||
switchMap(([userId, environment, authStatus]) => {
|
||||
if (userId == null || authStatus !== AuthenticationStatus.Unlocked) {
|
||||
return this.globalConfigFor$(environment.getApiUrl()).pipe(
|
||||
map((config) => [config, null, environment] as const),
|
||||
map((config): readonly [ServerConfig, any, Environment] => [config, null, environment]),
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -111,6 +114,10 @@ export class DefaultConfigService implements ConfigService {
|
|||
this.cloudRegion$ = this.serverConfig$.pipe(
|
||||
map((config) => config?.environment?.cloudRegion ?? Region.US),
|
||||
);
|
||||
|
||||
this.serverSettings$ = this.serverConfig$.pipe(
|
||||
map((config) => config?.settings ?? new ServerSettings()),
|
||||
);
|
||||
}
|
||||
|
||||
getFeatureFlag$<Flag extends FeatureFlag>(key: Flag) {
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
import { map, Observable } from "rxjs";
|
||||
|
||||
import { ConfigService } from "../abstractions/config/config.service";
|
||||
import { ServerSettings, Settings } from "../models/domain/server-settings";
|
||||
|
||||
export class ServerSettingsService {
|
||||
constructor(private configService: ConfigService) {}
|
||||
|
||||
getSettings$(): Observable<Settings> {
|
||||
return this.configService.serverSettings$.pipe(map((settings: ServerSettings) => settings));
|
||||
}
|
||||
|
||||
get isUserRegistrationDisabled$(): Observable<boolean> {
|
||||
return this.getSettings$().pipe(map((settings: Settings) => settings.disableUserRegistration));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue