]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/core/auth/auth-user.model.ts
Reorganize shared models
[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'
bd45d503
C
4import { peertubeLocalStorage } from '@app/helpers/peertube-web-storage'
5import { hasUserRight } from '@shared/core-utils/users'
67ed6552 6import {
67ed6552
C
7 MyUser as ServerMyUserModel,
8 MyUserSpecialPlaylist,
9 NSFWPolicyType,
10 User as ServerUserModel,
11 UserRight,
dfe3f7b7
K
12 UserRole,
13 UserVideoQuota
67ed6552 14} from '@shared/models'
4504f09f 15import { TokenOptions, Tokens } from '../../../root-helpers/pure-auth-user.model'
7da18e44 16
b7819090 17export class AuthUser extends User implements ServerMyUserModel {
df98563e 18 tokens: Tokens
b7819090 19 specialPlaylists: MyUserSpecialPlaylist[]
bd5c83a8 20
dfe3f7b7
K
21 canSeeVideosLink = true
22
df98563e 23 static load () {
0bd78bf3 24 const usernameLocalStorage = peertubeLocalStorage.getItem(this.KEYS.USERNAME)
bd5c83a8 25 if (usernameLocalStorage) {
7da18e44
C
26 return new AuthUser(
27 {
0bd78bf3
C
28 id: parseInt(peertubeLocalStorage.getItem(this.KEYS.ID), 10),
29 username: peertubeLocalStorage.getItem(this.KEYS.USERNAME),
30 email: peertubeLocalStorage.getItem(this.KEYS.EMAIL),
31 role: parseInt(peertubeLocalStorage.getItem(this.KEYS.ROLE), 10) as UserRole,
2243730c 32 nsfwPolicy: peertubeLocalStorage.getItem(this.KEYS.NSFW_POLICY) as NSFWPolicyType,
ed638e53 33 webTorrentEnabled: peertubeLocalStorage.getItem(this.KEYS.WEBTORRENT_ENABLED) === 'true',
276d9652
C
34 autoPlayVideo: peertubeLocalStorage.getItem(this.KEYS.AUTO_PLAY_VIDEO) === 'true',
35 videosHistoryEnabled: peertubeLocalStorage.getItem(this.KEYS.VIDEOS_HISTORY_ENABLED) === 'true'
7da18e44 36 },
629d8d6f 37 Tokens.load()
df98563e 38 )
bd5c83a8
C
39 }
40
df98563e 41 return null
bd5c83a8
C
42 }
43
df98563e 44 static flush () {
0bd78bf3
C
45 peertubeLocalStorage.removeItem(this.KEYS.USERNAME)
46 peertubeLocalStorage.removeItem(this.KEYS.ID)
47 peertubeLocalStorage.removeItem(this.KEYS.ROLE)
0bd78bf3 48 peertubeLocalStorage.removeItem(this.KEYS.EMAIL)
df98563e 49 Tokens.flush()
bd5c83a8
C
50 }
51
b7819090 52 constructor (userHash: Partial<ServerMyUserModel>, hashTokens: TokenOptions) {
df98563e 53 super(userHash)
b7819090 54
df98563e 55 this.tokens = new Tokens(hashTokens)
b7819090 56 this.specialPlaylists = userHash.specialPlaylists
bd5c83a8
C
57 }
58
df98563e
C
59 getAccessToken () {
60 return this.tokens.accessToken
bd5c83a8 61 }
bd5c83a8 62
df98563e
C
63 getRefreshToken () {
64 return this.tokens.refreshToken
bd5c83a8
C
65 }
66
df98563e
C
67 getTokenType () {
68 return this.tokens.tokenType
bd5c83a8
C
69 }
70
df98563e
C
71 refreshTokens (accessToken: string, refreshToken: string) {
72 this.tokens.accessToken = accessToken
73 this.tokens.refreshToken = refreshToken
bd5c83a8
C
74 }
75
757f0da3 76 hasRight (right: UserRight) {
954605a8
C
77 return hasUserRight(this.role, right)
78 }
79
a6051ba7 80 canManage (user: ServerUserModel) {
a95a4cc8
C
81 const myRole = this.role
82
83 if (myRole === UserRole.ADMINISTRATOR) return true
84
85 // I'm a moderator: I can only manage users
86 return user.role === UserRole.USER
87 }
88
df98563e 89 save () {
0bd78bf3
C
90 peertubeLocalStorage.setItem(AuthUser.KEYS.ID, this.id.toString())
91 peertubeLocalStorage.setItem(AuthUser.KEYS.USERNAME, this.username)
92 peertubeLocalStorage.setItem(AuthUser.KEYS.EMAIL, this.email)
93 peertubeLocalStorage.setItem(AuthUser.KEYS.ROLE, this.role.toString())
2243730c 94 peertubeLocalStorage.setItem(AuthUser.KEYS.NSFW_POLICY, this.nsfwPolicy.toString())
ed638e53 95 peertubeLocalStorage.setItem(AuthUser.KEYS.WEBTORRENT_ENABLED, JSON.stringify(this.webTorrentEnabled))
0bd78bf3 96 peertubeLocalStorage.setItem(AuthUser.KEYS.AUTO_PLAY_VIDEO, JSON.stringify(this.autoPlayVideo))
df98563e 97 this.tokens.save()
bd5c83a8 98 }
dfe3f7b7
K
99
100 computeCanSeeVideosLink (quotaObservable: Observable<UserVideoQuota>): Observable<boolean> {
101 if (!this.isUploadDisabled()) {
102 this.canSeeVideosLink = true
103 return of(this.canSeeVideosLink)
104 }
105
106 // Check if the user has videos
107 return quotaObservable.pipe(
108 map(({ videoQuotaUsed }) => {
109 if (videoQuotaUsed !== 0) {
110 // User already uploaded videos, so it can see the link
111 this.canSeeVideosLink = true
112 } else {
113 // No videos, no upload so the user don't need to see the videos link
114 this.canSeeVideosLink = false
115 }
116
117 return this.canSeeVideosLink
118 })
119 )
120 }
bd5c83a8 121}