]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/core/users/user.service.ts
Remove unnecessary function
[github/Chocobozzz/PeerTube.git] / client / src / app / core / users / user.service.ts
index 3de83152c32d992e0763ad681718815dcea1b9cc..f173c356a8ab2ba8c5f89f9e15d97c5cec085ae6 100644 (file)
@@ -1,11 +1,10 @@
 import { SortMeta } from 'primeng/api'
 import { from, Observable, of } from 'rxjs'
-import { catchError, concatMap, filter, first, map, shareReplay, throttleTime, toArray } from 'rxjs/operators'
+import { catchError, concatMap, first, map, shareReplay, tap, toArray } from 'rxjs/operators'
 import { HttpClient, HttpParams } from '@angular/common/http'
 import { Injectable } from '@angular/core'
 import { AuthService } from '@app/core/auth'
 import { getBytes } from '@root-helpers/bytes'
-import { UserLocalStorageKeys } from '@root-helpers/users'
 import {
   ActorImage,
   ResultList,
@@ -19,7 +18,7 @@ import {
 } from '@shared/models'
 import { environment } from '../../../environments/environment'
 import { RestExtractor, RestPagination, RestService } from '../rest'
-import { LocalStorageService, SessionStorageService } from '../wrappers/storage.service'
+import { UserLocalStorageService } from './'
 import { User } from './user.model'
 
 @Injectable()
@@ -28,14 +27,19 @@ export class UserService {
 
   private userCache: { [ id: number ]: Observable<UserServerModel> } = {}
 
+  private signupInThisSession = false
+
   constructor (
     private authHttp: HttpClient,
     private authService: AuthService,
     private restExtractor: RestExtractor,
     private restService: RestService,
-    private localStorageService: LocalStorageService,
-    private sessionStorageService: SessionStorageService
-    ) { }
+    private userLocalStorageService: UserLocalStorageService
+  ) { }
+
+  hasSignupInThisSession () {
+    return this.signupInThisSession
+  }
 
   changePassword (currentPassword: string, newPassword: string) {
     const url = UserService.BASE_USERS_URL + 'me'
@@ -45,10 +49,7 @@ export class UserService {
     }
 
     return this.authHttp.put(url, body)
-               .pipe(
-                 map(this.restExtractor.extractDataBool),
-                 catchError(err => this.restExtractor.handleError(err))
-               )
+               .pipe(catchError(err => this.restExtractor.handleError(err)))
   }
 
   changeEmail (password: string, newEmail: string) {
@@ -59,77 +60,38 @@ export class UserService {
     }
 
     return this.authHttp.put(url, body)
-               .pipe(
-                 map(this.restExtractor.extractDataBool),
-                 catchError(err => this.restExtractor.handleError(err))
-               )
+               .pipe(catchError(err => this.restExtractor.handleError(err)))
   }
 
-  updateMyProfile (profile: UserUpdateMe) {
-    const url = UserService.BASE_USERS_URL + 'me'
-
-    return this.authHttp.put(url, profile)
-               .pipe(
-                 map(this.restExtractor.extractDataBool),
-                 catchError(err => this.restExtractor.handleError(err))
-               )
-  }
+  // ---------------------------------------------------------------------------
 
   updateMyAnonymousProfile (profile: UserUpdateMe) {
-    const localStorageKeys: { [ id in keyof UserUpdateMe ]: string } = {
-      nsfwPolicy: UserLocalStorageKeys.NSFW_POLICY,
-      webTorrentEnabled: UserLocalStorageKeys.WEBTORRENT_ENABLED,
-      autoPlayNextVideo: UserLocalStorageKeys.AUTO_PLAY_VIDEO,
-      autoPlayNextVideoPlaylist: UserLocalStorageKeys.AUTO_PLAY_VIDEO_PLAYLIST,
-      theme: UserLocalStorageKeys.THEME,
-      videoLanguages: UserLocalStorageKeys.VIDEO_LANGUAGES
-    }
+    this.userLocalStorageService.setUserInfo(profile)
+  }
 
-    const obj = Object.keys(localStorageKeys)
-      .filter(key => key in profile)
-      .map(key => ([ localStorageKeys[key], profile[key] ]))
+  listenAnonymousUpdate () {
+    return this.userLocalStorageService.listenUserInfoChange()
+                                       .pipe(map(() => this.getAnonymousUser()))
+  }
 
-    for (const [ key, value ] of obj) {
-      try {
-        if (value === undefined) {
-          this.localStorageService.removeItem(key)
-          continue
-        }
+  getAnonymousUser () {
+    return new User(this.userLocalStorageService.getUserInfo())
+  }
 
-        const localStorageValue = typeof value === 'string'
-          ? value
-          : JSON.stringify(value)
+  // ---------------------------------------------------------------------------
 
-        this.localStorageService.setItem(key, localStorageValue)
-      } catch (err) {
-        console.error(`Cannot set ${key}->${value} in localStorage. Likely due to a value impossible to stringify.`, err)
-      }
-    }
-  }
+  updateMyProfile (profile: UserUpdateMe) {
+    const url = UserService.BASE_USERS_URL + 'me'
 
-  listenAnonymousUpdate () {
-    return this.localStorageService.watch([
-      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),
-      map(() => this.getAnonymousUser())
-    )
+    return this.authHttp.put(url, profile)
+               .pipe(catchError(err => this.restExtractor.handleError(err)))
   }
 
   deleteMe () {
     const url = UserService.BASE_USERS_URL + 'me'
 
     return this.authHttp.delete(url)
-               .pipe(
-                 map(this.restExtractor.extractDataBool),
-                 catchError(err => this.restExtractor.handleError(err))
-               )
+               .pipe(catchError(err => this.restExtractor.handleError(err)))
   }
 
   changeAvatar (avatarForm: FormData) {
@@ -143,16 +105,13 @@ export class UserService {
     const url = UserService.BASE_USERS_URL + 'me/avatar'
 
     return this.authHttp.delete(url)
-               .pipe(
-                 map(this.restExtractor.extractDataBool),
-                 catchError(err => this.restExtractor.handleError(err))
-               )
+               .pipe(catchError(err => this.restExtractor.handleError(err)))
   }
 
   signup (userCreate: UserRegister) {
     return this.authHttp.post(UserService.BASE_USERS_URL + 'register', userCreate)
                .pipe(
-                 map(this.restExtractor.extractDataBool),
+                 tap(() => this.signupInThisSession = true),
                  catchError(err => this.restExtractor.handleError(err))
                )
   }
@@ -168,10 +127,7 @@ export class UserService {
     const url = UserService.BASE_USERS_URL + '/ask-reset-password'
 
     return this.authHttp.post(url, { email })
-               .pipe(
-                 map(this.restExtractor.extractDataBool),
-                 catchError(err => this.restExtractor.handleError(err))
-               )
+               .pipe(catchError(err => this.restExtractor.handleError(err)))
   }
 
   resetPassword (userId: number, verificationString: string, password: string) {
@@ -182,10 +138,7 @@ export class UserService {
     }
 
     return this.authHttp.post(url, body)
-               .pipe(
-                 map(this.restExtractor.extractDataBool),
-                 catchError(res => this.restExtractor.handleError(res))
-               )
+               .pipe(catchError(res => this.restExtractor.handleError(res)))
   }
 
   verifyEmail (userId: number, verificationString: string, isPendingEmail: boolean) {
@@ -196,20 +149,14 @@ export class UserService {
     }
 
     return this.authHttp.post(url, body)
-               .pipe(
-                 map(this.restExtractor.extractDataBool),
-                 catchError(res => this.restExtractor.handleError(res))
-               )
+               .pipe(catchError(res => this.restExtractor.handleError(res)))
   }
 
   askSendVerifyEmail (email: string) {
     const url = UserService.BASE_USERS_URL + '/ask-send-verify-email'
 
     return this.authHttp.post(url, { email })
-               .pipe(
-                 map(this.restExtractor.extractDataBool),
-                 catchError(err => this.restExtractor.handleError(err))
-               )
+               .pipe(catchError(err => this.restExtractor.handleError(err)))
   }
 
   autocomplete (search: string): Observable<string[]> {
@@ -241,18 +188,12 @@ export class UserService {
 
   addUser (userCreate: UserCreate) {
     return this.authHttp.post(UserService.BASE_USERS_URL, userCreate)
-               .pipe(
-                 map(this.restExtractor.extractDataBool),
-                 catchError(err => this.restExtractor.handleError(err))
-               )
+               .pipe(catchError(err => this.restExtractor.handleError(err)))
   }
 
   updateUser (userId: number, userUpdate: UserUpdate) {
     return this.authHttp.put(UserService.BASE_USERS_URL + userId, userUpdate)
-               .pipe(
-                 map(this.restExtractor.extractDataBool),
-                 catchError(err => this.restExtractor.handleError(err))
-               )
+               .pipe(catchError(err => this.restExtractor.handleError(err)))
   }
 
   updateUsers (users: UserServerModel[], userUpdate: UserUpdate) {
@@ -266,7 +207,7 @@ export class UserService {
 
   getUserWithCache (userId: number) {
     if (!this.userCache[userId]) {
-      this.userCache[ userId ] = this.getUser(userId).pipe(shareReplay())
+      this.userCache[userId] = this.getUser(userId).pipe(shareReplay())
     }
 
     return this.userCache[userId]
@@ -278,34 +219,6 @@ export class UserService {
                .pipe(catchError(err => this.restExtractor.handleError(err)))
   }
 
-  getAnonymousUser () {
-    let videoLanguages: string[]
-
-    try {
-      const languagesString = this.localStorageService.getItem(UserLocalStorageKeys.VIDEO_LANGUAGES)
-      videoLanguages = languagesString && languagesString !== 'undefined'
-        ? JSON.parse(languagesString)
-        : null
-    } catch (err) {
-      videoLanguages = null
-      console.error('Cannot parse desired video languages from localStorage.', err)
-    }
-
-    return new User({
-      // local storage keys
-      nsfwPolicy: this.localStorageService.getItem(UserLocalStorageKeys.NSFW_POLICY),
-      webTorrentEnabled: this.localStorageService.getItem(UserLocalStorageKeys.WEBTORRENT_ENABLED) !== 'false',
-      theme: this.localStorageService.getItem(UserLocalStorageKeys.THEME) || 'instance-default',
-      videoLanguages,
-
-      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(UserLocalStorageKeys.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO) === 'true'
-    })
-  }
-
   getUsers (parameters: {
     pagination: RestPagination
     sort: SortMeta
@@ -320,13 +233,7 @@ export class UserService {
       const filters = this.restService.parseQueryStringFilter(search, {
         blocked: {
           prefix: 'banned:',
-          isBoolean: true,
-          handler: v => {
-            if (v === 'true') return v
-            if (v === 'false') return v
-
-            return undefined
-          }
+          isBoolean: true
         }
       })