1 import { Observable, of } from 'rxjs'
2 import { map } from 'rxjs/operators'
3 import { User } from '@app/core/users/user.model'
4 import { UserTokens } from '@root-helpers/users'
5 import { hasUserRight } from '@shared/core-utils/users'
7 MyUser as ServerMyUserModel,
9 User as ServerUserModel,
13 } from '@shared/models'
15 export class AuthUser extends User implements ServerMyUserModel {
17 specialPlaylists: MyUserSpecialPlaylist[]
19 canSeeVideosLink = true
21 constructor (userHash: Partial<ServerMyUserModel>, hashTokens: Partial<UserTokens>) {
24 this.tokens = new UserTokens(hashTokens)
25 this.specialPlaylists = userHash.specialPlaylists
29 return this.tokens.accessToken
33 return this.tokens.refreshToken
37 return this.tokens.tokenType
40 refreshTokens (accessToken: string, refreshToken: string) {
41 this.tokens.accessToken = accessToken
42 this.tokens.refreshToken = refreshToken
45 hasRight (right: UserRight) {
46 return hasUserRight(this.role, right)
49 canManage (user: ServerUserModel) {
50 const myRole = this.role
52 if (myRole === UserRole.ADMINISTRATOR) return true
54 // I'm a moderator: I can only manage users
55 return user.role === UserRole.USER
58 computeCanSeeVideosLink (quotaObservable: Observable<UserVideoQuota>): Observable<boolean> {
59 if (!this.isUploadDisabled()) {
60 this.canSeeVideosLink = true
61 return of(this.canSeeVideosLink)
64 // Check if the user has videos
65 return quotaObservable.pipe(
66 map(({ videoQuotaUsed }) => {
67 if (videoQuotaUsed !== 0) {
68 // User already uploaded videos, so it can see the link
69 this.canSeeVideosLink = true
71 // No videos, no upload so the user don't need to see the videos link
72 this.canSeeVideosLink = false
75 return this.canSeeVideosLink