]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/core/auth/auth-user.model.ts
Add ability for admins to set default p2p policy
[github/Chocobozzz/PeerTube.git] / client / src / app / core / auth / auth-user.model.ts
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'
6 import {
7 MyUser as ServerMyUserModel,
8 MyUserSpecialPlaylist,
9 User as ServerUserModel,
10 UserRight,
11 UserRole,
12 UserVideoQuota
13 } from '@shared/models'
14
15 export class AuthUser extends User implements ServerMyUserModel {
16 tokens: UserTokens
17 specialPlaylists: MyUserSpecialPlaylist[]
18
19 canSeeVideosLink = true
20
21 constructor (userHash: Partial<ServerMyUserModel>, hashTokens: Partial<UserTokens>) {
22 super(userHash)
23
24 this.tokens = new UserTokens(hashTokens)
25 this.specialPlaylists = userHash.specialPlaylists
26 }
27
28 getAccessToken () {
29 return this.tokens.accessToken
30 }
31
32 getRefreshToken () {
33 return this.tokens.refreshToken
34 }
35
36 getTokenType () {
37 return this.tokens.tokenType
38 }
39
40 refreshTokens (accessToken: string, refreshToken: string) {
41 this.tokens.accessToken = accessToken
42 this.tokens.refreshToken = refreshToken
43 }
44
45 hasRight (right: UserRight) {
46 return hasUserRight(this.role, right)
47 }
48
49 canManage (user: ServerUserModel) {
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
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 }
79 }