1 import { Observable, of } from 'rxjs'
2 import { map } from 'rxjs/operators'
3 import { User } from '@app/core/users/user.model'
5 flushUserInfoFromLocalStorage,
6 getUserInfoFromLocalStorage,
7 saveUserInfoIntoLocalStorage,
10 } from '@root-helpers/users'
11 import { hasUserRight } from '@shared/core-utils/users'
13 MyUser as ServerMyUserModel,
14 MyUserSpecialPlaylist,
15 User as ServerUserModel,
19 } from '@shared/models'
21 export class AuthUser extends User implements ServerMyUserModel {
23 specialPlaylists: MyUserSpecialPlaylist[]
25 canSeeVideosLink = true
28 const userInfo = getUserInfoFromLocalStorage()
30 if (!userInfo) return null
32 return new AuthUser(userInfo, Tokens.load())
36 flushUserInfoFromLocalStorage()
41 constructor (userHash: Partial<ServerMyUserModel>, hashTokens: TokenOptions) {
44 this.tokens = new Tokens(hashTokens)
45 this.specialPlaylists = userHash.specialPlaylists
49 return this.tokens.accessToken
53 return this.tokens.refreshToken
57 return this.tokens.tokenType
60 refreshTokens (accessToken: string, refreshToken: string) {
61 this.tokens.accessToken = accessToken
62 this.tokens.refreshToken = refreshToken
65 hasRight (right: UserRight) {
66 return hasUserRight(this.role, right)
69 canManage (user: ServerUserModel) {
70 const myRole = this.role
72 if (myRole === UserRole.ADMINISTRATOR) return true
74 // I'm a moderator: I can only manage users
75 return user.role === UserRole.USER
79 saveUserInfoIntoLocalStorage({
81 username: this.username,
84 nsfwPolicy: this.nsfwPolicy,
85 webTorrentEnabled: this.webTorrentEnabled,
86 autoPlayVideo: this.autoPlayVideo
92 computeCanSeeVideosLink (quotaObservable: Observable<UserVideoQuota>): Observable<boolean> {
93 if (!this.isUploadDisabled()) {
94 this.canSeeVideosLink = true
95 return of(this.canSeeVideosLink)
98 // Check if the user has videos
99 return quotaObservable.pipe(
100 map(({ videoQuotaUsed }) => {
101 if (videoQuotaUsed !== 0) {
102 // User already uploaded videos, so it can see the link
103 this.canSeeVideosLink = true
105 // No videos, no upload so the user don't need to see the videos link
106 this.canSeeVideosLink = false
109 return this.canSeeVideosLink