PeerTube/server/models/video/video-share.ts

85 lines
1.8 KiB
TypeScript
Raw Normal View History

2017-11-15 17:56:21 +01:00
import * as Sequelize from 'sequelize'
import { addMethodsToModel } from '../utils'
2017-11-16 15:55:01 +01:00
import { VideoShareAttributes, VideoShareInstance, VideoShareMethods } from './video-share-interface'
2017-11-15 17:56:21 +01:00
let VideoShare: Sequelize.Model<VideoShareInstance, VideoShareAttributes>
2017-11-16 15:55:01 +01:00
let loadAccountsByShare: VideoShareMethods.LoadAccountsByShare
let load: VideoShareMethods.Load
2017-11-15 17:56:21 +01:00
export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
VideoShare = sequelize.define<VideoShareInstance, VideoShareAttributes>('VideoShare',
{ },
{
indexes: [
{
fields: [ 'accountId' ]
},
{
fields: [ 'videoId' ]
}
]
}
)
const classMethods = [
2017-11-16 15:55:01 +01:00
associate,
loadAccountsByShare,
load
2017-11-15 17:56:21 +01:00
]
addMethodsToModel(VideoShare, classMethods)
return VideoShare
}
// ------------------------------ METHODS ------------------------------
function associate (models) {
VideoShare.belongsTo(models.Account, {
foreignKey: {
name: 'accountId',
allowNull: false
},
onDelete: 'cascade'
})
VideoShare.belongsTo(models.Video, {
foreignKey: {
name: 'videoId',
allowNull: true
},
onDelete: 'cascade'
})
}
2017-11-16 15:55:01 +01:00
load = function (accountId: number, videoId: number, t: Sequelize.Transaction) {
return VideoShare.findOne({
where: {
accountId,
videoId
},
include: [
VideoShare['sequelize'].models.Account
],
transaction: t
})
}
loadAccountsByShare = function (videoId: number, t: Sequelize.Transaction) {
2017-11-16 15:55:01 +01:00
const query = {
where: {
videoId
},
include: [
{
model: VideoShare['sequelize'].models.Account,
required: true
}
],
transaction: t
2017-11-16 15:55:01 +01:00
}
return VideoShare.findAll(query)
.then(res => res.map(r => r.Account))
}