]>
Commit | Line | Data |
---|---|---|
1 | import { catchError, map } from 'rxjs/operators' | |
2 | import { HttpClient, HttpParams } from '@angular/common/http' | |
3 | import { Injectable } from '@angular/core' | |
4 | import { BytesPipe } from 'ngx-pipes' | |
5 | import { SortMeta } from 'primeng/components/common/sortmeta' | |
6 | import { Observable } from 'rxjs' | |
7 | import { ResultList, UserCreate, UserUpdate, User, UserRole } from '../../../../../../shared' | |
8 | import { environment } from '../../../../environments/environment' | |
9 | import { RestExtractor, RestPagination, RestService } from '../../../shared' | |
10 | import { I18n } from '@ngx-translate/i18n-polyfill' | |
11 | ||
12 | @Injectable() | |
13 | export class UserService { | |
14 | private static BASE_USERS_URL = environment.apiUrl + '/api/v1/users/' | |
15 | private bytesPipe = new BytesPipe() | |
16 | ||
17 | constructor ( | |
18 | private authHttp: HttpClient, | |
19 | private restService: RestService, | |
20 | private restExtractor: RestExtractor, | |
21 | private i18n: I18n | |
22 | ) { } | |
23 | ||
24 | addUser (userCreate: UserCreate) { | |
25 | return this.authHttp.post(UserService.BASE_USERS_URL, userCreate) | |
26 | .pipe( | |
27 | map(this.restExtractor.extractDataBool), | |
28 | catchError(err => this.restExtractor.handleError(err)) | |
29 | ) | |
30 | } | |
31 | ||
32 | updateUser (userId: number, userUpdate: UserUpdate) { | |
33 | return this.authHttp.put(UserService.BASE_USERS_URL + userId, userUpdate) | |
34 | .pipe( | |
35 | map(this.restExtractor.extractDataBool), | |
36 | catchError(err => this.restExtractor.handleError(err)) | |
37 | ) | |
38 | } | |
39 | ||
40 | getUser (userId: number) { | |
41 | return this.authHttp.get<User>(UserService.BASE_USERS_URL + userId) | |
42 | .pipe(catchError(err => this.restExtractor.handleError(err))) | |
43 | } | |
44 | ||
45 | getUsers (pagination: RestPagination, sort: SortMeta): Observable<ResultList<User>> { | |
46 | let params = new HttpParams() | |
47 | params = this.restService.addRestGetParams(params, pagination, sort) | |
48 | ||
49 | return this.authHttp.get<ResultList<User>>(UserService.BASE_USERS_URL, { params }) | |
50 | .pipe( | |
51 | map(res => this.restExtractor.convertResultListDateToHuman(res)), | |
52 | map(res => this.restExtractor.applyToResultListData(res, this.formatUser.bind(this))), | |
53 | catchError(err => this.restExtractor.handleError(err)) | |
54 | ) | |
55 | } | |
56 | ||
57 | removeUser (user: User) { | |
58 | return this.authHttp.delete(UserService.BASE_USERS_URL + user.id) | |
59 | .pipe(catchError(err => this.restExtractor.handleError(err))) | |
60 | } | |
61 | ||
62 | banUser (user: User, reason?: string) { | |
63 | const body = reason ? { reason } : {} | |
64 | ||
65 | return this.authHttp.post(UserService.BASE_USERS_URL + user.id + '/block', body) | |
66 | .pipe(catchError(err => this.restExtractor.handleError(err))) | |
67 | } | |
68 | ||
69 | unbanUser (user: User) { | |
70 | return this.authHttp.post(UserService.BASE_USERS_URL + user.id + '/unblock', {}) | |
71 | .pipe(catchError(err => this.restExtractor.handleError(err))) | |
72 | } | |
73 | ||
74 | private formatUser (user: User) { | |
75 | let videoQuota | |
76 | if (user.videoQuota === -1) { | |
77 | videoQuota = this.i18n('Unlimited') | |
78 | } else { | |
79 | videoQuota = this.bytesPipe.transform(user.videoQuota, 0) | |
80 | } | |
81 | ||
82 | const videoQuotaUsed = this.bytesPipe.transform(user.videoQuotaUsed, 0) | |
83 | ||
84 | const roleLabels: { [ id in UserRole ]: string } = { | |
85 | [UserRole.USER]: this.i18n('User'), | |
86 | [UserRole.ADMINISTRATOR]: this.i18n('Administrator'), | |
87 | [UserRole.MODERATOR]: this.i18n('Moderator') | |
88 | } | |
89 | ||
90 | return Object.assign(user, { | |
91 | roleLabel: roleLabels[user.role], | |
92 | videoQuota, | |
93 | videoQuotaUsed | |
94 | }) | |
95 | } | |
96 | } |