]>
Commit | Line | Data |
---|---|---|
1 | import { Account } from '@app/shared/shared-main/account/account.model' | |
2 | import { objectKeysTyped } from '@shared/core-utils' | |
3 | import { hasUserRight } from '@shared/core-utils/users' | |
4 | import { | |
5 | ActorImage, | |
6 | HTMLServerConfig, | |
7 | NSFWPolicyType, | |
8 | User as UserServerModel, | |
9 | UserAdminFlag, | |
10 | UserNotificationSetting, | |
11 | UserRight, | |
12 | UserRole, | |
13 | VideoChannel | |
14 | } from '@shared/models' | |
15 | ||
16 | export class User implements UserServerModel { | |
17 | id: number | |
18 | username: string | |
19 | email: string | |
20 | pendingEmail: string | null | |
21 | ||
22 | emailVerified: boolean | |
23 | emailPublic: boolean | |
24 | nsfwPolicy: NSFWPolicyType | |
25 | ||
26 | adminFlags?: UserAdminFlag | |
27 | ||
28 | autoPlayVideo: boolean | |
29 | autoPlayNextVideo: boolean | |
30 | autoPlayNextVideoPlaylist: boolean | |
31 | ||
32 | p2pEnabled: boolean | |
33 | // FIXME: deprecated in 4.1 | |
34 | webTorrentEnabled: never | |
35 | ||
36 | videosHistoryEnabled: boolean | |
37 | videoLanguages: string[] | |
38 | ||
39 | role: { | |
40 | id: UserRole | |
41 | label: string | |
42 | } | |
43 | ||
44 | videoQuota: number | |
45 | videoQuotaDaily: number | |
46 | videoQuotaUsed?: number | |
47 | videoQuotaUsedDaily?: number | |
48 | ||
49 | videosCount?: number | |
50 | videoCommentsCount?: number | |
51 | ||
52 | abusesCount?: number | |
53 | abusesAcceptedCount?: number | |
54 | abusesCreatedCount?: number | |
55 | ||
56 | theme: string | |
57 | ||
58 | account: Account | |
59 | notificationSettings?: UserNotificationSetting | |
60 | videoChannels?: VideoChannel[] | |
61 | ||
62 | blocked: boolean | |
63 | blockedReason?: string | |
64 | ||
65 | noInstanceConfigWarningModal: boolean | |
66 | noWelcomeModal: boolean | |
67 | noAccountSetupWarningModal: boolean | |
68 | ||
69 | pluginAuth: string | null | |
70 | ||
71 | lastLoginDate: Date | null | |
72 | ||
73 | twoFactorEnabled: boolean | |
74 | ||
75 | createdAt: Date | |
76 | ||
77 | constructor (hash: Partial<UserServerModel>) { | |
78 | this.id = hash.id | |
79 | this.username = hash.username | |
80 | this.email = hash.email | |
81 | ||
82 | this.role = hash.role | |
83 | ||
84 | this.videoChannels = hash.videoChannels | |
85 | ||
86 | this.videoQuota = hash.videoQuota | |
87 | this.videoQuotaDaily = hash.videoQuotaDaily | |
88 | this.videoQuotaUsed = hash.videoQuotaUsed | |
89 | this.videoQuotaUsedDaily = hash.videoQuotaUsedDaily | |
90 | this.videosCount = hash.videosCount | |
91 | this.abusesCount = hash.abusesCount | |
92 | this.abusesAcceptedCount = hash.abusesAcceptedCount | |
93 | this.abusesCreatedCount = hash.abusesCreatedCount | |
94 | this.videoCommentsCount = hash.videoCommentsCount | |
95 | ||
96 | this.nsfwPolicy = hash.nsfwPolicy | |
97 | this.p2pEnabled = hash.p2pEnabled | |
98 | this.autoPlayVideo = hash.autoPlayVideo | |
99 | this.autoPlayNextVideo = hash.autoPlayNextVideo | |
100 | this.autoPlayNextVideoPlaylist = hash.autoPlayNextVideoPlaylist | |
101 | this.videosHistoryEnabled = hash.videosHistoryEnabled | |
102 | this.videoLanguages = hash.videoLanguages | |
103 | ||
104 | this.theme = hash.theme | |
105 | ||
106 | this.adminFlags = hash.adminFlags | |
107 | ||
108 | this.blocked = hash.blocked | |
109 | this.blockedReason = hash.blockedReason | |
110 | ||
111 | this.noInstanceConfigWarningModal = hash.noInstanceConfigWarningModal | |
112 | this.noWelcomeModal = hash.noWelcomeModal | |
113 | this.noAccountSetupWarningModal = hash.noAccountSetupWarningModal | |
114 | ||
115 | this.notificationSettings = hash.notificationSettings | |
116 | ||
117 | this.twoFactorEnabled = hash.twoFactorEnabled | |
118 | ||
119 | this.createdAt = hash.createdAt | |
120 | ||
121 | this.pluginAuth = hash.pluginAuth | |
122 | this.lastLoginDate = hash.lastLoginDate | |
123 | ||
124 | if (hash.account !== undefined) { | |
125 | this.account = new Account(hash.account) | |
126 | } | |
127 | } | |
128 | ||
129 | hasRight (right: UserRight) { | |
130 | return hasUserRight(this.role.id, right) | |
131 | } | |
132 | ||
133 | patch (obj: UserServerModel) { | |
134 | for (const key of objectKeysTyped(obj)) { | |
135 | // FIXME: typings | |
136 | (this as any)[key] = obj[key] | |
137 | } | |
138 | ||
139 | if (obj.account !== undefined) { | |
140 | this.account = new Account(obj.account) | |
141 | } | |
142 | } | |
143 | ||
144 | updateAccountAvatar (newAccountAvatars?: ActorImage[]) { | |
145 | if (newAccountAvatars) this.account.updateAvatar(newAccountAvatars) | |
146 | else this.account.resetAvatar() | |
147 | } | |
148 | ||
149 | isUploadDisabled () { | |
150 | return this.videoQuota === 0 || this.videoQuotaDaily === 0 | |
151 | } | |
152 | ||
153 | isAutoBlocked (serverConfig: HTMLServerConfig) { | |
154 | if (serverConfig.autoBlacklist.videos.ofUsers.enabled !== true) return false | |
155 | ||
156 | return this.role.id === UserRole.USER && this.adminFlags !== UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST | |
157 | } | |
158 | } |