import { User, UserRole } from '../../../shared/models/users'
import {
isUserAutoPlayVideoValid,
+ isUserBlockedReasonValid,
+ isUserBlockedValid,
isUserNSFWPolicyValid,
isUserPasswordValid,
isUserRoleValid,
import { values } from 'lodash'
import { NSFW_POLICY_TYPES } from '../../initializers'
+enum ScopeNames {
+ WITH_VIDEO_CHANNEL = 'WITH_VIDEO_CHANNEL'
+}
+
@DefaultScope({
include: [
{
]
})
@Scopes({
- withVideoChannel: {
+ [ScopeNames.WITH_VIDEO_CHANNEL]: {
include: [
{
model: () => AccountModel,
@Column
autoPlayVideo: boolean
+ @AllowNull(false)
+ @Default(false)
+ @Is('UserBlocked', value => throwIfNotValid(value, isUserBlockedValid, 'blocked boolean'))
+ @Column
+ blocked: boolean
+
+ @AllowNull(true)
+ @Default(null)
+ @Is('UserBlockedReason', value => throwIfNotValid(value, isUserBlockedReasonValid, 'blocked reason'))
+ @Column
+ blockedReason: string
+
@AllowNull(false)
@Is('UserRole', value => throwIfNotValid(value, isUserRoleValid, 'role'))
@Column
static listForApi (start: number, count: number, sort: string) {
const query = {
+ attributes: {
+ include: [
+ [
+ Sequelize.literal(
+ '(' +
+ 'SELECT COALESCE(SUM("size"), 0) FROM ' +
+ '(' +
+ 'SELECT MAX("videoFile"."size") AS "size" FROM "videoFile" ' +
+ 'INNER JOIN "video" ON "videoFile"."videoId" = "video"."id" ' +
+ 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
+ 'INNER JOIN "account" ON "videoChannel"."accountId" = "account"."id" ' +
+ 'WHERE "account"."userId" = "UserModel"."id" GROUP BY "video"."id"' +
+ ') t' +
+ ')'
+ ),
+ 'videoQuotaUsed'
+ ] as any // FIXME: typings
+ ]
+ },
offset: start,
limit: count,
order: getSort(sort)
return UserModel.findAndCountAll(query)
.then(({ rows, count }) => {
+ console.log(rows[0])
+ console.log(rows[0]['videoQuotaUsed'])
+ console.log(rows[0].get('videoQuotaUsed'))
return {
data: rows,
total: count
}
}
- return UserModel.scope('withVideoChannel').findOne(query)
+ return UserModel.scope(ScopeNames.WITH_VIDEO_CHANNEL).findOne(query)
}
static loadByEmail (email: string) {
'INNER JOIN "video" ON "videoFile"."videoId" = "video"."id" ' +
'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
'INNER JOIN "account" ON "videoChannel"."accountId" = "account"."id" ' +
- 'INNER JOIN "user" ON "account"."userId" = "user"."id" ' +
- 'WHERE "user"."id" = $userId GROUP BY "video"."id") t'
+ 'WHERE "account"."userId" = $userId GROUP BY "video"."id") t'
const options = {
bind: { userId: user.id },
}
toFormattedJSON (): User {
+ const videoQuotaUsed = this.get('videoQuotaUsed')
+
const json = {
id: this.id,
username: this.username,
roleLabel: USER_ROLE_LABELS[ this.role ],
videoQuota: this.videoQuota,
createdAt: this.createdAt,
+ blocked: this.blocked,
+ blockedReason: this.blockedReason,
account: this.Account.toFormattedJSON(),
- videoChannels: []
+ videoChannels: [],
+ videoQuotaUsed: videoQuotaUsed !== undefined ? parseInt(videoQuotaUsed, 10) : undefined
}
if (Array.isArray(this.Account.VideoChannels) === true) {
return json
}
- isAbleToUploadVideo (videoFile: Express.Multer.File) {
+ isAbleToUploadVideo (videoFile: { size: number }) {
if (this.videoQuota === -1) return Promise.resolve(true)
return UserModel.getOriginalVideoFileTotalFromUser(this)