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 (currentPassword: string, newPassword: string) {
21 const url = UserService.BASE_USERS_URL + 'me'
22 const body: UserUpdateMe = {
27 return this.authHttp.put(url, body)
29 map(this.restExtractor.extractDataBool),
30 catchError(err => this.restExtractor.handleError(err))
34 updateMyProfile (profile: UserUpdateMe) {
35 const url = UserService.BASE_USERS_URL + 'me'
37 return this.authHttp.put(url, profile)
39 map(this.restExtractor.extractDataBool),
40 catchError(err => this.restExtractor.handleError(err))
45 const url = UserService.BASE_USERS_URL + 'me'
47 return this.authHttp.delete(url)
49 map(this.restExtractor.extractDataBool),
50 catchError(err => this.restExtractor.handleError(err))
54 changeAvatar (avatarForm: FormData) {
55 const url = UserService.BASE_USERS_URL + 'me/avatar/pick'
57 return this.authHttp.post<{ avatar: Avatar }>(url, avatarForm)
58 .pipe(catchError(err => this.restExtractor.handleError(err)))
61 signup (userCreate: UserCreate) {
62 return this.authHttp.post(UserService.BASE_USERS_URL + 'register', userCreate)
64 map(this.restExtractor.extractDataBool),
65 catchError(err => this.restExtractor.handleError(err))
69 getMyVideoQuotaUsed () {
70 const url = UserService.BASE_USERS_URL + '/me/video-quota-used'
72 return this.authHttp.get<UserVideoQuota>(url)
73 .pipe(catchError(err => this.restExtractor.handleError(err)))
76 askResetPassword (email: string) {
77 const url = UserService.BASE_USERS_URL + '/ask-reset-password'
79 return this.authHttp.post(url, { email })
81 map(this.restExtractor.extractDataBool),
82 catchError(err => this.restExtractor.handleError(err))
86 resetPassword (userId: number, verificationString: string, password: string) {
87 const url = `${UserService.BASE_USERS_URL}/${userId}/reset-password`
93 return this.authHttp.post(url, body)
95 map(this.restExtractor.extractDataBool),
96 catchError(res => this.restExtractor.handleError(res))
100 verifyEmail (userId: number, verificationString: string) {
101 const url = `${UserService.BASE_USERS_URL}/${userId}/verify-email`
106 return this.authHttp.post(url, body)
108 map(this.restExtractor.extractDataBool),
109 catchError(res => this.restExtractor.handleError(res))
113 askSendVerifyEmail (email: string) {
114 const url = UserService.BASE_USERS_URL + '/ask-send-verify-email'
116 return this.authHttp.post(url, { email })
118 map(this.restExtractor.extractDataBool),
119 catchError(err => this.restExtractor.handleError(err))
123 autocomplete (search: string): Observable<string[]> {
124 const url = UserService.BASE_USERS_URL + 'autocomplete'
125 const params = new HttpParams().append('search', search)
128 .get<string[]>(url, { params })
129 .pipe(catchError(res => this.restExtractor.handleError(res)))