Users can change ownership of their video [#510] (#888)
[oweals/peertube.git] / client / src / app / shared / users / user.service.ts
1 import { Observable } from 'rxjs'
2 import { catchError, map } from 'rxjs/operators'
3 import { HttpClient, HttpParams } from '@angular/common/http'
4 import { Injectable } from '@angular/core'
5 import { UserCreate, UserUpdateMe, UserVideoQuota } from '../../../../../shared'
6 import { environment } from '../../../environments/environment'
7 import { RestExtractor } from '../rest'
8 import { Avatar } from '../../../../../shared/models/avatars/avatar.model'
9
10 @Injectable()
11 export class UserService {
12   static BASE_USERS_URL = environment.apiUrl + '/api/v1/users/'
13
14   constructor (
15     private authHttp: HttpClient,
16     private restExtractor: RestExtractor
17   ) {
18   }
19
20   changePassword (newPassword: string) {
21     const url = UserService.BASE_USERS_URL + 'me'
22     const body: UserUpdateMe = {
23       password: newPassword
24     }
25
26     return this.authHttp.put(url, body)
27                .pipe(
28                  map(this.restExtractor.extractDataBool),
29                  catchError(err => this.restExtractor.handleError(err))
30                )
31   }
32
33   updateMyProfile (profile: UserUpdateMe) {
34     const url = UserService.BASE_USERS_URL + 'me'
35
36     return this.authHttp.put(url, profile)
37                .pipe(
38                  map(this.restExtractor.extractDataBool),
39                  catchError(err => this.restExtractor.handleError(err))
40                )
41   }
42
43   deleteMe () {
44     const url = UserService.BASE_USERS_URL + 'me'
45
46     return this.authHttp.delete(url)
47                .pipe(
48                  map(this.restExtractor.extractDataBool),
49                  catchError(err => this.restExtractor.handleError(err))
50                )
51   }
52
53   changeAvatar (avatarForm: FormData) {
54     const url = UserService.BASE_USERS_URL + 'me/avatar/pick'
55
56     return this.authHttp.post<{ avatar: Avatar }>(url, avatarForm)
57                .pipe(catchError(err => this.restExtractor.handleError(err)))
58   }
59
60   signup (userCreate: UserCreate) {
61     return this.authHttp.post(UserService.BASE_USERS_URL + 'register', userCreate)
62                .pipe(
63                  map(this.restExtractor.extractDataBool),
64                  catchError(err => this.restExtractor.handleError(err))
65                )
66   }
67
68   getMyVideoQuotaUsed () {
69     const url = UserService.BASE_USERS_URL + '/me/video-quota-used'
70
71     return this.authHttp.get<UserVideoQuota>(url)
72                .pipe(catchError(err => this.restExtractor.handleError(err)))
73   }
74
75   askResetPassword (email: string) {
76     const url = UserService.BASE_USERS_URL + '/ask-reset-password'
77
78     return this.authHttp.post(url, { email })
79                .pipe(
80                  map(this.restExtractor.extractDataBool),
81                  catchError(err => this.restExtractor.handleError(err))
82                )
83   }
84
85   resetPassword (userId: number, verificationString: string, password: string) {
86     const url = `${UserService.BASE_USERS_URL}/${userId}/reset-password`
87     const body = {
88       verificationString,
89       password
90     }
91
92     return this.authHttp.post(url, body)
93                .pipe(
94                  map(this.restExtractor.extractDataBool),
95                  catchError(res => this.restExtractor.handleError(res))
96                )
97   }
98
99   verifyEmail (userId: number, verificationString: string) {
100     const url = `${UserService.BASE_USERS_URL}/${userId}/verify-email`
101     const body = {
102       verificationString
103     }
104
105     return this.authHttp.post(url, body)
106                .pipe(
107                  map(this.restExtractor.extractDataBool),
108                  catchError(res => this.restExtractor.handleError(res))
109                )
110   }
111
112   askSendVerifyEmail (email: string) {
113     const url = UserService.BASE_USERS_URL + '/ask-send-verify-email'
114
115     return this.authHttp.post(url, { email })
116                .pipe(
117                  map(this.restExtractor.extractDataBool),
118                  catchError(err => this.restExtractor.handleError(err))
119                )
120   }
121
122   autocomplete (search: string): Observable<string[]> {
123     const url = UserService.BASE_USERS_URL + 'autocomplete'
124     const params = new HttpParams().append('search', search)
125
126     return this.authHttp
127       .get<string[]>(url, { params })
128       .pipe(catchError(res => this.restExtractor.handleError(res)))
129   }
130 }