]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/core/auth/auth-user.model.ts
Fix socket io lazy loading
[github/Chocobozzz/PeerTube.git] / client / src / app / core / auth / auth-user.model.ts
CommitLineData
dfe3f7b7
K
1import { Observable, of } from 'rxjs'
2import { map } from 'rxjs/operators'
67ed6552 3import { User } from '@app/core/users/user.model'
a9bfa85d 4import { UserTokens } from '@root-helpers/users'
bd45d503 5import { hasUserRight } from '@shared/core-utils/users'
67ed6552 6import {
67ed6552
C
7 MyUser as ServerMyUserModel,
8 MyUserSpecialPlaylist,
67ed6552
C
9 User as ServerUserModel,
10 UserRight,
dfe3f7b7
K
11 UserRole,
12 UserVideoQuota
67ed6552 13} from '@shared/models'
7da18e44 14
b7819090 15export class AuthUser extends User implements ServerMyUserModel {
a9bfa85d 16 tokens: UserTokens
b7819090 17 specialPlaylists: MyUserSpecialPlaylist[]
bd5c83a8 18
dfe3f7b7
K
19 canSeeVideosLink = true
20
a9bfa85d 21 constructor (userHash: Partial<ServerMyUserModel>, hashTokens: Partial<UserTokens>) {
df98563e 22 super(userHash)
b7819090 23
a9bfa85d 24 this.tokens = new UserTokens(hashTokens)
b7819090 25 this.specialPlaylists = userHash.specialPlaylists
bd5c83a8
C
26 }
27
df98563e
C
28 getAccessToken () {
29 return this.tokens.accessToken
bd5c83a8 30 }
bd5c83a8 31
df98563e
C
32 getRefreshToken () {
33 return this.tokens.refreshToken
bd5c83a8
C
34 }
35
df98563e
C
36 getTokenType () {
37 return this.tokens.tokenType
bd5c83a8
C
38 }
39
df98563e
C
40 refreshTokens (accessToken: string, refreshToken: string) {
41 this.tokens.accessToken = accessToken
42 this.tokens.refreshToken = refreshToken
bd5c83a8
C
43 }
44
757f0da3 45 hasRight (right: UserRight) {
954605a8
C
46 return hasUserRight(this.role, right)
47 }
48
a6051ba7 49 canManage (user: ServerUserModel) {
a95a4cc8
C
50 const myRole = this.role
51
52 if (myRole === UserRole.ADMINISTRATOR) return true
53
54 // I'm a moderator: I can only manage users
55 return user.role === UserRole.USER
56 }
57
dfe3f7b7
K
58 computeCanSeeVideosLink (quotaObservable: Observable<UserVideoQuota>): Observable<boolean> {
59 if (!this.isUploadDisabled()) {
60 this.canSeeVideosLink = true
61 return of(this.canSeeVideosLink)
62 }
63
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
70 } else {
71 // No videos, no upload so the user don't need to see the videos link
72 this.canSeeVideosLink = false
73 }
74
75 return this.canSeeVideosLink
76 })
77 )
78 }
bd5c83a8 79}