2021-02-26 09:26:27 -06:00
|
|
|
import { literal, Op, QueryTypes, Transaction } from 'sequelize'
|
2024-02-22 03:12:04 -06:00
|
|
|
import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
|
2023-07-31 07:34:36 -05:00
|
|
|
import { forceNumber } from '@peertube/peertube-core-utils'
|
|
|
|
import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc.js'
|
|
|
|
import { CONSTRAINTS_FIELDS } from '../../initializers/constants.js'
|
|
|
|
import { MActorDefault, MActorFollowersUrl, MActorId } from '../../types/models/index.js'
|
|
|
|
import { MVideoShareActor, MVideoShareFull } from '../../types/models/video/index.js'
|
|
|
|
import { ActorModel } from '../actor/actor.js'
|
2024-02-22 03:12:04 -06:00
|
|
|
import { buildLocalActorIdsIn, SequelizeModel, throwIfNotValid } from '../shared/index.js'
|
2023-07-31 07:34:36 -05:00
|
|
|
import { VideoModel } from './video.js'
|
2017-11-15 10:56:21 -06:00
|
|
|
|
2017-12-14 03:07:57 -06:00
|
|
|
enum ScopeNames {
|
|
|
|
FULL = 'FULL',
|
2017-12-14 10:38:41 -06:00
|
|
|
WITH_ACTOR = 'WITH_ACTOR'
|
2017-12-14 03:07:57 -06:00
|
|
|
}
|
|
|
|
|
2019-04-23 02:50:57 -05:00
|
|
|
@Scopes(() => ({
|
2017-12-14 03:07:57 -06:00
|
|
|
[ScopeNames.FULL]: {
|
|
|
|
include: [
|
|
|
|
{
|
2019-04-23 02:50:57 -05:00
|
|
|
model: ActorModel,
|
2017-12-14 03:07:57 -06:00
|
|
|
required: true
|
|
|
|
},
|
|
|
|
{
|
2019-04-23 02:50:57 -05:00
|
|
|
model: VideoModel,
|
2017-12-14 03:07:57 -06:00
|
|
|
required: true
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
2017-12-14 10:38:41 -06:00
|
|
|
[ScopeNames.WITH_ACTOR]: {
|
2017-12-14 03:07:57 -06:00
|
|
|
include: [
|
|
|
|
{
|
2019-04-23 02:50:57 -05:00
|
|
|
model: ActorModel,
|
2017-12-14 03:07:57 -06:00
|
|
|
required: true
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
2019-04-23 02:50:57 -05:00
|
|
|
}))
|
2017-12-12 10:53:50 -06:00
|
|
|
@Table({
|
|
|
|
tableName: 'videoShare',
|
|
|
|
indexes: [
|
2017-11-15 10:56:21 -06:00
|
|
|
{
|
2017-12-14 10:38:41 -06:00
|
|
|
fields: [ 'actorId' ]
|
2017-12-12 10:53:50 -06:00
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [ 'videoId' ]
|
2018-01-26 08:49:57 -06:00
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [ 'url' ],
|
|
|
|
unique: true
|
2017-11-15 10:56:21 -06:00
|
|
|
}
|
|
|
|
]
|
2017-12-12 10:53:50 -06:00
|
|
|
})
|
2024-02-22 03:12:04 -06:00
|
|
|
export class VideoShareModel extends SequelizeModel<VideoShareModel> {
|
2018-01-26 08:49:57 -06:00
|
|
|
|
|
|
|
@AllowNull(false)
|
|
|
|
@Is('VideoShareUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
|
|
|
|
@Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_SHARE.URL.max))
|
|
|
|
url: string
|
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
@CreatedAt
|
|
|
|
createdAt: Date
|
2017-11-15 10:56:21 -06:00
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
@UpdatedAt
|
|
|
|
updatedAt: Date
|
2017-11-15 10:56:21 -06:00
|
|
|
|
2017-12-14 10:38:41 -06:00
|
|
|
@ForeignKey(() => ActorModel)
|
2017-12-12 10:53:50 -06:00
|
|
|
@Column
|
2017-12-14 10:38:41 -06:00
|
|
|
actorId: number
|
2017-11-15 10:56:21 -06:00
|
|
|
|
2017-12-14 10:38:41 -06:00
|
|
|
@BelongsTo(() => ActorModel, {
|
2017-11-15 10:56:21 -06:00
|
|
|
foreignKey: {
|
|
|
|
allowNull: false
|
|
|
|
},
|
|
|
|
onDelete: 'cascade'
|
|
|
|
})
|
2023-07-31 07:34:36 -05:00
|
|
|
Actor: Awaited<ActorModel>
|
2017-11-15 10:56:21 -06:00
|
|
|
|
2017-12-12 10:53:50 -06:00
|
|
|
@ForeignKey(() => VideoModel)
|
|
|
|
@Column
|
|
|
|
videoId: number
|
|
|
|
|
|
|
|
@BelongsTo(() => VideoModel, {
|
2017-11-15 10:56:21 -06:00
|
|
|
foreignKey: {
|
2017-12-12 10:53:50 -06:00
|
|
|
allowNull: false
|
2017-11-15 10:56:21 -06:00
|
|
|
},
|
|
|
|
onDelete: 'cascade'
|
|
|
|
})
|
2023-07-31 07:34:36 -05:00
|
|
|
Video: Awaited<VideoModel>
|
2017-11-27 07:44:51 -06:00
|
|
|
|
2020-12-08 07:30:29 -06:00
|
|
|
static load (actorId: number | string, videoId: number | string, t?: Transaction): Promise<MVideoShareActor> {
|
2017-12-14 10:38:41 -06:00
|
|
|
return VideoShareModel.scope(ScopeNames.WITH_ACTOR).findOne({
|
2017-12-12 10:53:50 -06:00
|
|
|
where: {
|
2017-12-14 10:38:41 -06:00
|
|
|
actorId,
|
2017-12-12 10:53:50 -06:00
|
|
|
videoId
|
|
|
|
},
|
|
|
|
transaction: t
|
|
|
|
})
|
2017-11-16 08:55:01 -06:00
|
|
|
}
|
|
|
|
|
2020-12-08 07:30:29 -06:00
|
|
|
static loadByUrl (url: string, t: Transaction): Promise<MVideoShareFull> {
|
2018-05-31 03:23:56 -05:00
|
|
|
return VideoShareModel.scope(ScopeNames.FULL).findOne({
|
2018-05-11 08:10:13 -05:00
|
|
|
where: {
|
|
|
|
url
|
|
|
|
},
|
|
|
|
transaction: t
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-06-17 03:49:37 -05:00
|
|
|
static listActorIdsAndFollowerUrlsByShare (videoId: number, t: Transaction) {
|
|
|
|
const query = `SELECT "actor"."id" AS "id", "actor"."followersUrl" AS "followersUrl" ` +
|
|
|
|
`FROM "videoShare" ` +
|
|
|
|
`INNER JOIN "actor" ON "actor"."id" = "videoShare"."actorId" ` +
|
|
|
|
`WHERE "videoShare"."videoId" = :videoId`
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
type: QueryTypes.SELECT as QueryTypes.SELECT,
|
|
|
|
replacements: { videoId },
|
2017-12-12 10:53:50 -06:00
|
|
|
transaction: t
|
|
|
|
}
|
|
|
|
|
2022-06-17 03:49:37 -05:00
|
|
|
return VideoShareModel.sequelize.query<MActorId & MActorFollowersUrl>(query, options)
|
2017-12-12 10:53:50 -06:00
|
|
|
}
|
2018-01-03 09:38:50 -06:00
|
|
|
|
2020-12-08 07:30:29 -06:00
|
|
|
static loadActorsWhoSharedVideosOf (actorOwnerId: number, t: Transaction): Promise<MActorDefault[]> {
|
2022-11-15 07:41:55 -06:00
|
|
|
const safeOwnerId = forceNumber(actorOwnerId)
|
2020-03-19 04:54:02 -05:00
|
|
|
|
|
|
|
// /!\ On actor model
|
2018-01-03 09:38:50 -06:00
|
|
|
const query = {
|
2020-03-19 04:54:02 -05: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 09:38:50 -06:00
|
|
|
transaction: t
|
|
|
|
}
|
|
|
|
|
2020-03-19 04:54:02 -05:00
|
|
|
return ActorModel.findAll(query)
|
2018-01-03 09:38:50 -06:00
|
|
|
}
|
2018-02-15 07:46:26 -06:00
|
|
|
|
2020-12-08 07:30:29 -06:00
|
|
|
static loadActorsByVideoChannel (videoChannelId: number, t: Transaction): Promise<MActorDefault[]> {
|
2022-11-15 07:41:55 -06:00
|
|
|
const safeChannelId = forceNumber(videoChannelId)
|
2020-03-19 04:54:02 -05:00
|
|
|
|
|
|
|
// /!\ On actor model
|
2018-02-15 07:46:26 -06:00
|
|
|
const query = {
|
2020-03-19 04:54:02 -05: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` +
|
|
|
|
`)`
|
|
|
|
)
|
|
|
|
]
|
|
|
|
},
|
2018-02-15 07:46:26 -06:00
|
|
|
transaction: t
|
|
|
|
}
|
|
|
|
|
2020-03-19 04:54:02 -05:00
|
|
|
return ActorModel.findAll(query)
|
2018-02-15 07:46:26 -06:00
|
|
|
}
|
2018-05-25 09:21:16 -05:00
|
|
|
|
2019-08-01 03:15:28 -05:00
|
|
|
static listAndCountByVideoId (videoId: number, start: number, count: number, t?: Transaction) {
|
2018-05-25 09:21:16 -05:00
|
|
|
const query = {
|
2018-06-13 10:43:30 -05:00
|
|
|
offset: start,
|
|
|
|
limit: count,
|
2018-05-25 09:21:16 -05:00
|
|
|
where: {
|
|
|
|
videoId
|
|
|
|
},
|
|
|
|
transaction: t
|
|
|
|
}
|
|
|
|
|
2022-02-28 01:34:43 -06:00
|
|
|
return Promise.all([
|
|
|
|
VideoShareModel.count(query),
|
|
|
|
VideoShareModel.findAll(query)
|
|
|
|
]).then(([ total, data ]) => ({ total, data }))
|
2018-05-25 09:21:16 -05:00
|
|
|
}
|
2019-03-19 10:23:02 -05:00
|
|
|
|
2021-02-26 09:26:27 -06: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 10:23:02 -05:00
|
|
|
static cleanOldSharesOf (videoId: number, beforeUpdatedAt: Date) {
|
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
updatedAt: {
|
2019-08-01 03:15:28 -05:00
|
|
|
[Op.lt]: beforeUpdatedAt
|
2019-03-19 10:23:02 -05:00
|
|
|
},
|
2019-08-06 10:19:53 -05:00
|
|
|
videoId,
|
|
|
|
actorId: {
|
|
|
|
[Op.notIn]: buildLocalActorIdsIn()
|
2019-08-01 03:15:28 -05:00
|
|
|
}
|
2019-08-06 10:19:53 -05:00
|
|
|
}
|
2019-03-19 10:23:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return VideoShareModel.destroy(query)
|
|
|
|
}
|
2017-11-16 08:55:01 -06:00
|
|
|
}
|