]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/core/users/user.service.ts
allow sorting notifications
[github/Chocobozzz/PeerTube.git] / client / src / app / core / users / user.service.ts
CommitLineData
5c20a455
C
1import { has } from 'lodash-es'
2import { BytesPipe } from 'ngx-pipes'
3import { SortMeta } from 'primeng/api'
4import { from, Observable, of } from 'rxjs'
67ed6552 5import { catchError, concatMap, filter, first, map, shareReplay, throttleTime, toArray } from 'rxjs/operators'
74d63469 6import { HttpClient, HttpParams } from '@angular/common/http'
63c4db6d 7import { Injectable } from '@angular/core'
5c20a455 8import { AuthService } from '@app/core/auth'
e724fa93 9import { I18n } from '@ngx-translate/i18n-polyfill'
67ed6552
C
10import {
11 Avatar,
12 NSFWPolicyType,
13 ResultList,
14 User as UserServerModel,
15 UserCreate,
16 UserRegister,
17 UserRole,
18 UserUpdate,
19 UserUpdateMe,
20 UserVideoQuota
21} from '@shared/models'
5c20a455 22import { environment } from '../../../environments/environment'
5c20a455 23import { RestExtractor, RestPagination, RestService } from '../rest'
67ed6552 24import { LocalStorageService, SessionStorageService } from '../wrappers/storage.service'
5c20a455 25import { User } from './user.model'
629d8d6f
C
26
27@Injectable()
e2a2d6c8 28export class UserService {
63c4db6d 29 static BASE_USERS_URL = environment.apiUrl + '/api/v1/users/'
629d8d6f 30
e724fa93
C
31 private bytesPipe = new BytesPipe()
32
d3217560 33 private userCache: { [ id: number ]: Observable<UserServerModel> } = {}
218b0874 34
df98563e 35 constructor (
d592e0a9 36 private authHttp: HttpClient,
5c20a455 37 private authService: AuthService,
e724fa93
C
38 private restExtractor: RestExtractor,
39 private restService: RestService,
d3217560
RK
40 private localStorageService: LocalStorageService,
41 private sessionStorageService: SessionStorageService,
e724fa93
C
42 private i18n: I18n
43 ) { }
629d8d6f 44
a890d1e0 45 changePassword (currentPassword: string, newPassword: string) {
8094a898
C
46 const url = UserService.BASE_USERS_URL + 'me'
47 const body: UserUpdateMe = {
a890d1e0 48 currentPassword,
629d8d6f 49 password: newPassword
df98563e 50 }
629d8d6f 51
de59c48f 52 return this.authHttp.put(url, body)
db400f44
C
53 .pipe(
54 map(this.restExtractor.extractDataBool),
e4f0e92e 55 catchError(err => this.restExtractor.handleError(err))
db400f44 56 )
629d8d6f 57 }
af5e743b 58
0ba5f5ba
C
59 changeEmail (password: string, newEmail: string) {
60 const url = UserService.BASE_USERS_URL + 'me'
61 const body: UserUpdateMe = {
62 currentPassword: password,
63 email: newEmail
64 }
65
66 return this.authHttp.put(url, body)
67 .pipe(
68 map(this.restExtractor.extractDataBool),
69 catchError(err => this.restExtractor.handleError(err))
70 )
71 }
72
ed56ad11 73 updateMyProfile (profile: UserUpdateMe) {
8094a898 74 const url = UserService.BASE_USERS_URL + 'me'
af5e743b 75
ed56ad11 76 return this.authHttp.put(url, profile)
db400f44
C
77 .pipe(
78 map(this.restExtractor.extractDataBool),
e4f0e92e 79 catchError(err => this.restExtractor.handleError(err))
db400f44 80 )
af5e743b 81 }
a184c71b 82
d3217560
RK
83 updateMyAnonymousProfile (profile: UserUpdateMe) {
84 const supportedKeys = {
85 // local storage keys
86 nsfwPolicy: (val: NSFWPolicyType) => this.localStorageService.setItem(User.KEYS.NSFW_POLICY, val),
87 webTorrentEnabled: (val: boolean) => this.localStorageService.setItem(User.KEYS.WEBTORRENT_ENABLED, String(val)),
88 autoPlayVideo: (val: boolean) => this.localStorageService.setItem(User.KEYS.AUTO_PLAY_VIDEO, String(val)),
89 autoPlayNextVideoPlaylist: (val: boolean) => this.localStorageService.setItem(User.KEYS.AUTO_PLAY_VIDEO_PLAYLIST, String(val)),
90 theme: (val: string) => this.localStorageService.setItem(User.KEYS.THEME, val),
91 videoLanguages: (val: string[]) => this.localStorageService.setItem(User.KEYS.VIDEO_LANGUAGES, JSON.stringify(val)),
92
93 // session storage keys
94 autoPlayNextVideo: (val: boolean) =>
95 this.sessionStorageService.setItem(User.KEYS.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO, String(val))
96 }
97
98 for (const key of Object.keys(profile)) {
99 try {
100 if (has(supportedKeys, key)) supportedKeys[key](profile[key])
101 } catch (err) {
102 console.error(`Cannot set item ${key} in localStorage. Likely due to a value impossible to stringify.`, err)
103 }
104 }
105 }
106
5c20a455
C
107 listenAnonymousUpdate () {
108 return this.localStorageService.watch([
109 User.KEYS.NSFW_POLICY,
110 User.KEYS.WEBTORRENT_ENABLED,
111 User.KEYS.AUTO_PLAY_VIDEO,
112 User.KEYS.AUTO_PLAY_VIDEO_PLAYLIST,
113 User.KEYS.THEME,
114 User.KEYS.VIDEO_LANGUAGES
115 ]).pipe(
116 throttleTime(200),
117 filter(() => this.authService.isLoggedIn() !== true),
118 map(() => this.getAnonymousUser())
119 )
120 }
121
92b9d60c
C
122 deleteMe () {
123 const url = UserService.BASE_USERS_URL + 'me'
124
125 return this.authHttp.delete(url)
126 .pipe(
127 map(this.restExtractor.extractDataBool),
128 catchError(err => this.restExtractor.handleError(err))
129 )
130 }
131
c5911fd3
C
132 changeAvatar (avatarForm: FormData) {
133 const url = UserService.BASE_USERS_URL + 'me/avatar/pick'
134
5fcbd898 135 return this.authHttp.post<{ avatar: Avatar }>(url, avatarForm)
e4f0e92e 136 .pipe(catchError(err => this.restExtractor.handleError(err)))
c5911fd3
C
137 }
138
1d5342ab 139 signup (userCreate: UserRegister) {
d592e0a9 140 return this.authHttp.post(UserService.BASE_USERS_URL + 'register', userCreate)
db400f44
C
141 .pipe(
142 map(this.restExtractor.extractDataBool),
e4f0e92e 143 catchError(err => this.restExtractor.handleError(err))
db400f44 144 )
a184c71b 145 }
c5911fd3 146
ce5496d6 147 getMyVideoQuotaUsed () {
bf64ed41 148 const url = UserService.BASE_USERS_URL + 'me/video-quota-used'
c5911fd3 149
5fcbd898 150 return this.authHttp.get<UserVideoQuota>(url)
e4f0e92e 151 .pipe(catchError(err => this.restExtractor.handleError(err)))
c5911fd3 152 }
ecb4e35f
C
153
154 askResetPassword (email: string) {
155 const url = UserService.BASE_USERS_URL + '/ask-reset-password'
156
157 return this.authHttp.post(url, { email })
db400f44
C
158 .pipe(
159 map(this.restExtractor.extractDataBool),
e4f0e92e 160 catchError(err => this.restExtractor.handleError(err))
db400f44 161 )
ecb4e35f
C
162 }
163
164 resetPassword (userId: number, verificationString: string, password: string) {
165 const url = `${UserService.BASE_USERS_URL}/${userId}/reset-password`
166 const body = {
167 verificationString,
168 password
169 }
170
171 return this.authHttp.post(url, body)
db400f44
C
172 .pipe(
173 map(this.restExtractor.extractDataBool),
174 catchError(res => this.restExtractor.handleError(res))
175 )
ecb4e35f 176 }
d9eaee39 177
0ba5f5ba 178 verifyEmail (userId: number, verificationString: string, isPendingEmail: boolean) {
d9eaee39
JM
179 const url = `${UserService.BASE_USERS_URL}/${userId}/verify-email`
180 const body = {
0ba5f5ba
C
181 verificationString,
182 isPendingEmail
d9eaee39
JM
183 }
184
185 return this.authHttp.post(url, body)
186 .pipe(
187 map(this.restExtractor.extractDataBool),
188 catchError(res => this.restExtractor.handleError(res))
189 )
190 }
191
192 askSendVerifyEmail (email: string) {
193 const url = UserService.BASE_USERS_URL + '/ask-send-verify-email'
194
195 return this.authHttp.post(url, { email })
196 .pipe(
197 map(this.restExtractor.extractDataBool),
198 catchError(err => this.restExtractor.handleError(err))
199 )
200 }
74d63469
GR
201
202 autocomplete (search: string): Observable<string[]> {
203 const url = UserService.BASE_USERS_URL + 'autocomplete'
204 const params = new HttpParams().append('search', search)
205
206 return this.authHttp
207 .get<string[]>(url, { params })
208 .pipe(catchError(res => this.restExtractor.handleError(res)))
209 }
e724fa93 210
1f20622f
C
211 getNewUsername (oldDisplayName: string, newDisplayName: string, currentUsername: string) {
212 // Don't update display name, the user seems to have changed it
213 if (this.displayNameToUsername(oldDisplayName) !== currentUsername) return currentUsername
214
215 return this.displayNameToUsername(newDisplayName)
216 }
217
218 displayNameToUsername (displayName: string) {
219 if (!displayName) return ''
220
221 return displayName
222 .toLowerCase()
223 .replace(/\s/g, '_')
224 .replace(/[^a-z0-9_.]/g, '')
225 }
226
e724fa93
C
227 /* ###### Admin methods ###### */
228
229 addUser (userCreate: UserCreate) {
230 return this.authHttp.post(UserService.BASE_USERS_URL, userCreate)
231 .pipe(
232 map(this.restExtractor.extractDataBool),
233 catchError(err => this.restExtractor.handleError(err))
234 )
235 }
236
237 updateUser (userId: number, userUpdate: UserUpdate) {
238 return this.authHttp.put(UserService.BASE_USERS_URL + userId, userUpdate)
239 .pipe(
240 map(this.restExtractor.extractDataBool),
241 catchError(err => this.restExtractor.handleError(err))
242 )
243 }
244
d3217560 245 updateUsers (users: UserServerModel[], userUpdate: UserUpdate) {
fc2ec87a
JM
246 return from(users)
247 .pipe(
248 concatMap(u => this.authHttp.put(UserService.BASE_USERS_URL + u.id, userUpdate)),
249 toArray(),
250 catchError(err => this.restExtractor.handleError(err))
251 )
252 }
253
218b0874
C
254 getUserWithCache (userId: number) {
255 if (!this.userCache[userId]) {
256 this.userCache[ userId ] = this.getUser(userId).pipe(shareReplay())
257 }
258
259 return this.userCache[userId]
260 }
261
76314386
RK
262 getUser (userId: number, withStats = false) {
263 const params = new HttpParams().append('withStats', withStats + '')
264 return this.authHttp.get<UserServerModel>(UserService.BASE_USERS_URL + userId, { params })
e724fa93
C
265 .pipe(catchError(err => this.restExtractor.handleError(err)))
266 }
267
d3217560 268 getAnonymousUser () {
5c20a455 269 let videoLanguages: string[]
9870329f 270
d3217560
RK
271 try {
272 videoLanguages = JSON.parse(this.localStorageService.getItem(User.KEYS.VIDEO_LANGUAGES))
273 } catch (err) {
274 videoLanguages = null
275 console.error('Cannot parse desired video languages from localStorage.', err)
276 }
277
278 return new User({
279 // local storage keys
280 nsfwPolicy: this.localStorageService.getItem(User.KEYS.NSFW_POLICY) as NSFWPolicyType,
281 webTorrentEnabled: this.localStorageService.getItem(User.KEYS.WEBTORRENT_ENABLED) !== 'false',
3e95b683 282 theme: this.localStorageService.getItem(User.KEYS.THEME) || 'instance-default',
d3217560
RK
283 videoLanguages,
284
9870329f
C
285 autoPlayNextVideoPlaylist: this.localStorageService.getItem(User.KEYS.AUTO_PLAY_VIDEO_PLAYLIST) !== 'false',
286 autoPlayVideo: this.localStorageService.getItem(User.KEYS.AUTO_PLAY_VIDEO) === 'true',
287
d3217560
RK
288 // session storage keys
289 autoPlayNextVideo: this.sessionStorageService.getItem(User.KEYS.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO) === 'true'
290 })
291 }
292
8491293b
RK
293 getUsers (parameters: {
294 pagination: RestPagination
295 sort: SortMeta
296 search?: string
297 }): Observable<ResultList<UserServerModel>> {
298 const { pagination, sort, search } = parameters
299
e724fa93
C
300 let params = new HttpParams()
301 params = this.restService.addRestGetParams(params, pagination, sort)
302
8491293b
RK
303 if (search) {
304 const filters = this.restService.parseQueryStringFilter(search, {
305 blocked: {
306 prefix: 'banned:',
307 isBoolean: true,
308 handler: v => {
309 if (v === 'true') return v
310 if (v === 'false') return v
311
312 return undefined
313 }
314 }
315 })
316
317 params = this.restService.addObjectParams(params, filters)
318 }
24b9417c 319
d3217560 320 return this.authHttp.get<ResultList<UserServerModel>>(UserService.BASE_USERS_URL, { params })
e724fa93
C
321 .pipe(
322 map(res => this.restExtractor.convertResultListDateToHuman(res)),
323 map(res => this.restExtractor.applyToResultListData(res, this.formatUser.bind(this))),
324 catchError(err => this.restExtractor.handleError(err))
325 )
326 }
327
d3217560 328 removeUser (usersArg: UserServerModel | UserServerModel[]) {
791645e6
C
329 const users = Array.isArray(usersArg) ? usersArg : [ usersArg ]
330
331 return from(users)
332 .pipe(
333 concatMap(u => this.authHttp.delete(UserService.BASE_USERS_URL + u.id)),
334 toArray(),
335 catchError(err => this.restExtractor.handleError(err))
336 )
e724fa93
C
337 }
338
d3217560 339 banUsers (usersArg: UserServerModel | UserServerModel[], reason?: string) {
e724fa93 340 const body = reason ? { reason } : {}
791645e6 341 const users = Array.isArray(usersArg) ? usersArg : [ usersArg ]
e724fa93 342
791645e6
C
343 return from(users)
344 .pipe(
345 concatMap(u => this.authHttp.post(UserService.BASE_USERS_URL + u.id + '/block', body)),
346 toArray(),
347 catchError(err => this.restExtractor.handleError(err))
348 )
e724fa93
C
349 }
350
d3217560 351 unbanUsers (usersArg: UserServerModel | UserServerModel[]) {
791645e6
C
352 const users = Array.isArray(usersArg) ? usersArg : [ usersArg ]
353
354 return from(users)
355 .pipe(
356 concatMap(u => this.authHttp.post(UserService.BASE_USERS_URL + u.id + '/unblock', {})),
357 toArray(),
358 catchError(err => this.restExtractor.handleError(err))
359 )
e724fa93
C
360 }
361
5c20a455
C
362 getAnonymousOrLoggedUser () {
363 if (!this.authService.isLoggedIn()) {
364 return of(this.getAnonymousUser())
365 }
366
367 return this.authService.userInformationLoaded
368 .pipe(
369 first(),
370 map(() => this.authService.getUser())
371 )
372 }
373
d3217560 374 private formatUser (user: UserServerModel) {
e724fa93
C
375 let videoQuota
376 if (user.videoQuota === -1) {
377 videoQuota = this.i18n('Unlimited')
378 } else {
379 videoQuota = this.bytesPipe.transform(user.videoQuota, 0)
380 }
381
382 const videoQuotaUsed = this.bytesPipe.transform(user.videoQuotaUsed, 0)
383
384 const roleLabels: { [ id in UserRole ]: string } = {
385 [UserRole.USER]: this.i18n('User'),
386 [UserRole.ADMINISTRATOR]: this.i18n('Administrator'),
387 [UserRole.MODERATOR]: this.i18n('Moderator')
388 }
389
390 return Object.assign(user, {
391 roleLabel: roleLabels[user.role],
392 videoQuota,
393 videoQuotaUsed
394 })
395 }
629d8d6f 396}