1 import * as Sequelize from 'sequelize'
2 import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
3 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
4 import { CONSTRAINTS_FIELDS } from '../../initializers'
5 import { AccountModel } from '../account/account'
6 import { ActorModel } from '../activitypub/actor'
7 import { throwIfNotValid } from '../utils'
8 import { VideoModel } from './video'
9 import { VideoChannelModel } from './video-channel'
13 WITH_ACTOR = 'WITH_ACTOR'
20 model: () => ActorModel,
24 model: () => VideoModel,
29 [ScopeNames.WITH_ACTOR]: {
32 model: () => ActorModel,
39 tableName: 'videoShare',
53 export class VideoShareModel extends Model<VideoShareModel> {
56 @Is('VideoShareUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
57 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_SHARE.URL.max))
66 @ForeignKey(() => ActorModel)
70 @BelongsTo(() => ActorModel, {
78 @ForeignKey(() => VideoModel)
82 @BelongsTo(() => VideoModel, {
90 static load (actorId: number, videoId: number, t: Sequelize.Transaction) {
91 return VideoShareModel.scope(ScopeNames.WITH_ACTOR).findOne({
100 static loadActorsByShare (videoId: number, t: Sequelize.Transaction) {
114 return VideoShareModel.scope(ScopeNames.FULL).findAll(query)
115 .then(res => res.map(r => r.Actor))
118 static loadActorsByVideoOwner (actorOwnerId: number, t: Sequelize.Transaction) {
133 model: VideoChannelModel.unscoped(),
138 model: AccountModel.unscoped(),
141 actorId: actorOwnerId
152 return VideoShareModel.scope(ScopeNames.FULL).findAll(query)
153 .then(res => res.map(r => r.Actor))