2017-11-15 10:56:21 -06:00
|
|
|
import * as Sequelize from 'sequelize'
|
2017-12-14 03:07:57 -06:00
|
|
|
import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
|
2017-12-14 10:38:41 -06:00
|
|
|
import { ActorModel } from '../activitypub/actor'
|
2017-12-12 10:53:50 -06:00
|
|
|
import { VideoModel } from './video'
|
2017-11-15 10:56:21 -06:00
|
|
|
|
2017-12-14 03:07:57 -06:00
|
|
|
enum ScopeNames {
|
|
|
|
FULL = 'FULL',
|
2017-12-14 10:38:41 -06:00
|
|
|
WITH_ACTOR = 'WITH_ACTOR'
|
2017-12-14 03:07:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
@Scopes({
|
|
|
|
[ScopeNames.FULL]: {
|
|
|
|
include: [
|
|
|
|
{
|
2017-12-14 10:38:41 -06:00
|
|
|
model: () => ActorModel,
|
2017-12-14 03:07:57 -06:00
|
|
|
required: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
model: () => VideoModel,
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
2017-12-14 10:38:41 -06:00
|
|
|
[ScopeNames.WITH_ACTOR]: {
|
2017-12-14 03:07:57 -06:00
|
|
|
include: [
|
|
|
|
{
|
2017-12-14 10:38:41 -06:00
|
|
|
model: () => ActorModel,
|
2017-12-14 03:07:57 -06:00
|
|
|
required: true
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
})
|
2017-12-12 10:53:50 -06:00
|
|
|
@Table({
|
|
|
|
tableName: 'videoShare',
|
|
|
|
indexes: [
|
2017-11-15 10:56:21 -06:00
|
|
|
{
|
2017-12-14 10:38:41 -06:00
|
|
|
fields: [ 'actorId' ]
|
2017-12-12 10:53:50 -06:00
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [ 'videoId' ]
|
2017-11-15 10:56:21 -06:00
|
|
|
}
|
|
|
|
]
|
2017-12-12 10:53:50 -06:00
|
|
|
})
|
|
|
|
export class VideoShareModel extends Model<VideoShareModel> {
|
|
|
|
@CreatedAt
|
|
|
|
createdAt: Date
|
2017-11-15 10:56:21 -06:00
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
@UpdatedAt
|
|
|
|
updatedAt: Date
|
2017-11-15 10:56:21 -06:00
|
|
|
|
2017-12-14 10:38:41 -06:00
|
|
|
@ForeignKey(() => ActorModel)
|
2017-12-12 10:53:50 -06:00
|
|
|
@Column
|
2017-12-14 10:38:41 -06:00
|
|
|
actorId: number
|
2017-11-15 10:56:21 -06:00
|
|
|
|
2017-12-14 10:38:41 -06:00
|
|
|
@BelongsTo(() => ActorModel, {
|
2017-11-15 10:56:21 -06:00
|
|
|
foreignKey: {
|
|
|
|
allowNull: false
|
|
|
|
},
|
|
|
|
onDelete: 'cascade'
|
|
|
|
})
|
2017-12-14 10:38:41 -06:00
|
|
|
Actor: ActorModel
|
2017-11-15 10:56:21 -06:00
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
@ForeignKey(() => VideoModel)
|
|
|
|
@Column
|
|
|
|
videoId: number
|
|
|
|
|
|
|
|
@BelongsTo(() => VideoModel, {
|
2017-11-15 10:56:21 -06:00
|
|
|
foreignKey: {
|
2017-12-12 10:53:50 -06:00
|
|
|
allowNull: false
|
2017-11-15 10:56:21 -06:00
|
|
|
},
|
|
|
|
onDelete: 'cascade'
|
|
|
|
})
|
2017-12-12 10:53:50 -06:00
|
|
|
Video: VideoModel
|
2017-11-27 07:44:51 -06:00
|
|
|
|
2017-12-14 10:38:41 -06:00
|
|
|
static load (actorId: number, videoId: number, t: Sequelize.Transaction) {
|
|
|
|
return VideoShareModel.scope(ScopeNames.WITH_ACTOR).findOne({
|
2017-12-12 10:53:50 -06:00
|
|
|
where: {
|
2017-12-14 10:38:41 -06:00
|
|
|
actorId,
|
2017-12-12 10:53:50 -06:00
|
|
|
videoId
|
|
|
|
},
|
|
|
|
transaction: t
|
|
|
|
})
|
2017-11-16 08:55:01 -06:00
|
|
|
}
|
|
|
|
|
2017-12-14 10:38:41 -06:00
|
|
|
static loadActorsByShare (videoId: number, t: Sequelize.Transaction) {
|
2017-12-12 10:53:50 -06:00
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
videoId
|
|
|
|
},
|
|
|
|
include: [
|
|
|
|
{
|
2017-12-14 10:38:41 -06:00
|
|
|
model: ActorModel,
|
2017-12-12 10:53:50 -06:00
|
|
|
required: true
|
|
|
|
}
|
|
|
|
],
|
|
|
|
transaction: t
|
|
|
|
}
|
|
|
|
|
2017-12-14 03:07:57 -06:00
|
|
|
return VideoShareModel.scope(ScopeNames.FULL).findAll(query)
|
2017-12-14 10:38:41 -06:00
|
|
|
.then(res => res.map(r => r.Actor))
|
2017-12-12 10:53:50 -06:00
|
|
|
}
|
2017-11-16 08:55:01 -06:00
|
|
|
}
|