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,
23 isUserVideoQuotaValid, isUserAutoPlayVideoValid
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'))
87 @Is('UserAutoPlayVideo', value => throwIfNotValid(value, isUserAutoPlayVideoValid, 'auto play video boolean'))
89 autoPlayVideo: boolean
92 @Is('UserRole', value => throwIfNotValid(value, isUserRoleValid, 'role'))
97 @Is('UserVideoQuota', value => throwIfNotValid(value, isUserVideoQuotaValid, 'video quota'))
98 @Column(DataType.BIGINT)
107 @HasOne(() => AccountModel, {
108 foreignKey: 'userId',
111 Account: AccountModel
113 @HasMany(() => OAuthTokenModel, {
114 foreignKey: 'userId',
117 OAuthTokens: OAuthTokenModel[]
121 static cryptPasswordIfNeeded (instance: UserModel) {
122 if (instance.changed('password')) {
123 return cryptPassword(instance.password)
125 instance.password = hash
131 static countTotal () {
135 static getByUsername (username: string) {
140 include: [ { model: AccountModel, required: true } ]
143 return UserModel.findOne(query)
146 static listForApi (start: number, count: number, sort: string) {
150 order: [ getSort(sort) ]
153 return UserModel.findAndCountAll(query)
154 .then(({ rows, count }) => {
162 static loadById (id: number) {
163 return UserModel.findById(id)
166 static loadByUsername (username: string) {
173 return UserModel.findOne(query)
176 static loadByUsernameAndPopulateChannels (username: string) {
183 return UserModel.scope('withVideoChannel').findOne(query)
186 static loadByUsernameOrEmail (username: string, email: string) {
189 [ Sequelize.Op.or ]: [ { username }, { email } ]
193 return UserModel.findOne(query)
196 private static getOriginalVideoFileTotalFromUser (user: UserModel) {
197 // Don't use sequelize because we need to use a sub query
198 const query = 'SELECT SUM("size") AS "total" FROM ' +
199 '(SELECT MAX("videoFile"."size") AS "size" FROM "videoFile" ' +
200 'INNER JOIN "video" ON "videoFile"."videoId" = "video"."id" ' +
201 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
202 'INNER JOIN "account" ON "videoChannel"."accountId" = "account"."id" ' +
203 'INNER JOIN "user" ON "account"."userId" = "user"."id" ' +
204 'WHERE "user"."id" = $userId GROUP BY "video"."id") t'
207 bind: { userId: user.id },
208 type: Sequelize.QueryTypes.SELECT
210 return UserModel.sequelize.query(query, options)
211 .then(([ { total } ]) => {
212 if (total === null) return 0
214 return parseInt(total, 10)
218 hasRight (right: UserRight) {
219 return hasUserRight(this.role, right)
222 isPasswordMatch (password: string) {
223 return comparePassword(password, this.password)
229 username: this.username,
231 displayNSFW: this.displayNSFW,
232 autoPlayVideo: this.autoPlayVideo,
234 roleLabel: USER_ROLE_LABELS[ this.role ],
235 videoQuota: this.videoQuota,
236 createdAt: this.createdAt,
237 account: this.Account.toFormattedJSON()
240 if (Array.isArray(this.Account.VideoChannels) === true) {
241 json['videoChannels'] = this.Account.VideoChannels
242 .map(c => c.toFormattedJSON())
244 if (v1.createdAt < v2.createdAt) return -1
245 if (v1.createdAt === v2.createdAt) return 0
254 isAbleToUploadVideo (videoFile: Express.Multer.File) {
255 if (this.videoQuota === -1) return Promise.resolve(true)
257 return UserModel.getOriginalVideoFileTotalFromUser(this)
258 .then(totalBytes => {
259 return (videoFile.size + totalBytes) < this.videoQuota