1 import * as Sequelize from 'sequelize'
2 import { Op } from 'sequelize'
3 import * as Bluebird from 'bluebird'
4 import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
5 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
6 import { CONSTRAINTS_FIELDS } from '../../initializers'
7 import { AccountModel } from '../account/account'
8 import { ActorModel } from '../activitypub/actor'
9 import { throwIfNotValid } from '../utils'
10 import { VideoModel } from './video'
11 import { VideoChannelModel } from './video-channel'
15 WITH_ACTOR = 'WITH_ACTOR'
22 model: () => ActorModel,
26 model: () => VideoModel,
31 [ScopeNames.WITH_ACTOR]: {
34 model: () => ActorModel,
41 tableName: 'videoShare',
55 export class VideoShareModel extends Model<VideoShareModel> {
58 @Is('VideoShareUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
59 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_SHARE.URL.max))
68 @ForeignKey(() => ActorModel)
72 @BelongsTo(() => ActorModel, {
80 @ForeignKey(() => VideoModel)
84 @BelongsTo(() => VideoModel, {
92 static load (actorId: number, videoId: number, t?: Sequelize.Transaction) {
93 return VideoShareModel.scope(ScopeNames.WITH_ACTOR).findOne({
102 static loadByUrl (url: string, t: Sequelize.Transaction) {
103 return VideoShareModel.scope(ScopeNames.FULL).findOne({
111 static loadActorsByShare (videoId: number, t: Sequelize.Transaction) {
125 return VideoShareModel.scope(ScopeNames.FULL).findAll(query)
126 .then(res => res.map(r => r.Actor))
129 static loadActorsWhoSharedVideosOf (actorOwnerId: number, t: Sequelize.Transaction): Bluebird<ActorModel[]> {
144 model: VideoChannelModel.unscoped(),
149 model: AccountModel.unscoped(),
152 actorId: actorOwnerId
163 return VideoShareModel.scope(ScopeNames.FULL).findAll(query)
164 .then(res => res.map(r => r.Actor))
167 static loadActorsByVideoChannel (videoChannelId: number, t: Sequelize.Transaction): Bluebird<ActorModel[]> {
180 channelId: videoChannelId
187 return VideoShareModel.scope(ScopeNames.FULL)
189 .then(res => res.map(r => r.Actor))
192 static listAndCountByVideoId (videoId: number, start: number, count: number, t?: Sequelize.Transaction) {
202 return VideoShareModel.findAndCountAll(query)
205 static cleanOldSharesOf (videoId: number, beforeUpdatedAt: Date) {
209 [Op.lt]: beforeUpdatedAt
215 return VideoShareModel.destroy(query)