2017-05-22 14:58:25 -04:00
|
|
|
import * as Sequelize from 'sequelize'
|
2017-09-04 14:07:54 -04:00
|
|
|
import * as Promise from 'bluebird'
|
2017-05-15 16:22:03 -04:00
|
|
|
|
2017-10-31 11:31:24 -04:00
|
|
|
import { getSort, addMethodsToModel } from '../utils'
|
2017-05-15 16:22:03 -04:00
|
|
|
import {
|
|
|
|
cryptPassword,
|
|
|
|
comparePassword,
|
|
|
|
isUserPasswordValid,
|
|
|
|
isUserUsernameValid,
|
2017-09-04 14:07:54 -04:00
|
|
|
isUserDisplayNSFWValid,
|
2017-10-27 10:55:03 -04:00
|
|
|
isUserVideoQuotaValid,
|
|
|
|
isUserRoleValid
|
2017-06-16 03:45:46 -04:00
|
|
|
} from '../../helpers'
|
2017-10-27 10:55:03 -04:00
|
|
|
import { UserRight, USER_ROLE_LABELS, hasUserRight } from '../../../shared'
|
2016-08-04 16:32:36 -04:00
|
|
|
|
2017-05-22 14:58:25 -04:00
|
|
|
import {
|
|
|
|
UserInstance,
|
|
|
|
UserAttributes,
|
|
|
|
|
|
|
|
UserMethods
|
|
|
|
} from './user-interface'
|
|
|
|
|
|
|
|
let User: Sequelize.Model<UserInstance, UserAttributes>
|
|
|
|
let isPasswordMatch: UserMethods.IsPasswordMatch
|
2017-10-27 10:55:03 -04:00
|
|
|
let hasRight: UserMethods.HasRight
|
2017-08-25 05:45:31 -04:00
|
|
|
let toFormattedJSON: UserMethods.ToFormattedJSON
|
2017-05-22 14:58:25 -04:00
|
|
|
let countTotal: UserMethods.CountTotal
|
|
|
|
let getByUsername: UserMethods.GetByUsername
|
|
|
|
let listForApi: UserMethods.ListForApi
|
|
|
|
let loadById: UserMethods.LoadById
|
|
|
|
let loadByUsername: UserMethods.LoadByUsername
|
2017-10-24 13:41:09 -04:00
|
|
|
let loadByUsernameAndPopulateChannels: UserMethods.LoadByUsernameAndPopulateChannels
|
2017-05-22 14:58:25 -04:00
|
|
|
let loadByUsernameOrEmail: UserMethods.LoadByUsernameOrEmail
|
2017-09-04 14:07:54 -04:00
|
|
|
let isAbleToUploadVideo: UserMethods.IsAbleToUploadVideo
|
2017-05-22 14:58:25 -04:00
|
|
|
|
2017-06-11 11:35:32 -04:00
|
|
|
export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
|
|
|
|
User = sequelize.define<UserInstance, UserAttributes>('User',
|
2016-12-11 15:50:51 -05:00
|
|
|
{
|
|
|
|
password: {
|
2016-12-28 09:49:23 -05:00
|
|
|
type: DataTypes.STRING,
|
|
|
|
allowNull: false,
|
|
|
|
validate: {
|
2017-07-11 11:04:57 -04:00
|
|
|
passwordValid: value => {
|
2017-05-15 16:22:03 -04:00
|
|
|
const res = isUserPasswordValid(value)
|
2016-12-28 09:49:23 -05:00
|
|
|
if (res === false) throw new Error('Password not valid.')
|
|
|
|
}
|
|
|
|
}
|
2016-12-11 15:50:51 -05:00
|
|
|
},
|
|
|
|
username: {
|
2016-12-28 09:49:23 -05:00
|
|
|
type: DataTypes.STRING,
|
|
|
|
allowNull: false,
|
|
|
|
validate: {
|
2017-07-11 11:04:57 -04:00
|
|
|
usernameValid: value => {
|
2017-05-15 16:22:03 -04:00
|
|
|
const res = isUserUsernameValid(value)
|
2016-12-28 09:49:23 -05:00
|
|
|
if (res === false) throw new Error('Username not valid.')
|
|
|
|
}
|
|
|
|
}
|
2016-12-11 15:50:51 -05:00
|
|
|
},
|
2017-02-18 03:29:59 -05:00
|
|
|
email: {
|
2017-02-18 05:56:28 -05:00
|
|
|
type: DataTypes.STRING(400),
|
2017-02-18 03:29:59 -05:00
|
|
|
allowNull: false,
|
|
|
|
validate: {
|
|
|
|
isEmail: true
|
|
|
|
}
|
|
|
|
},
|
2017-04-03 15:24:36 -04:00
|
|
|
displayNSFW: {
|
|
|
|
type: DataTypes.BOOLEAN,
|
|
|
|
allowNull: false,
|
|
|
|
defaultValue: false,
|
|
|
|
validate: {
|
2017-07-11 11:04:57 -04:00
|
|
|
nsfwValid: value => {
|
2017-05-15 16:22:03 -04:00
|
|
|
const res = isUserDisplayNSFWValid(value)
|
2017-04-03 15:24:36 -04:00
|
|
|
if (res === false) throw new Error('Display NSFW is not valid.')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2016-12-11 15:50:51 -05:00
|
|
|
role: {
|
2017-10-27 10:55:03 -04:00
|
|
|
type: DataTypes.INTEGER,
|
|
|
|
allowNull: false,
|
|
|
|
validate: {
|
|
|
|
roleValid: value => {
|
|
|
|
const res = isUserRoleValid(value)
|
|
|
|
if (res === false) throw new Error('Role is not valid.')
|
|
|
|
}
|
|
|
|
}
|
2017-09-04 14:07:54 -04:00
|
|
|
},
|
|
|
|
videoQuota: {
|
|
|
|
type: DataTypes.BIGINT,
|
|
|
|
allowNull: false,
|
|
|
|
validate: {
|
|
|
|
videoQuotaValid: value => {
|
|
|
|
const res = isUserVideoQuotaValid(value)
|
|
|
|
if (res === false) throw new Error('Video quota is not valid.')
|
|
|
|
}
|
|
|
|
}
|
2016-12-11 15:50:51 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2016-12-29 03:33:28 -05:00
|
|
|
indexes: [
|
|
|
|
{
|
2017-02-16 13:24:34 -05:00
|
|
|
fields: [ 'username' ],
|
|
|
|
unique: true
|
2017-02-18 03:29:59 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [ 'email' ],
|
|
|
|
unique: true
|
2016-12-29 03:33:28 -05:00
|
|
|
}
|
|
|
|
],
|
2016-12-11 15:50:51 -05:00
|
|
|
hooks: {
|
|
|
|
beforeCreate: beforeCreateOrUpdate,
|
|
|
|
beforeUpdate: beforeCreateOrUpdate
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2017-05-22 14:58:25 -04:00
|
|
|
const classMethods = [
|
|
|
|
associate,
|
|
|
|
|
|
|
|
countTotal,
|
|
|
|
getByUsername,
|
|
|
|
listForApi,
|
|
|
|
loadById,
|
|
|
|
loadByUsername,
|
2017-10-24 13:41:09 -04:00
|
|
|
loadByUsernameAndPopulateChannels,
|
2017-05-22 14:58:25 -04:00
|
|
|
loadByUsernameOrEmail
|
|
|
|
]
|
|
|
|
const instanceMethods = [
|
2017-10-27 10:55:03 -04:00
|
|
|
hasRight,
|
2017-05-22 14:58:25 -04:00
|
|
|
isPasswordMatch,
|
2017-08-25 05:45:31 -04:00
|
|
|
toFormattedJSON,
|
2017-09-04 14:07:54 -04:00
|
|
|
isAbleToUploadVideo
|
2017-05-22 14:58:25 -04:00
|
|
|
]
|
|
|
|
addMethodsToModel(User, classMethods, instanceMethods)
|
|
|
|
|
2016-12-11 15:50:51 -05:00
|
|
|
return User
|
2016-08-04 16:32:36 -04:00
|
|
|
}
|
2016-07-01 10:03:53 -04:00
|
|
|
|
2017-06-10 16:15:25 -04:00
|
|
|
function beforeCreateOrUpdate (user: UserInstance) {
|
2017-11-04 13:09:23 -04:00
|
|
|
if (user.changed('password')) {
|
|
|
|
return cryptPassword(user.password)
|
|
|
|
.then(hash => {
|
|
|
|
user.password = hash
|
|
|
|
return undefined
|
|
|
|
})
|
|
|
|
}
|
2016-12-11 15:50:51 -05:00
|
|
|
}
|
2016-07-01 10:03:53 -04:00
|
|
|
|
2016-08-25 11:57:37 -04:00
|
|
|
// ------------------------------ METHODS ------------------------------
|
|
|
|
|
2017-10-27 10:55:03 -04:00
|
|
|
hasRight = function (this: UserInstance, right: UserRight) {
|
|
|
|
return hasUserRight(this.role, right)
|
|
|
|
}
|
|
|
|
|
2017-07-05 07:26:25 -04:00
|
|
|
isPasswordMatch = function (this: UserInstance, password: string) {
|
|
|
|
return comparePassword(password, this.password)
|
2016-08-25 11:57:37 -04:00
|
|
|
}
|
|
|
|
|
2017-08-25 05:45:31 -04:00
|
|
|
toFormattedJSON = function (this: UserInstance) {
|
2017-10-24 13:41:09 -04:00
|
|
|
const json = {
|
2016-12-11 15:50:51 -05:00
|
|
|
id: this.id,
|
2016-08-25 11:57:37 -04:00
|
|
|
username: this.username,
|
2017-02-18 03:29:59 -05:00
|
|
|
email: this.email,
|
2017-04-03 15:24:36 -04:00
|
|
|
displayNSFW: this.displayNSFW,
|
2016-09-23 11:19:57 -04:00
|
|
|
role: this.role,
|
2017-10-27 10:55:03 -04:00
|
|
|
roleLabel: USER_ROLE_LABELS[this.role],
|
2017-09-04 14:07:54 -04:00
|
|
|
videoQuota: this.videoQuota,
|
2017-10-24 13:41:09 -04:00
|
|
|
createdAt: this.createdAt,
|
|
|
|
author: {
|
|
|
|
id: this.Author.id,
|
|
|
|
uuid: this.Author.uuid
|
|
|
|
}
|
2016-08-25 11:57:37 -04:00
|
|
|
}
|
2017-10-24 13:41:09 -04:00
|
|
|
|
|
|
|
if (Array.isArray(this.Author.VideoChannels) === true) {
|
|
|
|
const videoChannels = this.Author.VideoChannels
|
|
|
|
.map(c => c.toFormattedJSON())
|
|
|
|
.sort((v1, v2) => {
|
|
|
|
if (v1.createdAt < v2.createdAt) return -1
|
|
|
|
if (v1.createdAt === v2.createdAt) return 0
|
|
|
|
|
|
|
|
return 1
|
|
|
|
})
|
|
|
|
|
|
|
|
json['videoChannels'] = videoChannels
|
|
|
|
}
|
|
|
|
|
|
|
|
return json
|
2016-08-25 11:57:37 -04:00
|
|
|
}
|
2017-04-26 15:22:10 -04:00
|
|
|
|
2017-09-04 14:07:54 -04:00
|
|
|
isAbleToUploadVideo = function (this: UserInstance, videoFile: Express.Multer.File) {
|
|
|
|
if (this.videoQuota === -1) return Promise.resolve(true)
|
|
|
|
|
|
|
|
return getOriginalVideoFileTotalFromUser(this).then(totalBytes => {
|
|
|
|
return (videoFile.size + totalBytes) < this.videoQuota
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-08-25 11:57:37 -04:00
|
|
|
// ------------------------------ STATICS ------------------------------
|
2016-07-01 10:03:53 -04:00
|
|
|
|
2016-12-11 15:50:51 -05:00
|
|
|
function associate (models) {
|
2017-05-22 14:58:25 -04:00
|
|
|
User.hasOne(models.Author, {
|
2016-12-29 04:33:36 -05:00
|
|
|
foreignKey: 'userId',
|
|
|
|
onDelete: 'cascade'
|
|
|
|
})
|
|
|
|
|
2017-05-22 14:58:25 -04:00
|
|
|
User.hasMany(models.OAuthToken, {
|
2016-12-11 15:50:51 -05:00
|
|
|
foreignKey: 'userId',
|
|
|
|
onDelete: 'cascade'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-07-05 07:26:25 -04:00
|
|
|
countTotal = function () {
|
|
|
|
return this.count()
|
2016-08-16 15:51:35 -04:00
|
|
|
}
|
|
|
|
|
2017-06-10 16:15:25 -04:00
|
|
|
getByUsername = function (username: string) {
|
2016-12-11 15:50:51 -05:00
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
username: username
|
2017-10-24 13:41:09 -04:00
|
|
|
},
|
|
|
|
include: [ { model: User['sequelize'].models.Author, required: true } ]
|
2016-12-11 15:50:51 -05:00
|
|
|
}
|
|
|
|
|
2017-05-22 14:58:25 -04:00
|
|
|
return User.findOne(query)
|
2016-08-04 16:32:36 -04:00
|
|
|
}
|
|
|
|
|
2017-07-05 07:26:25 -04:00
|
|
|
listForApi = function (start: number, count: number, sort: string) {
|
2016-12-11 15:50:51 -05:00
|
|
|
const query = {
|
|
|
|
offset: start,
|
|
|
|
limit: count,
|
2017-10-24 13:41:09 -04:00
|
|
|
order: [ getSort(sort) ],
|
|
|
|
include: [ { model: User['sequelize'].models.Author, required: true } ]
|
2016-12-11 15:50:51 -05:00
|
|
|
}
|
|
|
|
|
2017-07-05 07:26:25 -04:00
|
|
|
return User.findAndCountAll(query).then(({ rows, count }) => {
|
|
|
|
return {
|
|
|
|
data: rows,
|
|
|
|
total: count
|
|
|
|
}
|
2016-12-11 15:50:51 -05:00
|
|
|
})
|
2016-07-01 10:03:53 -04:00
|
|
|
}
|
|
|
|
|
2017-07-05 07:26:25 -04:00
|
|
|
loadById = function (id: number) {
|
2017-10-24 13:41:09 -04:00
|
|
|
const options = {
|
|
|
|
include: [ { model: User['sequelize'].models.Author, required: true } ]
|
|
|
|
}
|
|
|
|
|
|
|
|
return User.findById(id, options)
|
2016-08-09 15:44:45 -04:00
|
|
|
}
|
|
|
|
|
2017-07-05 07:26:25 -04:00
|
|
|
loadByUsername = function (username: string) {
|
2016-12-11 15:50:51 -05:00
|
|
|
const query = {
|
|
|
|
where: {
|
2017-08-25 12:36:49 -04:00
|
|
|
username
|
2017-10-24 13:41:09 -04:00
|
|
|
},
|
|
|
|
include: [ { model: User['sequelize'].models.Author, required: true } ]
|
|
|
|
}
|
|
|
|
|
|
|
|
return User.findOne(query)
|
|
|
|
}
|
|
|
|
|
|
|
|
loadByUsernameAndPopulateChannels = function (username: string) {
|
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
username
|
|
|
|
},
|
|
|
|
include: [
|
|
|
|
{
|
|
|
|
model: User['sequelize'].models.Author,
|
|
|
|
required: true,
|
|
|
|
include: [ User['sequelize'].models.VideoChannel ]
|
|
|
|
}
|
|
|
|
]
|
2016-12-11 15:50:51 -05:00
|
|
|
}
|
|
|
|
|
2017-07-05 07:26:25 -04:00
|
|
|
return User.findOne(query)
|
2016-08-04 16:32:36 -04:00
|
|
|
}
|
2017-02-18 03:29:59 -05:00
|
|
|
|
2017-07-05 07:26:25 -04:00
|
|
|
loadByUsernameOrEmail = function (username: string, email: string) {
|
2017-02-18 03:29:59 -05:00
|
|
|
const query = {
|
2017-10-24 13:41:09 -04:00
|
|
|
include: [ { model: User['sequelize'].models.Author, required: true } ],
|
2017-02-18 03:29:59 -05:00
|
|
|
where: {
|
2017-10-26 10:59:02 -04:00
|
|
|
[Sequelize.Op.or]: [ { username }, { email } ]
|
2017-02-18 03:29:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-25 12:36:49 -04:00
|
|
|
// FIXME: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/18387
|
|
|
|
return (User as any).findOne(query)
|
2017-02-18 03:29:59 -05:00
|
|
|
}
|
2017-09-04 14:07:54 -04:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
function getOriginalVideoFileTotalFromUser (user: UserInstance) {
|
2017-10-24 13:41:09 -04:00
|
|
|
// Don't use sequelize because we need to use a sub query
|
2017-10-09 05:06:13 -04:00
|
|
|
const query = 'SELECT SUM("size") AS "total" FROM ' +
|
|
|
|
'(SELECT MAX("VideoFiles"."size") AS "size" FROM "VideoFiles" ' +
|
|
|
|
'INNER JOIN "Videos" ON "VideoFiles"."videoId" = "Videos"."id" ' +
|
2017-10-24 13:41:09 -04:00
|
|
|
'INNER JOIN "VideoChannels" ON "VideoChannels"."id" = "Videos"."channelId" ' +
|
|
|
|
'INNER JOIN "Authors" ON "VideoChannels"."authorId" = "Authors"."id" ' +
|
2017-10-09 05:06:13 -04:00
|
|
|
'INNER JOIN "Users" ON "Authors"."userId" = "Users"."id" ' +
|
|
|
|
'WHERE "Users"."id" = $userId GROUP BY "Videos"."id") t'
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
bind: { userId: user.id },
|
|
|
|
type: Sequelize.QueryTypes.SELECT
|
2017-09-04 14:07:54 -04:00
|
|
|
}
|
2017-10-09 05:06:13 -04:00
|
|
|
return User['sequelize'].query(query, options).then(([ { total } ]) => {
|
|
|
|
if (total === null) return 0
|
2017-09-04 14:07:54 -04:00
|
|
|
|
2017-10-09 05:06:13 -04:00
|
|
|
return parseInt(total, 10)
|
|
|
|
})
|
2017-09-04 14:07:54 -04:00
|
|
|
}
|