]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+admin/users/shared/user.service.ts
Update FAQ.md
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / users / shared / user.service.ts
CommitLineData
db400f44 1import { catchError, map } from 'rxjs/operators'
d592e0a9 2import { HttpClient, HttpParams } from '@angular/common/http'
c30745f3
C
3import { Injectable } from '@angular/core'
4import { BytesPipe } from 'ngx-pipes'
5import { SortMeta } from 'primeng/components/common/sortmeta'
db400f44 6import { Observable } from 'rxjs'
baf0a8c9 7import { ResultList, UserCreate, UserUpdate, User, UserRole } from '../../../../../../shared'
63c4db6d 8import { environment } from '../../../../environments/environment'
a76138ff 9import { RestExtractor, RestPagination, RestService } from '../../../shared'
b1d40cff 10import { I18n } from '@ngx-translate/i18n-polyfill'
7da18e44
C
11
12@Injectable()
13export 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
baf0a8c9
C
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
d592e0a9 90 return Object.assign(user, {
baf0a8c9 91 roleLabel: roleLabels[user.role],
a76138ff
C
92 videoQuota,
93 videoQuotaUsed
b0f9f39e 94 })
b0f9f39e 95 }
7da18e44 96}