]>
Commit | Line | Data |
---|---|---|
e02643f3 | 1 | import * as Sequelize from 'sequelize' |
3fd3ab2d | 2 | import { |
a73c582e C |
3 | AllowNull, |
4 | BeforeCreate, | |
5 | BeforeUpdate, | |
6 | Column, | |
7 | CreatedAt, | |
8 | DataType, | |
9 | Default, | |
10 | DefaultScope, | |
11 | HasMany, | |
12 | HasOne, | |
13 | Is, | |
14 | IsEmail, | |
15 | Model, | |
16 | Scopes, | |
17 | Table, | |
18 | UpdatedAt | |
3fd3ab2d | 19 | } from 'sequelize-typescript' |
e34c85e5 | 20 | import { hasUserRight, USER_ROLE_LABELS, UserRight } from '../../../shared' |
ba75d268 | 21 | import { User, UserRole } from '../../../shared/models/users' |
65fcc311 | 22 | import { |
a73c582e | 23 | isUserAutoPlayVideoValid, |
0883b324 | 24 | isUserNSFWPolicyValid, |
a73c582e C |
25 | isUserPasswordValid, |
26 | isUserRoleValid, | |
27 | isUserUsernameValid, | |
50d6de9c | 28 | isUserVideoQuotaValid |
3fd3ab2d | 29 | } from '../../helpers/custom-validators/users' |
da854ddd | 30 | import { comparePassword, cryptPassword } from '../../helpers/peertube-crypto' |
3fd3ab2d C |
31 | import { OAuthTokenModel } from '../oauth/oauth-token' |
32 | import { getSort, throwIfNotValid } from '../utils' | |
33 | import { VideoChannelModel } from '../video/video-channel' | |
34 | import { AccountModel } from './account' | |
0883b324 C |
35 | import { NSFWPolicyType } from '../../../shared/models/videos/nsfw-policy.type' |
36 | import { values } from 'lodash' | |
37 | import { NSFW_POLICY_TYPES } from '../../initializers' | |
3fd3ab2d | 38 | |
d48ff09d C |
39 | @DefaultScope({ |
40 | include: [ | |
41 | { | |
42 | model: () => AccountModel, | |
43 | required: true | |
44 | } | |
45 | ] | |
46 | }) | |
47 | @Scopes({ | |
48 | withVideoChannel: { | |
49 | include: [ | |
50 | { | |
51 | model: () => AccountModel, | |
52 | required: true, | |
53 | include: [ () => VideoChannelModel ] | |
54 | } | |
55 | ] | |
56 | } | |
57 | }) | |
3fd3ab2d C |
58 | @Table({ |
59 | tableName: 'user', | |
60 | indexes: [ | |
feb4bdfd | 61 | { |
3fd3ab2d C |
62 | fields: [ 'username' ], |
63 | unique: true | |
feb4bdfd C |
64 | }, |
65 | { | |
3fd3ab2d C |
66 | fields: [ 'email' ], |
67 | unique: true | |
feb4bdfd | 68 | } |
e02643f3 | 69 | ] |
3fd3ab2d C |
70 | }) |
71 | export class UserModel extends Model<UserModel> { | |
72 | ||
73 | @AllowNull(false) | |
74 | @Is('UserPassword', value => throwIfNotValid(value, isUserPasswordValid, 'user password')) | |
75 | @Column | |
76 | password: string | |
77 | ||
78 | @AllowNull(false) | |
79 | @Is('UserPassword', value => throwIfNotValid(value, isUserUsernameValid, 'user name')) | |
80 | @Column | |
81 | username: string | |
82 | ||
83 | @AllowNull(false) | |
84 | @IsEmail | |
85 | @Column(DataType.STRING(400)) | |
86 | email: string | |
87 | ||
88 | @AllowNull(false) | |
0883b324 C |
89 | @Is('UserNSFWPolicy', value => throwIfNotValid(value, isUserNSFWPolicyValid, 'NSFW policy')) |
90 | @Column(DataType.ENUM(values(NSFW_POLICY_TYPES))) | |
91 | nsfwPolicy: NSFWPolicyType | |
3fd3ab2d | 92 | |
7efe153b AL |
93 | @AllowNull(false) |
94 | @Default(true) | |
95 | @Is('UserAutoPlayVideo', value => throwIfNotValid(value, isUserAutoPlayVideoValid, 'auto play video boolean')) | |
96 | @Column | |
97 | autoPlayVideo: boolean | |
98 | ||
3fd3ab2d C |
99 | @AllowNull(false) |
100 | @Is('UserRole', value => throwIfNotValid(value, isUserRoleValid, 'role')) | |
101 | @Column | |
102 | role: number | |
103 | ||
104 | @AllowNull(false) | |
105 | @Is('UserVideoQuota', value => throwIfNotValid(value, isUserVideoQuotaValid, 'video quota')) | |
106 | @Column(DataType.BIGINT) | |
107 | videoQuota: number | |
108 | ||
109 | @CreatedAt | |
110 | createdAt: Date | |
111 | ||
112 | @UpdatedAt | |
113 | updatedAt: Date | |
114 | ||
115 | @HasOne(() => AccountModel, { | |
116 | foreignKey: 'userId', | |
f05a1c30 C |
117 | onDelete: 'cascade', |
118 | hooks: true | |
3fd3ab2d C |
119 | }) |
120 | Account: AccountModel | |
69b0a27c | 121 | |
3fd3ab2d C |
122 | @HasMany(() => OAuthTokenModel, { |
123 | foreignKey: 'userId', | |
124 | onDelete: 'cascade' | |
125 | }) | |
126 | OAuthTokens: OAuthTokenModel[] | |
127 | ||
128 | @BeforeCreate | |
129 | @BeforeUpdate | |
130 | static cryptPasswordIfNeeded (instance: UserModel) { | |
131 | if (instance.changed('password')) { | |
132 | return cryptPassword(instance.password) | |
133 | .then(hash => { | |
134 | instance.password = hash | |
135 | return undefined | |
136 | }) | |
137 | } | |
59557c46 | 138 | } |
26d7d31b | 139 | |
3fd3ab2d C |
140 | static countTotal () { |
141 | return this.count() | |
142 | } | |
954605a8 | 143 | |
3fd3ab2d C |
144 | static listForApi (start: number, count: number, sort: string) { |
145 | const query = { | |
146 | offset: start, | |
147 | limit: count, | |
6ff9c676 | 148 | order: getSort(sort) |
3fd3ab2d | 149 | } |
72c7248b | 150 | |
3fd3ab2d C |
151 | return UserModel.findAndCountAll(query) |
152 | .then(({ rows, count }) => { | |
153 | return { | |
154 | data: rows, | |
155 | total: count | |
156 | } | |
72c7248b | 157 | }) |
72c7248b C |
158 | } |
159 | ||
ba75d268 C |
160 | static listEmailsWithRight (right: UserRight) { |
161 | const roles = Object.keys(USER_ROLE_LABELS) | |
162 | .map(k => parseInt(k, 10) as UserRole) | |
163 | .filter(role => hasUserRight(role, right)) | |
164 | ||
165 | console.log(roles) | |
166 | ||
167 | const query = { | |
168 | attribute: [ 'email' ], | |
169 | where: { | |
170 | role: { | |
171 | [Sequelize.Op.in]: roles | |
172 | } | |
173 | } | |
174 | } | |
175 | ||
176 | return UserModel.unscoped() | |
177 | .findAll(query) | |
178 | .then(u => u.map(u => u.email)) | |
179 | } | |
180 | ||
3fd3ab2d | 181 | static loadById (id: number) { |
d48ff09d | 182 | return UserModel.findById(id) |
3fd3ab2d | 183 | } |
feb4bdfd | 184 | |
3fd3ab2d C |
185 | static loadByUsername (username: string) { |
186 | const query = { | |
187 | where: { | |
188 | username | |
d48ff09d | 189 | } |
3fd3ab2d | 190 | } |
089ff2f2 | 191 | |
3fd3ab2d | 192 | return UserModel.findOne(query) |
feb4bdfd C |
193 | } |
194 | ||
3fd3ab2d C |
195 | static loadByUsernameAndPopulateChannels (username: string) { |
196 | const query = { | |
197 | where: { | |
198 | username | |
d48ff09d | 199 | } |
3fd3ab2d | 200 | } |
9bd26629 | 201 | |
d48ff09d | 202 | return UserModel.scope('withVideoChannel').findOne(query) |
feb4bdfd C |
203 | } |
204 | ||
ecb4e35f C |
205 | static loadByEmail (email: string) { |
206 | const query = { | |
207 | where: { | |
208 | ||
209 | } | |
210 | } | |
211 | ||
212 | return UserModel.findOne(query) | |
213 | } | |
214 | ||
ba12e8b3 C |
215 | static loadByUsernameOrEmail (username: string, email?: string) { |
216 | if (!email) email = username | |
217 | ||
3fd3ab2d | 218 | const query = { |
3fd3ab2d C |
219 | where: { |
220 | [ Sequelize.Op.or ]: [ { username }, { email } ] | |
221 | } | |
6fcd19ba | 222 | } |
69b0a27c | 223 | |
d48ff09d | 224 | return UserModel.findOne(query) |
72c7248b C |
225 | } |
226 | ||
ce5496d6 | 227 | static getOriginalVideoFileTotalFromUser (user: UserModel) { |
3fd3ab2d C |
228 | // Don't use sequelize because we need to use a sub query |
229 | const query = 'SELECT SUM("size") AS "total" FROM ' + | |
230 | '(SELECT MAX("videoFile"."size") AS "size" FROM "videoFile" ' + | |
231 | 'INNER JOIN "video" ON "videoFile"."videoId" = "video"."id" ' + | |
232 | 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' + | |
233 | 'INNER JOIN "account" ON "videoChannel"."accountId" = "account"."id" ' + | |
234 | 'INNER JOIN "user" ON "account"."userId" = "user"."id" ' + | |
235 | 'WHERE "user"."id" = $userId GROUP BY "video"."id") t' | |
236 | ||
237 | const options = { | |
238 | bind: { userId: user.id }, | |
239 | type: Sequelize.QueryTypes.SELECT | |
240 | } | |
241 | return UserModel.sequelize.query(query, options) | |
242 | .then(([ { total } ]) => { | |
243 | if (total === null) return 0 | |
68a3b9f2 | 244 | |
3fd3ab2d C |
245 | return parseInt(total, 10) |
246 | }) | |
72c7248b C |
247 | } |
248 | ||
09cababd C |
249 | static async getStats () { |
250 | const totalUsers = await UserModel.count() | |
251 | ||
252 | return { | |
253 | totalUsers | |
254 | } | |
255 | } | |
256 | ||
3fd3ab2d C |
257 | hasRight (right: UserRight) { |
258 | return hasUserRight(this.role, right) | |
259 | } | |
72c7248b | 260 | |
3fd3ab2d C |
261 | isPasswordMatch (password: string) { |
262 | return comparePassword(password, this.password) | |
feb4bdfd C |
263 | } |
264 | ||
c5911fd3 | 265 | toFormattedJSON (): User { |
3fd3ab2d C |
266 | const json = { |
267 | id: this.id, | |
268 | username: this.username, | |
269 | email: this.email, | |
0883b324 | 270 | nsfwPolicy: this.nsfwPolicy, |
7efe153b | 271 | autoPlayVideo: this.autoPlayVideo, |
3fd3ab2d C |
272 | role: this.role, |
273 | roleLabel: USER_ROLE_LABELS[ this.role ], | |
274 | videoQuota: this.videoQuota, | |
275 | createdAt: this.createdAt, | |
c5911fd3 C |
276 | account: this.Account.toFormattedJSON(), |
277 | videoChannels: [] | |
3fd3ab2d C |
278 | } |
279 | ||
280 | if (Array.isArray(this.Account.VideoChannels) === true) { | |
c5911fd3 | 281 | json.videoChannels = this.Account.VideoChannels |
3fd3ab2d C |
282 | .map(c => c.toFormattedJSON()) |
283 | .sort((v1, v2) => { | |
284 | if (v1.createdAt < v2.createdAt) return -1 | |
285 | if (v1.createdAt === v2.createdAt) return 0 | |
ad4a8a1c | 286 | |
3fd3ab2d C |
287 | return 1 |
288 | }) | |
ad4a8a1c | 289 | } |
3fd3ab2d C |
290 | |
291 | return json | |
ad4a8a1c C |
292 | } |
293 | ||
3fd3ab2d C |
294 | isAbleToUploadVideo (videoFile: Express.Multer.File) { |
295 | if (this.videoQuota === -1) return Promise.resolve(true) | |
b0f9f39e | 296 | |
3fd3ab2d C |
297 | return UserModel.getOriginalVideoFileTotalFromUser(this) |
298 | .then(totalBytes => { | |
299 | return (videoFile.size + totalBytes) < this.videoQuota | |
300 | }) | |
b0f9f39e | 301 | } |
b0f9f39e | 302 | } |