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 loadByUrl (url: string, t: Sequelize.Transaction) {
102 return VideoShareModel.scope(ScopeNames.FULL).findOne({
110 static loadActorsByShare (videoId: number, t: Sequelize.Transaction) {
124 return VideoShareModel.scope(ScopeNames.FULL).findAll(query)
125 .then(res => res.map(r => r.Actor))
128 static loadActorsWhoSharedVideosOf (actorOwnerId: number, t: Sequelize.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: Sequelize.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?: Sequelize.Transaction) {
201 return VideoShareModel.findAndCountAll(query)
204 static cleanOldSharesOf (videoId: number, beforeUpdatedAt: Date) {
208 [Sequelize.Op.lt]: beforeUpdatedAt
214 return VideoShareModel.destroy(query)