]>
Commit | Line | Data |
---|---|---|
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 { hasUserRight } from '@shared/core-utils/users' | |
6 | import { | |
7 | MyUser as ServerMyUserModel, | |
8 | MyUserSpecialPlaylist, | |
9 | NSFWPolicyType, | |
10 | User as ServerUserModel, | |
11 | UserRight, | |
12 | UserRole, | |
13 | UserVideoQuota | |
14 | } from '@shared/models' | |
15 | import { TokenOptions, Tokens } from '../../../root-helpers/pure-auth-user.model' | |
16 | ||
17 | export class AuthUser extends User implements ServerMyUserModel { | |
18 | tokens: Tokens | |
19 | specialPlaylists: MyUserSpecialPlaylist[] | |
20 | ||
21 | canSeeVideosLink = true | |
22 | ||
23 | static load () { | |
24 | const usernameLocalStorage = peertubeLocalStorage.getItem(this.KEYS.USERNAME) | |
25 | if (usernameLocalStorage) { | |
26 | return new AuthUser( | |
27 | { | |
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, | |
32 | nsfwPolicy: peertubeLocalStorage.getItem(this.KEYS.NSFW_POLICY) as NSFWPolicyType, | |
33 | webTorrentEnabled: peertubeLocalStorage.getItem(this.KEYS.WEBTORRENT_ENABLED) === 'true', | |
34 | autoPlayVideo: peertubeLocalStorage.getItem(this.KEYS.AUTO_PLAY_VIDEO) === 'true', | |
35 | videosHistoryEnabled: peertubeLocalStorage.getItem(this.KEYS.VIDEOS_HISTORY_ENABLED) === 'true' | |
36 | }, | |
37 | Tokens.load() | |
38 | ) | |
39 | } | |
40 | ||
41 | return null | |
42 | } | |
43 | ||
44 | static flush () { | |
45 | peertubeLocalStorage.removeItem(this.KEYS.USERNAME) | |
46 | peertubeLocalStorage.removeItem(this.KEYS.ID) | |
47 | peertubeLocalStorage.removeItem(this.KEYS.ROLE) | |
48 | peertubeLocalStorage.removeItem(this.KEYS.EMAIL) | |
49 | Tokens.flush() | |
50 | } | |
51 | ||
52 | constructor (userHash: Partial<ServerMyUserModel>, hashTokens: TokenOptions) { | |
53 | super(userHash) | |
54 | ||
55 | this.tokens = new Tokens(hashTokens) | |
56 | this.specialPlaylists = userHash.specialPlaylists | |
57 | } | |
58 | ||
59 | getAccessToken () { | |
60 | return this.tokens.accessToken | |
61 | } | |
62 | ||
63 | getRefreshToken () { | |
64 | return this.tokens.refreshToken | |
65 | } | |
66 | ||
67 | getTokenType () { | |
68 | return this.tokens.tokenType | |
69 | } | |
70 | ||
71 | refreshTokens (accessToken: string, refreshToken: string) { | |
72 | this.tokens.accessToken = accessToken | |
73 | this.tokens.refreshToken = refreshToken | |
74 | } | |
75 | ||
76 | hasRight (right: UserRight) { | |
77 | return hasUserRight(this.role, right) | |
78 | } | |
79 | ||
80 | canManage (user: ServerUserModel) { | |
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 | ||
89 | save () { | |
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()) | |
94 | peertubeLocalStorage.setItem(AuthUser.KEYS.NSFW_POLICY, this.nsfwPolicy.toString()) | |
95 | peertubeLocalStorage.setItem(AuthUser.KEYS.WEBTORRENT_ENABLED, JSON.stringify(this.webTorrentEnabled)) | |
96 | peertubeLocalStorage.setItem(AuthUser.KEYS.AUTO_PLAY_VIDEO, JSON.stringify(this.autoPlayVideo)) | |
97 | this.tokens.save() | |
98 | } | |
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 | } | |
121 | } |