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