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'
7 MyUser as ServerMyUserModel,
10 User as ServerUserModel,
14 } from '@shared/models'
16 export type TokenOptions = {
22 // Private class only used by User
24 private static KEYS = {
25 ACCESS_TOKEN: 'access_token',
26 REFRESH_TOKEN: 'refresh_token',
27 TOKEN_TYPE: 'token_type'
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)
39 if (accessTokenLocalStorage && refreshTokenLocalStorage && tokenTypeLocalStorage) {
41 accessToken: accessTokenLocalStorage,
42 refreshToken: refreshTokenLocalStorage,
43 tokenType: tokenTypeLocalStorage
51 peertubeLocalStorage.removeItem(this.KEYS.ACCESS_TOKEN)
52 peertubeLocalStorage.removeItem(this.KEYS.REFRESH_TOKEN)
53 peertubeLocalStorage.removeItem(this.KEYS.TOKEN_TYPE)
56 constructor (hash?: TokenOptions) {
58 this.accessToken = hash.accessToken
59 this.refreshToken = hash.refreshToken
61 if (hash.tokenType === 'bearer') {
62 this.tokenType = 'Bearer'
64 this.tokenType = hash.tokenType
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)
76 export class AuthUser extends User implements ServerMyUserModel {
78 specialPlaylists: MyUserSpecialPlaylist[]
80 canSeeVideosLink = true
83 const usernameLocalStorage = peertubeLocalStorage.getItem(this.KEYS.USERNAME)
84 if (usernameLocalStorage) {
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'
104 peertubeLocalStorage.removeItem(this.KEYS.USERNAME)
105 peertubeLocalStorage.removeItem(this.KEYS.ID)
106 peertubeLocalStorage.removeItem(this.KEYS.ROLE)
107 peertubeLocalStorage.removeItem(this.KEYS.EMAIL)
111 constructor (userHash: Partial<ServerMyUserModel>, hashTokens: TokenOptions) {
114 this.tokens = new Tokens(hashTokens)
115 this.specialPlaylists = userHash.specialPlaylists
119 return this.tokens.accessToken
123 return this.tokens.refreshToken
127 return this.tokens.tokenType
130 refreshTokens (accessToken: string, refreshToken: string) {
131 this.tokens.accessToken = accessToken
132 this.tokens.refreshToken = refreshToken
135 hasRight (right: UserRight) {
136 return hasUserRight(this.role, right)
139 canManage (user: ServerUserModel) {
140 const myRole = this.role
142 if (myRole === UserRole.ADMINISTRATOR) return true
144 // I'm a moderator: I can only manage users
145 return user.role === UserRole.USER
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))
159 computeCanSeeVideosLink (quotaObservable: Observable<UserVideoQuota>): Observable<boolean> {
160 if (!this.isUploadDisabled()) {
161 this.canSeeVideosLink = true
162 return of(this.canSeeVideosLink)
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
172 // No videos, no upload so the user don't need to see the videos link
173 this.canSeeVideosLink = false
176 return this.canSeeVideosLink