1 import { Observable, of } from 'rxjs'
2 import { catchError, first, map, shareReplay } from 'rxjs/operators'
3 import { HttpClient, HttpParams } from '@angular/common/http'
4 import { Injectable } from '@angular/core'
5 import { AuthService } from '@app/core/auth'
6 import { ActorImage, User as UserServerModel, UserUpdateMe, UserVideoQuota } from '@shared/models'
7 import { environment } from '../../../environments/environment'
8 import { RestExtractor } from '../rest'
9 import { UserLocalStorageService } from './user-local-storage.service'
10 import { User } from './user.model'
13 export class UserService {
14 static BASE_USERS_URL = environment.apiUrl + '/api/v1/users/'
16 private userCache: { [ id: number ]: Observable<UserServerModel> } = {}
17 private signupInThisSession = false
20 private authHttp: HttpClient,
21 private authService: AuthService,
22 private restExtractor: RestExtractor,
23 private userLocalStorageService: UserLocalStorageService
26 // ---------------------------------------------------------------------------
28 getUserWithCache (userId: number) {
29 if (!this.userCache[userId]) {
30 this.userCache[userId] = this.getUser(userId).pipe(shareReplay())
33 return this.userCache[userId]
36 getUser (userId: number, withStats = false) {
37 const params = new HttpParams().append('withStats', withStats + '')
39 return this.authHttp.get<UserServerModel>(UserService.BASE_USERS_URL + userId, { params })
40 .pipe(catchError(err => this.restExtractor.handleError(err)))
43 // ---------------------------------------------------------------------------
45 setSignupInThisSession (value: boolean) {
46 this.signupInThisSession = value
49 hasSignupInThisSession () {
50 return this.signupInThisSession
53 // ---------------------------------------------------------------------------
55 updateMyAnonymousProfile (profile: UserUpdateMe) {
56 this.userLocalStorageService.setUserInfo(profile)
59 listenAnonymousUpdate () {
60 return this.userLocalStorageService.listenUserInfoChange()
61 .pipe(map(() => this.getAnonymousUser()))
65 return new User(this.userLocalStorageService.getUserInfo())
68 getAnonymousOrLoggedUser () {
69 if (!this.authService.isLoggedIn()) {
70 return of(this.getAnonymousUser())
73 return this.authService.userInformationLoaded
76 map(() => this.authService.getUser())
80 // ---------------------------------------------------------------------------
82 changePassword (currentPassword: string, newPassword: string) {
83 const url = UserService.BASE_USERS_URL + 'me'
84 const body: UserUpdateMe = {
89 return this.authHttp.put(url, body)
90 .pipe(catchError(err => this.restExtractor.handleError(err)))
93 changeEmail (password: string, newEmail: string) {
94 const url = UserService.BASE_USERS_URL + 'me'
95 const body: UserUpdateMe = {
96 currentPassword: password,
100 return this.authHttp.put(url, body)
101 .pipe(catchError(err => this.restExtractor.handleError(err)))
104 updateMyProfile (profile: UserUpdateMe) {
105 const url = UserService.BASE_USERS_URL + 'me'
107 return this.authHttp.put(url, profile)
108 .pipe(catchError(err => this.restExtractor.handleError(err)))
112 const url = UserService.BASE_USERS_URL + 'me'
114 return this.authHttp.delete(url)
115 .pipe(catchError(err => this.restExtractor.handleError(err)))
118 changeAvatar (avatarForm: FormData) {
119 const url = UserService.BASE_USERS_URL + 'me/avatar/pick'
121 return this.authHttp.post<{ avatars: ActorImage[] }>(url, avatarForm)
122 .pipe(catchError(err => this.restExtractor.handleError(err)))
126 const url = UserService.BASE_USERS_URL + 'me/avatar'
128 return this.authHttp.delete(url)
129 .pipe(catchError(err => this.restExtractor.handleError(err)))
132 getMyVideoQuotaUsed () {
133 const url = UserService.BASE_USERS_URL + 'me/video-quota-used'
135 return this.authHttp.get<UserVideoQuota>(url)
136 .pipe(catchError(err => this.restExtractor.handleError(err)))
139 askResetPassword (email: string) {
140 const url = UserService.BASE_USERS_URL + '/ask-reset-password'
142 return this.authHttp.post(url, { email })
143 .pipe(catchError(err => this.restExtractor.handleError(err)))
146 resetPassword (userId: number, verificationString: string, password: string) {
147 const url = `${UserService.BASE_USERS_URL}/${userId}/reset-password`
153 return this.authHttp.post(url, body)
154 .pipe(catchError(res => this.restExtractor.handleError(res)))
157 autocomplete (search: string): Observable<string[]> {
158 const url = UserService.BASE_USERS_URL + 'autocomplete'
159 const params = new HttpParams().append('search', search)
162 .get<string[]>(url, { params })
163 .pipe(catchError(res => this.restExtractor.handleError(res)))