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

80 lines
1.6 KiB
TypeScript
Raw Normal View History

2017-11-15 16:56:21 +00:00
import * as Sequelize from 'sequelize'
2017-12-12 16:53:50 +00:00
import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript'
import { AccountModel } from '../account/account'
import { VideoChannelModel } from './video-channel'
2017-11-15 16:56:21 +00:00
2017-12-12 16:53:50 +00:00
@Table({
tableName: 'videoChannelShare',
indexes: [
2017-11-15 16:56:21 +00:00
{
2017-12-12 16:53:50 +00:00
fields: [ 'accountId' ]
},
{
fields: [ 'videoChannelId' ]
2017-11-15 16:56:21 +00:00
}
]
2017-12-12 16:53:50 +00:00
})
export class VideoChannelShareModel extends Model<VideoChannelShareModel> {
@CreatedAt
createdAt: Date
2017-11-15 16:56:21 +00:00
2017-12-12 16:53:50 +00:00
@UpdatedAt
updatedAt: Date
2017-11-15 16:56:21 +00:00
2017-12-12 16:53:50 +00:00
@ForeignKey(() => AccountModel)
@Column
accountId: number
2017-11-15 16:56:21 +00:00
2017-12-12 16:53:50 +00:00
@BelongsTo(() => AccountModel, {
2017-11-15 16:56:21 +00:00
foreignKey: {
allowNull: false
},
onDelete: 'cascade'
})
2017-12-12 16:53:50 +00:00
Account: AccountModel
2017-11-15 16:56:21 +00:00
2017-12-12 16:53:50 +00:00
@ForeignKey(() => VideoChannelModel)
@Column
videoChannelId: number
@BelongsTo(() => VideoChannelModel, {
2017-11-15 16:56:21 +00:00
foreignKey: {
2017-12-12 16:53:50 +00:00
allowNull: false
2017-11-15 16:56:21 +00:00
},
onDelete: 'cascade'
})
2017-12-12 16:53:50 +00:00
VideoChannel: VideoChannelModel
2017-12-12 16:53:50 +00:00
static load (accountId: number, videoChannelId: number, t: Sequelize.Transaction) {
return VideoChannelShareModel.findOne({
where: {
accountId,
videoChannelId
},
include: [
AccountModel,
VideoChannelModel
],
transaction: t
})
2017-11-16 14:55:01 +00:00
}
2017-12-12 16:53:50 +00:00
static loadAccountsByShare (videoChannelId: number, t: Sequelize.Transaction) {
const query = {
where: {
videoChannelId
},
include: [
{
model: AccountModel,
required: true
}
],
transaction: t
}
return VideoChannelShareModel.findAll(query)
.then(res => res.map(r => r.Account))
}
2017-11-16 14:55:01 +00:00
}