]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/users/user.service.ts
Fix anonymous nsfw policy
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / users / user.service.ts
1 import { has } from 'lodash-es'
2 import { BytesPipe } from 'ngx-pipes'
3 import { SortMeta } from 'primeng/api'
4 import { from, Observable, of } from 'rxjs'
5 import { catchError, concatMap, first, map, shareReplay, toArray, throttleTime, filter } from 'rxjs/operators'
6 import { HttpClient, HttpParams } from '@angular/common/http'
7 import { Injectable } from '@angular/core'
8 import { AuthService } from '@app/core/auth'
9 import { I18n } from '@ngx-translate/i18n-polyfill'
10 import { UserRegister } from '@shared/models/users/user-register.model'
11 import { NSFWPolicyType } from '@shared/models/videos/nsfw-policy.type'
12 import { ResultList, User as UserServerModel, UserCreate, UserRole, UserUpdate, UserUpdateMe, UserVideoQuota } from '../../../../../shared'
13 import { Avatar } from '../../../../../shared/models/avatars/avatar.model'
14 import { environment } from '../../../environments/environment'
15 import { LocalStorageService, SessionStorageService } from '../misc/storage.service'
16 import { RestExtractor, RestPagination, RestService } from '../rest'
17 import { User } from './user.model'
18
19 @Injectable()
20 export class UserService {
21 static BASE_USERS_URL = environment.apiUrl + '/api/v1/users/'
22
23 private bytesPipe = new BytesPipe()
24
25 private userCache: { [ id: number ]: Observable<UserServerModel> } = {}
26
27 constructor (
28 private authHttp: HttpClient,
29 private authService: AuthService,
30 private restExtractor: RestExtractor,
31 private restService: RestService,
32 private localStorageService: LocalStorageService,
33 private sessionStorageService: SessionStorageService,
34 private i18n: I18n
35 ) { }
36
37 changePassword (currentPassword: string, newPassword: string) {
38 const url = UserService.BASE_USERS_URL + 'me'
39 const body: UserUpdateMe = {
40 currentPassword,
41 password: newPassword
42 }
43
44 return this.authHttp.put(url, body)
45 .pipe(
46 map(this.restExtractor.extractDataBool),
47 catchError(err => this.restExtractor.handleError(err))
48 )
49 }
50
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
65 updateMyProfile (profile: UserUpdateMe) {
66 const url = UserService.BASE_USERS_URL + 'me'
67
68 return this.authHttp.put(url, profile)
69 .pipe(
70 map(this.restExtractor.extractDataBool),
71 catchError(err => this.restExtractor.handleError(err))
72 )
73 }
74
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
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
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
124 changeAvatar (avatarForm: FormData) {
125 const url = UserService.BASE_USERS_URL + 'me/avatar/pick'
126
127 return this.authHttp.post<{ avatar: Avatar }>(url, avatarForm)
128 .pipe(catchError(err => this.restExtractor.handleError(err)))
129 }
130
131 signup (userCreate: UserRegister) {
132 return this.authHttp.post(UserService.BASE_USERS_URL + 'register', userCreate)
133 .pipe(
134 map(this.restExtractor.extractDataBool),
135 catchError(err => this.restExtractor.handleError(err))
136 )
137 }
138
139 getMyVideoQuotaUsed () {
140 const url = UserService.BASE_USERS_URL + 'me/video-quota-used'
141
142 return this.authHttp.get<UserVideoQuota>(url)
143 .pipe(catchError(err => this.restExtractor.handleError(err)))
144 }
145
146 askResetPassword (email: string) {
147 const url = UserService.BASE_USERS_URL + '/ask-reset-password'
148
149 return this.authHttp.post(url, { email })
150 .pipe(
151 map(this.restExtractor.extractDataBool),
152 catchError(err => this.restExtractor.handleError(err))
153 )
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)
164 .pipe(
165 map(this.restExtractor.extractDataBool),
166 catchError(res => this.restExtractor.handleError(res))
167 )
168 }
169
170 verifyEmail (userId: number, verificationString: string, isPendingEmail: boolean) {
171 const url = `${UserService.BASE_USERS_URL}/${userId}/verify-email`
172 const body = {
173 verificationString,
174 isPendingEmail
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 }
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 }
202
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
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
237 updateUsers (users: UserServerModel[], userUpdate: UserUpdate) {
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
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
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 })
257 .pipe(catchError(err => this.restExtractor.handleError(err)))
258 }
259
260 getAnonymousUser () {
261 let videoLanguages: string[]
262
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',
274 theme: this.localStorageService.getItem(User.KEYS.THEME) || 'instance-default',
275 videoLanguages,
276
277 autoPlayNextVideoPlaylist: this.localStorageService.getItem(User.KEYS.AUTO_PLAY_VIDEO_PLAYLIST) !== 'false',
278 autoPlayVideo: this.localStorageService.getItem(User.KEYS.AUTO_PLAY_VIDEO) === 'true',
279
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>> {
286 let params = new HttpParams()
287 params = this.restService.addRestGetParams(params, pagination, sort)
288
289 if (search) params = params.append('search', search)
290
291 return this.authHttp.get<ResultList<UserServerModel>>(UserService.BASE_USERS_URL, { params })
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
299 removeUser (usersArg: UserServerModel | UserServerModel[]) {
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 )
308 }
309
310 banUsers (usersArg: UserServerModel | UserServerModel[], reason?: string) {
311 const body = reason ? { reason } : {}
312 const users = Array.isArray(usersArg) ? usersArg : [ usersArg ]
313
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 )
320 }
321
322 unbanUsers (usersArg: UserServerModel | UserServerModel[]) {
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 )
331 }
332
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
345 private formatUser (user: UserServerModel) {
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 }
367 }