PeerTube/server/models/user-video-rate.ts

75 lines
1.3 KiB
TypeScript
Raw Normal View History

2017-03-08 20:35:43 +00:00
/*
User rates per video.
*/
2017-05-15 20:22:03 +00:00
import { values } from 'lodash'
2017-03-08 20:35:43 +00:00
2017-05-15 20:22:03 +00:00
import { VIDEO_RATE_TYPES } from '../initializers'
2017-03-08 20:35:43 +00:00
// ---------------------------------------------------------------------------
module.exports = function (sequelize, DataTypes) {
const UserVideoRate = sequelize.define('UserVideoRate',
{
type: {
2017-05-15 20:22:03 +00:00
type: DataTypes.ENUM(values(VIDEO_RATE_TYPES)),
2017-03-08 20:35:43 +00:00
allowNull: false
}
},
{
indexes: [
{
fields: [ 'videoId', 'userId', 'type' ],
unique: true
}
],
classMethods: {
associate,
load
}
}
)
return UserVideoRate
}
// ------------------------------ STATICS ------------------------------
function associate (models) {
this.belongsTo(models.Video, {
foreignKey: {
name: 'videoId',
allowNull: false
},
onDelete: 'CASCADE'
})
this.belongsTo(models.User, {
foreignKey: {
name: 'userId',
allowNull: false
},
onDelete: 'CASCADE'
})
}
function load (userId, videoId, transaction, callback) {
if (!callback) {
callback = transaction
transaction = null
}
const query = {
where: {
userId,
videoId
}
}
2017-05-15 20:22:03 +00:00
const options: any = {}
2017-03-08 20:35:43 +00:00
if (transaction) options.transaction = transaction
return this.findOne(query, options).asCallback(callback)
}