]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/users/user.service.ts
Fix anonymous nsfw policy
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / users / user.service.ts
CommitLineData
5c20a455
C
1import { has } from 'lodash-es'
2import { BytesPipe } from 'ngx-pipes'
3import { SortMeta } from 'primeng/api'
4import { from, Observable, of } from 'rxjs'
5import { catchError, concatMap, first, map, shareReplay, toArray, throttleTime, filter } from 'rxjs/operators'
74d63469 6import { HttpClient, HttpParams } from '@angular/common/http'
63c4db6d 7import { Injectable } from '@angular/core'
5c20a455 8import { AuthService } from '@app/core/auth'
e724fa93 9import { I18n } from '@ngx-translate/i18n-polyfill'
1d5342ab 10import { UserRegister } from '@shared/models/users/user-register.model'
d3217560 11import { NSFWPolicyType } from '@shared/models/videos/nsfw-policy.type'
5c20a455
C
12import { ResultList, User as UserServerModel, UserCreate, UserRole, UserUpdate, UserUpdateMe, UserVideoQuota } from '../../../../../shared'
13import { Avatar } from '../../../../../shared/models/avatars/avatar.model'
14import { environment } from '../../../environments/environment'
d3217560 15import { LocalStorageService, SessionStorageService } from '../misc/storage.service'
5c20a455
C
16import { RestExtractor, RestPagination, RestService } from '../rest'
17import { User } from './user.model'
629d8d6f
C
18
19@Injectable()
e2a2d6c8 20export class UserService {
63c4db6d 21 static BASE_USERS_URL = environment.apiUrl + '/api/v1/users/'
629d8d6f 22
e724fa93
C
23 private bytesPipe = new BytesPipe()
24
d3217560 25 private userCache: { [ id: number ]: Observable<UserServerModel> } = {}
218b0874 26
df98563e 27 constructor (
d592e0a9 28 private authHttp: HttpClient,
5c20a455 29 private authService: AuthService,
e724fa93
C
30 private restExtractor: RestExtractor,
31 private restService: RestService,
d3217560
RK
32 private localStorageService: LocalStorageService,
33 private sessionStorageService: SessionStorageService,
e724fa93
C
34 private i18n: I18n
35 ) { }
629d8d6f 36
a890d1e0 37 changePassword (currentPassword: string, newPassword: string) {
8094a898
C
38 const url = UserService.BASE_USERS_URL + 'me'
39 const body: UserUpdateMe = {
a890d1e0 40 currentPassword,
629d8d6f 41 password: newPassword
df98563e 42 }
629d8d6f 43
de59c48f 44 return this.authHttp.put(url, body)
db400f44
C
45 .pipe(
46 map(this.restExtractor.extractDataBool),
e4f0e92e 47 catchError(err => this.restExtractor.handleError(err))
db400f44 48 )
629d8d6f 49 }
af5e743b 50
0ba5f5ba
C
51 changeEmail (password: string, newEmail: string) {
52 const url = UserService.BASE_USERS_URL + 'me'
53 const body: UserUpdateMe = {
54 currentPassword: password,
55 email: newEmail
56 }
57
58 return this.authHttp.put(url, body)
59 .pipe(
60 map(this.restExtractor.extractDataBool),
61 catchError(err => this.restExtractor.handleError(err))
62 )
63 }
64
ed56ad11 65 updateMyProfile (profile: UserUpdateMe) {
8094a898 66 const url = UserService.BASE_USERS_URL + 'me'
af5e743b 67
ed56ad11 68 return this.authHttp.put(url, profile)
db400f44
C
69 .pipe(
70 map(this.restExtractor.extractDataBool),
e4f0e92e 71 catchError(err => this.restExtractor.handleError(err))
db400f44 72 )
af5e743b 73 }
a184c71b 74
d3217560
RK
75 updateMyAnonymousProfile (profile: UserUpdateMe) {
76 const supportedKeys = {
77 // local storage keys
78 nsfwPolicy: (val: NSFWPolicyType) => this.localStorageService.setItem(User.KEYS.NSFW_POLICY, val),
79 webTorrentEnabled: (val: boolean) => this.localStorageService.setItem(User.KEYS.WEBTORRENT_ENABLED, String(val)),
80 autoPlayVideo: (val: boolean) => this.localStorageService.setItem(User.KEYS.AUTO_PLAY_VIDEO, String(val)),
81 autoPlayNextVideoPlaylist: (val: boolean) => this.localStorageService.setItem(User.KEYS.AUTO_PLAY_VIDEO_PLAYLIST, String(val)),
82 theme: (val: string) => this.localStorageService.setItem(User.KEYS.THEME, val),
83 videoLanguages: (val: string[]) => this.localStorageService.setItem(User.KEYS.VIDEO_LANGUAGES, JSON.stringify(val)),
84
85 // session storage keys
86 autoPlayNextVideo: (val: boolean) =>
87 this.sessionStorageService.setItem(User.KEYS.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO, String(val))
88 }
89
90 for (const key of Object.keys(profile)) {
91 try {
92 if (has(supportedKeys, key)) supportedKeys[key](profile[key])
93 } catch (err) {
94 console.error(`Cannot set item ${key} in localStorage. Likely due to a value impossible to stringify.`, err)
95 }
96 }
97 }
98
5c20a455
C
99 listenAnonymousUpdate () {
100 return this.localStorageService.watch([
101 User.KEYS.NSFW_POLICY,
102 User.KEYS.WEBTORRENT_ENABLED,
103 User.KEYS.AUTO_PLAY_VIDEO,
104 User.KEYS.AUTO_PLAY_VIDEO_PLAYLIST,
105 User.KEYS.THEME,
106 User.KEYS.VIDEO_LANGUAGES
107 ]).pipe(
108 throttleTime(200),
109 filter(() => this.authService.isLoggedIn() !== true),
110 map(() => this.getAnonymousUser())
111 )
112 }
113
92b9d60c
C
114 deleteMe () {
115 const url = UserService.BASE_USERS_URL + 'me'
116
117 return this.authHttp.delete(url)
118 .pipe(
119 map(this.restExtractor.extractDataBool),
120 catchError(err => this.restExtractor.handleError(err))
121 )
122 }
123
c5911fd3
C
124 changeAvatar (avatarForm: FormData) {
125 const url = UserService.BASE_USERS_URL + 'me/avatar/pick'
126
5fcbd898 127 return this.authHttp.post<{ avatar: Avatar }>(url, avatarForm)
e4f0e92e 128 .pipe(catchError(err => this.restExtractor.handleError(err)))
c5911fd3
C
129 }
130
1d5342ab 131 signup (userCreate: UserRegister) {
d592e0a9 132 return this.authHttp.post(UserService.BASE_USERS_URL + 'register', userCreate)
db400f44
C
133 .pipe(
134 map(this.restExtractor.extractDataBool),
e4f0e92e 135 catchError(err => this.restExtractor.handleError(err))
db400f44 136 )
a184c71b 137 }
c5911fd3 138
ce5496d6 139 getMyVideoQuotaUsed () {
bf64ed41 140 const url = UserService.BASE_USERS_URL + 'me/video-quota-used'
c5911fd3 141
5fcbd898 142 return this.authHttp.get<UserVideoQuota>(url)
e4f0e92e 143 .pipe(catchError(err => this.restExtractor.handleError(err)))
c5911fd3 144 }
ecb4e35f
C
145
146 askResetPassword (email: string) {
147 const url = UserService.BASE_USERS_URL + '/ask-reset-password'
148
149 return this.authHttp.post(url, { email })
db400f44
C
150 .pipe(
151 map(this.restExtractor.extractDataBool),
e4f0e92e 152 catchError(err => this.restExtractor.handleError(err))
db400f44 153 )
ecb4e35f
C
154 }
155
156 resetPassword (userId: number, verificationString: string, password: string) {
157 const url = `${UserService.BASE_USERS_URL}/${userId}/reset-password`
158 const body = {
159 verificationString,
160 password
161 }
162
163 return this.authHttp.post(url, body)
db400f44
C
164 .pipe(
165 map(this.restExtractor.extractDataBool),
166 catchError(res => this.restExtractor.handleError(res))
167 )
ecb4e35f 168 }
d9eaee39 169
0ba5f5ba 170 verifyEmail (userId: number, verificationString: string, isPendingEmail: boolean) {
d9eaee39
JM
171 const url = `${UserService.BASE_USERS_URL}/${userId}/verify-email`
172 const body = {
0ba5f5ba
C
173 verificationString,
174 isPendingEmail
d9eaee39
JM
175 }
176
177 return this.authHttp.post(url, body)
178 .pipe(
179 map(this.restExtractor.extractDataBool),
180 catchError(res => this.restExtractor.handleError(res))
181 )
182 }
183
184 askSendVerifyEmail (email: string) {
185 const url = UserService.BASE_USERS_URL + '/ask-send-verify-email'
186
187 return this.authHttp.post(url, { email })
188 .pipe(
189 map(this.restExtractor.extractDataBool),
190 catchError(err => this.restExtractor.handleError(err))
191 )
192 }
74d63469
GR
193
194 autocomplete (search: string): Observable<string[]> {
195 const url = UserService.BASE_USERS_URL + 'autocomplete'
196 const params = new HttpParams().append('search', search)
197
198 return this.authHttp
199 .get<string[]>(url, { params })
200 .pipe(catchError(res => this.restExtractor.handleError(res)))
201 }
e724fa93 202
1f20622f
C
203 getNewUsername (oldDisplayName: string, newDisplayName: string, currentUsername: string) {
204 // Don't update display name, the user seems to have changed it
205 if (this.displayNameToUsername(oldDisplayName) !== currentUsername) return currentUsername
206
207 return this.displayNameToUsername(newDisplayName)
208 }
209
210 displayNameToUsername (displayName: string) {
211 if (!displayName) return ''
212
213 return displayName
214 .toLowerCase()
215 .replace(/\s/g, '_')
216 .replace(/[^a-z0-9_.]/g, '')
217 }
218
e724fa93
C
219 /* ###### Admin methods ###### */
220
221 addUser (userCreate: UserCreate) {
222 return this.authHttp.post(UserService.BASE_USERS_URL, userCreate)
223 .pipe(
224 map(this.restExtractor.extractDataBool),
225 catchError(err => this.restExtractor.handleError(err))
226 )
227 }
228
229 updateUser (userId: number, userUpdate: UserUpdate) {
230 return this.authHttp.put(UserService.BASE_USERS_URL + userId, userUpdate)
231 .pipe(
232 map(this.restExtractor.extractDataBool),
233 catchError(err => this.restExtractor.handleError(err))
234 )
235 }
236
d3217560 237 updateUsers (users: UserServerModel[], userUpdate: UserUpdate) {
fc2ec87a
JM
238 return from(users)
239 .pipe(
240 concatMap(u => this.authHttp.put(UserService.BASE_USERS_URL + u.id, userUpdate)),
241 toArray(),
242 catchError(err => this.restExtractor.handleError(err))
243 )
244 }
245
218b0874
C
246 getUserWithCache (userId: number) {
247 if (!this.userCache[userId]) {
248 this.userCache[ userId ] = this.getUser(userId).pipe(shareReplay())
249 }
250
251 return this.userCache[userId]
252 }
253
76314386
RK
254 getUser (userId: number, withStats = false) {
255 const params = new HttpParams().append('withStats', withStats + '')
256 return this.authHttp.get<UserServerModel>(UserService.BASE_USERS_URL + userId, { params })
e724fa93
C
257 .pipe(catchError(err => this.restExtractor.handleError(err)))
258 }
259
d3217560 260 getAnonymousUser () {
5c20a455 261 let videoLanguages: string[]
9870329f 262
d3217560
RK
263 try {
264 videoLanguages = JSON.parse(this.localStorageService.getItem(User.KEYS.VIDEO_LANGUAGES))
265 } catch (err) {
266 videoLanguages = null
267 console.error('Cannot parse desired video languages from localStorage.', err)
268 }
269
270 return new User({
271 // local storage keys
272 nsfwPolicy: this.localStorageService.getItem(User.KEYS.NSFW_POLICY) as NSFWPolicyType,
273 webTorrentEnabled: this.localStorageService.getItem(User.KEYS.WEBTORRENT_ENABLED) !== 'false',
3e95b683 274 theme: this.localStorageService.getItem(User.KEYS.THEME) || 'instance-default',
d3217560
RK
275 videoLanguages,
276
9870329f
C
277 autoPlayNextVideoPlaylist: this.localStorageService.getItem(User.KEYS.AUTO_PLAY_VIDEO_PLAYLIST) !== 'false',
278 autoPlayVideo: this.localStorageService.getItem(User.KEYS.AUTO_PLAY_VIDEO) === 'true',
279
d3217560
RK
280 // session storage keys
281 autoPlayNextVideo: this.sessionStorageService.getItem(User.KEYS.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO) === 'true'
282 })
283 }
284
285 getUsers (pagination: RestPagination, sort: SortMeta, search?: string): Observable<ResultList<UserServerModel>> {
e724fa93
C
286 let params = new HttpParams()
287 params = this.restService.addRestGetParams(params, pagination, sort)
288
24b9417c
C
289 if (search) params = params.append('search', search)
290
d3217560 291 return this.authHttp.get<ResultList<UserServerModel>>(UserService.BASE_USERS_URL, { params })
e724fa93
C
292 .pipe(
293 map(res => this.restExtractor.convertResultListDateToHuman(res)),
294 map(res => this.restExtractor.applyToResultListData(res, this.formatUser.bind(this))),
295 catchError(err => this.restExtractor.handleError(err))
296 )
297 }
298
d3217560 299 removeUser (usersArg: UserServerModel | UserServerModel[]) {
791645e6
C
300 const users = Array.isArray(usersArg) ? usersArg : [ usersArg ]
301
302 return from(users)
303 .pipe(
304 concatMap(u => this.authHttp.delete(UserService.BASE_USERS_URL + u.id)),
305 toArray(),
306 catchError(err => this.restExtractor.handleError(err))
307 )
e724fa93
C
308 }
309
d3217560 310 banUsers (usersArg: UserServerModel | UserServerModel[], reason?: string) {
e724fa93 311 const body = reason ? { reason } : {}
791645e6 312 const users = Array.isArray(usersArg) ? usersArg : [ usersArg ]
e724fa93 313
791645e6
C
314 return from(users)
315 .pipe(
316 concatMap(u => this.authHttp.post(UserService.BASE_USERS_URL + u.id + '/block', body)),
317 toArray(),
318 catchError(err => this.restExtractor.handleError(err))
319 )
e724fa93
C
320 }
321
d3217560 322 unbanUsers (usersArg: UserServerModel | UserServerModel[]) {
791645e6
C
323 const users = Array.isArray(usersArg) ? usersArg : [ usersArg ]
324
325 return from(users)
326 .pipe(
327 concatMap(u => this.authHttp.post(UserService.BASE_USERS_URL + u.id + '/unblock', {})),
328 toArray(),
329 catchError(err => this.restExtractor.handleError(err))
330 )
e724fa93
C
331 }
332
5c20a455
C
333 getAnonymousOrLoggedUser () {
334 if (!this.authService.isLoggedIn()) {
335 return of(this.getAnonymousUser())
336 }
337
338 return this.authService.userInformationLoaded
339 .pipe(
340 first(),
341 map(() => this.authService.getUser())
342 )
343 }
344
d3217560 345 private formatUser (user: UserServerModel) {
e724fa93
C
346 let videoQuota
347 if (user.videoQuota === -1) {
348 videoQuota = this.i18n('Unlimited')
349 } else {
350 videoQuota = this.bytesPipe.transform(user.videoQuota, 0)
351 }
352
353 const videoQuotaUsed = this.bytesPipe.transform(user.videoQuotaUsed, 0)
354
355 const roleLabels: { [ id in UserRole ]: string } = {
356 [UserRole.USER]: this.i18n('User'),
357 [UserRole.ADMINISTRATOR]: this.i18n('Administrator'),
358 [UserRole.MODERATOR]: this.i18n('Moderator')
359 }
360
361 return Object.assign(user, {
362 roleLabel: roleLabels[user.role],
363 videoQuota,
364 videoQuotaUsed
365 })
366 }
629d8d6f 367}