1 import { catchError, map } from 'rxjs/operators'
2 import { HttpClient } from '@angular/common/http'
3 import { Injectable } from '@angular/core'
4 import { UserCreate, UserUpdateMe, UserVideoQuota } from '../../../../../shared'
5 import { environment } from '../../../environments/environment'
6 import { RestExtractor } from '../rest'
7 import { Avatar } from '../../../../../shared/models/avatars/avatar.model'
10 export class UserService {
11 static BASE_USERS_URL = environment.apiUrl + '/api/v1/users/'
14 private authHttp: HttpClient,
15 private restExtractor: RestExtractor
19 changePassword (newPassword: string) {
20 const url = UserService.BASE_USERS_URL + 'me'
21 const body: UserUpdateMe = {
25 return this.authHttp.put(url, body)
27 map(this.restExtractor.extractDataBool),
28 catchError(err => this.restExtractor.handleError(err))
32 updateMyProfile (profile: UserUpdateMe) {
33 const url = UserService.BASE_USERS_URL + 'me'
35 return this.authHttp.put(url, profile)
37 map(this.restExtractor.extractDataBool),
38 catchError(err => this.restExtractor.handleError(err))
43 const url = UserService.BASE_USERS_URL + 'me'
45 return this.authHttp.delete(url)
47 map(this.restExtractor.extractDataBool),
48 catchError(err => this.restExtractor.handleError(err))
52 changeAvatar (avatarForm: FormData) {
53 const url = UserService.BASE_USERS_URL + 'me/avatar/pick'
55 return this.authHttp.post<{ avatar: Avatar }>(url, avatarForm)
56 .pipe(catchError(err => this.restExtractor.handleError(err)))
59 signup (userCreate: UserCreate) {
60 return this.authHttp.post(UserService.BASE_USERS_URL + 'register', userCreate)
62 map(this.restExtractor.extractDataBool),
63 catchError(err => this.restExtractor.handleError(err))
67 getMyVideoQuotaUsed () {
68 const url = UserService.BASE_USERS_URL + '/me/video-quota-used'
70 return this.authHttp.get<UserVideoQuota>(url)
71 .pipe(catchError(err => this.restExtractor.handleError(err)))
74 askResetPassword (email: string) {
75 const url = UserService.BASE_USERS_URL + '/ask-reset-password'
77 return this.authHttp.post(url, { email })
79 map(this.restExtractor.extractDataBool),
80 catchError(err => this.restExtractor.handleError(err))
84 resetPassword (userId: number, verificationString: string, password: string) {
85 const url = `${UserService.BASE_USERS_URL}/${userId}/reset-password`
91 return this.authHttp.post(url, body)
93 map(this.restExtractor.extractDataBool),
94 catchError(res => this.restExtractor.handleError(res))
98 verifyEmail (userId: number, verificationString: string) {
99 const url = `${UserService.BASE_USERS_URL}/${userId}/verify-email`
104 return this.authHttp.post(url, body)
106 map(this.restExtractor.extractDataBool),
107 catchError(res => this.restExtractor.handleError(res))
111 askSendVerifyEmail (email: string) {
112 const url = UserService.BASE_USERS_URL + '/ask-send-verify-email'
114 return this.authHttp.post(url, { email })
116 map(this.restExtractor.extractDataBool),
117 catchError(err => this.restExtractor.handleError(err))