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 { ActorModel } from '../activitypub/actor'
6 import { buildLocalActorIdsIn, throwIfNotValid } from '../utils'
7 import { VideoModel } from './video'
8 import { literal, Op, Transaction } from 'sequelize'
9 import { MVideoShareActor, MVideoShareFull } from '../../typings/models/video'
10 import { MActorDefault } from '../../typings/models'
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 | string, videoId: number | string, t?: Transaction): Bluebird<MVideoShareActor> {
92 return VideoShareModel.scope(ScopeNames.WITH_ACTOR).findOne({
101 static loadByUrl (url: string, t: Transaction): Bluebird<MVideoShareFull> {
102 return VideoShareModel.scope(ScopeNames.FULL).findOne({
110 static loadActorsByShare (videoId: number, t: Transaction): Bluebird<MActorDefault[]> {
124 return VideoShareModel.scope(ScopeNames.FULL).findAll(query)
125 .then((res: MVideoShareFull[]) => res.map(r => r.Actor))
128 static loadActorsWhoSharedVideosOf (actorOwnerId: number, t: Transaction): Bluebird<MActorDefault[]> {
129 const safeOwnerId = parseInt(actorOwnerId + '', 10)
131 // /!\ On actor model
137 ` SELECT 1 FROM "videoShare" ` +
138 ` INNER JOIN "video" ON "videoShare"."videoId" = "video"."id" ` +
139 ` INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ` +
140 ` INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId" ` +
141 ` WHERE "videoShare"."actorId" = "ActorModel"."id" AND "account"."actorId" = ${safeOwnerId} ` +
150 return ActorModel.findAll(query)
153 static loadActorsByVideoChannel (videoChannelId: number, t: Transaction): Bluebird<MActorDefault[]> {
154 const safeChannelId = parseInt(videoChannelId + '', 10)
156 // /!\ On actor model
162 ` SELECT 1 FROM "videoShare" ` +
163 ` INNER JOIN "video" ON "videoShare"."videoId" = "video"."id" ` +
164 ` WHERE "videoShare"."actorId" = "ActorModel"."id" AND "video"."channelId" = ${safeChannelId} ` +
173 return ActorModel.findAll(query)
176 static listAndCountByVideoId (videoId: number, start: number, count: number, t?: Transaction) {
186 return VideoShareModel.findAndCountAll(query)
189 static cleanOldSharesOf (videoId: number, beforeUpdatedAt: Date) {
193 [Op.lt]: beforeUpdatedAt
197 [Op.notIn]: buildLocalActorIdsIn()
202 return VideoShareModel.destroy(query)