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