1 import * as Bluebird from 'bluebird'
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/constants'
5 import { AccountModel } from '../account/account'
6 import { ActorModel } from '../activitypub/actor'
7 import { buildLocalActorIdsIn, throwIfNotValid } from '../utils'
8 import { VideoModel } from './video'
9 import { VideoChannelModel } from './video-channel'
10 import { Op, Transaction } from 'sequelize'
11 import { MVideoShareActor, MVideoShareFull } from '../../typings/models/video'
12 import { MActorDefault } from '../../typings/models'
16 WITH_ACTOR = 'WITH_ACTOR'
32 [ScopeNames.WITH_ACTOR]: {
42 tableName: 'videoShare',
56 export class VideoShareModel extends Model<VideoShareModel> {
59 @Is('VideoShareUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
60 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_SHARE.URL.max))
69 @ForeignKey(() => ActorModel)
73 @BelongsTo(() => ActorModel, {
81 @ForeignKey(() => VideoModel)
85 @BelongsTo(() => VideoModel, {
93 static load (actorId: number | string, videoId: number | string, t?: Transaction): Bluebird<MVideoShareActor> {
94 return VideoShareModel.scope(ScopeNames.WITH_ACTOR).findOne({
103 static loadByUrl (url: string, t: Transaction): Bluebird<MVideoShareFull> {
104 return VideoShareModel.scope(ScopeNames.FULL).findOne({
112 static loadActorsByShare (videoId: number, t: Transaction): Bluebird<MActorDefault[]> {
126 return VideoShareModel.scope(ScopeNames.FULL).findAll(query)
127 .then((res: MVideoShareFull[]) => res.map(r => r.Actor))
130 static loadActorsWhoSharedVideosOf (actorOwnerId: number, t: Transaction): Bluebird<MActorDefault[]> {
145 model: VideoChannelModel.unscoped(),
150 model: AccountModel.unscoped(),
153 actorId: actorOwnerId
164 return VideoShareModel.scope(ScopeNames.FULL).findAll(query)
165 .then(res => res.map(r => r.Actor))
168 static loadActorsByVideoChannel (videoChannelId: number, t: Transaction): Bluebird<MActorDefault[]> {
181 channelId: videoChannelId
188 return VideoShareModel.scope(ScopeNames.FULL)
190 .then(res => res.map(r => r.Actor))
193 static listAndCountByVideoId (videoId: number, start: number, count: number, t?: Transaction) {
203 return VideoShareModel.findAndCountAll(query)
206 static cleanOldSharesOf (videoId: number, beforeUpdatedAt: Date) {
210 [Op.lt]: beforeUpdatedAt
214 [Op.notIn]: buildLocalActorIdsIn()
219 return VideoShareModel.destroy(query)