diff options
author | Chocobozzz <me@florianbigard.com> | 2018-10-05 15:24:29 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-10-05 17:02:09 +0200 |
commit | e724fa93c71d76d709e819a05e5e2904a3c4205b (patch) | |
tree | c9bd410253f4ceac8e330541580445ca313583ac /client/src/app/shared/users | |
parent | 21c54ac5f684f8b72bcde45cd8327ee21f574f22 (diff) | |
download | PeerTube-e724fa93c71d76d709e819a05e5e2904a3c4205b.tar.gz PeerTube-e724fa93c71d76d709e819a05e5e2904a3c4205b.tar.zst PeerTube-e724fa93c71d76d709e819a05e5e2904a3c4205b.zip |
Move user moderation tool in a separate component
Diffstat (limited to 'client/src/app/shared/users')
-rw-r--r-- | client/src/app/shared/users/user.service.ts | 91 |
1 files changed, 86 insertions, 5 deletions
diff --git a/client/src/app/shared/users/user.service.ts b/client/src/app/shared/users/user.service.ts index bd5cd45d4..5ab290a59 100644 --- a/client/src/app/shared/users/user.service.ts +++ b/client/src/app/shared/users/user.service.ts | |||
@@ -2,20 +2,26 @@ import { Observable } from 'rxjs' | |||
2 | import { catchError, map } from 'rxjs/operators' | 2 | import { catchError, map } from 'rxjs/operators' |
3 | import { HttpClient, HttpParams } from '@angular/common/http' | 3 | import { HttpClient, HttpParams } from '@angular/common/http' |
4 | import { Injectable } from '@angular/core' | 4 | import { Injectable } from '@angular/core' |
5 | import { UserCreate, UserUpdateMe, UserVideoQuota } from '../../../../../shared' | 5 | import { ResultList, User, UserCreate, UserRole, UserUpdate, UserUpdateMe, UserVideoQuota } from '../../../../../shared' |
6 | import { environment } from '../../../environments/environment' | 6 | import { environment } from '../../../environments/environment' |
7 | import { RestExtractor } from '../rest' | 7 | import { RestExtractor, RestPagination, RestService } from '../rest' |
8 | import { Avatar } from '../../../../../shared/models/avatars/avatar.model' | 8 | import { Avatar } from '../../../../../shared/models/avatars/avatar.model' |
9 | import { SortMeta } from 'primeng/api' | ||
10 | import { BytesPipe } from 'ngx-pipes' | ||
11 | import { I18n } from '@ngx-translate/i18n-polyfill' | ||
9 | 12 | ||
10 | @Injectable() | 13 | @Injectable() |
11 | export class UserService { | 14 | export class UserService { |
12 | static BASE_USERS_URL = environment.apiUrl + '/api/v1/users/' | 15 | static BASE_USERS_URL = environment.apiUrl + '/api/v1/users/' |
13 | 16 | ||
17 | private bytesPipe = new BytesPipe() | ||
18 | |||
14 | constructor ( | 19 | constructor ( |
15 | private authHttp: HttpClient, | 20 | private authHttp: HttpClient, |
16 | private restExtractor: RestExtractor | 21 | private restExtractor: RestExtractor, |
17 | ) { | 22 | private restService: RestService, |
18 | } | 23 | private i18n: I18n |
24 | ) { } | ||
19 | 25 | ||
20 | changePassword (currentPassword: string, newPassword: string) { | 26 | changePassword (currentPassword: string, newPassword: string) { |
21 | const url = UserService.BASE_USERS_URL + 'me' | 27 | const url = UserService.BASE_USERS_URL + 'me' |
@@ -128,4 +134,79 @@ export class UserService { | |||
128 | .get<string[]>(url, { params }) | 134 | .get<string[]>(url, { params }) |
129 | .pipe(catchError(res => this.restExtractor.handleError(res))) | 135 | .pipe(catchError(res => this.restExtractor.handleError(res))) |
130 | } | 136 | } |
137 | |||
138 | /* ###### Admin methods ###### */ | ||
139 | |||
140 | addUser (userCreate: UserCreate) { | ||
141 | return this.authHttp.post(UserService.BASE_USERS_URL, userCreate) | ||
142 | .pipe( | ||
143 | map(this.restExtractor.extractDataBool), | ||
144 | catchError(err => this.restExtractor.handleError(err)) | ||
145 | ) | ||
146 | } | ||
147 | |||
148 | updateUser (userId: number, userUpdate: UserUpdate) { | ||
149 | return this.authHttp.put(UserService.BASE_USERS_URL + userId, userUpdate) | ||
150 | .pipe( | ||
151 | map(this.restExtractor.extractDataBool), | ||
152 | catchError(err => this.restExtractor.handleError(err)) | ||
153 | ) | ||
154 | } | ||
155 | |||
156 | getUser (userId: number) { | ||
157 | return this.authHttp.get<User>(UserService.BASE_USERS_URL + userId) | ||
158 | .pipe(catchError(err => this.restExtractor.handleError(err))) | ||
159 | } | ||
160 | |||
161 | getUsers (pagination: RestPagination, sort: SortMeta): Observable<ResultList<User>> { | ||
162 | let params = new HttpParams() | ||
163 | params = this.restService.addRestGetParams(params, pagination, sort) | ||
164 | |||
165 | return this.authHttp.get<ResultList<User>>(UserService.BASE_USERS_URL, { params }) | ||
166 | .pipe( | ||
167 | map(res => this.restExtractor.convertResultListDateToHuman(res)), | ||
168 | map(res => this.restExtractor.applyToResultListData(res, this.formatUser.bind(this))), | ||
169 | catchError(err => this.restExtractor.handleError(err)) | ||
170 | ) | ||
171 | } | ||
172 | |||
173 | removeUser (user: User) { | ||
174 | return this.authHttp.delete(UserService.BASE_USERS_URL + user.id) | ||
175 | .pipe(catchError(err => this.restExtractor.handleError(err))) | ||
176 | } | ||
177 | |||
178 | banUser (user: User, reason?: string) { | ||
179 | const body = reason ? { reason } : {} | ||
180 | |||
181 | return this.authHttp.post(UserService.BASE_USERS_URL + user.id + '/block', body) | ||
182 | .pipe(catchError(err => this.restExtractor.handleError(err))) | ||
183 | } | ||
184 | |||
185 | unbanUser (user: User) { | ||
186 | return this.authHttp.post(UserService.BASE_USERS_URL + user.id + '/unblock', {}) | ||
187 | .pipe(catchError(err => this.restExtractor.handleError(err))) | ||
188 | } | ||
189 | |||
190 | private formatUser (user: User) { | ||
191 | let videoQuota | ||
192 | if (user.videoQuota === -1) { | ||
193 | videoQuota = this.i18n('Unlimited') | ||
194 | } else { | ||
195 | videoQuota = this.bytesPipe.transform(user.videoQuota, 0) | ||
196 | } | ||
197 | |||
198 | const videoQuotaUsed = this.bytesPipe.transform(user.videoQuotaUsed, 0) | ||
199 | |||
200 | const roleLabels: { [ id in UserRole ]: string } = { | ||
201 | [UserRole.USER]: this.i18n('User'), | ||
202 | [UserRole.ADMINISTRATOR]: this.i18n('Administrator'), | ||
203 | [UserRole.MODERATOR]: this.i18n('Moderator') | ||
204 | } | ||
205 | |||
206 | return Object.assign(user, { | ||
207 | roleLabel: roleLabels[user.role], | ||
208 | videoQuota, | ||
209 | videoQuotaUsed | ||
210 | }) | ||
211 | } | ||
131 | } | 212 | } |