1
0
Fork 0
peertube/server/models/video/video-share.ts

215 lines
5.3 KiB
TypeScript
Raw Normal View History

import { literal, Op, QueryTypes, Transaction } from 'sequelize'
import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
2020-12-08 08:30:29 -05:00
import { MActorDefault } from '../../types/models'
import { MVideoShareActor, MVideoShareFull } from '../../types/models/video'
2017-12-14 11:38:41 -05:00
import { ActorModel } from '../activitypub/actor'
2019-08-06 11:19:53 -04:00
import { buildLocalActorIdsIn, throwIfNotValid } from '../utils'
2017-12-12 11:53:50 -05:00
import { VideoModel } from './video'
2017-11-15 11:56:21 -05:00
2017-12-14 04:07:57 -05:00
enum ScopeNames {
FULL = 'FULL',
2017-12-14 11:38:41 -05:00
WITH_ACTOR = 'WITH_ACTOR'
2017-12-14 04:07:57 -05:00
}
2019-04-23 03:50:57 -04:00
@Scopes(() => ({
2017-12-14 04:07:57 -05:00
[ScopeNames.FULL]: {
include: [
{
2019-04-23 03:50:57 -04:00
model: ActorModel,
2017-12-14 04:07:57 -05:00
required: true
},
{
2019-04-23 03:50:57 -04:00
model: VideoModel,
2017-12-14 04:07:57 -05:00
required: true
}
]
},
2017-12-14 11:38:41 -05:00
[ScopeNames.WITH_ACTOR]: {
2017-12-14 04:07:57 -05:00
include: [
{
2019-04-23 03:50:57 -04:00
model: ActorModel,
2017-12-14 04:07:57 -05:00
required: true
}
]
}
2019-04-23 03:50:57 -04:00
}))
2017-12-12 11:53:50 -05:00
@Table({
tableName: 'videoShare',
indexes: [
2017-11-15 11:56:21 -05:00
{
2017-12-14 11:38:41 -05:00
fields: [ 'actorId' ]
2017-12-12 11:53:50 -05:00
},
{
fields: [ 'videoId' ]
},
{
fields: [ 'url' ],
unique: true
2017-11-15 11:56:21 -05:00
}
]
2017-12-12 11:53:50 -05:00
})
2020-12-08 08:30:29 -05:00
export class VideoShareModel extends Model {
@AllowNull(false)
@Is('VideoShareUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
@Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_SHARE.URL.max))
url: string
2017-12-12 11:53:50 -05:00
@CreatedAt
createdAt: Date
2017-11-15 11:56:21 -05:00
2017-12-12 11:53:50 -05:00
@UpdatedAt
updatedAt: Date
2017-11-15 11:56:21 -05:00
2017-12-14 11:38:41 -05:00
@ForeignKey(() => ActorModel)
2017-12-12 11:53:50 -05:00
@Column
2017-12-14 11:38:41 -05:00
actorId: number
2017-11-15 11:56:21 -05:00
2017-12-14 11:38:41 -05:00
@BelongsTo(() => ActorModel, {
2017-11-15 11:56:21 -05:00
foreignKey: {
allowNull: false
},
onDelete: 'cascade'
})
2017-12-14 11:38:41 -05:00
Actor: ActorModel
2017-11-15 11:56:21 -05:00
2017-12-12 11:53:50 -05:00
@ForeignKey(() => VideoModel)
@Column
videoId: number
@BelongsTo(() => VideoModel, {
2017-11-15 11:56:21 -05:00
foreignKey: {
2017-12-12 11:53:50 -05:00
allowNull: false
2017-11-15 11:56:21 -05:00
},
onDelete: 'cascade'
})
2017-12-12 11:53:50 -05:00
Video: VideoModel
2020-12-08 08:30:29 -05:00
static load (actorId: number | string, videoId: number | string, t?: Transaction): Promise<MVideoShareActor> {
2017-12-14 11:38:41 -05:00
return VideoShareModel.scope(ScopeNames.WITH_ACTOR).findOne({
2017-12-12 11:53:50 -05:00
where: {
2017-12-14 11:38:41 -05:00
actorId,
2017-12-12 11:53:50 -05:00
videoId
},
transaction: t
})
2017-11-16 09:55:01 -05:00
}
2020-12-08 08:30:29 -05:00
static loadByUrl (url: string, t: Transaction): Promise<MVideoShareFull> {
return VideoShareModel.scope(ScopeNames.FULL).findOne({
2018-05-11 09:10:13 -04:00
where: {
url
},
transaction: t
})
}
2020-12-08 08:30:29 -05:00
static loadActorsByShare (videoId: number, t: Transaction): Promise<MActorDefault[]> {
2017-12-12 11:53:50 -05:00
const query = {
where: {
videoId
},
include: [
{
2017-12-14 11:38:41 -05:00
model: ActorModel,
2017-12-12 11:53:50 -05:00
required: true
}
],
transaction: t
}
2017-12-14 04:07:57 -05:00
return VideoShareModel.scope(ScopeNames.FULL).findAll(query)
2020-03-19 05:54:02 -04:00
.then((res: MVideoShareFull[]) => res.map(r => r.Actor))
2017-12-12 11:53:50 -05:00
}
2018-01-03 10:38:50 -05:00
2020-12-08 08:30:29 -05:00
static loadActorsWhoSharedVideosOf (actorOwnerId: number, t: Transaction): Promise<MActorDefault[]> {
2020-03-19 05:54:02 -04:00
const safeOwnerId = parseInt(actorOwnerId + '', 10)
// /!\ On actor model
2018-01-03 10:38:50 -05:00
const query = {
2020-03-19 05:54:02 -04:00
where: {
[Op.and]: [
literal(
`EXISTS (` +
` SELECT 1 FROM "videoShare" ` +
` INNER JOIN "video" ON "videoShare"."videoId" = "video"."id" ` +
` INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ` +
` INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId" ` +
` WHERE "videoShare"."actorId" = "ActorModel"."id" AND "account"."actorId" = ${safeOwnerId} ` +
` LIMIT 1` +
`)`
)
]
},
2018-01-03 10:38:50 -05:00
transaction: t
}
2020-03-19 05:54:02 -04:00
return ActorModel.findAll(query)
2018-01-03 10:38:50 -05:00
}
2020-12-08 08:30:29 -05:00
static loadActorsByVideoChannel (videoChannelId: number, t: Transaction): Promise<MActorDefault[]> {
2020-03-19 05:54:02 -04:00
const safeChannelId = parseInt(videoChannelId + '', 10)
// /!\ On actor model
const query = {
2020-03-19 05:54:02 -04:00
where: {
[Op.and]: [
literal(
`EXISTS (` +
` SELECT 1 FROM "videoShare" ` +
` INNER JOIN "video" ON "videoShare"."videoId" = "video"."id" ` +
` WHERE "videoShare"."actorId" = "ActorModel"."id" AND "video"."channelId" = ${safeChannelId} ` +
` LIMIT 1` +
`)`
)
]
},
transaction: t
}
2020-03-19 05:54:02 -04:00
return ActorModel.findAll(query)
}
2019-08-01 04:15:28 -04:00
static listAndCountByVideoId (videoId: number, start: number, count: number, t?: Transaction) {
const query = {
offset: start,
limit: count,
where: {
videoId
},
transaction: t
}
return VideoShareModel.findAndCountAll(query)
}
2019-03-19 11:23:02 -04:00
static listRemoteShareUrlsOfLocalVideos () {
const query = `SELECT "videoShare".url FROM "videoShare" ` +
`INNER JOIN actor ON actor.id = "videoShare"."actorId" AND actor."serverId" IS NOT NULL ` +
`INNER JOIN video ON video.id = "videoShare"."videoId" AND video.remote IS FALSE`
return VideoShareModel.sequelize.query<{ url: string }>(query, {
type: QueryTypes.SELECT,
raw: true
}).then(rows => rows.map(r => r.url))
}
2019-03-19 11:23:02 -04:00
static cleanOldSharesOf (videoId: number, beforeUpdatedAt: Date) {
const query = {
where: {
updatedAt: {
2019-08-01 04:15:28 -04:00
[Op.lt]: beforeUpdatedAt
2019-03-19 11:23:02 -04:00
},
2019-08-06 11:19:53 -04:00
videoId,
actorId: {
[Op.notIn]: buildLocalActorIdsIn()
2019-08-01 04:15:28 -04:00
}
2019-08-06 11:19:53 -04:00
}
2019-03-19 11:23:02 -04:00
}
return VideoShareModel.destroy(query)
}
2017-11-16 09:55:01 -05:00
}