1 import { literal, Op, QueryTypes, Transaction } from 'sequelize'
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 { MActorDefault } from '../../types/models'
6 import { MVideoShareActor, MVideoShareFull } from '../../types/models/video'
7 import { ActorModel } from '../activitypub/actor'
8 import { buildLocalActorIdsIn, throwIfNotValid } from '../utils'
9 import { VideoModel } from './video'
13 WITH_ACTOR = 'WITH_ACTOR'
29 [ScopeNames.WITH_ACTOR]: {
39 tableName: 'videoShare',
53 export class VideoShareModel extends Model {
56 @Is('VideoShareUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
57 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_SHARE.URL.max))
66 @ForeignKey(() => ActorModel)
70 @BelongsTo(() => ActorModel, {
78 @ForeignKey(() => VideoModel)
82 @BelongsTo(() => VideoModel, {
90 static load (actorId: number | string, videoId: number | string, t?: Transaction): Promise<MVideoShareActor> {
91 return VideoShareModel.scope(ScopeNames.WITH_ACTOR).findOne({
100 static loadByUrl (url: string, t: Transaction): Promise<MVideoShareFull> {
101 return VideoShareModel.scope(ScopeNames.FULL).findOne({
109 static loadActorsByShare (videoId: number, t: Transaction): Promise<MActorDefault[]> {
123 return VideoShareModel.scope(ScopeNames.FULL).findAll(query)
124 .then((res: MVideoShareFull[]) => res.map(r => r.Actor))
127 static loadActorsWhoSharedVideosOf (actorOwnerId: number, t: Transaction): Promise<MActorDefault[]> {
128 const safeOwnerId = parseInt(actorOwnerId + '', 10)
130 // /!\ On actor model
136 ` SELECT 1 FROM "videoShare" ` +
137 ` INNER JOIN "video" ON "videoShare"."videoId" = "video"."id" ` +
138 ` INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ` +
139 ` INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId" ` +
140 ` WHERE "videoShare"."actorId" = "ActorModel"."id" AND "account"."actorId" = ${safeOwnerId} ` +
149 return ActorModel.findAll(query)
152 static loadActorsByVideoChannel (videoChannelId: number, t: Transaction): Promise<MActorDefault[]> {
153 const safeChannelId = parseInt(videoChannelId + '', 10)
155 // /!\ On actor model
161 ` SELECT 1 FROM "videoShare" ` +
162 ` INNER JOIN "video" ON "videoShare"."videoId" = "video"."id" ` +
163 ` WHERE "videoShare"."actorId" = "ActorModel"."id" AND "video"."channelId" = ${safeChannelId} ` +
172 return ActorModel.findAll(query)
175 static listAndCountByVideoId (videoId: number, start: number, count: number, t?: Transaction) {
185 return VideoShareModel.findAndCountAll(query)
188 static listRemoteShareUrlsOfLocalVideos () {
189 const query = `SELECT "videoShare".url FROM "videoShare" ` +
190 `INNER JOIN actor ON actor.id = "videoShare"."actorId" AND actor."serverId" IS NOT NULL ` +
191 `INNER JOIN video ON video.id = "videoShare"."videoId" AND video.remote IS FALSE`
193 return VideoShareModel.sequelize.query<{ url: string }>(query, {
194 type: QueryTypes.SELECT,
196 }).then(rows => rows.map(r => r.url))
199 static cleanOldSharesOf (videoId: number, beforeUpdatedAt: Date) {
203 [Op.lt]: beforeUpdatedAt
207 [Op.notIn]: buildLocalActorIdsIn()
212 return VideoShareModel.destroy(query)