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