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'
20 export class UserService {
21 static BASE_USERS_URL = environment.apiUrl + '/api/v1/users/'
23 private bytesPipe = new BytesPipe()
25 private userCache: { [ id: number ]: Observable<UserServerModel> } = {}
28 private authHttp: HttpClient,
29 private authService: AuthService,
30 private restExtractor: RestExtractor,
31 private restService: RestService,
32 private localStorageService: LocalStorageService,
33 private sessionStorageService: SessionStorageService,
37 changePassword (currentPassword: string, newPassword: string) {
38 const url = UserService.BASE_USERS_URL + 'me'
39 const body: UserUpdateMe = {
44 return this.authHttp.put(url, body)
46 map(this.restExtractor.extractDataBool),
47 catchError(err => this.restExtractor.handleError(err))
51 changeEmail (password: string, newEmail: string) {
52 const url = UserService.BASE_USERS_URL + 'me'
53 const body: UserUpdateMe = {
54 currentPassword: password,
58 return this.authHttp.put(url, body)
60 map(this.restExtractor.extractDataBool),
61 catchError(err => this.restExtractor.handleError(err))
65 updateMyProfile (profile: UserUpdateMe) {
66 const url = UserService.BASE_USERS_URL + 'me'
68 return this.authHttp.put(url, profile)
70 map(this.restExtractor.extractDataBool),
71 catchError(err => this.restExtractor.handleError(err))
75 updateMyAnonymousProfile (profile: UserUpdateMe) {
76 const supportedKeys = {
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)),
85 // session storage keys
86 autoPlayNextVideo: (val: boolean) =>
87 this.sessionStorageService.setItem(User.KEYS.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO, String(val))
90 for (const key of Object.keys(profile)) {
92 if (has(supportedKeys, key)) supportedKeys[key](profile[key])
94 console.error(`Cannot set item ${key} in localStorage. Likely due to a value impossible to stringify.`, err)
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,
106 User.KEYS.VIDEO_LANGUAGES
109 filter(() => this.authService.isLoggedIn() !== true),
110 map(() => this.getAnonymousUser())
115 const url = UserService.BASE_USERS_URL + 'me'
117 return this.authHttp.delete(url)
119 map(this.restExtractor.extractDataBool),
120 catchError(err => this.restExtractor.handleError(err))
124 changeAvatar (avatarForm: FormData) {
125 const url = UserService.BASE_USERS_URL + 'me/avatar/pick'
127 return this.authHttp.post<{ avatar: Avatar }>(url, avatarForm)
128 .pipe(catchError(err => this.restExtractor.handleError(err)))
131 signup (userCreate: UserRegister) {
132 return this.authHttp.post(UserService.BASE_USERS_URL + 'register', userCreate)
134 map(this.restExtractor.extractDataBool),
135 catchError(err => this.restExtractor.handleError(err))
139 getMyVideoQuotaUsed () {
140 const url = UserService.BASE_USERS_URL + 'me/video-quota-used'
142 return this.authHttp.get<UserVideoQuota>(url)
143 .pipe(catchError(err => this.restExtractor.handleError(err)))
146 askResetPassword (email: string) {
147 const url = UserService.BASE_USERS_URL + '/ask-reset-password'
149 return this.authHttp.post(url, { email })
151 map(this.restExtractor.extractDataBool),
152 catchError(err => this.restExtractor.handleError(err))
156 resetPassword (userId: number, verificationString: string, password: string) {
157 const url = `${UserService.BASE_USERS_URL}/${userId}/reset-password`
163 return this.authHttp.post(url, body)
165 map(this.restExtractor.extractDataBool),
166 catchError(res => this.restExtractor.handleError(res))
170 verifyEmail (userId: number, verificationString: string, isPendingEmail: boolean) {
171 const url = `${UserService.BASE_USERS_URL}/${userId}/verify-email`
177 return this.authHttp.post(url, body)
179 map(this.restExtractor.extractDataBool),
180 catchError(res => this.restExtractor.handleError(res))
184 askSendVerifyEmail (email: string) {
185 const url = UserService.BASE_USERS_URL + '/ask-send-verify-email'
187 return this.authHttp.post(url, { email })
189 map(this.restExtractor.extractDataBool),
190 catchError(err => this.restExtractor.handleError(err))
194 autocomplete (search: string): Observable<string[]> {
195 const url = UserService.BASE_USERS_URL + 'autocomplete'
196 const params = new HttpParams().append('search', search)
199 .get<string[]>(url, { params })
200 .pipe(catchError(res => this.restExtractor.handleError(res)))
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
207 return this.displayNameToUsername(newDisplayName)
210 displayNameToUsername (displayName: string) {
211 if (!displayName) return ''
216 .replace(/[^a-z0-9_.]/g, '')
219 /* ###### Admin methods ###### */
221 addUser (userCreate: UserCreate) {
222 return this.authHttp.post(UserService.BASE_USERS_URL, userCreate)
224 map(this.restExtractor.extractDataBool),
225 catchError(err => this.restExtractor.handleError(err))
229 updateUser (userId: number, userUpdate: UserUpdate) {
230 return this.authHttp.put(UserService.BASE_USERS_URL + userId, userUpdate)
232 map(this.restExtractor.extractDataBool),
233 catchError(err => this.restExtractor.handleError(err))
237 updateUsers (users: UserServerModel[], userUpdate: UserUpdate) {
240 concatMap(u => this.authHttp.put(UserService.BASE_USERS_URL + u.id, userUpdate)),
242 catchError(err => this.restExtractor.handleError(err))
246 getUserWithCache (userId: number) {
247 if (!this.userCache[userId]) {
248 this.userCache[ userId ] = this.getUser(userId).pipe(shareReplay())
251 return this.userCache[userId]
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)))
260 getAnonymousUser () {
261 let videoLanguages: string[]
264 videoLanguages = JSON.parse(this.localStorageService.getItem(User.KEYS.VIDEO_LANGUAGES))
266 videoLanguages = null
267 console.error('Cannot parse desired video languages from localStorage.', err)
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',
277 autoPlayNextVideoPlaylist: this.localStorageService.getItem(User.KEYS.AUTO_PLAY_VIDEO_PLAYLIST) !== 'false',
278 autoPlayVideo: this.localStorageService.getItem(User.KEYS.AUTO_PLAY_VIDEO) === 'true',
280 // session storage keys
281 autoPlayNextVideo: this.sessionStorageService.getItem(User.KEYS.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO) === 'true'
285 getUsers (pagination: RestPagination, sort: SortMeta, search?: string): Observable<ResultList<UserServerModel>> {
286 let params = new HttpParams()
287 params = this.restService.addRestGetParams(params, pagination, sort)
289 if (search) params = params.append('search', search)
291 return this.authHttp.get<ResultList<UserServerModel>>(UserService.BASE_USERS_URL, { params })
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))
299 removeUser (usersArg: UserServerModel | UserServerModel[]) {
300 const users = Array.isArray(usersArg) ? usersArg : [ usersArg ]
304 concatMap(u => this.authHttp.delete(UserService.BASE_USERS_URL + u.id)),
306 catchError(err => this.restExtractor.handleError(err))
310 banUsers (usersArg: UserServerModel | UserServerModel[], reason?: string) {
311 const body = reason ? { reason } : {}
312 const users = Array.isArray(usersArg) ? usersArg : [ usersArg ]
316 concatMap(u => this.authHttp.post(UserService.BASE_USERS_URL + u.id + '/block', body)),
318 catchError(err => this.restExtractor.handleError(err))
322 unbanUsers (usersArg: UserServerModel | UserServerModel[]) {
323 const users = Array.isArray(usersArg) ? usersArg : [ usersArg ]
327 concatMap(u => this.authHttp.post(UserService.BASE_USERS_URL + u.id + '/unblock', {})),
329 catchError(err => this.restExtractor.handleError(err))
333 getAnonymousOrLoggedUser () {
334 if (!this.authService.isLoggedIn()) {
335 return of(this.getAnonymousUser())
338 return this.authService.userInformationLoaded
341 map(() => this.authService.getUser())
345 private formatUser (user: UserServerModel) {
347 if (user.videoQuota === -1) {
348 videoQuota = this.i18n('Unlimited')
350 videoQuota = this.bytesPipe.transform(user.videoQuota, 0)
353 const videoQuotaUsed = this.bytesPipe.transform(user.videoQuotaUsed, 0)
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')
361 return Object.assign(user, {
362 roleLabel: roleLabels[user.role],