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'
34 fields: [ 'username' ],
43 export class UserModel extends Model<UserModel> {
46 @Is('UserPassword', value => throwIfNotValid(value, isUserPasswordValid, 'user password'))
51 @Is('UserPassword', value => throwIfNotValid(value, isUserUsernameValid, 'user name'))
57 @Column(DataType.STRING(400))
62 @Is('UserDisplayNSFW', value => throwIfNotValid(value, isUserDisplayNSFWValid, 'display NSFW boolean'))
67 @Is('UserRole', value => throwIfNotValid(value, isUserRoleValid, 'role'))
72 @Is('UserVideoQuota', value => throwIfNotValid(value, isUserVideoQuotaValid, 'video quota'))
73 @Column(DataType.BIGINT)
82 @HasOne(() => AccountModel, {
88 @HasMany(() => OAuthTokenModel, {
92 OAuthTokens: OAuthTokenModel[]
96 static cryptPasswordIfNeeded (instance: UserModel) {
97 if (instance.changed('password')) {
98 return cryptPassword(instance.password)
100 instance.password = hash
106 static countTotal () {
110 static getByUsername (username: string) {
115 include: [ { model: AccountModel, required: true } ]
118 return UserModel.findOne(query)
121 static listForApi (start: number, count: number, sort: string) {
125 order: [ getSort(sort) ],
126 include: [ { model: AccountModel, required: true } ]
129 return UserModel.findAndCountAll(query)
130 .then(({ rows, count }) => {
138 static loadById (id: number) {
140 include: [ { model: AccountModel, required: true } ]
143 return UserModel.findById(id, options)
146 static loadByUsername (username: string) {
151 include: [ { model: AccountModel, required: true } ]
154 return UserModel.findOne(query)
157 static loadByUsernameAndPopulateChannels (username: string) {
166 include: [ VideoChannelModel ]
171 return UserModel.findOne(query)
174 static loadByUsernameOrEmail (username: string, email: string) {
176 include: [ { model: AccountModel, required: true } ],
178 [ Sequelize.Op.or ]: [ { username }, { email } ]
182 // FIXME: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/18387
183 return (UserModel as any).findOne(query)
186 private static getOriginalVideoFileTotalFromUser (user: UserModel) {
187 // Don't use sequelize because we need to use a sub query
188 const query = 'SELECT SUM("size") AS "total" FROM ' +
189 '(SELECT MAX("videoFile"."size") AS "size" FROM "videoFile" ' +
190 'INNER JOIN "video" ON "videoFile"."videoId" = "video"."id" ' +
191 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
192 'INNER JOIN "account" ON "videoChannel"."accountId" = "account"."id" ' +
193 'INNER JOIN "user" ON "account"."userId" = "user"."id" ' +
194 'WHERE "user"."id" = $userId GROUP BY "video"."id") t'
197 bind: { userId: user.id },
198 type: Sequelize.QueryTypes.SELECT
200 return UserModel.sequelize.query(query, options)
201 .then(([ { total } ]) => {
202 if (total === null) return 0
204 return parseInt(total, 10)
208 hasRight (right: UserRight) {
209 return hasUserRight(this.role, right)
212 isPasswordMatch (password: string) {
213 return comparePassword(password, this.password)
219 username: this.username,
221 displayNSFW: this.displayNSFW,
223 roleLabel: USER_ROLE_LABELS[ this.role ],
224 videoQuota: this.videoQuota,
225 createdAt: this.createdAt,
226 account: this.Account.toFormattedJSON()
229 if (Array.isArray(this.Account.VideoChannels) === true) {
230 json['videoChannels'] = this.Account.VideoChannels
231 .map(c => c.toFormattedJSON())
233 if (v1.createdAt < v2.createdAt) return -1
234 if (v1.createdAt === v2.createdAt) return 0
243 isAbleToUploadVideo (videoFile: Express.Multer.File) {
244 if (this.videoQuota === -1) return Promise.resolve(true)
246 return UserModel.getOriginalVideoFileTotalFromUser(this)
247 .then(totalBytes => {
248 return (videoFile.size + totalBytes) < this.videoQuota