]>
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 { | |
5 | flushUserInfoFromLocalStorage, | |
6 | getUserInfoFromLocalStorage, | |
7 | saveUserInfoIntoLocalStorage, | |
8 | TokenOptions, | |
9 | Tokens | |
10 | } from '@root-helpers/users' | |
11 | import { hasUserRight } from '@shared/core-utils/users' | |
12 | import { | |
13 | MyUser as ServerMyUserModel, | |
14 | MyUserSpecialPlaylist, | |
15 | User as ServerUserModel, | |
16 | UserRight, | |
17 | UserRole, | |
18 | UserVideoQuota | |
19 | } from '@shared/models' | |
20 | ||
21 | export class AuthUser extends User implements ServerMyUserModel { | |
22 | tokens: Tokens | |
23 | specialPlaylists: MyUserSpecialPlaylist[] | |
24 | ||
25 | canSeeVideosLink = true | |
26 | ||
27 | static load () { | |
28 | const tokens = Tokens.load() | |
29 | if (!tokens) return null | |
30 | ||
31 | const userInfo = getUserInfoFromLocalStorage() | |
32 | if (!userInfo) return null | |
33 | ||
34 | return new AuthUser(userInfo, tokens) | |
35 | } | |
36 | ||
37 | static flush () { | |
38 | flushUserInfoFromLocalStorage() | |
39 | ||
40 | Tokens.flush() | |
41 | } | |
42 | ||
43 | constructor (userHash: Partial<ServerMyUserModel>, hashTokens: TokenOptions) { | |
44 | super(userHash) | |
45 | ||
46 | this.tokens = new Tokens(hashTokens) | |
47 | this.specialPlaylists = userHash.specialPlaylists | |
48 | } | |
49 | ||
50 | getAccessToken () { | |
51 | return this.tokens.accessToken | |
52 | } | |
53 | ||
54 | getRefreshToken () { | |
55 | return this.tokens.refreshToken | |
56 | } | |
57 | ||
58 | getTokenType () { | |
59 | return this.tokens.tokenType | |
60 | } | |
61 | ||
62 | refreshTokens (accessToken: string, refreshToken: string) { | |
63 | this.tokens.accessToken = accessToken | |
64 | this.tokens.refreshToken = refreshToken | |
65 | } | |
66 | ||
67 | hasRight (right: UserRight) { | |
68 | return hasUserRight(this.role, right) | |
69 | } | |
70 | ||
71 | canManage (user: ServerUserModel) { | |
72 | const myRole = this.role | |
73 | ||
74 | if (myRole === UserRole.ADMINISTRATOR) return true | |
75 | ||
76 | // I'm a moderator: I can only manage users | |
77 | return user.role === UserRole.USER | |
78 | } | |
79 | ||
80 | save () { | |
81 | saveUserInfoIntoLocalStorage({ | |
82 | id: this.id, | |
83 | username: this.username, | |
84 | email: this.email, | |
85 | role: this.role, | |
86 | nsfwPolicy: this.nsfwPolicy, | |
87 | webTorrentEnabled: this.webTorrentEnabled, | |
88 | autoPlayVideo: this.autoPlayVideo | |
89 | }) | |
90 | ||
91 | this.tokens.save() | |
92 | } | |
93 | ||
94 | computeCanSeeVideosLink (quotaObservable: Observable<UserVideoQuota>): Observable<boolean> { | |
95 | if (!this.isUploadDisabled()) { | |
96 | this.canSeeVideosLink = true | |
97 | return of(this.canSeeVideosLink) | |
98 | } | |
99 | ||
100 | // Check if the user has videos | |
101 | return quotaObservable.pipe( | |
102 | map(({ videoQuotaUsed }) => { | |
103 | if (videoQuotaUsed !== 0) { | |
104 | // User already uploaded videos, so it can see the link | |
105 | this.canSeeVideosLink = true | |
106 | } else { | |
107 | // No videos, no upload so the user don't need to see the videos link | |
108 | this.canSeeVideosLink = false | |
109 | } | |
110 | ||
111 | return this.canSeeVideosLink | |
112 | }) | |
113 | ) | |
114 | } | |
115 | } |