import { Injectable } from '@angular/core'; import { BaseHttpParams } from '@common/http/base-http-params'; import { InterceptorType } from '@common/http/interceptors/interceptor-type'; import { BaseComponent } from '@common/base/base.component'; import { catchError, takeUntil } from 'rxjs/operators'; import { Observable, throwError } from 'rxjs'; import { HelpService } from '@app/core/model/configuration-models/help-service.model'; import { Logging } from '@app/core/model/configuration-models/logging.model'; import { HttpClient } from '@angular/common/http'; import { KeycloakConfiguration } from '@app/core/model/configuration-models/keycloak-configuration.model'; import { Guid } from '@common/types/guid'; @Injectable({ providedIn: 'root', }) export class ConfigurationService extends BaseComponent { constructor(private http: HttpClient) { super(); } private _server: string; get server(): string { return this._server; } private _app: string; get app(): string { return this._app; } private _helpService: HelpService; get helpService(): HelpService { return this._helpService; } private _defaultCulture: string; get defaultCulture(): string { return this._defaultCulture; } private _defaultBlueprintId: Guid; get defaultBlueprintId(): Guid { return this._defaultBlueprintId; } private _defaultTimezone: string; get defaultTimezone(): string { return this._defaultTimezone || 'UTC'; } private _logging: Logging; get logging(): Logging { return this._logging; } private _lockInterval: number; get lockInterval(): number { return this._lockInterval; } private _guideAssets: string; get guideAssets(): string { return this._guideAssets; } private _allowOrganizationCreator: boolean; get allowOrganizationCreator(): boolean { return this._allowOrganizationCreator; } private _useSplash: string; get useSplash(): string { return this._useSplash; } private _orcidPath: string; get orcidPath(): string { return this._orcidPath; } private _matomoEnabled: boolean; get matomoEnabled(): boolean { return this._matomoEnabled; } private _matomoSiteUrl: string; get matomoSiteUrl(): string { return this._matomoSiteUrl; } private _matomoSiteId: number; get matomoSiteId(): number { return this._matomoSiteId; } private _maxFileSizeInMB: number; get maxFileSizeInMB(): number { return this._maxFileSizeInMB; } private _keycloak: KeycloakConfiguration; get keycloak(): KeycloakConfiguration { return this._keycloak; } private _userSettingsVersion: string; get userSettingsVersion(): string { return this._userSettingsVersion; } private _notificationServiceAddress: string; get notificationServiceAddress(): string { return this._notificationServiceAddress || './'; } private _notificationServiceEnabled: boolean; get notificationServiceEnabled(): boolean { return this._notificationServiceEnabled; } private _annotationServiceAddress: string; get annotationServiceAddress(): string { return this._annotationServiceAddress || './'; } private _annotationServiceEnabled: boolean; get annotationServiceEnabled(): boolean { return this._annotationServiceEnabled; } private _inAppNotificationsCountInterval: number; get inAppNotificationsCountInterval(): number { return this._inAppNotificationsCountInterval || 3200; } private _newReleaseNotificationLink: number; get newReleaseNotificationLink(): number { return this._newReleaseNotificationLink; } private _newReleaseNotificationExpires: number; get newReleaseNotificationExpires(): number { return this._newReleaseNotificationExpires; } private _newReleaseNotificationVersionCode: number; get newReleaseNotificationVersionCode(): number { return this._newReleaseNotificationVersionCode; } public loadConfiguration(): Promise { return new Promise((r, e) => { // We need to exclude all interceptors here, for the initial configuration request. const params = new BaseHttpParams(); params.interceptorContext = { excludedInterceptors: [ InterceptorType.AuthToken, InterceptorType.JSONContentType, InterceptorType.Locale, InterceptorType.ProgressIndication, InterceptorType.RequestTiming, InterceptorType.UnauthorizedResponse, ], }; this.http .get("./assets/config/config.json", { params: params }) .pipe( catchError((err: any, caught: Observable) => throwError(err) ) ) .pipe(takeUntil(this._destroyed)) .subscribe( (content: ConfigurationService) => { this.parseResponse(content); r(this); }, (reason) => e(reason) ); }); } private parseResponse(config: any) { this._server = config.Server; this._app = config.App; this._helpService = HelpService.parseValue(config.HelpService); this._defaultCulture = config.defaultCulture; this._defaultBlueprintId = config.defaultBlueprintId; this._defaultTimezone = config.defaultTimezone; this._keycloak = KeycloakConfiguration.parseValue(config.keycloak); this._logging = Logging.parseValue(config.logging); this._lockInterval = config.lockInterval; this._guideAssets = config.guideAssets; this._allowOrganizationCreator = config.allowOrganizationCreator; this._useSplash = config.useSplash; this._orcidPath = config.orcidPath; if (config.matomo) { this._matomoEnabled = config.matomo.enabled; this._matomoSiteUrl = config.matomo.url; this._matomoSiteId = config.matomo.siteId; } this._maxFileSizeInMB = config.maxFileSizeInMB; this._userSettingsVersion = config.userSettingsVersion; if (config.notification_service) { this._notificationServiceEnabled = config.notification_service.enabled; this._notificationServiceAddress = config.notification_service.address; } if (config.annotation_service) { this._annotationServiceEnabled = config.annotation_service.enabled; this._annotationServiceAddress = config.annotation_service.address; } this._inAppNotificationsCountInterval = config.inAppNotificationsCountInterval; this._newReleaseNotificationExpires = config.newReleaseNotification?.expires; this._newReleaseNotificationLink = config.newReleaseNotification?.link; this._newReleaseNotificationVersionCode = config.newReleaseNotification?.versionCode; } }