]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/users/user.service.ts
Fix typings
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / users / user.service.ts
1 import { catchError, map } from 'rxjs/operators'
2 import { HttpClient } from '@angular/common/http'
3 import { Injectable } from '@angular/core'
4 import { UserCreate, UserUpdateMe, UserVideoQuota } from '../../../../../shared'
5 import { environment } from '../../../environments/environment'
6 import { RestExtractor } from '../rest'
7 import { Avatar } from '../../../../../shared/models/avatars/avatar.model'
8
9 @Injectable()
10 export class UserService {
11 static BASE_USERS_URL = environment.apiUrl + '/api/v1/users/'
12
13 constructor (
14 private authHttp: HttpClient,
15 private restExtractor: RestExtractor
16 ) {
17 }
18
19 changePassword (newPassword: string) {
20 const url = UserService.BASE_USERS_URL + 'me'
21 const body: UserUpdateMe = {
22 password: newPassword
23 }
24
25 return this.authHttp.put(url, body)
26 .pipe(
27 map(this.restExtractor.extractDataBool),
28 catchError(res => this.restExtractor.handleError(res))
29 )
30 }
31
32 updateMyProfile (profile: UserUpdateMe) {
33 const url = UserService.BASE_USERS_URL + 'me'
34
35 return this.authHttp.put(url, profile)
36 .pipe(
37 map(this.restExtractor.extractDataBool),
38 catchError(res => this.restExtractor.handleError(res))
39 )
40 }
41
42 changeAvatar (avatarForm: FormData) {
43 const url = UserService.BASE_USERS_URL + 'me/avatar/pick'
44
45 return this.authHttp.post<{ avatar: Avatar }>(url, avatarForm)
46 .pipe(catchError(this.restExtractor.handleError))
47 }
48
49 signup (userCreate: UserCreate) {
50 return this.authHttp.post(UserService.BASE_USERS_URL + 'register', userCreate)
51 .pipe(
52 map(this.restExtractor.extractDataBool),
53 catchError(res => this.restExtractor.handleError(res))
54 )
55 }
56
57 getMyVideoQuotaUsed () {
58 const url = UserService.BASE_USERS_URL + '/me/video-quota-used'
59
60 return this.authHttp.get<UserVideoQuota>(url)
61 .pipe(catchError(res => this.restExtractor.handleError(res)))
62 }
63
64 askResetPassword (email: string) {
65 const url = UserService.BASE_USERS_URL + '/ask-reset-password'
66
67 return this.authHttp.post(url, { email })
68 .pipe(
69 map(this.restExtractor.extractDataBool),
70 catchError(res => this.restExtractor.handleError(res))
71 )
72 }
73
74 resetPassword (userId: number, verificationString: string, password: string) {
75 const url = `${UserService.BASE_USERS_URL}/${userId}/reset-password`
76 const body = {
77 verificationString,
78 password
79 }
80
81 return this.authHttp.post(url, body)
82 .pipe(
83 map(this.restExtractor.extractDataBool),
84 catchError(res => this.restExtractor.handleError(res))
85 )
86 }
87 }