]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/users/shared/user.service.ts
Upgrade to rxjs 6
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / users / shared / user.service.ts
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 } from '../../../../../../shared'
8 import { environment } from '../../../../environments/environment'
9 import { RestExtractor, RestPagination, RestService, User } from '../../../shared'
10
11 @Injectable()
12 export class UserService {
13 private static BASE_USERS_URL = environment.apiUrl + '/api/v1/users/'
14 private bytesPipe = new BytesPipe()
15
16 constructor (
17 private authHttp: HttpClient,
18 private restService: RestService,
19 private restExtractor: RestExtractor
20 ) {
21 }
22
23 addUser (userCreate: UserCreate) {
24 return this.authHttp.post(UserService.BASE_USERS_URL, userCreate)
25 .pipe(
26 map(this.restExtractor.extractDataBool),
27 catchError(err => this.restExtractor.handleError(err))
28 )
29 }
30
31 updateUser (userId: number, userUpdate: UserUpdate) {
32 return this.authHttp.put(UserService.BASE_USERS_URL + userId, userUpdate)
33 .pipe(
34 map(this.restExtractor.extractDataBool),
35 catchError(err => this.restExtractor.handleError(err))
36 )
37 }
38
39 getUser (userId: number) {
40 return this.authHttp.get<User>(UserService.BASE_USERS_URL + userId)
41 .pipe(catchError(err => this.restExtractor.handleError(err)))
42 }
43
44 getUsers (pagination: RestPagination, sort: SortMeta): Observable<ResultList<User>> {
45 let params = new HttpParams()
46 params = this.restService.addRestGetParams(params, pagination, sort)
47
48 return this.authHttp.get<ResultList<User>>(UserService.BASE_USERS_URL, { params })
49 .pipe(
50 map(res => this.restExtractor.convertResultListDateToHuman(res)),
51 map(res => this.restExtractor.applyToResultListData(res, this.formatUser.bind(this))),
52 catchError(err => this.restExtractor.handleError(err))
53 )
54 }
55
56 removeUser (user: User) {
57 return this.authHttp.delete(UserService.BASE_USERS_URL + user.id)
58 .pipe(catchError(err => this.restExtractor.handleError(err)))
59 }
60
61 private formatUser (user: User) {
62 let videoQuota
63 if (user.videoQuota === -1) {
64 videoQuota = 'Unlimited'
65 } else {
66 videoQuota = this.bytesPipe.transform(user.videoQuota)
67 }
68
69 return Object.assign(user, {
70 videoQuota
71 })
72 }
73 }