]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - client/src/app/core/users/user.service.ts
add blocked filter in users list to filter banned users
[github/Chocobozzz/PeerTube.git] / client / src / app / core / users / user.service.ts
... / ...
CommitLineData
1import { has } from 'lodash-es'
2import { BytesPipe } from 'ngx-pipes'
3import { SortMeta } from 'primeng/api'
4import { from, Observable, of } from 'rxjs'
5import { catchError, concatMap, filter, first, map, shareReplay, throttleTime, toArray } from 'rxjs/operators'
6import { HttpClient, HttpParams } from '@angular/common/http'
7import { Injectable } from '@angular/core'
8import { AuthService } from '@app/core/auth'
9import { I18n } from '@ngx-translate/i18n-polyfill'
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'
22import { environment } from '../../../environments/environment'
23import { RestExtractor, RestPagination, RestService } from '../rest'
24import { LocalStorageService, SessionStorageService } from '../wrappers/storage.service'
25import { User } from './user.model'
26
27@Injectable()
28export class UserService {
29 static BASE_USERS_URL = environment.apiUrl + '/api/v1/users/'
30
31 private bytesPipe = new BytesPipe()
32
33 private userCache: { [ id: number ]: Observable<UserServerModel> } = {}
34
35 constructor (
36 private authHttp: HttpClient,
37 private authService: AuthService,
38 private restExtractor: RestExtractor,
39 private restService: RestService,
40 private localStorageService: LocalStorageService,
41 private sessionStorageService: SessionStorageService,
42 private i18n: I18n
43 ) { }
44
45 changePassword (currentPassword: string, newPassword: string) {
46 const url = UserService.BASE_USERS_URL + 'me'
47 const body: UserUpdateMe = {
48 currentPassword,
49 password: newPassword
50 }
51
52 return this.authHttp.put(url, body)
53 .pipe(
54 map(this.restExtractor.extractDataBool),
55 catchError(err => this.restExtractor.handleError(err))
56 )
57 }
58
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
73 updateMyProfile (profile: UserUpdateMe) {
74 const url = UserService.BASE_USERS_URL + 'me'
75
76 return this.authHttp.put(url, profile)
77 .pipe(
78 map(this.restExtractor.extractDataBool),
79 catchError(err => this.restExtractor.handleError(err))
80 )
81 }
82
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
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
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
132 changeAvatar (avatarForm: FormData) {
133 const url = UserService.BASE_USERS_URL + 'me/avatar/pick'
134
135 return this.authHttp.post<{ avatar: Avatar }>(url, avatarForm)
136 .pipe(catchError(err => this.restExtractor.handleError(err)))
137 }
138
139 signup (userCreate: UserRegister) {
140 return this.authHttp.post(UserService.BASE_USERS_URL + 'register', userCreate)
141 .pipe(
142 map(this.restExtractor.extractDataBool),
143 catchError(err => this.restExtractor.handleError(err))
144 )
145 }
146
147 getMyVideoQuotaUsed () {
148 const url = UserService.BASE_USERS_URL + 'me/video-quota-used'
149
150 return this.authHttp.get<UserVideoQuota>(url)
151 .pipe(catchError(err => this.restExtractor.handleError(err)))
152 }
153
154 askResetPassword (email: string) {
155 const url = UserService.BASE_USERS_URL + '/ask-reset-password'
156
157 return this.authHttp.post(url, { email })
158 .pipe(
159 map(this.restExtractor.extractDataBool),
160 catchError(err => this.restExtractor.handleError(err))
161 )
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)
172 .pipe(
173 map(this.restExtractor.extractDataBool),
174 catchError(res => this.restExtractor.handleError(res))
175 )
176 }
177
178 verifyEmail (userId: number, verificationString: string, isPendingEmail: boolean) {
179 const url = `${UserService.BASE_USERS_URL}/${userId}/verify-email`
180 const body = {
181 verificationString,
182 isPendingEmail
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 }
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 }
210
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
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
245 updateUsers (users: UserServerModel[], userUpdate: UserUpdate) {
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
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
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 })
265 .pipe(catchError(err => this.restExtractor.handleError(err)))
266 }
267
268 getAnonymousUser () {
269 let videoLanguages: string[]
270
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',
282 theme: this.localStorageService.getItem(User.KEYS.THEME) || 'instance-default',
283 videoLanguages,
284
285 autoPlayNextVideoPlaylist: this.localStorageService.getItem(User.KEYS.AUTO_PLAY_VIDEO_PLAYLIST) !== 'false',
286 autoPlayVideo: this.localStorageService.getItem(User.KEYS.AUTO_PLAY_VIDEO) === 'true',
287
288 // session storage keys
289 autoPlayNextVideo: this.sessionStorageService.getItem(User.KEYS.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO) === 'true'
290 })
291 }
292
293 getUsers (parameters: {
294 pagination: RestPagination
295 sort: SortMeta
296 search?: string
297 }): Observable<ResultList<UserServerModel>> {
298 const { pagination, sort, search } = parameters
299
300 let params = new HttpParams()
301 params = this.restService.addRestGetParams(params, pagination, sort)
302
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 }
319
320 return this.authHttp.get<ResultList<UserServerModel>>(UserService.BASE_USERS_URL, { params })
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
328 removeUser (usersArg: UserServerModel | UserServerModel[]) {
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 )
337 }
338
339 banUsers (usersArg: UserServerModel | UserServerModel[], reason?: string) {
340 const body = reason ? { reason } : {}
341 const users = Array.isArray(usersArg) ? usersArg : [ usersArg ]
342
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 )
349 }
350
351 unbanUsers (usersArg: UserServerModel | UserServerModel[]) {
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 )
360 }
361
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
374 private formatUser (user: UserServerModel) {
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 }
396}