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

80 lines
1.6 KiB
TypeScript
Raw Normal View History

2017-11-15 17:56:21 +01:00
import * as Sequelize from 'sequelize'
2017-12-12 17:53:50 +01: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 17:56:21 +01:00
2017-12-12 17:53:50 +01:00
@Table({
tableName: 'videoChannelShare',
indexes: [
2017-11-15 17:56:21 +01:00
{
2017-12-12 17:53:50 +01:00
fields: [ 'accountId' ]
},
{
fields: [ 'videoChannelId' ]
2017-11-15 17:56:21 +01:00
}
]
2017-12-12 17:53:50 +01:00
})
export class VideoChannelShareModel extends Model<VideoChannelShareModel> {
@CreatedAt
createdAt: Date
2017-11-15 17:56:21 +01:00
2017-12-12 17:53:50 +01:00
@UpdatedAt
updatedAt: Date
2017-11-15 17:56:21 +01:00
2017-12-12 17:53:50 +01:00
@ForeignKey(() => AccountModel)
@Column
accountId: number
2017-11-15 17:56:21 +01:00
2017-12-12 17:53:50 +01:00
@BelongsTo(() => AccountModel, {
2017-11-15 17:56:21 +01:00
foreignKey: {
allowNull: false
},
onDelete: 'cascade'
})
2017-12-12 17:53:50 +01:00
Account: AccountModel
2017-11-15 17:56:21 +01:00
2017-12-12 17:53:50 +01:00
@ForeignKey(() => VideoChannelModel)
@Column
videoChannelId: number
@BelongsTo(() => VideoChannelModel, {
2017-11-15 17:56:21 +01:00
foreignKey: {
2017-12-12 17:53:50 +01:00
allowNull: false
2017-11-15 17:56:21 +01:00
},
onDelete: 'cascade'
})
2017-12-12 17:53:50 +01:00
VideoChannel: VideoChannelModel
2017-12-12 17:53:50 +01:00
static load (accountId: number, videoChannelId: number, t: Sequelize.Transaction) {
return VideoChannelShareModel.findOne({
where: {
accountId,
videoChannelId
},
include: [
AccountModel,
VideoChannelModel
],
transaction: t
})
2017-11-16 15:55:01 +01:00
}
2017-12-12 17:53:50 +01: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 15:55:01 +01:00
}