]>
Commit | Line | Data |
---|---|---|
db400f44 | 1 | import { catchError, map } from 'rxjs/operators' |
d592e0a9 | 2 | import { HttpClient, HttpParams } from '@angular/common/http' |
c30745f3 C |
3 | import { Injectable } from '@angular/core' |
4 | import { BytesPipe } from 'ngx-pipes' | |
5 | import { SortMeta } from 'primeng/components/common/sortmeta' | |
db400f44 | 6 | import { Observable } from 'rxjs' |
a76138ff | 7 | import { ResultList, UserCreate, UserUpdate, User } from '../../../../../../shared' |
63c4db6d | 8 | import { environment } from '../../../../environments/environment' |
a76138ff | 9 | import { RestExtractor, RestPagination, RestService } from '../../../shared' |
b1d40cff | 10 | import { I18n } from '@ngx-translate/i18n-polyfill' |
7da18e44 C |
11 | |
12 | @Injectable() | |
13 | export class UserService { | |
63c4db6d | 14 | private static BASE_USERS_URL = environment.apiUrl + '/api/v1/users/' |
b0f9f39e | 15 | private bytesPipe = new BytesPipe() |
7da18e44 | 16 | |
df98563e | 17 | constructor ( |
d592e0a9 C |
18 | private authHttp: HttpClient, |
19 | private restService: RestService, | |
b1d40cff C |
20 | private restExtractor: RestExtractor, |
21 | private i18n: I18n | |
22 | ) { } | |
7da18e44 | 23 | |
4771e000 C |
24 | addUser (userCreate: UserCreate) { |
25 | return this.authHttp.post(UserService.BASE_USERS_URL, userCreate) | |
db400f44 C |
26 | .pipe( |
27 | map(this.restExtractor.extractDataBool), | |
28 | catchError(err => this.restExtractor.handleError(err)) | |
29 | ) | |
7da18e44 C |
30 | } |
31 | ||
8094a898 C |
32 | updateUser (userId: number, userUpdate: UserUpdate) { |
33 | return this.authHttp.put(UserService.BASE_USERS_URL + userId, userUpdate) | |
db400f44 C |
34 | .pipe( |
35 | map(this.restExtractor.extractDataBool), | |
36 | catchError(err => this.restExtractor.handleError(err)) | |
37 | ) | |
8094a898 C |
38 | } |
39 | ||
40 | getUser (userId: number) { | |
d592e0a9 | 41 | return this.authHttp.get<User>(UserService.BASE_USERS_URL + userId) |
db400f44 | 42 | .pipe(catchError(err => this.restExtractor.handleError(err))) |
8094a898 C |
43 | } |
44 | ||
d592e0a9 C |
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 }) | |
db400f44 C |
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 | ) | |
7da18e44 C |
55 | } |
56 | ||
df98563e C |
57 | removeUser (user: User) { |
58 | return this.authHttp.delete(UserService.BASE_USERS_URL + user.id) | |
db400f44 | 59 | .pipe(catchError(err => this.restExtractor.handleError(err))) |
7da18e44 | 60 | } |
b0f9f39e | 61 | |
141b177d C |
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 | ||
d592e0a9 C |
74 | private formatUser (user: User) { |
75 | let videoQuota | |
76 | if (user.videoQuota === -1) { | |
8c40b7dc | 77 | videoQuota = this.i18n('Unlimited') |
d592e0a9 | 78 | } else { |
a2788c69 | 79 | videoQuota = this.bytesPipe.transform(user.videoQuota, 0) |
d592e0a9 | 80 | } |
b0f9f39e | 81 | |
a76138ff C |
82 | const videoQuotaUsed = this.bytesPipe.transform(user.videoQuotaUsed, 0) |
83 | ||
d592e0a9 | 84 | return Object.assign(user, { |
a76138ff C |
85 | videoQuota, |
86 | videoQuotaUsed | |
b0f9f39e | 87 | }) |
b0f9f39e | 88 | } |
7da18e44 | 89 | } |