From a4ff3100d36f2fe9a4dfc00e8487c28a94433c4f Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 6 Aug 2020 15:25:19 +0200 Subject: Cleanup tokens logic in embed --- .../recommended-videos.component.ts | 12 +++-- client/src/app/core/auth/auth-user.model.ts | 54 +++++++++------------ client/src/app/core/theme/theme.service.ts | 16 +++---- client/src/app/core/users/user.model.ts | 3 -- client/src/app/core/users/user.service.ts | 55 +++++++++------------- 5 files changed, 62 insertions(+), 78 deletions(-) (limited to 'client/src/app') diff --git a/client/src/app/+videos/+video-watch/recommendations/recommended-videos.component.ts b/client/src/app/+videos/+video-watch/recommendations/recommended-videos.component.ts index 016975341..d2372023f 100644 --- a/client/src/app/+videos/+video-watch/recommendations/recommended-videos.component.ts +++ b/client/src/app/+videos/+video-watch/recommendations/recommended-videos.component.ts @@ -7,6 +7,7 @@ import { VideoPlaylist } from '@app/shared/shared-video-playlist' import { I18n } from '@ngx-translate/i18n-polyfill' import { RecommendationInfo } from './recommendation-info.model' import { RecommendedVideosStore } from './recommended-videos.store' +import { UserLocalStorageKeys } from '@root-helpers/users' @Component({ selector: 'my-recommended-videos', @@ -48,9 +49,12 @@ export class RecommendedVideosComponent implements OnInit, OnChanges { if (this.authService.isLoggedIn()) { this.autoPlayNextVideo = this.authService.getUser().autoPlayNextVideo } else { - this.autoPlayNextVideo = this.sessionStorageService.getItem(User.KEYS.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO) === 'true' || false - this.sessionStorageService.watch([User.KEYS.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO]).subscribe( - () => this.autoPlayNextVideo = this.sessionStorageService.getItem(User.KEYS.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO) === 'true' + this.autoPlayNextVideo = this.sessionStorageService.getItem(UserLocalStorageKeys.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO) === 'true' + + this.sessionStorageService.watch([UserLocalStorageKeys.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO]).subscribe( + () => { + this.autoPlayNextVideo = this.sessionStorageService.getItem(UserLocalStorageKeys.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO) === 'true' + } ) } @@ -73,7 +77,7 @@ export class RecommendedVideosComponent implements OnInit, OnChanges { } switchAutoPlayNextVideo () { - this.sessionStorageService.setItem(User.KEYS.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO, this.autoPlayNextVideo.toString()) + this.sessionStorageService.setItem(UserLocalStorageKeys.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO, this.autoPlayNextVideo.toString()) if (this.authService.isLoggedIn()) { const details = { diff --git a/client/src/app/core/auth/auth-user.model.ts b/client/src/app/core/auth/auth-user.model.ts index 5679d28a9..34efa24fc 100644 --- a/client/src/app/core/auth/auth-user.model.ts +++ b/client/src/app/core/auth/auth-user.model.ts @@ -1,18 +1,22 @@ import { Observable, of } from 'rxjs' import { map } from 'rxjs/operators' import { User } from '@app/core/users/user.model' -import { peertubeLocalStorage } from '@root-helpers/peertube-web-storage' +import { + flushUserInfoFromLocalStorage, + getUserInfoFromLocalStorage, + saveUserInfoIntoLocalStorage, + TokenOptions, + Tokens +} from '@root-helpers/users' import { hasUserRight } from '@shared/core-utils/users' import { MyUser as ServerMyUserModel, MyUserSpecialPlaylist, - NSFWPolicyType, User as ServerUserModel, UserRight, UserRole, UserVideoQuota } from '@shared/models' -import { TokenOptions, Tokens } from '@root-helpers/pure-auth-user.model' export class AuthUser extends User implements ServerMyUserModel { tokens: Tokens @@ -21,31 +25,16 @@ export class AuthUser extends User implements ServerMyUserModel { canSeeVideosLink = true static load () { - const usernameLocalStorage = peertubeLocalStorage.getItem(this.KEYS.USERNAME) - if (usernameLocalStorage) { - return new AuthUser( - { - id: parseInt(peertubeLocalStorage.getItem(this.KEYS.ID), 10), - username: peertubeLocalStorage.getItem(this.KEYS.USERNAME), - email: peertubeLocalStorage.getItem(this.KEYS.EMAIL), - role: parseInt(peertubeLocalStorage.getItem(this.KEYS.ROLE), 10) as UserRole, - nsfwPolicy: peertubeLocalStorage.getItem(this.KEYS.NSFW_POLICY) as NSFWPolicyType, - webTorrentEnabled: peertubeLocalStorage.getItem(this.KEYS.WEBTORRENT_ENABLED) === 'true', - autoPlayVideo: peertubeLocalStorage.getItem(this.KEYS.AUTO_PLAY_VIDEO) === 'true', - videosHistoryEnabled: peertubeLocalStorage.getItem(this.KEYS.VIDEOS_HISTORY_ENABLED) === 'true' - }, - Tokens.load() - ) - } + const userInfo = getUserInfoFromLocalStorage() + + if (!userInfo) return null - return null + return new AuthUser(userInfo, Tokens.load()) } static flush () { - peertubeLocalStorage.removeItem(this.KEYS.USERNAME) - peertubeLocalStorage.removeItem(this.KEYS.ID) - peertubeLocalStorage.removeItem(this.KEYS.ROLE) - peertubeLocalStorage.removeItem(this.KEYS.EMAIL) + flushUserInfoFromLocalStorage() + Tokens.flush() } @@ -87,13 +76,16 @@ export class AuthUser extends User implements ServerMyUserModel { } save () { - peertubeLocalStorage.setItem(AuthUser.KEYS.ID, this.id.toString()) - peertubeLocalStorage.setItem(AuthUser.KEYS.USERNAME, this.username) - peertubeLocalStorage.setItem(AuthUser.KEYS.EMAIL, this.email) - peertubeLocalStorage.setItem(AuthUser.KEYS.ROLE, this.role.toString()) - peertubeLocalStorage.setItem(AuthUser.KEYS.NSFW_POLICY, this.nsfwPolicy.toString()) - peertubeLocalStorage.setItem(AuthUser.KEYS.WEBTORRENT_ENABLED, JSON.stringify(this.webTorrentEnabled)) - peertubeLocalStorage.setItem(AuthUser.KEYS.AUTO_PLAY_VIDEO, JSON.stringify(this.autoPlayVideo)) + saveUserInfoIntoLocalStorage({ + id: this.id, + username: this.username, + email: this.email, + role: this.role, + nsfwPolicy: this.nsfwPolicy, + webTorrentEnabled: this.webTorrentEnabled, + autoPlayVideo: this.autoPlayVideo + }) + this.tokens.save() } diff --git a/client/src/app/core/theme/theme.service.ts b/client/src/app/core/theme/theme.service.ts index 7e05fdddd..4c4611d01 100644 --- a/client/src/app/core/theme/theme.service.ts +++ b/client/src/app/core/theme/theme.service.ts @@ -1,13 +1,13 @@ import { first } from 'rxjs/operators' import { Injectable } from '@angular/core' +import { UserLocalStorageKeys } from '@root-helpers/users' +import { ServerConfig, ServerConfigTheme } from '@shared/models' +import { environment } from '../../../environments/environment' import { AuthService } from '../auth' import { PluginService } from '../plugins/plugin.service' import { ServerService } from '../server' -import { LocalStorageService } from '../wrappers/storage.service' -import { User } from '../users/user.model' import { UserService } from '../users/user.service' -import { ServerConfig, ServerConfigTheme } from '@shared/models' -import { environment } from '../../../environments/environment' +import { LocalStorageService } from '../wrappers/storage.service' @Injectable() export class ThemeService { @@ -111,9 +111,9 @@ export class ThemeService { this.pluginService.reloadLoadedScopes() - this.localStorageService.setItem(User.KEYS.LAST_ACTIVE_THEME, JSON.stringify(theme), false) + this.localStorageService.setItem(UserLocalStorageKeys.LAST_ACTIVE_THEME, JSON.stringify(theme), false) } else { - this.localStorageService.removeItem(User.KEYS.LAST_ACTIVE_THEME, false) + this.localStorageService.removeItem(UserLocalStorageKeys.LAST_ACTIVE_THEME, false) } this.oldThemeName = currentTheme @@ -127,7 +127,7 @@ export class ThemeService { if (!this.auth.isLoggedIn()) { this.updateCurrentTheme() - this.localStorageService.watch([ User.KEYS.THEME ]).subscribe( + this.localStorageService.watch([ UserLocalStorageKeys.THEME ]).subscribe( () => this.updateCurrentTheme() ) } @@ -138,7 +138,7 @@ export class ThemeService { } private loadAndSetFromLocalStorage () { - const lastActiveThemeString = this.localStorageService.getItem(User.KEYS.LAST_ACTIVE_THEME) + const lastActiveThemeString = this.localStorageService.getItem(UserLocalStorageKeys.LAST_ACTIVE_THEME) if (!lastActiveThemeString) return try { diff --git a/client/src/app/core/users/user.model.ts b/client/src/app/core/users/user.model.ts index f0d3a08b8..7c9569ed4 100644 --- a/client/src/app/core/users/user.model.ts +++ b/client/src/app/core/users/user.model.ts @@ -10,11 +10,8 @@ import { UserRole, VideoChannel } from '@shared/models' -import { UserKeys } from '@root-helpers/user-keys' export class User implements UserServerModel { - static KEYS = UserKeys - id: number username: string email: string diff --git a/client/src/app/core/users/user.service.ts b/client/src/app/core/users/user.service.ts index c98b3844c..aac2a0206 100644 --- a/client/src/app/core/users/user.service.ts +++ b/client/src/app/core/users/user.service.ts @@ -1,4 +1,3 @@ -import { has } from 'lodash-es' import { BytesPipe } from 'ngx-pipes' import { SortMeta } from 'primeng/api' import { from, Observable, of } from 'rxjs' @@ -7,6 +6,7 @@ import { HttpClient, HttpParams } from '@angular/common/http' import { Injectable } from '@angular/core' import { AuthService } from '@app/core/auth' import { I18n } from '@ngx-translate/i18n-polyfill' +import { UserLocalStorageKeys } from '@root-helpers/users' import { Avatar, NSFWPolicyType, @@ -81,37 +81,28 @@ export class UserService { } updateMyAnonymousProfile (profile: UserUpdateMe) { - const supportedKeys = { - // local storage keys - nsfwPolicy: (val: NSFWPolicyType) => this.localStorageService.setItem(User.KEYS.NSFW_POLICY, val), - webTorrentEnabled: (val: boolean) => this.localStorageService.setItem(User.KEYS.WEBTORRENT_ENABLED, String(val)), - autoPlayVideo: (val: boolean) => this.localStorageService.setItem(User.KEYS.AUTO_PLAY_VIDEO, String(val)), - autoPlayNextVideoPlaylist: (val: boolean) => this.localStorageService.setItem(User.KEYS.AUTO_PLAY_VIDEO_PLAYLIST, String(val)), - theme: (val: string) => this.localStorageService.setItem(User.KEYS.THEME, val), - videoLanguages: (val: string[]) => this.localStorageService.setItem(User.KEYS.VIDEO_LANGUAGES, JSON.stringify(val)), + try { + this.localStorageService.setItem(UserLocalStorageKeys.NSFW_POLICY, profile.nsfwPolicy) + this.localStorageService.setItem(UserLocalStorageKeys.WEBTORRENT_ENABLED, profile.webTorrentEnabled) - // session storage keys - autoPlayNextVideo: (val: boolean) => - this.sessionStorageService.setItem(User.KEYS.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO, String(val)) - } + this.localStorageService.setItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO, profile.autoPlayNextVideo) + this.localStorageService.setItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO_PLAYLIST, profile.autoPlayNextVideoPlaylist) - for (const key of Object.keys(profile)) { - try { - if (has(supportedKeys, key)) supportedKeys[key](profile[key]) - } catch (err) { - console.error(`Cannot set item ${key} in localStorage. Likely due to a value impossible to stringify.`, err) - } + this.localStorageService.setItem(UserLocalStorageKeys.THEME, profile.theme) + this.localStorageService.setItem(UserLocalStorageKeys.VIDEO_LANGUAGES, profile.videoLanguages) + } catch (err) { + console.error(`Cannot set item in localStorage. Likely due to a value impossible to stringify.`, err) } } listenAnonymousUpdate () { return this.localStorageService.watch([ - User.KEYS.NSFW_POLICY, - User.KEYS.WEBTORRENT_ENABLED, - User.KEYS.AUTO_PLAY_VIDEO, - User.KEYS.AUTO_PLAY_VIDEO_PLAYLIST, - User.KEYS.THEME, - User.KEYS.VIDEO_LANGUAGES + UserLocalStorageKeys.NSFW_POLICY, + UserLocalStorageKeys.WEBTORRENT_ENABLED, + UserLocalStorageKeys.AUTO_PLAY_VIDEO, + UserLocalStorageKeys.AUTO_PLAY_VIDEO_PLAYLIST, + UserLocalStorageKeys.THEME, + UserLocalStorageKeys.VIDEO_LANGUAGES ]).pipe( throttleTime(200), filter(() => this.authService.isLoggedIn() !== true), @@ -269,7 +260,7 @@ export class UserService { let videoLanguages: string[] try { - videoLanguages = JSON.parse(this.localStorageService.getItem(User.KEYS.VIDEO_LANGUAGES)) + videoLanguages = JSON.parse(this.localStorageService.getItem(UserLocalStorageKeys.VIDEO_LANGUAGES)) } catch (err) { videoLanguages = null console.error('Cannot parse desired video languages from localStorage.', err) @@ -277,16 +268,16 @@ export class UserService { return new User({ // local storage keys - nsfwPolicy: this.localStorageService.getItem(User.KEYS.NSFW_POLICY) as NSFWPolicyType, - webTorrentEnabled: this.localStorageService.getItem(User.KEYS.WEBTORRENT_ENABLED) !== 'false', - theme: this.localStorageService.getItem(User.KEYS.THEME) || 'instance-default', + nsfwPolicy: this.localStorageService.getItem(UserLocalStorageKeys.NSFW_POLICY) as NSFWPolicyType, + webTorrentEnabled: this.localStorageService.getItem(UserLocalStorageKeys.WEBTORRENT_ENABLED) !== 'false', + theme: this.localStorageService.getItem(UserLocalStorageKeys.THEME) || 'instance-default', videoLanguages, - autoPlayNextVideoPlaylist: this.localStorageService.getItem(User.KEYS.AUTO_PLAY_VIDEO_PLAYLIST) !== 'false', - autoPlayVideo: this.localStorageService.getItem(User.KEYS.AUTO_PLAY_VIDEO) === 'true', + autoPlayNextVideoPlaylist: this.localStorageService.getItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO_PLAYLIST) !== 'false', + autoPlayVideo: this.localStorageService.getItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO) === 'true', // session storage keys - autoPlayNextVideo: this.sessionStorageService.getItem(User.KEYS.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO) === 'true' + autoPlayNextVideo: this.sessionStorageService.getItem(UserLocalStorageKeys.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO) === 'true' }) } -- cgit v1.2.3