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'
11 export class UserService {
12 static BASE_USERS_URL = environment.apiUrl + '/api/v1/users/'
15 private authHttp: HttpClient,
16 private restExtractor: RestExtractor
20 changePassword (newPassword: string) {
21 const url = UserService.BASE_USERS_URL + 'me'
22 const body: UserUpdateMe = {
26 return this.authHttp.put(url, body)
28 map(this.restExtractor.extractDataBool),
29 catchError(err => this.restExtractor.handleError(err))
33 updateMyProfile (profile: UserUpdateMe) {
34 const url = UserService.BASE_USERS_URL + 'me'
36 return this.authHttp.put(url, profile)
38 map(this.restExtractor.extractDataBool),
39 catchError(err => this.restExtractor.handleError(err))
44 const url = UserService.BASE_USERS_URL + 'me'
46 return this.authHttp.delete(url)
48 map(this.restExtractor.extractDataBool),
49 catchError(err => this.restExtractor.handleError(err))
53 changeAvatar (avatarForm: FormData) {
54 const url = UserService.BASE_USERS_URL + 'me/avatar/pick'
56 return this.authHttp.post<{ avatar: Avatar }>(url, avatarForm)
57 .pipe(catchError(err => this.restExtractor.handleError(err)))
60 signup (userCreate: UserCreate) {
61 return this.authHttp.post(UserService.BASE_USERS_URL + 'register', userCreate)
63 map(this.restExtractor.extractDataBool),
64 catchError(err => this.restExtractor.handleError(err))
68 getMyVideoQuotaUsed () {
69 const url = UserService.BASE_USERS_URL + '/me/video-quota-used'
71 return this.authHttp.get<UserVideoQuota>(url)
72 .pipe(catchError(err => this.restExtractor.handleError(err)))
75 askResetPassword (email: string) {
76 const url = UserService.BASE_USERS_URL + '/ask-reset-password'
78 return this.authHttp.post(url, { email })
80 map(this.restExtractor.extractDataBool),
81 catchError(err => this.restExtractor.handleError(err))
85 resetPassword (userId: number, verificationString: string, password: string) {
86 const url = `${UserService.BASE_USERS_URL}/${userId}/reset-password`
92 return this.authHttp.post(url, body)
94 map(this.restExtractor.extractDataBool),
95 catchError(res => this.restExtractor.handleError(res))
99 verifyEmail (userId: number, verificationString: string) {
100 const url = `${UserService.BASE_USERS_URL}/${userId}/verify-email`
105 return this.authHttp.post(url, body)
107 map(this.restExtractor.extractDataBool),
108 catchError(res => this.restExtractor.handleError(res))
112 askSendVerifyEmail (email: string) {
113 const url = UserService.BASE_USERS_URL + '/ask-send-verify-email'
115 return this.authHttp.post(url, { email })
117 map(this.restExtractor.extractDataBool),
118 catchError(err => this.restExtractor.handleError(err))
122 autocomplete (search: string): Observable<string[]> {
123 const url = UserService.BASE_USERS_URL + 'autocomplete'
124 const params = new HttpParams().append('search', search)
127 .get<string[]>(url, { params })
128 .pipe(catchError(res => this.restExtractor.handleError(res)))