]>
Commit | Line | Data |
---|---|---|
db400f44 | 1 | import { catchError, map } from 'rxjs/operators' |
d592e0a9 | 2 | import { HttpClient } from '@angular/common/http' |
63c4db6d | 3 | import { Injectable } from '@angular/core' |
5fcbd898 | 4 | import { UserCreate, UserUpdateMe, UserVideoQuota } from '../../../../../shared' |
63c4db6d C |
5 | import { environment } from '../../../environments/environment' |
6 | import { RestExtractor } from '../rest' | |
5fcbd898 | 7 | import { Avatar } from '../../../../../shared/models/avatars/avatar.model' |
629d8d6f C |
8 | |
9 | @Injectable() | |
e2a2d6c8 | 10 | export 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 | |
c5911fd3 C |
42 | changeAvatar (avatarForm: FormData) { |
43 | const url = UserService.BASE_USERS_URL + 'me/avatar/pick' | |
44 | ||
5fcbd898 | 45 | return this.authHttp.post<{ avatar: Avatar }>(url, avatarForm) |
e4f0e92e | 46 | .pipe(catchError(err => this.restExtractor.handleError(err))) |
c5911fd3 C |
47 | } |
48 | ||
4771e000 | 49 | signup (userCreate: UserCreate) { |
d592e0a9 | 50 | return this.authHttp.post(UserService.BASE_USERS_URL + 'register', userCreate) |
db400f44 C |
51 | .pipe( |
52 | map(this.restExtractor.extractDataBool), | |
e4f0e92e | 53 | catchError(err => this.restExtractor.handleError(err)) |
db400f44 | 54 | ) |
a184c71b | 55 | } |
c5911fd3 | 56 | |
ce5496d6 C |
57 | getMyVideoQuotaUsed () { |
58 | const url = UserService.BASE_USERS_URL + '/me/video-quota-used' | |
c5911fd3 | 59 | |
5fcbd898 | 60 | return this.authHttp.get<UserVideoQuota>(url) |
e4f0e92e | 61 | .pipe(catchError(err => this.restExtractor.handleError(err))) |
c5911fd3 | 62 | } |
ecb4e35f C |
63 | |
64 | askResetPassword (email: string) { | |
65 | const url = UserService.BASE_USERS_URL + '/ask-reset-password' | |
66 | ||
67 | return this.authHttp.post(url, { email }) | |
db400f44 C |
68 | .pipe( |
69 | map(this.restExtractor.extractDataBool), | |
e4f0e92e | 70 | catchError(err => this.restExtractor.handleError(err)) |
db400f44 | 71 | ) |
ecb4e35f C |
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) | |
db400f44 C |
82 | .pipe( |
83 | map(this.restExtractor.extractDataBool), | |
84 | catchError(res => this.restExtractor.handleError(res)) | |
85 | ) | |
ecb4e35f | 86 | } |
629d8d6f | 87 | } |