X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=client%2Fsrc%2Fapp%2Fcore%2Fusers%2Fuser.service.ts;h=f173c356a8ab2ba8c5f89f9e15d97c5cec085ae6;hb=e8bffe9690307f2686ed5573cae2b86ee5f57789;hp=632361e9bef3bdf929d50c5f398ae64bf55f1546;hpb=6d210220be0875d63461829d83c6e3a59d05cf7a;p=github%2FChocobozzz%2FPeerTube.git diff --git a/client/src/app/core/users/user.service.ts b/client/src/app/core/users/user.service.ts index 632361e9b..f173c356a 100644 --- a/client/src/app/core/users/user.service.ts +++ b/client/src/app/core/users/user.service.ts @@ -1,11 +1,10 @@ import { SortMeta } from 'primeng/api' import { from, Observable, of } from 'rxjs' -import { catchError, concatMap, filter, first, map, shareReplay, tap, 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, @@ -17,10 +16,9 @@ import { UserUpdateMe, UserVideoQuota } from '@shared/models' -import { ServerService } from '../' 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() @@ -33,12 +31,10 @@ export class UserService { constructor ( private authHttp: HttpClient, - private server: ServerService, private authService: AuthService, private restExtractor: RestExtractor, private restService: RestService, - private localStorageService: LocalStorageService, - private sessionStorageService: SessionStorageService + private userLocalStorageService: UserLocalStorageService ) { } hasSignupInThisSession () { @@ -53,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) { @@ -67,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) { @@ -151,16 +105,12 @@ 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)) ) @@ -177,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) { @@ -191,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) { @@ -205,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 { @@ -250,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) { @@ -287,36 +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) - } - - const defaultNSFWPolicy = this.server.getHTMLConfig().instance.defaultNSFWPolicy - - return new User({ - // local storage keys - nsfwPolicy: this.localStorageService.getItem(UserLocalStorageKeys.NSFW_POLICY) || defaultNSFWPolicy, - 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