]>
Commit | Line | Data |
---|---|---|
c4a1811e | 1 | import { FindOptions, literal, Op, QueryTypes, where, fn, col } from 'sequelize' |
3fd3ab2d | 2 | import { |
d175a6f7 | 3 | AfterDestroy, |
f201a749 | 4 | AfterUpdate, |
a73c582e C |
5 | AllowNull, |
6 | BeforeCreate, | |
7 | BeforeUpdate, | |
8 | Column, | |
9 | CreatedAt, | |
10 | DataType, | |
11 | Default, | |
12 | DefaultScope, | |
13 | HasMany, | |
14 | HasOne, | |
15 | Is, | |
16 | IsEmail, | |
17 | Model, | |
18 | Scopes, | |
19 | Table, | |
20 | UpdatedAt | |
3fd3ab2d | 21 | } from 'sequelize-typescript' |
ac0868bc | 22 | import { hasUserRight, MyUser, USER_ROLE_LABELS, UserRight, VideoPlaylistType, VideoPrivacy } from '../../../shared' |
ba75d268 | 23 | import { User, UserRole } from '../../../shared/models/users' |
65fcc311 | 24 | import { |
43d0ea7f | 25 | isNoInstanceConfigWarningModal, |
ac0868bc | 26 | isNoWelcomeModal, |
1eddc9a7 | 27 | isUserAdminFlagsValid, |
bee29df8 | 28 | isUserAutoPlayNextVideoPlaylistValid, |
ac0868bc C |
29 | isUserAutoPlayNextVideoValid, |
30 | isUserAutoPlayVideoValid, | |
eacb25c4 | 31 | isUserBlockedReasonValid, |
e6921918 | 32 | isUserBlockedValid, |
d9eaee39 | 33 | isUserEmailVerifiedValid, |
5cf84858 | 34 | isUserNSFWPolicyValid, |
a73c582e C |
35 | isUserPasswordValid, |
36 | isUserRoleValid, | |
37 | isUserUsernameValid, | |
3caf77d3 | 38 | isUserVideoLanguages, |
5cf84858 | 39 | isUserVideoQuotaDailyValid, |
64cc5e85 | 40 | isUserVideoQuotaValid, |
cef534ed | 41 | isUserVideosHistoryEnabledValid, |
ac0868bc | 42 | isUserWebTorrentEnabledValid |
3fd3ab2d | 43 | } from '../../helpers/custom-validators/users' |
da854ddd | 44 | import { comparePassword, cryptPassword } from '../../helpers/peertube-crypto' |
3fd3ab2d C |
45 | import { OAuthTokenModel } from '../oauth/oauth-token' |
46 | import { getSort, throwIfNotValid } from '../utils' | |
47 | import { VideoChannelModel } from '../video/video-channel' | |
29128b2f | 48 | import { VideoPlaylistModel } from '../video/video-playlist' |
3fd3ab2d | 49 | import { AccountModel } from './account' |
0883b324 C |
50 | import { NSFWPolicyType } from '../../../shared/models/videos/nsfw-policy.type' |
51 | import { values } from 'lodash' | |
ffb321be | 52 | import { DEFAULT_THEME_NAME, DEFAULT_USER_THEME_NAME, NSFW_POLICY_TYPES } from '../../initializers/constants' |
f201a749 | 53 | import { clearCacheByUserId } from '../../lib/oauth-model' |
cef534ed C |
54 | import { UserNotificationSettingModel } from './user-notification-setting' |
55 | import { VideoModel } from '../video/video' | |
56 | import { ActorModel } from '../activitypub/actor' | |
57 | import { ActorFollowModel } from '../activitypub/actor-follow' | |
dc133480 | 58 | import { VideoImportModel } from '../video/video-import' |
1eddc9a7 | 59 | import { UserAdminFlag } from '../../../shared/models/users/user-flag.model' |
503c6f44 | 60 | import { isThemeNameValid } from '../../helpers/custom-validators/plugins' |
7cd4d2ba | 61 | import { getThemeOrDefault } from '../../lib/plugins/theme-utils' |
453e83ea | 62 | import * as Bluebird from 'bluebird' |
1ca9f7c3 | 63 | import { |
ac0868bc | 64 | MMyUserFormattable, |
1ca9f7c3 C |
65 | MUserDefault, |
66 | MUserFormattable, | |
67 | MUserId, | |
68 | MUserNotifSettingChannelDefault, | |
ac0868bc C |
69 | MUserWithNotificationSetting, |
70 | MVideoFullLight | |
1ca9f7c3 | 71 | } from '@server/typings/models' |
3fd3ab2d | 72 | |
9c2e0dbf | 73 | enum ScopeNames { |
ac0868bc | 74 | FOR_ME_API = 'FOR_ME_API' |
9c2e0dbf C |
75 | } |
76 | ||
3acc5084 | 77 | @DefaultScope(() => ({ |
d48ff09d C |
78 | include: [ |
79 | { | |
3acc5084 | 80 | model: AccountModel, |
d48ff09d | 81 | required: true |
cef534ed C |
82 | }, |
83 | { | |
3acc5084 | 84 | model: UserNotificationSettingModel, |
cef534ed | 85 | required: true |
d48ff09d C |
86 | } |
87 | ] | |
3acc5084 C |
88 | })) |
89 | @Scopes(() => ({ | |
ac0868bc | 90 | [ScopeNames.FOR_ME_API]: { |
d48ff09d C |
91 | include: [ |
92 | { | |
3acc5084 | 93 | model: AccountModel, |
ac0868bc C |
94 | include: [ |
95 | { | |
96 | model: VideoChannelModel | |
97 | }, | |
98 | { | |
99 | attributes: [ 'id', 'name', 'type' ], | |
100 | model: VideoPlaylistModel.unscoped(), | |
101 | required: true, | |
102 | where: { | |
103 | type: { | |
104 | [ Op.ne ]: VideoPlaylistType.REGULAR | |
105 | } | |
106 | } | |
107 | } | |
108 | ] | |
cef534ed C |
109 | }, |
110 | { | |
3acc5084 | 111 | model: UserNotificationSettingModel, |
cef534ed | 112 | required: true |
d48ff09d | 113 | } |
3acc5084 | 114 | ] |
d48ff09d | 115 | } |
3acc5084 | 116 | })) |
3fd3ab2d C |
117 | @Table({ |
118 | tableName: 'user', | |
119 | indexes: [ | |
feb4bdfd | 120 | { |
3fd3ab2d C |
121 | fields: [ 'username' ], |
122 | unique: true | |
feb4bdfd C |
123 | }, |
124 | { | |
3fd3ab2d C |
125 | fields: [ 'email' ], |
126 | unique: true | |
feb4bdfd | 127 | } |
e02643f3 | 128 | ] |
3fd3ab2d C |
129 | }) |
130 | export class UserModel extends Model<UserModel> { | |
131 | ||
132 | @AllowNull(false) | |
133 | @Is('UserPassword', value => throwIfNotValid(value, isUserPasswordValid, 'user password')) | |
134 | @Column | |
135 | password: string | |
136 | ||
137 | @AllowNull(false) | |
138 | @Is('UserPassword', value => throwIfNotValid(value, isUserUsernameValid, 'user name')) | |
139 | @Column | |
140 | username: string | |
141 | ||
142 | @AllowNull(false) | |
143 | @IsEmail | |
144 | @Column(DataType.STRING(400)) | |
145 | email: string | |
146 | ||
d1ab89de C |
147 | @AllowNull(true) |
148 | @IsEmail | |
149 | @Column(DataType.STRING(400)) | |
150 | pendingEmail: string | |
151 | ||
d9eaee39 JM |
152 | @AllowNull(true) |
153 | @Default(null) | |
1735c825 | 154 | @Is('UserEmailVerified', value => throwIfNotValid(value, isUserEmailVerifiedValid, 'email verified boolean', true)) |
d9eaee39 JM |
155 | @Column |
156 | emailVerified: boolean | |
157 | ||
3fd3ab2d | 158 | @AllowNull(false) |
0883b324 | 159 | @Is('UserNSFWPolicy', value => throwIfNotValid(value, isUserNSFWPolicyValid, 'NSFW policy')) |
1735c825 | 160 | @Column(DataType.ENUM(...values(NSFW_POLICY_TYPES))) |
0883b324 | 161 | nsfwPolicy: NSFWPolicyType |
3fd3ab2d | 162 | |
64cc5e85 | 163 | @AllowNull(false) |
0229b014 | 164 | @Default(true) |
ed638e53 RK |
165 | @Is('UserWebTorrentEnabled', value => throwIfNotValid(value, isUserWebTorrentEnabledValid, 'WebTorrent enabled')) |
166 | @Column | |
167 | webTorrentEnabled: boolean | |
64cc5e85 | 168 | |
8b9a525a C |
169 | @AllowNull(false) |
170 | @Default(true) | |
171 | @Is('UserVideosHistoryEnabled', value => throwIfNotValid(value, isUserVideosHistoryEnabledValid, 'Videos history enabled')) | |
172 | @Column | |
173 | videosHistoryEnabled: boolean | |
174 | ||
7efe153b AL |
175 | @AllowNull(false) |
176 | @Default(true) | |
177 | @Is('UserAutoPlayVideo', value => throwIfNotValid(value, isUserAutoPlayVideoValid, 'auto play video boolean')) | |
178 | @Column | |
179 | autoPlayVideo: boolean | |
180 | ||
6aa54148 L |
181 | @AllowNull(false) |
182 | @Default(false) | |
183 | @Is('UserAutoPlayNextVideo', value => throwIfNotValid(value, isUserAutoPlayNextVideoValid, 'auto play next video boolean')) | |
184 | @Column | |
185 | autoPlayNextVideo: boolean | |
186 | ||
bee29df8 RK |
187 | @AllowNull(false) |
188 | @Default(true) | |
189 | @Is('UserAutoPlayNextVideoPlaylist', value => throwIfNotValid(value, isUserAutoPlayNextVideoPlaylistValid, 'auto play next video for playlists boolean')) | |
190 | @Column | |
191 | autoPlayNextVideoPlaylist: boolean | |
192 | ||
3caf77d3 C |
193 | @AllowNull(true) |
194 | @Default(null) | |
195 | @Is('UserVideoLanguages', value => throwIfNotValid(value, isUserVideoLanguages, 'video languages')) | |
196 | @Column(DataType.ARRAY(DataType.STRING)) | |
197 | videoLanguages: string[] | |
198 | ||
1eddc9a7 C |
199 | @AllowNull(false) |
200 | @Default(UserAdminFlag.NONE) | |
201 | @Is('UserAdminFlags', value => throwIfNotValid(value, isUserAdminFlagsValid, 'user admin flags')) | |
202 | @Column | |
203 | adminFlags?: UserAdminFlag | |
204 | ||
e6921918 C |
205 | @AllowNull(false) |
206 | @Default(false) | |
207 | @Is('UserBlocked', value => throwIfNotValid(value, isUserBlockedValid, 'blocked boolean')) | |
208 | @Column | |
209 | blocked: boolean | |
210 | ||
eacb25c4 C |
211 | @AllowNull(true) |
212 | @Default(null) | |
1735c825 | 213 | @Is('UserBlockedReason', value => throwIfNotValid(value, isUserBlockedReasonValid, 'blocked reason', true)) |
eacb25c4 C |
214 | @Column |
215 | blockedReason: string | |
216 | ||
3fd3ab2d C |
217 | @AllowNull(false) |
218 | @Is('UserRole', value => throwIfNotValid(value, isUserRoleValid, 'role')) | |
219 | @Column | |
220 | role: number | |
221 | ||
222 | @AllowNull(false) | |
223 | @Is('UserVideoQuota', value => throwIfNotValid(value, isUserVideoQuotaValid, 'video quota')) | |
224 | @Column(DataType.BIGINT) | |
225 | videoQuota: number | |
226 | ||
bee0abff FA |
227 | @AllowNull(false) |
228 | @Is('UserVideoQuotaDaily', value => throwIfNotValid(value, isUserVideoQuotaDailyValid, 'video quota daily')) | |
229 | @Column(DataType.BIGINT) | |
230 | videoQuotaDaily: number | |
231 | ||
7cd4d2ba | 232 | @AllowNull(false) |
ffb321be | 233 | @Default(DEFAULT_THEME_NAME) |
503c6f44 | 234 | @Is('UserTheme', value => throwIfNotValid(value, isThemeNameValid, 'theme')) |
7cd4d2ba C |
235 | @Column |
236 | theme: string | |
237 | ||
43d0ea7f C |
238 | @AllowNull(false) |
239 | @Default(false) | |
240 | @Is( | |
241 | 'UserNoInstanceConfigWarningModal', | |
242 | value => throwIfNotValid(value, isNoInstanceConfigWarningModal, 'no instance config warning modal') | |
243 | ) | |
244 | @Column | |
245 | noInstanceConfigWarningModal: boolean | |
246 | ||
247 | @AllowNull(false) | |
248 | @Default(false) | |
249 | @Is( | |
250 | 'UserNoInstanceConfigWarningModal', | |
251 | value => throwIfNotValid(value, isNoWelcomeModal, 'no welcome modal') | |
252 | ) | |
253 | @Column | |
254 | noWelcomeModal: boolean | |
255 | ||
3fd3ab2d C |
256 | @CreatedAt |
257 | createdAt: Date | |
258 | ||
259 | @UpdatedAt | |
260 | updatedAt: Date | |
261 | ||
262 | @HasOne(() => AccountModel, { | |
263 | foreignKey: 'userId', | |
f05a1c30 C |
264 | onDelete: 'cascade', |
265 | hooks: true | |
3fd3ab2d C |
266 | }) |
267 | Account: AccountModel | |
69b0a27c | 268 | |
cef534ed C |
269 | @HasOne(() => UserNotificationSettingModel, { |
270 | foreignKey: 'userId', | |
271 | onDelete: 'cascade', | |
272 | hooks: true | |
273 | }) | |
274 | NotificationSetting: UserNotificationSettingModel | |
275 | ||
dc133480 C |
276 | @HasMany(() => VideoImportModel, { |
277 | foreignKey: 'userId', | |
278 | onDelete: 'cascade' | |
279 | }) | |
280 | VideoImports: VideoImportModel[] | |
281 | ||
3fd3ab2d C |
282 | @HasMany(() => OAuthTokenModel, { |
283 | foreignKey: 'userId', | |
284 | onDelete: 'cascade' | |
285 | }) | |
286 | OAuthTokens: OAuthTokenModel[] | |
287 | ||
288 | @BeforeCreate | |
289 | @BeforeUpdate | |
290 | static cryptPasswordIfNeeded (instance: UserModel) { | |
291 | if (instance.changed('password')) { | |
292 | return cryptPassword(instance.password) | |
293 | .then(hash => { | |
294 | instance.password = hash | |
295 | return undefined | |
296 | }) | |
297 | } | |
59557c46 | 298 | } |
26d7d31b | 299 | |
f201a749 | 300 | @AfterUpdate |
d175a6f7 | 301 | @AfterDestroy |
f201a749 C |
302 | static removeTokenCache (instance: UserModel) { |
303 | return clearCacheByUserId(instance.id) | |
304 | } | |
305 | ||
3fd3ab2d C |
306 | static countTotal () { |
307 | return this.count() | |
308 | } | |
954605a8 | 309 | |
24b9417c C |
310 | static listForApi (start: number, count: number, sort: string, search?: string) { |
311 | let where = undefined | |
312 | if (search) { | |
313 | where = { | |
3acc5084 | 314 | [Op.or]: [ |
24b9417c C |
315 | { |
316 | email: { | |
3acc5084 | 317 | [Op.iLike]: '%' + search + '%' |
24b9417c C |
318 | } |
319 | }, | |
320 | { | |
321 | username: { | |
3acc5084 | 322 | [ Op.iLike ]: '%' + search + '%' |
24b9417c C |
323 | } |
324 | } | |
325 | ] | |
326 | } | |
327 | } | |
328 | ||
3acc5084 | 329 | const query: FindOptions = { |
a76138ff C |
330 | attributes: { |
331 | include: [ | |
332 | [ | |
3acc5084 | 333 | literal( |
a76138ff | 334 | '(' + |
8b604880 C |
335 | 'SELECT COALESCE(SUM("size"), 0) ' + |
336 | 'FROM (' + | |
a76138ff C |
337 | 'SELECT MAX("videoFile"."size") AS "size" FROM "videoFile" ' + |
338 | 'INNER JOIN "video" ON "videoFile"."videoId" = "video"."id" ' + | |
339 | 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' + | |
340 | 'INNER JOIN "account" ON "videoChannel"."accountId" = "account"."id" ' + | |
341 | 'WHERE "account"."userId" = "UserModel"."id" GROUP BY "video"."id"' + | |
342 | ') t' + | |
343 | ')' | |
344 | ), | |
345 | 'videoQuotaUsed' | |
3acc5084 | 346 | ] |
a76138ff C |
347 | ] |
348 | }, | |
3fd3ab2d C |
349 | offset: start, |
350 | limit: count, | |
24b9417c C |
351 | order: getSort(sort), |
352 | where | |
3fd3ab2d | 353 | } |
72c7248b | 354 | |
3fd3ab2d C |
355 | return UserModel.findAndCountAll(query) |
356 | .then(({ rows, count }) => { | |
357 | return { | |
358 | data: rows, | |
359 | total: count | |
360 | } | |
72c7248b | 361 | }) |
72c7248b C |
362 | } |
363 | ||
453e83ea | 364 | static listWithRight (right: UserRight): Bluebird<MUserDefault[]> { |
ba75d268 C |
365 | const roles = Object.keys(USER_ROLE_LABELS) |
366 | .map(k => parseInt(k, 10) as UserRole) | |
367 | .filter(role => hasUserRight(role, right)) | |
368 | ||
ba75d268 | 369 | const query = { |
ba75d268 C |
370 | where: { |
371 | role: { | |
3acc5084 | 372 | [Op.in]: roles |
ba75d268 C |
373 | } |
374 | } | |
375 | } | |
376 | ||
cef534ed C |
377 | return UserModel.findAll(query) |
378 | } | |
379 | ||
453e83ea | 380 | static listUserSubscribersOf (actorId: number): Bluebird<MUserWithNotificationSetting[]> { |
cef534ed C |
381 | const query = { |
382 | include: [ | |
383 | { | |
384 | model: UserNotificationSettingModel.unscoped(), | |
385 | required: true | |
386 | }, | |
387 | { | |
388 | attributes: [ 'userId' ], | |
389 | model: AccountModel.unscoped(), | |
390 | required: true, | |
391 | include: [ | |
392 | { | |
393 | attributes: [ ], | |
394 | model: ActorModel.unscoped(), | |
395 | required: true, | |
396 | where: { | |
397 | serverId: null | |
398 | }, | |
399 | include: [ | |
400 | { | |
401 | attributes: [ ], | |
402 | as: 'ActorFollowings', | |
403 | model: ActorFollowModel.unscoped(), | |
404 | required: true, | |
405 | where: { | |
406 | targetActorId: actorId | |
407 | } | |
408 | } | |
409 | ] | |
410 | } | |
411 | ] | |
412 | } | |
413 | ] | |
414 | } | |
415 | ||
416 | return UserModel.unscoped().findAll(query) | |
ba75d268 C |
417 | } |
418 | ||
453e83ea | 419 | static listByUsernames (usernames: string[]): Bluebird<MUserDefault[]> { |
f7cc67b4 C |
420 | const query = { |
421 | where: { | |
422 | username: usernames | |
423 | } | |
424 | } | |
425 | ||
426 | return UserModel.findAll(query) | |
427 | } | |
428 | ||
453e83ea | 429 | static loadById (id: number): Bluebird<MUserDefault> { |
9b39106d | 430 | return UserModel.findByPk(id) |
3fd3ab2d | 431 | } |
feb4bdfd | 432 | |
453e83ea | 433 | static loadByUsername (username: string): Bluebird<MUserDefault> { |
3fd3ab2d C |
434 | const query = { |
435 | where: { | |
50b4dcce | 436 | username: { [ Op.iLike ]: username } |
d48ff09d | 437 | } |
3fd3ab2d | 438 | } |
089ff2f2 | 439 | |
3fd3ab2d | 440 | return UserModel.findOne(query) |
feb4bdfd C |
441 | } |
442 | ||
ac0868bc | 443 | static loadForMeAPI (username: string): Bluebird<MUserNotifSettingChannelDefault> { |
3fd3ab2d C |
444 | const query = { |
445 | where: { | |
50b4dcce | 446 | username: { [ Op.iLike ]: username } |
d48ff09d | 447 | } |
3fd3ab2d | 448 | } |
9bd26629 | 449 | |
ac0868bc | 450 | return UserModel.scope(ScopeNames.FOR_ME_API).findOne(query) |
feb4bdfd C |
451 | } |
452 | ||
453e83ea | 453 | static loadByEmail (email: string): Bluebird<MUserDefault> { |
ecb4e35f C |
454 | const query = { |
455 | where: { | |
456 | ||
457 | } | |
458 | } | |
459 | ||
460 | return UserModel.findOne(query) | |
461 | } | |
462 | ||
453e83ea | 463 | static loadByUsernameOrEmail (username: string, email?: string): Bluebird<MUserDefault> { |
ba12e8b3 C |
464 | if (!email) email = username |
465 | ||
3fd3ab2d | 466 | const query = { |
3fd3ab2d | 467 | where: { |
c4a1811e C |
468 | [ Op.or ]: [ |
469 | where(fn('lower', col('username')), fn('lower', username)), | |
470 | ||
471 | { email } | |
472 | ] | |
3fd3ab2d | 473 | } |
6fcd19ba | 474 | } |
69b0a27c | 475 | |
d48ff09d | 476 | return UserModel.findOne(query) |
72c7248b C |
477 | } |
478 | ||
453e83ea | 479 | static loadByVideoId (videoId: number): Bluebird<MUserDefault> { |
cef534ed C |
480 | const query = { |
481 | include: [ | |
482 | { | |
483 | required: true, | |
484 | attributes: [ 'id' ], | |
485 | model: AccountModel.unscoped(), | |
486 | include: [ | |
487 | { | |
488 | required: true, | |
489 | attributes: [ 'id' ], | |
490 | model: VideoChannelModel.unscoped(), | |
491 | include: [ | |
492 | { | |
493 | required: true, | |
494 | attributes: [ 'id' ], | |
495 | model: VideoModel.unscoped(), | |
496 | where: { | |
497 | id: videoId | |
498 | } | |
499 | } | |
500 | ] | |
501 | } | |
502 | ] | |
503 | } | |
504 | ] | |
505 | } | |
506 | ||
507 | return UserModel.findOne(query) | |
508 | } | |
509 | ||
453e83ea | 510 | static loadByVideoImportId (videoImportId: number): Bluebird<MUserDefault> { |
dc133480 C |
511 | const query = { |
512 | include: [ | |
513 | { | |
514 | required: true, | |
515 | attributes: [ 'id' ], | |
516 | model: VideoImportModel.unscoped(), | |
517 | where: { | |
518 | id: videoImportId | |
519 | } | |
520 | } | |
521 | ] | |
522 | } | |
523 | ||
524 | return UserModel.findOne(query) | |
525 | } | |
526 | ||
453e83ea | 527 | static loadByChannelActorId (videoChannelActorId: number): Bluebird<MUserDefault> { |
f7cc67b4 C |
528 | const query = { |
529 | include: [ | |
530 | { | |
531 | required: true, | |
532 | attributes: [ 'id' ], | |
533 | model: AccountModel.unscoped(), | |
534 | include: [ | |
535 | { | |
536 | required: true, | |
537 | attributes: [ 'id' ], | |
538 | model: VideoChannelModel.unscoped(), | |
539 | where: { | |
540 | actorId: videoChannelActorId | |
541 | } | |
542 | } | |
543 | ] | |
544 | } | |
545 | ] | |
546 | } | |
547 | ||
548 | return UserModel.findOne(query) | |
549 | } | |
550 | ||
453e83ea | 551 | static loadByAccountActorId (accountActorId: number): Bluebird<MUserDefault> { |
f7cc67b4 C |
552 | const query = { |
553 | include: [ | |
554 | { | |
555 | required: true, | |
556 | attributes: [ 'id' ], | |
557 | model: AccountModel.unscoped(), | |
558 | where: { | |
559 | actorId: accountActorId | |
560 | } | |
561 | } | |
562 | ] | |
563 | } | |
564 | ||
565 | return UserModel.findOne(query) | |
566 | } | |
567 | ||
453e83ea | 568 | static getOriginalVideoFileTotalFromUser (user: MUserId) { |
3fd3ab2d | 569 | // Don't use sequelize because we need to use a sub query |
8b604880 | 570 | const query = UserModel.generateUserQuotaBaseSQL() |
bee0abff | 571 | |
8b604880 | 572 | return UserModel.getTotalRawQuery(query, user.id) |
bee0abff FA |
573 | } |
574 | ||
8b604880 | 575 | // Returns cumulative size of all video files uploaded in the last 24 hours. |
453e83ea | 576 | static getOriginalVideoFileTotalDailyFromUser (user: MUserId) { |
bee0abff | 577 | // Don't use sequelize because we need to use a sub query |
8b604880 | 578 | const query = UserModel.generateUserQuotaBaseSQL('"video"."createdAt" > now() - interval \'24 hours\'') |
68a3b9f2 | 579 | |
8b604880 | 580 | return UserModel.getTotalRawQuery(query, user.id) |
72c7248b C |
581 | } |
582 | ||
09cababd C |
583 | static async getStats () { |
584 | const totalUsers = await UserModel.count() | |
585 | ||
586 | return { | |
587 | totalUsers | |
588 | } | |
589 | } | |
590 | ||
5cf84858 C |
591 | static autoComplete (search: string) { |
592 | const query = { | |
593 | where: { | |
594 | username: { | |
3acc5084 | 595 | [ Op.like ]: `%${search}%` |
5cf84858 C |
596 | } |
597 | }, | |
598 | limit: 10 | |
599 | } | |
600 | ||
601 | return UserModel.findAll(query) | |
602 | .then(u => u.map(u => u.username)) | |
603 | } | |
604 | ||
22a73cb8 | 605 | canGetVideo (video: MVideoFullLight) { |
2a5518a6 | 606 | const videoUserId = video.VideoChannel.Account.userId |
22a73cb8 | 607 | |
2a5518a6 C |
608 | if (video.isBlacklisted()) { |
609 | return videoUserId === this.id || this.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) | |
22a73cb8 C |
610 | } |
611 | ||
2a5518a6 C |
612 | if (video.privacy === VideoPrivacy.PRIVATE) { |
613 | return video.VideoChannel && videoUserId === this.id || this.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) | |
22a73cb8 C |
614 | } |
615 | ||
2a5518a6 C |
616 | if (video.privacy === VideoPrivacy.INTERNAL) return true |
617 | ||
22a73cb8 C |
618 | return false |
619 | } | |
620 | ||
3fd3ab2d C |
621 | hasRight (right: UserRight) { |
622 | return hasUserRight(this.role, right) | |
623 | } | |
72c7248b | 624 | |
1eddc9a7 C |
625 | hasAdminFlag (flag: UserAdminFlag) { |
626 | return this.adminFlags & flag | |
627 | } | |
628 | ||
3fd3ab2d C |
629 | isPasswordMatch (password: string) { |
630 | return comparePassword(password, this.password) | |
feb4bdfd C |
631 | } |
632 | ||
ac0868bc | 633 | toFormattedJSON (this: MUserFormattable, parameters: { withAdminFlags?: boolean } = {}): User { |
a76138ff | 634 | const videoQuotaUsed = this.get('videoQuotaUsed') |
bee0abff | 635 | const videoQuotaUsedDaily = this.get('videoQuotaUsedDaily') |
a76138ff | 636 | |
ac0868bc | 637 | const json: User = { |
3fd3ab2d C |
638 | id: this.id, |
639 | username: this.username, | |
640 | email: this.email, | |
43d0ea7f C |
641 | theme: getThemeOrDefault(this.theme, DEFAULT_USER_THEME_NAME), |
642 | ||
d1ab89de | 643 | pendingEmail: this.pendingEmail, |
d9eaee39 | 644 | emailVerified: this.emailVerified, |
43d0ea7f | 645 | |
0883b324 | 646 | nsfwPolicy: this.nsfwPolicy, |
ed638e53 | 647 | webTorrentEnabled: this.webTorrentEnabled, |
276d9652 | 648 | videosHistoryEnabled: this.videosHistoryEnabled, |
7efe153b | 649 | autoPlayVideo: this.autoPlayVideo, |
6aa54148 | 650 | autoPlayNextVideo: this.autoPlayNextVideo, |
bee29df8 | 651 | autoPlayNextVideoPlaylist: this.autoPlayNextVideoPlaylist, |
3caf77d3 | 652 | videoLanguages: this.videoLanguages, |
43d0ea7f | 653 | |
3fd3ab2d C |
654 | role: this.role, |
655 | roleLabel: USER_ROLE_LABELS[ this.role ], | |
43d0ea7f | 656 | |
3fd3ab2d | 657 | videoQuota: this.videoQuota, |
bee0abff | 658 | videoQuotaDaily: this.videoQuotaDaily, |
43d0ea7f C |
659 | videoQuotaUsed: videoQuotaUsed !== undefined |
660 | ? parseInt(videoQuotaUsed + '', 10) | |
661 | : undefined, | |
662 | videoQuotaUsedDaily: videoQuotaUsedDaily !== undefined | |
663 | ? parseInt(videoQuotaUsedDaily + '', 10) | |
664 | : undefined, | |
665 | ||
666 | noInstanceConfigWarningModal: this.noInstanceConfigWarningModal, | |
667 | noWelcomeModal: this.noWelcomeModal, | |
668 | ||
eacb25c4 C |
669 | blocked: this.blocked, |
670 | blockedReason: this.blockedReason, | |
43d0ea7f | 671 | |
c5911fd3 | 672 | account: this.Account.toFormattedJSON(), |
43d0ea7f C |
673 | |
674 | notificationSettings: this.NotificationSetting | |
675 | ? this.NotificationSetting.toFormattedJSON() | |
676 | : undefined, | |
677 | ||
a76138ff | 678 | videoChannels: [], |
43d0ea7f C |
679 | |
680 | createdAt: this.createdAt | |
3fd3ab2d C |
681 | } |
682 | ||
1eddc9a7 C |
683 | if (parameters.withAdminFlags) { |
684 | Object.assign(json, { adminFlags: this.adminFlags }) | |
685 | } | |
686 | ||
3fd3ab2d | 687 | if (Array.isArray(this.Account.VideoChannels) === true) { |
c5911fd3 | 688 | json.videoChannels = this.Account.VideoChannels |
3fd3ab2d C |
689 | .map(c => c.toFormattedJSON()) |
690 | .sort((v1, v2) => { | |
691 | if (v1.createdAt < v2.createdAt) return -1 | |
692 | if (v1.createdAt === v2.createdAt) return 0 | |
ad4a8a1c | 693 | |
3fd3ab2d C |
694 | return 1 |
695 | }) | |
ad4a8a1c | 696 | } |
3fd3ab2d C |
697 | |
698 | return json | |
ad4a8a1c C |
699 | } |
700 | ||
ac0868bc C |
701 | toMeFormattedJSON (this: MMyUserFormattable): MyUser { |
702 | const formatted = this.toFormattedJSON() | |
703 | ||
704 | const specialPlaylists = this.Account.VideoPlaylists | |
705 | .map(p => ({ id: p.id, name: p.name, type: p.type })) | |
706 | ||
707 | return Object.assign(formatted, { specialPlaylists }) | |
708 | } | |
709 | ||
bee0abff FA |
710 | async isAbleToUploadVideo (videoFile: { size: number }) { |
711 | if (this.videoQuota === -1 && this.videoQuotaDaily === -1) return Promise.resolve(true) | |
b0f9f39e | 712 | |
bee0abff FA |
713 | const [ totalBytes, totalBytesDaily ] = await Promise.all([ |
714 | UserModel.getOriginalVideoFileTotalFromUser(this), | |
715 | UserModel.getOriginalVideoFileTotalDailyFromUser(this) | |
716 | ]) | |
717 | ||
718 | const uploadedTotal = videoFile.size + totalBytes | |
719 | const uploadedDaily = videoFile.size + totalBytesDaily | |
bee0abff | 720 | |
3acc5084 C |
721 | if (this.videoQuotaDaily === -1) return uploadedTotal < this.videoQuota |
722 | if (this.videoQuota === -1) return uploadedDaily < this.videoQuotaDaily | |
723 | ||
724 | return uploadedTotal < this.videoQuota && uploadedDaily < this.videoQuotaDaily | |
b0f9f39e | 725 | } |
8b604880 C |
726 | |
727 | private static generateUserQuotaBaseSQL (where?: string) { | |
728 | const andWhere = where ? 'AND ' + where : '' | |
729 | ||
730 | return 'SELECT SUM("size") AS "total" ' + | |
731 | 'FROM (' + | |
732 | 'SELECT MAX("videoFile"."size") AS "size" FROM "videoFile" ' + | |
733 | 'INNER JOIN "video" ON "videoFile"."videoId" = "video"."id" ' + | |
734 | 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' + | |
735 | 'INNER JOIN "account" ON "videoChannel"."accountId" = "account"."id" ' + | |
736 | 'WHERE "account"."userId" = $userId ' + andWhere + | |
737 | 'GROUP BY "video"."id"' + | |
738 | ') t' | |
739 | } | |
740 | ||
741 | private static getTotalRawQuery (query: string, userId: number) { | |
742 | const options = { | |
743 | bind: { userId }, | |
3acc5084 | 744 | type: QueryTypes.SELECT as QueryTypes.SELECT |
8b604880 C |
745 | } |
746 | ||
3acc5084 | 747 | return UserModel.sequelize.query<{ total: string }>(query, options) |
8b604880 C |
748 | .then(([ { total } ]) => { |
749 | if (total === null) return 0 | |
750 | ||
3acc5084 | 751 | return parseInt(total, 10) |
8b604880 C |
752 | }) |
753 | } | |
b0f9f39e | 754 | } |