Merge branch 'master' into develop
[oweals/peertube.git] / client / src / app / core / auth / auth-user.model.ts
index 81bff99a040bdee257799ac007d09797352b6a47..abb11fdc271aa1c38709f9a704544fd5abad9b44 100644 (file)
@@ -1,6 +1,10 @@
+import { peertubeLocalStorage } from '@app/shared/misc/peertube-local-storage'
+import { UserRight } from '../../../../../shared/models/users/user-right.enum'
+import { User as ServerUserModel } from '../../../../../shared/models/users/user.model'
 // Do not use the barrel (dependency loop)
-import { UserRole } from '../../../../../shared/models/users/user-role.type'
-import { User, UserConstructorHash } from '../../shared/users/user.model'
+import { hasUserRight, UserRole } from '../../../../../shared/models/users/user-role'
+import { User } from '../../shared/users/user.model'
+import { NSFWPolicyType } from '../../../../../shared/models/videos/nsfw-policy.type'
 
 export type TokenOptions = {
   accessToken: string
@@ -21,9 +25,9 @@ class Tokens {
   tokenType: string
 
   static load () {
-    const accessTokenLocalStorage = localStorage.getItem(this.KEYS.ACCESS_TOKEN)
-    const refreshTokenLocalStorage = localStorage.getItem(this.KEYS.REFRESH_TOKEN)
-    const tokenTypeLocalStorage = localStorage.getItem(this.KEYS.TOKEN_TYPE)
+    const accessTokenLocalStorage = peertubeLocalStorage.getItem(this.KEYS.ACCESS_TOKEN)
+    const refreshTokenLocalStorage = peertubeLocalStorage.getItem(this.KEYS.REFRESH_TOKEN)
+    const tokenTypeLocalStorage = peertubeLocalStorage.getItem(this.KEYS.TOKEN_TYPE)
 
     if (accessTokenLocalStorage && refreshTokenLocalStorage && tokenTypeLocalStorage) {
       return new Tokens({
@@ -37,9 +41,9 @@ class Tokens {
   }
 
   static flush () {
-    localStorage.removeItem(this.KEYS.ACCESS_TOKEN)
-    localStorage.removeItem(this.KEYS.REFRESH_TOKEN)
-    localStorage.removeItem(this.KEYS.TOKEN_TYPE)
+    peertubeLocalStorage.removeItem(this.KEYS.ACCESS_TOKEN)
+    peertubeLocalStorage.removeItem(this.KEYS.REFRESH_TOKEN)
+    peertubeLocalStorage.removeItem(this.KEYS.TOKEN_TYPE)
   }
 
   constructor (hash?: TokenOptions) {
@@ -56,9 +60,9 @@ class Tokens {
   }
 
   save () {
-    localStorage.setItem(Tokens.KEYS.ACCESS_TOKEN, this.accessToken)
-    localStorage.setItem(Tokens.KEYS.REFRESH_TOKEN, this.refreshToken)
-    localStorage.setItem(Tokens.KEYS.TOKEN_TYPE, this.tokenType)
+    peertubeLocalStorage.setItem(Tokens.KEYS.ACCESS_TOKEN, this.accessToken)
+    peertubeLocalStorage.setItem(Tokens.KEYS.REFRESH_TOKEN, this.refreshToken)
+    peertubeLocalStorage.setItem(Tokens.KEYS.TOKEN_TYPE, this.tokenType)
   }
 }
 
@@ -67,22 +71,28 @@ export class AuthUser extends User {
     ID: 'id',
     ROLE: 'role',
     EMAIL: 'email',
+    VIDEOS_HISTORY_ENABLED: 'videos-history-enabled',
     USERNAME: 'username',
-    DISPLAY_NSFW: 'display_nsfw'
+    NSFW_POLICY: 'nsfw_policy',
+    WEBTORRENT_ENABLED: 'peertube-videojs-' + 'webtorrent_enabled',
+    AUTO_PLAY_VIDEO: 'auto_play_video'
   }
 
   tokens: Tokens
 
   static load () {
-    const usernameLocalStorage = localStorage.getItem(this.KEYS.USERNAME)
+    const usernameLocalStorage = peertubeLocalStorage.getItem(this.KEYS.USERNAME)
     if (usernameLocalStorage) {
       return new AuthUser(
         {
-          id: parseInt(localStorage.getItem(this.KEYS.ID), 10),
-          username: localStorage.getItem(this.KEYS.USERNAME),
-          email: localStorage.getItem(this.KEYS.EMAIL),
-          role: localStorage.getItem(this.KEYS.ROLE) as UserRole,
-          displayNSFW: localStorage.getItem(this.KEYS.DISPLAY_NSFW) === 'true'
+          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()
       )
@@ -92,15 +102,18 @@ export class AuthUser extends User {
   }
 
   static flush () {
-    localStorage.removeItem(this.KEYS.USERNAME)
-    localStorage.removeItem(this.KEYS.ID)
-    localStorage.removeItem(this.KEYS.ROLE)
-    localStorage.removeItem(this.KEYS.DISPLAY_NSFW)
-    localStorage.removeItem(this.KEYS.EMAIL)
+    peertubeLocalStorage.removeItem(this.KEYS.USERNAME)
+    peertubeLocalStorage.removeItem(this.KEYS.ID)
+    peertubeLocalStorage.removeItem(this.KEYS.ROLE)
+    peertubeLocalStorage.removeItem(this.KEYS.NSFW_POLICY)
+    peertubeLocalStorage.removeItem(this.KEYS.WEBTORRENT_ENABLED)
+    peertubeLocalStorage.removeItem(this.KEYS.VIDEOS_HISTORY_ENABLED)
+    peertubeLocalStorage.removeItem(this.KEYS.AUTO_PLAY_VIDEO)
+    peertubeLocalStorage.removeItem(this.KEYS.EMAIL)
     Tokens.flush()
   }
 
-  constructor (userHash: UserConstructorHash, hashTokens: TokenOptions) {
+  constructor (userHash: Partial<ServerUserModel>, hashTokens: TokenOptions) {
     super(userHash)
     this.tokens = new Tokens(hashTokens)
   }
@@ -122,12 +135,18 @@ export class AuthUser extends User {
     this.tokens.refreshToken = refreshToken
   }
 
+  hasRight (right: UserRight) {
+    return hasUserRight(this.role, right)
+  }
+
   save () {
-    localStorage.setItem(AuthUser.KEYS.ID, this.id.toString())
-    localStorage.setItem(AuthUser.KEYS.USERNAME, this.username)
-    localStorage.setItem(AuthUser.KEYS.EMAIL, this.email)
-    localStorage.setItem(AuthUser.KEYS.ROLE, this.role)
-    localStorage.setItem(AuthUser.KEYS.DISPLAY_NSFW, JSON.stringify(this.displayNSFW))
+    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))
     this.tokens.save()
   }
 }