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