aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared/users/user.service.ts
diff options
context:
space:
mode:
Diffstat (limited to 'client/src/app/shared/users/user.service.ts')
-rw-r--r--client/src/app/shared/users/user.service.ts82
1 files changed, 69 insertions, 13 deletions
diff --git a/client/src/app/shared/users/user.service.ts b/client/src/app/shared/users/user.service.ts
index e24d91df3..abb4088b5 100644
--- a/client/src/app/shared/users/user.service.ts
+++ b/client/src/app/shared/users/user.service.ts
@@ -1,8 +1,8 @@
1import { from, Observable, of } from 'rxjs' 1import { from, Observable } from 'rxjs'
2import { catchError, concatMap, map, share, shareReplay, tap, toArray } from 'rxjs/operators' 2import { catchError, concatMap, map, shareReplay, toArray } from 'rxjs/operators'
3import { HttpClient, HttpParams } from '@angular/common/http' 3import { HttpClient, HttpParams } from '@angular/common/http'
4import { Injectable } from '@angular/core' 4import { Injectable } from '@angular/core'
5import { ResultList, User, UserCreate, UserRole, UserUpdate, UserUpdateMe, UserVideoQuota } from '../../../../../shared' 5import { ResultList, User as UserServerModel, UserCreate, UserRole, UserUpdate, UserUpdateMe, UserVideoQuota } from '../../../../../shared'
6import { environment } from '../../../environments/environment' 6import { environment } from '../../../environments/environment'
7import { RestExtractor, RestPagination, RestService } from '../rest' 7import { RestExtractor, RestPagination, RestService } from '../rest'
8import { Avatar } from '../../../../../shared/models/avatars/avatar.model' 8import { Avatar } from '../../../../../shared/models/avatars/avatar.model'
@@ -10,6 +10,10 @@ import { SortMeta } from 'primeng/api'
10import { BytesPipe } from 'ngx-pipes' 10import { BytesPipe } from 'ngx-pipes'
11import { I18n } from '@ngx-translate/i18n-polyfill' 11import { I18n } from '@ngx-translate/i18n-polyfill'
12import { UserRegister } from '@shared/models/users/user-register.model' 12import { UserRegister } from '@shared/models/users/user-register.model'
13import { User } from './user.model'
14import { NSFWPolicyType } from '@shared/models/videos/nsfw-policy.type'
15import { has } from 'lodash-es'
16import { LocalStorageService, SessionStorageService } from '../misc/storage.service'
13 17
14@Injectable() 18@Injectable()
15export class UserService { 19export class UserService {
@@ -17,12 +21,14 @@ export class UserService {
17 21
18 private bytesPipe = new BytesPipe() 22 private bytesPipe = new BytesPipe()
19 23
20 private userCache: { [ id: number ]: Observable<User> } = {} 24 private userCache: { [ id: number ]: Observable<UserServerModel> } = {}
21 25
22 constructor ( 26 constructor (
23 private authHttp: HttpClient, 27 private authHttp: HttpClient,
24 private restExtractor: RestExtractor, 28 private restExtractor: RestExtractor,
25 private restService: RestService, 29 private restService: RestService,
30 private localStorageService: LocalStorageService,
31 private sessionStorageService: SessionStorageService,
26 private i18n: I18n 32 private i18n: I18n
27 ) { } 33 ) { }
28 34
@@ -64,6 +70,30 @@ export class UserService {
64 ) 70 )
65 } 71 }
66 72
73 updateMyAnonymousProfile (profile: UserUpdateMe) {
74 const supportedKeys = {
75 // local storage keys
76 nsfwPolicy: (val: NSFWPolicyType) => this.localStorageService.setItem(User.KEYS.NSFW_POLICY, val),
77 webTorrentEnabled: (val: boolean) => this.localStorageService.setItem(User.KEYS.WEBTORRENT_ENABLED, String(val)),
78 autoPlayVideo: (val: boolean) => this.localStorageService.setItem(User.KEYS.AUTO_PLAY_VIDEO, String(val)),
79 autoPlayNextVideoPlaylist: (val: boolean) => this.localStorageService.setItem(User.KEYS.AUTO_PLAY_VIDEO_PLAYLIST, String(val)),
80 theme: (val: string) => this.localStorageService.setItem(User.KEYS.THEME, val),
81 videoLanguages: (val: string[]) => this.localStorageService.setItem(User.KEYS.VIDEO_LANGUAGES, JSON.stringify(val)),
82
83 // session storage keys
84 autoPlayNextVideo: (val: boolean) =>
85 this.sessionStorageService.setItem(User.KEYS.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO, String(val))
86 }
87
88 for (const key of Object.keys(profile)) {
89 try {
90 if (has(supportedKeys, key)) supportedKeys[key](profile[key])
91 } catch (err) {
92 console.error(`Cannot set item ${key} in localStorage. Likely due to a value impossible to stringify.`, err)
93 }
94 }
95 }
96
67 deleteMe () { 97 deleteMe () {
68 const url = UserService.BASE_USERS_URL + 'me' 98 const url = UserService.BASE_USERS_URL + 'me'
69 99
@@ -187,7 +217,7 @@ export class UserService {
187 ) 217 )
188 } 218 }
189 219
190 updateUsers (users: User[], userUpdate: UserUpdate) { 220 updateUsers (users: UserServerModel[], userUpdate: UserUpdate) {
191 return from(users) 221 return from(users)
192 .pipe( 222 .pipe(
193 concatMap(u => this.authHttp.put(UserService.BASE_USERS_URL + u.id, userUpdate)), 223 concatMap(u => this.authHttp.put(UserService.BASE_USERS_URL + u.id, userUpdate)),
@@ -204,18 +234,44 @@ export class UserService {
204 return this.userCache[userId] 234 return this.userCache[userId]
205 } 235 }
206 236
207 getUser (userId: number) { 237 getUser (userId: number, withStats = false) {
208 return this.authHttp.get<User>(UserService.BASE_USERS_URL + userId) 238 const params = new HttpParams().append('withStats', withStats + '')
239 return this.authHttp.get<UserServerModel>(UserService.BASE_USERS_URL + userId, { params })
209 .pipe(catchError(err => this.restExtractor.handleError(err))) 240 .pipe(catchError(err => this.restExtractor.handleError(err)))
210 } 241 }
211 242
212 getUsers (pagination: RestPagination, sort: SortMeta, search?: string): Observable<ResultList<User>> { 243 getAnonymousUser () {
244 let videoLanguages
245
246 try {
247 videoLanguages = JSON.parse(this.localStorageService.getItem(User.KEYS.VIDEO_LANGUAGES))
248 } catch (err) {
249 videoLanguages = null
250 console.error('Cannot parse desired video languages from localStorage.', err)
251 }
252
253 return new User({
254 // local storage keys
255 nsfwPolicy: this.localStorageService.getItem(User.KEYS.NSFW_POLICY) as NSFWPolicyType,
256 webTorrentEnabled: this.localStorageService.getItem(User.KEYS.WEBTORRENT_ENABLED) !== 'false',
257 theme: this.localStorageService.getItem(User.KEYS.THEME) || 'default',
258 videoLanguages,
259
260 autoPlayNextVideoPlaylist: this.localStorageService.getItem(User.KEYS.AUTO_PLAY_VIDEO_PLAYLIST) !== 'false',
261 autoPlayVideo: this.localStorageService.getItem(User.KEYS.AUTO_PLAY_VIDEO) === 'true',
262
263 // session storage keys
264 autoPlayNextVideo: this.sessionStorageService.getItem(User.KEYS.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO) === 'true'
265 })
266 }
267
268 getUsers (pagination: RestPagination, sort: SortMeta, search?: string): Observable<ResultList<UserServerModel>> {
213 let params = new HttpParams() 269 let params = new HttpParams()
214 params = this.restService.addRestGetParams(params, pagination, sort) 270 params = this.restService.addRestGetParams(params, pagination, sort)
215 271
216 if (search) params = params.append('search', search) 272 if (search) params = params.append('search', search)
217 273
218 return this.authHttp.get<ResultList<User>>(UserService.BASE_USERS_URL, { params }) 274 return this.authHttp.get<ResultList<UserServerModel>>(UserService.BASE_USERS_URL, { params })
219 .pipe( 275 .pipe(
220 map(res => this.restExtractor.convertResultListDateToHuman(res)), 276 map(res => this.restExtractor.convertResultListDateToHuman(res)),
221 map(res => this.restExtractor.applyToResultListData(res, this.formatUser.bind(this))), 277 map(res => this.restExtractor.applyToResultListData(res, this.formatUser.bind(this))),
@@ -223,7 +279,7 @@ export class UserService {
223 ) 279 )
224 } 280 }
225 281
226 removeUser (usersArg: User | User[]) { 282 removeUser (usersArg: UserServerModel | UserServerModel[]) {
227 const users = Array.isArray(usersArg) ? usersArg : [ usersArg ] 283 const users = Array.isArray(usersArg) ? usersArg : [ usersArg ]
228 284
229 return from(users) 285 return from(users)
@@ -234,7 +290,7 @@ export class UserService {
234 ) 290 )
235 } 291 }
236 292
237 banUsers (usersArg: User | User[], reason?: string) { 293 banUsers (usersArg: UserServerModel | UserServerModel[], reason?: string) {
238 const body = reason ? { reason } : {} 294 const body = reason ? { reason } : {}
239 const users = Array.isArray(usersArg) ? usersArg : [ usersArg ] 295 const users = Array.isArray(usersArg) ? usersArg : [ usersArg ]
240 296
@@ -246,7 +302,7 @@ export class UserService {
246 ) 302 )
247 } 303 }
248 304
249 unbanUsers (usersArg: User | User[]) { 305 unbanUsers (usersArg: UserServerModel | UserServerModel[]) {
250 const users = Array.isArray(usersArg) ? usersArg : [ usersArg ] 306 const users = Array.isArray(usersArg) ? usersArg : [ usersArg ]
251 307
252 return from(users) 308 return from(users)
@@ -257,7 +313,7 @@ export class UserService {
257 ) 313 )
258 } 314 }
259 315
260 private formatUser (user: User) { 316 private formatUser (user: UserServerModel) {
261 let videoQuota 317 let videoQuota
262 if (user.videoQuota === -1) { 318 if (user.videoQuota === -1) {
263 videoQuota = this.i18n('Unlimited') 319 videoQuota = this.i18n('Unlimited')