]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/core/auth/auth-user.model.ts
Add alert and hide upload view when no upload is possible (#2966)
[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 { peertubeLocalStorage } from '@app/helpers/peertube-web-storage'
5 import {
6 hasUserRight,
7 MyUser as ServerMyUserModel,
8 MyUserSpecialPlaylist,
9 NSFWPolicyType,
10 User as ServerUserModel,
11 UserRight,
12 UserRole,
13 UserVideoQuota
14 } from '@shared/models'
15
16 export type TokenOptions = {
17 accessToken: string
18 refreshToken: string
19 tokenType: string
20 }
21
22 // Private class only used by User
23 class Tokens {
24 private static KEYS = {
25 ACCESS_TOKEN: 'access_token',
26 REFRESH_TOKEN: 'refresh_token',
27 TOKEN_TYPE: 'token_type'
28 }
29
30 accessToken: string
31 refreshToken: string
32 tokenType: string
33
34 static load () {
35 const accessTokenLocalStorage = peertubeLocalStorage.getItem(this.KEYS.ACCESS_TOKEN)
36 const refreshTokenLocalStorage = peertubeLocalStorage.getItem(this.KEYS.REFRESH_TOKEN)
37 const tokenTypeLocalStorage = peertubeLocalStorage.getItem(this.KEYS.TOKEN_TYPE)
38
39 if (accessTokenLocalStorage && refreshTokenLocalStorage && tokenTypeLocalStorage) {
40 return new Tokens({
41 accessToken: accessTokenLocalStorage,
42 refreshToken: refreshTokenLocalStorage,
43 tokenType: tokenTypeLocalStorage
44 })
45 }
46
47 return null
48 }
49
50 static flush () {
51 peertubeLocalStorage.removeItem(this.KEYS.ACCESS_TOKEN)
52 peertubeLocalStorage.removeItem(this.KEYS.REFRESH_TOKEN)
53 peertubeLocalStorage.removeItem(this.KEYS.TOKEN_TYPE)
54 }
55
56 constructor (hash?: TokenOptions) {
57 if (hash) {
58 this.accessToken = hash.accessToken
59 this.refreshToken = hash.refreshToken
60
61 if (hash.tokenType === 'bearer') {
62 this.tokenType = 'Bearer'
63 } else {
64 this.tokenType = hash.tokenType
65 }
66 }
67 }
68
69 save () {
70 peertubeLocalStorage.setItem(Tokens.KEYS.ACCESS_TOKEN, this.accessToken)
71 peertubeLocalStorage.setItem(Tokens.KEYS.REFRESH_TOKEN, this.refreshToken)
72 peertubeLocalStorage.setItem(Tokens.KEYS.TOKEN_TYPE, this.tokenType)
73 }
74 }
75
76 export class AuthUser extends User implements ServerMyUserModel {
77 tokens: Tokens
78 specialPlaylists: MyUserSpecialPlaylist[]
79
80 canSeeVideosLink = true
81
82 static load () {
83 const usernameLocalStorage = peertubeLocalStorage.getItem(this.KEYS.USERNAME)
84 if (usernameLocalStorage) {
85 return new AuthUser(
86 {
87 id: parseInt(peertubeLocalStorage.getItem(this.KEYS.ID), 10),
88 username: peertubeLocalStorage.getItem(this.KEYS.USERNAME),
89 email: peertubeLocalStorage.getItem(this.KEYS.EMAIL),
90 role: parseInt(peertubeLocalStorage.getItem(this.KEYS.ROLE), 10) as UserRole,
91 nsfwPolicy: peertubeLocalStorage.getItem(this.KEYS.NSFW_POLICY) as NSFWPolicyType,
92 webTorrentEnabled: peertubeLocalStorage.getItem(this.KEYS.WEBTORRENT_ENABLED) === 'true',
93 autoPlayVideo: peertubeLocalStorage.getItem(this.KEYS.AUTO_PLAY_VIDEO) === 'true',
94 videosHistoryEnabled: peertubeLocalStorage.getItem(this.KEYS.VIDEOS_HISTORY_ENABLED) === 'true'
95 },
96 Tokens.load()
97 )
98 }
99
100 return null
101 }
102
103 static flush () {
104 peertubeLocalStorage.removeItem(this.KEYS.USERNAME)
105 peertubeLocalStorage.removeItem(this.KEYS.ID)
106 peertubeLocalStorage.removeItem(this.KEYS.ROLE)
107 peertubeLocalStorage.removeItem(this.KEYS.EMAIL)
108 Tokens.flush()
109 }
110
111 constructor (userHash: Partial<ServerMyUserModel>, hashTokens: TokenOptions) {
112 super(userHash)
113
114 this.tokens = new Tokens(hashTokens)
115 this.specialPlaylists = userHash.specialPlaylists
116 }
117
118 getAccessToken () {
119 return this.tokens.accessToken
120 }
121
122 getRefreshToken () {
123 return this.tokens.refreshToken
124 }
125
126 getTokenType () {
127 return this.tokens.tokenType
128 }
129
130 refreshTokens (accessToken: string, refreshToken: string) {
131 this.tokens.accessToken = accessToken
132 this.tokens.refreshToken = refreshToken
133 }
134
135 hasRight (right: UserRight) {
136 return hasUserRight(this.role, right)
137 }
138
139 canManage (user: ServerUserModel) {
140 const myRole = this.role
141
142 if (myRole === UserRole.ADMINISTRATOR) return true
143
144 // I'm a moderator: I can only manage users
145 return user.role === UserRole.USER
146 }
147
148 save () {
149 peertubeLocalStorage.setItem(AuthUser.KEYS.ID, this.id.toString())
150 peertubeLocalStorage.setItem(AuthUser.KEYS.USERNAME, this.username)
151 peertubeLocalStorage.setItem(AuthUser.KEYS.EMAIL, this.email)
152 peertubeLocalStorage.setItem(AuthUser.KEYS.ROLE, this.role.toString())
153 peertubeLocalStorage.setItem(AuthUser.KEYS.NSFW_POLICY, this.nsfwPolicy.toString())
154 peertubeLocalStorage.setItem(AuthUser.KEYS.WEBTORRENT_ENABLED, JSON.stringify(this.webTorrentEnabled))
155 peertubeLocalStorage.setItem(AuthUser.KEYS.AUTO_PLAY_VIDEO, JSON.stringify(this.autoPlayVideo))
156 this.tokens.save()
157 }
158
159 computeCanSeeVideosLink (quotaObservable: Observable<UserVideoQuota>): Observable<boolean> {
160 if (!this.isUploadDisabled()) {
161 this.canSeeVideosLink = true
162 return of(this.canSeeVideosLink)
163 }
164
165 // Check if the user has videos
166 return quotaObservable.pipe(
167 map(({ videoQuotaUsed }) => {
168 if (videoQuotaUsed !== 0) {
169 // User already uploaded videos, so it can see the link
170 this.canSeeVideosLink = true
171 } else {
172 // No videos, no upload so the user don't need to see the videos link
173 this.canSeeVideosLink = false
174 }
175
176 return this.canSeeVideosLink
177 })
178 )
179 }
180 }