From ca3112dcaff80efb41124fe0f64c796c3acb1c68 Mon Sep 17 00:00:00 2001 From: Aman Harwara Date: Thu, 17 Feb 2022 01:13:05 +0530 Subject: [PATCH] fix: check if user is entitled to theme before activating it (#867) --- .../javascripts/services/themeManager.ts | 71 +++++++++++++------ 1 file changed, 51 insertions(+), 20 deletions(-) diff --git a/app/assets/javascripts/services/themeManager.ts b/app/assets/javascripts/services/themeManager.ts index 3b167392d..2dbf103bc 100644 --- a/app/assets/javascripts/services/themeManager.ts +++ b/app/assets/javascripts/services/themeManager.ts @@ -69,26 +69,51 @@ export class ThemeManager extends ApplicationService { async onAppEvent(event: ApplicationEvent) { super.onAppEvent(event); - if (event === ApplicationEvent.SignedOut) { - this.deactivateAllThemes(); - this.activeThemes = []; - this.application?.removeValue( - CACHED_THEMES_KEY, - StorageValueModes.Nonwrapped - ); - } else if (event === ApplicationEvent.StorageReady) { - await this.activateCachedThemes(); - } else if (event === ApplicationEvent.FeaturesUpdated) { - this.reloadThemeStatus(); - } else if (event === ApplicationEvent.Launched) { - window - .matchMedia('(prefers-color-scheme: dark)') - .addEventListener('change', this.colorSchemeEventHandler); - } else if (event === ApplicationEvent.PreferencesChanged) { - const prefersDarkColorScheme = window.matchMedia( - '(prefers-color-scheme: dark)' - ); - this.setThemeAsPerColorScheme(prefersDarkColorScheme.matches); + switch (event) { + case ApplicationEvent.SignedOut: { + this.deactivateAllThemes(); + this.activeThemes = []; + this.application?.removeValue( + CACHED_THEMES_KEY, + StorageValueModes.Nonwrapped + ); + break; + } + case ApplicationEvent.StorageReady: { + await this.activateCachedThemes(); + break; + } + case ApplicationEvent.FeaturesUpdated: { + this.reloadThemeStatus(); + break; + } + case ApplicationEvent.Launched: { + window + .matchMedia('(prefers-color-scheme: dark)') + .addEventListener('change', this.colorSchemeEventHandler); + break; + } + case ApplicationEvent.PreferencesChanged: { + const prefersDarkColorScheme = window.matchMedia( + '(prefers-color-scheme: dark)' + ); + this.setThemeAsPerColorScheme(prefersDarkColorScheme.matches); + break; + } + case ApplicationEvent.LocalDataLoaded: { + const themes = this.application.getDisplayableItems( + ContentType.Theme + ) as SNTheme[]; + themes.forEach((theme) => { + if ( + theme.active && + this.application.getFeatureStatus(theme.identifier) !== + FeatureStatus.Entitled + ) { + this.application.toggleTheme(theme); + } + }); + } } } @@ -181,6 +206,12 @@ export class ThemeManager extends ApplicationService { if (this.activeThemes.find((uuid) => uuid === theme.uuid)) { return; } + if ( + this.application.getFeatureStatus(theme.identifier) !== + FeatureStatus.Entitled + ) { + return; + } this.activeThemes.push(theme.uuid); const url = this.application.componentManager.urlForComponent(theme); if (!url) {