From 3d0b7a0ea28d421aaa57a48118072fc82e9d5ccf Mon Sep 17 00:00:00 2001 From: Alec Rippberger Date: Thu, 3 Oct 2024 21:40:12 -0500 Subject: [PATCH] Add server settings model and service. --- .../abstractions/config/config.service.ts | 3 +++ .../abstractions/config/server-config.ts | 3 +++ .../platform/models/data/server-config.data.ts | 3 +++ .../platform/models/domain/server-settings.ts | 11 +++++++++++ .../models/response/server-config.response.ts | 3 +++ .../services/config/default-config.service.ts | 11 +++++++++-- .../platform/services/server-settings.service.ts | 16 ++++++++++++++++ 7 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 libs/common/src/platform/models/domain/server-settings.ts create mode 100644 libs/common/src/platform/services/server-settings.service.ts diff --git a/libs/common/src/platform/abstractions/config/config.service.ts b/libs/common/src/platform/abstractions/config/config.service.ts index 9b16cee385..05a3dcd148 100644 --- a/libs/common/src/platform/abstractions/config/config.service.ts +++ b/libs/common/src/platform/abstractions/config/config.service.ts @@ -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; + /** The server settings of the currently active user */ + serverSettings$: Observable; /** The cloud region of the currently active user */ cloudRegion$: Observable; /** diff --git a/libs/common/src/platform/abstractions/config/server-config.ts b/libs/common/src/platform/abstractions/config/server-config.ts index bb18605964..b51628cbf5 100644 --- a/libs/common/src/platform/abstractions/config/server-config.ts +++ b/libs/common/src/platform/abstractions/config/server-config.ts @@ -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; diff --git a/libs/common/src/platform/models/data/server-config.data.ts b/libs/common/src/platform/models/data/server-config.data.ts index 57e8fbc628..d5f17fd0ac 100644 --- a/libs/common/src/platform/models/data/server-config.data.ts +++ b/libs/common/src/platform/models/data/server-config.data.ts @@ -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) { 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 { diff --git a/libs/common/src/platform/models/domain/server-settings.ts b/libs/common/src/platform/models/domain/server-settings.ts new file mode 100644 index 0000000000..abe6df62ab --- /dev/null +++ b/libs/common/src/platform/models/domain/server-settings.ts @@ -0,0 +1,11 @@ +export interface Settings { + disableUserRegistration: boolean; +} + +export class ServerSettings implements Settings { + disableUserRegistration: boolean; + + constructor(data?: Settings) { + this.disableUserRegistration = data?.disableUserRegistration ?? false; + } +} diff --git a/libs/common/src/platform/models/response/server-config.response.ts b/libs/common/src/platform/models/response/server-config.response.ts index a546d2d3de..d295634830 100644 --- a/libs/common/src/platform/models/response/server-config.response.ts +++ b/libs/common/src/platform/models/response/server-config.response.ts @@ -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")); } } diff --git a/libs/common/src/platform/services/config/default-config.service.ts b/libs/common/src/platform/services/config/default-config.service.ts index e0603ed509..6472c0ad06 100644 --- a/libs/common/src/platform/services/config/default-config.service.ts +++ b/libs/common/src/platform/services/config/default-config.service.ts @@ -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; + serverSettings$: Observable; + cloudRegion$: Observable; 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$(key: Flag) { diff --git a/libs/common/src/platform/services/server-settings.service.ts b/libs/common/src/platform/services/server-settings.service.ts new file mode 100644 index 0000000000..87441c2b2c --- /dev/null +++ b/libs/common/src/platform/services/server-settings.service.ts @@ -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 { + return this.configService.serverSettings$.pipe(map((settings: ServerSettings) => settings)); + } + + get isUserRegistrationDisabled$(): Observable { + return this.getSettings$().pipe(map((settings: Settings) => settings.disableUserRegistration)); + } +}