]>
Commit | Line | Data |
---|---|---|
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' | |
11 | ||
12 | @Injectable() | |
13 | export class UserService { | |
14 | static BASE_USERS_URL = environment.apiUrl + '/api/v1/users/' | |
15 | ||
16 | private userCache: { [ id: number ]: Observable<UserServerModel> } = {} | |
17 | private signupInThisSession = false | |
18 | ||
19 | constructor ( | |
20 | private authHttp: HttpClient, | |
21 | private authService: AuthService, | |
22 | private restExtractor: RestExtractor, | |
23 | private userLocalStorageService: UserLocalStorageService | |
24 | ) { } | |
25 | ||
26 | // --------------------------------------------------------------------------- | |
27 | ||
28 | getUserWithCache (userId: number) { | |
29 | if (!this.userCache[userId]) { | |
30 | this.userCache[userId] = this.getUser(userId).pipe(shareReplay()) | |
31 | } | |
32 | ||
33 | return this.userCache[userId] | |
34 | } | |
35 | ||
36 | getUser (userId: number, withStats = false) { | |
37 | const params = new HttpParams().append('withStats', withStats + '') | |
38 | ||
39 | return this.authHttp.get<UserServerModel>(UserService.BASE_USERS_URL + userId, { params }) | |
40 | .pipe(catchError(err => this.restExtractor.handleError(err))) | |
41 | } | |
42 | ||
43 | // --------------------------------------------------------------------------- | |
44 | ||
45 | setSignupInThisSession (value: boolean) { | |
46 | this.signupInThisSession = value | |
47 | } | |
48 | ||
49 | hasSignupInThisSession () { | |
50 | return this.signupInThisSession | |
51 | } | |
52 | ||
53 | // --------------------------------------------------------------------------- | |
54 | ||
55 | updateMyAnonymousProfile (profile: UserUpdateMe) { | |
56 | this.userLocalStorageService.setUserInfo(profile) | |
57 | } | |
58 | ||
59 | listenAnonymousUpdate () { | |
60 | return this.userLocalStorageService.listenUserInfoChange() | |
61 | .pipe(map(() => this.getAnonymousUser())) | |
62 | } | |
63 | ||
64 | getAnonymousUser () { | |
65 | return new User(this.userLocalStorageService.getUserInfo()) | |
66 | } | |
67 | ||
68 | getAnonymousOrLoggedUser () { | |
69 | if (!this.authService.isLoggedIn()) { | |
70 | return of(this.getAnonymousUser()) | |
71 | } | |
72 | ||
73 | return this.authService.userInformationLoaded | |
74 | .pipe( | |
75 | first(), | |
76 | map(() => this.authService.getUser()) | |
77 | ) | |
78 | } | |
79 | ||
80 | // --------------------------------------------------------------------------- | |
81 | ||
82 | changePassword (currentPassword: string, newPassword: string) { | |
83 | const url = UserService.BASE_USERS_URL + 'me' | |
84 | const body: UserUpdateMe = { | |
85 | currentPassword, | |
86 | password: newPassword | |
87 | } | |
88 | ||
89 | return this.authHttp.put(url, body) | |
90 | .pipe(catchError(err => this.restExtractor.handleError(err))) | |
91 | } | |
92 | ||
93 | changeEmail (password: string, newEmail: string) { | |
94 | const url = UserService.BASE_USERS_URL + 'me' | |
95 | const body: UserUpdateMe = { | |
96 | currentPassword: password, | |
97 | email: newEmail | |
98 | } | |
99 | ||
100 | return this.authHttp.put(url, body) | |
101 | .pipe(catchError(err => this.restExtractor.handleError(err))) | |
102 | } | |
103 | ||
104 | updateMyProfile (profile: UserUpdateMe) { | |
105 | const url = UserService.BASE_USERS_URL + 'me' | |
106 | ||
107 | return this.authHttp.put(url, profile) | |
108 | .pipe(catchError(err => this.restExtractor.handleError(err))) | |
109 | } | |
110 | ||
111 | deleteMe () { | |
112 | const url = UserService.BASE_USERS_URL + 'me' | |
113 | ||
114 | return this.authHttp.delete(url) | |
115 | .pipe(catchError(err => this.restExtractor.handleError(err))) | |
116 | } | |
117 | ||
118 | changeAvatar (avatarForm: FormData) { | |
119 | const url = UserService.BASE_USERS_URL + 'me/avatar/pick' | |
120 | ||
121 | return this.authHttp.post<{ avatar: ActorImage }>(url, avatarForm) | |
122 | .pipe(catchError(err => this.restExtractor.handleError(err))) | |
123 | } | |
124 | ||
125 | deleteAvatar () { | |
126 | const url = UserService.BASE_USERS_URL + 'me/avatar' | |
127 | ||
128 | return this.authHttp.delete(url) | |
129 | .pipe(catchError(err => this.restExtractor.handleError(err))) | |
130 | } | |
131 | ||
132 | getMyVideoQuotaUsed () { | |
133 | const url = UserService.BASE_USERS_URL + 'me/video-quota-used' | |
134 | ||
135 | return this.authHttp.get<UserVideoQuota>(url) | |
136 | .pipe(catchError(err => this.restExtractor.handleError(err))) | |
137 | } | |
138 | ||
139 | askResetPassword (email: string) { | |
140 | const url = UserService.BASE_USERS_URL + '/ask-reset-password' | |
141 | ||
142 | return this.authHttp.post(url, { email }) | |
143 | .pipe(catchError(err => this.restExtractor.handleError(err))) | |
144 | } | |
145 | ||
146 | resetPassword (userId: number, verificationString: string, password: string) { | |
147 | const url = `${UserService.BASE_USERS_URL}/${userId}/reset-password` | |
148 | const body = { | |
149 | verificationString, | |
150 | password | |
151 | } | |
152 | ||
153 | return this.authHttp.post(url, body) | |
154 | .pipe(catchError(res => this.restExtractor.handleError(res))) | |
155 | } | |
156 | ||
157 | autocomplete (search: string): Observable<string[]> { | |
158 | const url = UserService.BASE_USERS_URL + 'autocomplete' | |
159 | const params = new HttpParams().append('search', search) | |
160 | ||
161 | return this.authHttp | |
162 | .get<string[]>(url, { params }) | |
163 | .pipe(catchError(res => this.restExtractor.handleError(res))) | |
164 | } | |
165 | } |