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