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