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 { throwIfNotValid } from '../utils'
8 import { VideoModel } from './video'
9 import { VideoChannelModel } from './video-channel'
10 import { Op, Transaction } from 'sequelize'
14 WITH_ACTOR = 'WITH_ACTOR'
30 [ScopeNames.WITH_ACTOR]: {
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?: Transaction) {
92 return VideoShareModel.scope(ScopeNames.WITH_ACTOR).findOne({
101 static loadByUrl (url: string, t: Transaction) {
102 return VideoShareModel.scope(ScopeNames.FULL).findOne({
110 static loadActorsByShare (videoId: number, t: Transaction) {
124 return VideoShareModel.scope(ScopeNames.FULL).findAll(query)
125 .then(res => res.map(r => r.Actor))
128 static loadActorsWhoSharedVideosOf (actorOwnerId: number, t: Transaction): Bluebird<ActorModel[]> {
143 model: VideoChannelModel.unscoped(),
148 model: AccountModel.unscoped(),
151 actorId: actorOwnerId
162 return VideoShareModel.scope(ScopeNames.FULL).findAll(query)
163 .then(res => res.map(r => r.Actor))
166 static loadActorsByVideoChannel (videoChannelId: number, t: Transaction): Bluebird<ActorModel[]> {
179 channelId: videoChannelId
186 return VideoShareModel.scope(ScopeNames.FULL)
188 .then(res => res.map(r => r.Actor))
191 static listAndCountByVideoId (videoId: number, start: number, count: number, t?: Transaction) {
201 return VideoShareModel.findAndCountAll(query)
204 static cleanOldSharesOf (videoId: number, beforeUpdatedAt: Date) {
208 [Op.lt]: beforeUpdatedAt
214 model: ActorModel.unscoped(),
225 return VideoShareModel.destroy(query)