1 import * as Sequelize from 'sequelize'
2 import * as Bluebird from 'bluebird'
3 import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
4 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
5 import { CONSTRAINTS_FIELDS } from '../../initializers'
6 import { AccountModel } from '../account/account'
7 import { ActorModel } from '../activitypub/actor'
8 import { throwIfNotValid } from '../utils'
9 import { VideoModel } from './video'
10 import { VideoChannelModel } from './video-channel'
14 WITH_ACTOR = 'WITH_ACTOR'
21 model: () => ActorModel,
25 model: () => VideoModel,
30 [ScopeNames.WITH_ACTOR]: {
33 model: () => ActorModel,
40 tableName: 'videoShare',
54 export class VideoShareModel extends Model<VideoShareModel> {
57 @Is('VideoShareUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
58 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_SHARE.URL.max))
67 @ForeignKey(() => ActorModel)
71 @BelongsTo(() => ActorModel, {
79 @ForeignKey(() => VideoModel)
83 @BelongsTo(() => VideoModel, {
91 static load (actorId: number, videoId: number, t: Sequelize.Transaction) {
92 return VideoShareModel.scope(ScopeNames.WITH_ACTOR).findOne({
101 static loadActorsByShare (videoId: number, t: Sequelize.Transaction) {
115 return VideoShareModel.scope(ScopeNames.FULL).findAll(query)
116 .then(res => res.map(r => r.Actor))
119 static loadActorsByVideoOwner (actorOwnerId: number, t: Sequelize.Transaction): Bluebird<ActorModel[]> {
134 model: VideoChannelModel.unscoped(),
139 model: AccountModel.unscoped(),
142 actorId: actorOwnerId
153 return VideoShareModel.scope(ScopeNames.FULL).findAll(query)
154 .then(res => res.map(r => r.Actor))
157 static loadActorsByVideoChannel (videoChannelId: number, t: Sequelize.Transaction): Bluebird<ActorModel[]> {
170 channelId: videoChannelId
177 return VideoShareModel.scope(ScopeNames.FULL)
179 .then(res => res.map(r => r.Actor))