1 import * as Sequelize from 'sequelize'
15 } from 'sequelize-typescript'
16 import { hasUserRight, USER_ROLE_LABELS, UserRight } from '../../../shared'
20 } from '../../helpers'
22 isUserDisplayNSFWValid, isUserPasswordValid, isUserRoleValid, isUserUsernameValid,
24 } from '../../helpers/custom-validators/users'
25 import { OAuthTokenModel } from '../oauth/oauth-token'
26 import { getSort, throwIfNotValid } from '../utils'
27 import { VideoChannelModel } from '../video/video-channel'
28 import { AccountModel } from './account'
33 model: () => AccountModel,
42 model: () => AccountModel,
44 include: [ () => VideoChannelModel ]
53 fields: [ 'username' ],
62 export class UserModel extends Model<UserModel> {
65 @Is('UserPassword', value => throwIfNotValid(value, isUserPasswordValid, 'user password'))
70 @Is('UserPassword', value => throwIfNotValid(value, isUserUsernameValid, 'user name'))
76 @Column(DataType.STRING(400))
81 @Is('UserDisplayNSFW', value => throwIfNotValid(value, isUserDisplayNSFWValid, 'display NSFW boolean'))
86 @Is('UserRole', value => throwIfNotValid(value, isUserRoleValid, 'role'))
91 @Is('UserVideoQuota', value => throwIfNotValid(value, isUserVideoQuotaValid, 'video quota'))
92 @Column(DataType.BIGINT)
101 @HasOne(() => AccountModel, {
102 foreignKey: 'userId',
105 Account: AccountModel
107 @HasMany(() => OAuthTokenModel, {
108 foreignKey: 'userId',
111 OAuthTokens: OAuthTokenModel[]
115 static cryptPasswordIfNeeded (instance: UserModel) {
116 if (instance.changed('password')) {
117 return cryptPassword(instance.password)
119 instance.password = hash
125 static countTotal () {
129 static getByUsername (username: string) {
134 include: [ { model: AccountModel, required: true } ]
137 return UserModel.findOne(query)
140 static listForApi (start: number, count: number, sort: string) {
144 order: [ getSort(sort) ]
147 return UserModel.findAndCountAll(query)
148 .then(({ rows, count }) => {
156 static loadById (id: number) {
157 return UserModel.findById(id)
160 static loadByUsername (username: string) {
167 return UserModel.findOne(query)
170 static loadByUsernameAndPopulateChannels (username: string) {
177 return UserModel.scope('withVideoChannel').findOne(query)
180 static loadByUsernameOrEmail (username: string, email: string) {
183 [ Sequelize.Op.or ]: [ { username }, { email } ]
187 return UserModel.findOne(query)
190 private static getOriginalVideoFileTotalFromUser (user: UserModel) {
191 // Don't use sequelize because we need to use a sub query
192 const query = 'SELECT SUM("size") AS "total" FROM ' +
193 '(SELECT MAX("videoFile"."size") AS "size" FROM "videoFile" ' +
194 'INNER JOIN "video" ON "videoFile"."videoId" = "video"."id" ' +
195 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
196 'INNER JOIN "account" ON "videoChannel"."accountId" = "account"."id" ' +
197 'INNER JOIN "user" ON "account"."userId" = "user"."id" ' +
198 'WHERE "user"."id" = $userId GROUP BY "video"."id") t'
201 bind: { userId: user.id },
202 type: Sequelize.QueryTypes.SELECT
204 return UserModel.sequelize.query(query, options)
205 .then(([ { total } ]) => {
206 if (total === null) return 0
208 return parseInt(total, 10)
212 hasRight (right: UserRight) {
213 return hasUserRight(this.role, right)
216 isPasswordMatch (password: string) {
217 return comparePassword(password, this.password)
223 username: this.username,
225 displayNSFW: this.displayNSFW,
227 roleLabel: USER_ROLE_LABELS[ this.role ],
228 videoQuota: this.videoQuota,
229 createdAt: this.createdAt,
230 account: this.Account.toFormattedJSON()
233 if (Array.isArray(this.Account.VideoChannels) === true) {
234 json['videoChannels'] = this.Account.VideoChannels
235 .map(c => c.toFormattedJSON())
237 if (v1.createdAt < v2.createdAt) return -1
238 if (v1.createdAt === v2.createdAt) return 0
247 isAbleToUploadVideo (videoFile: Express.Multer.File) {
248 if (this.videoQuota === -1) return Promise.resolve(true)
250 return UserModel.getOriginalVideoFileTotalFromUser(this)
251 .then(totalBytes => {
252 return (videoFile.size + totalBytes) < this.videoQuota