]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/core/users/user.service.ts
Split user service
[github/Chocobozzz/PeerTube.git] / client / src / app / core / users / user.service.ts
CommitLineData
d92d070c
C
1import { Observable, of } from 'rxjs'
2import { catchError, first, map, shareReplay } from 'rxjs/operators'
74d63469 3import { HttpClient, HttpParams } from '@angular/common/http'
63c4db6d 4import { Injectable } from '@angular/core'
5c20a455 5import { AuthService } from '@app/core/auth'
d92d070c 6import { ActorImage, User as UserServerModel, UserUpdateMe, UserVideoQuota } from '@shared/models'
5c20a455 7import { environment } from '../../../environments/environment'
d92d070c
C
8import { RestExtractor } from '../rest'
9import { UserLocalStorageService } from './user-local-storage.service'
5c20a455 10import { User } from './user.model'
629d8d6f
C
11
12@Injectable()
e2a2d6c8 13export class UserService {
63c4db6d 14 static BASE_USERS_URL = environment.apiUrl + '/api/v1/users/'
629d8d6f 15
d3217560 16 private userCache: { [ id: number ]: Observable<UserServerModel> } = {}
bf80903f
C
17 private signupInThisSession = false
18
df98563e 19 constructor (
d592e0a9 20 private authHttp: HttpClient,
5c20a455 21 private authService: AuthService,
e724fa93 22 private restExtractor: RestExtractor,
a9bfa85d 23 private userLocalStorageService: UserLocalStorageService
9df52d66 24 ) { }
629d8d6f 25
d92d070c 26 // ---------------------------------------------------------------------------
bf80903f 27
d92d070c
C
28 getUserWithCache (userId: number) {
29 if (!this.userCache[userId]) {
30 this.userCache[userId] = this.getUser(userId).pipe(shareReplay())
df98563e 31 }
629d8d6f 32
d92d070c 33 return this.userCache[userId]
629d8d6f 34 }
af5e743b 35
d92d070c
C
36 getUser (userId: number, withStats = false) {
37 const params = new HttpParams().append('withStats', withStats + '')
0ba5f5ba 38
d92d070c 39 return this.authHttp.get<UserServerModel>(UserService.BASE_USERS_URL + userId, { params })
e8bffe96 40 .pipe(catchError(err => this.restExtractor.handleError(err)))
0ba5f5ba
C
41 }
42
a9bfa85d
C
43 // ---------------------------------------------------------------------------
44
d92d070c
C
45 setSignupInThisSession (value: boolean) {
46 this.signupInThisSession = value
47 }
48
49 hasSignupInThisSession () {
50 return this.signupInThisSession
51 }
52
53 // ---------------------------------------------------------------------------
54
a9bfa85d
C
55 updateMyAnonymousProfile (profile: UserUpdateMe) {
56 this.userLocalStorageService.setUserInfo(profile)
57 }
58
59 listenAnonymousUpdate () {
60 return this.userLocalStorageService.listenUserInfoChange()
61 .pipe(map(() => this.getAnonymousUser()))
62 }
63
64 getAnonymousUser () {
65 return new User(this.userLocalStorageService.getUserInfo())
66 }
67
d92d070c
C
68 getAnonymousOrLoggedUser () {
69 if (!this.authService.isLoggedIn()) {
70 return of(this.getAnonymousUser())
71 }
72
73 return this.authService.userInformationLoaded
74 .pipe(
75 first(),
76 map(() => this.authService.getUser())
77 )
78 }
79
a9bfa85d
C
80 // ---------------------------------------------------------------------------
81
d92d070c
C
82 changePassword (currentPassword: string, newPassword: string) {
83 const url = UserService.BASE_USERS_URL + 'me'
84 const body: UserUpdateMe = {
85 currentPassword,
86 password: newPassword
87 }
88
89 return this.authHttp.put(url, body)
90 .pipe(catchError(err => this.restExtractor.handleError(err)))
91 }
92
93 changeEmail (password: string, newEmail: string) {
94 const url = UserService.BASE_USERS_URL + 'me'
95 const body: UserUpdateMe = {
96 currentPassword: password,
97 email: newEmail
98 }
99
100 return this.authHttp.put(url, body)
101 .pipe(catchError(err => this.restExtractor.handleError(err)))
102 }
103
ed56ad11 104 updateMyProfile (profile: UserUpdateMe) {
8094a898 105 const url = UserService.BASE_USERS_URL + 'me'
af5e743b 106
ed56ad11 107 return this.authHttp.put(url, profile)
e8bffe96 108 .pipe(catchError(err => this.restExtractor.handleError(err)))
af5e743b 109 }
a184c71b 110
92b9d60c
C
111 deleteMe () {
112 const url = UserService.BASE_USERS_URL + 'me'
113
114 return this.authHttp.delete(url)
e8bffe96 115 .pipe(catchError(err => this.restExtractor.handleError(err)))
92b9d60c
C
116 }
117
c5911fd3
C
118 changeAvatar (avatarForm: FormData) {
119 const url = UserService.BASE_USERS_URL + 'me/avatar/pick'
120
f4796856 121 return this.authHttp.post<{ avatar: ActorImage }>(url, avatarForm)
e4f0e92e 122 .pipe(catchError(err => this.restExtractor.handleError(err)))
c5911fd3
C
123 }
124
1ea7da81
RK
125 deleteAvatar () {
126 const url = UserService.BASE_USERS_URL + 'me/avatar'
127
128 return this.authHttp.delete(url)
e8bffe96 129 .pipe(catchError(err => this.restExtractor.handleError(err)))
1ea7da81
RK
130 }
131
ce5496d6 132 getMyVideoQuotaUsed () {
bf64ed41 133 const url = UserService.BASE_USERS_URL + 'me/video-quota-used'
c5911fd3 134
5fcbd898 135 return this.authHttp.get<UserVideoQuota>(url)
e4f0e92e 136 .pipe(catchError(err => this.restExtractor.handleError(err)))
c5911fd3 137 }
ecb4e35f
C
138
139 askResetPassword (email: string) {
140 const url = UserService.BASE_USERS_URL + '/ask-reset-password'
141
142 return this.authHttp.post(url, { email })
e8bffe96 143 .pipe(catchError(err => this.restExtractor.handleError(err)))
ecb4e35f
C
144 }
145
146 resetPassword (userId: number, verificationString: string, password: string) {
147 const url = `${UserService.BASE_USERS_URL}/${userId}/reset-password`
148 const body = {
149 verificationString,
150 password
151 }
152
153 return this.authHttp.post(url, body)
e8bffe96 154 .pipe(catchError(res => this.restExtractor.handleError(res)))
ecb4e35f 155 }
d9eaee39 156
74d63469
GR
157 autocomplete (search: string): Observable<string[]> {
158 const url = UserService.BASE_USERS_URL + 'autocomplete'
159 const params = new HttpParams().append('search', search)
160
161 return this.authHttp
162 .get<string[]>(url, { params })
163 .pipe(catchError(res => this.restExtractor.handleError(res)))
164 }
629d8d6f 165}