2020-12-08 07:30:29 -06:00
|
|
|
import { AggregateOptions, Op, ScopeOptions, Sequelize, Transaction } from 'sequelize'
|
2019-02-26 03:55:40 -06:00
|
|
|
import {
|
|
|
|
AllowNull,
|
|
|
|
BelongsTo,
|
|
|
|
Column,
|
|
|
|
CreatedAt,
|
|
|
|
DataType,
|
|
|
|
Default,
|
|
|
|
ForeignKey,
|
|
|
|
Is,
|
|
|
|
IsInt,
|
|
|
|
Min,
|
|
|
|
Model,
|
|
|
|
Table,
|
|
|
|
UpdatedAt
|
|
|
|
} from 'sequelize-typescript'
|
2020-01-07 07:56:07 -06:00
|
|
|
import validator from 'validator'
|
2020-12-08 07:30:29 -06:00
|
|
|
import { MUserAccountId } from '@server/types/models'
|
2019-08-20 12:05:31 -05:00
|
|
|
import {
|
|
|
|
MVideoPlaylistElement,
|
|
|
|
MVideoPlaylistElementAP,
|
|
|
|
MVideoPlaylistElementFormattable,
|
2019-08-21 07:31:57 -05:00
|
|
|
MVideoPlaylistElementVideoUrlPlaylistPrivacy,
|
2019-08-20 12:05:31 -05:00
|
|
|
MVideoPlaylistVideoThumbnail
|
2020-06-18 03:45:25 -05:00
|
|
|
} from '@server/types/models/video/video-playlist-element'
|
2022-11-15 07:41:55 -06:00
|
|
|
import { forceNumber } from '@shared/core-utils'
|
2022-02-28 01:34:43 -06:00
|
|
|
import { AttributesOnly } from '@shared/typescript-utils'
|
2020-12-08 07:30:29 -06:00
|
|
|
import { PlaylistElementObject } from '../../../shared/models/activitypub/objects/playlist-element-object'
|
|
|
|
import { VideoPrivacy } from '../../../shared/models/videos'
|
|
|
|
import { VideoPlaylistElement, VideoPlaylistElementType } from '../../../shared/models/videos/playlist/video-playlist-element.model'
|
|
|
|
import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
|
|
|
|
import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
|
|
|
|
import { AccountModel } from '../account/account'
|
2023-01-10 04:09:30 -06:00
|
|
|
import { getSort, throwIfNotValid } from '../shared'
|
2020-12-08 07:30:29 -06:00
|
|
|
import { ForAPIOptions, ScopeNames as VideoScopeNames, VideoModel } from './video'
|
|
|
|
import { VideoPlaylistModel } from './video-playlist'
|
2019-02-26 03:55:40 -06:00
|
|
|
|
|
|
|
@Table({
|
|
|
|
tableName: 'videoPlaylistElement',
|
|
|
|
indexes: [
|
|
|
|
{
|
|
|
|
fields: [ 'videoPlaylistId' ]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [ 'videoId' ]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [ 'url' ],
|
|
|
|
unique: true
|
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
2021-05-12 07:09:04 -05:00
|
|
|
export class VideoPlaylistElementModel extends Model<Partial<AttributesOnly<VideoPlaylistElementModel>>> {
|
2019-02-26 03:55:40 -06:00
|
|
|
@CreatedAt
|
|
|
|
createdAt: Date
|
|
|
|
|
|
|
|
@UpdatedAt
|
|
|
|
updatedAt: Date
|
|
|
|
|
2020-08-17 09:39:32 -05:00
|
|
|
@AllowNull(true)
|
|
|
|
@Is('VideoPlaylistUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url', true))
|
2019-02-26 03:55:40 -06:00
|
|
|
@Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_PLAYLISTS.URL.max))
|
|
|
|
url: string
|
|
|
|
|
|
|
|
@AllowNull(false)
|
|
|
|
@Default(1)
|
|
|
|
@IsInt
|
|
|
|
@Min(1)
|
|
|
|
@Column
|
|
|
|
position: number
|
|
|
|
|
|
|
|
@AllowNull(true)
|
|
|
|
@IsInt
|
|
|
|
@Min(0)
|
|
|
|
@Column
|
|
|
|
startTimestamp: number
|
|
|
|
|
|
|
|
@AllowNull(true)
|
|
|
|
@IsInt
|
|
|
|
@Min(0)
|
|
|
|
@Column
|
|
|
|
stopTimestamp: number
|
|
|
|
|
|
|
|
@ForeignKey(() => VideoPlaylistModel)
|
|
|
|
@Column
|
|
|
|
videoPlaylistId: number
|
|
|
|
|
|
|
|
@BelongsTo(() => VideoPlaylistModel, {
|
|
|
|
foreignKey: {
|
|
|
|
allowNull: false
|
|
|
|
},
|
|
|
|
onDelete: 'CASCADE'
|
|
|
|
})
|
|
|
|
VideoPlaylist: VideoPlaylistModel
|
|
|
|
|
|
|
|
@ForeignKey(() => VideoModel)
|
|
|
|
@Column
|
|
|
|
videoId: number
|
|
|
|
|
|
|
|
@BelongsTo(() => VideoModel, {
|
|
|
|
foreignKey: {
|
2019-07-31 08:57:32 -05:00
|
|
|
allowNull: true
|
2019-02-26 03:55:40 -06:00
|
|
|
},
|
2019-07-31 08:57:32 -05:00
|
|
|
onDelete: 'set null'
|
2019-02-26 03:55:40 -06:00
|
|
|
})
|
|
|
|
Video: VideoModel
|
|
|
|
|
2019-04-18 04:28:17 -05:00
|
|
|
static deleteAllOf (videoPlaylistId: number, transaction?: Transaction) {
|
2019-02-26 03:55:40 -06:00
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
videoPlaylistId
|
|
|
|
},
|
|
|
|
transaction
|
|
|
|
}
|
|
|
|
|
|
|
|
return VideoPlaylistElementModel.destroy(query)
|
|
|
|
}
|
|
|
|
|
2019-07-31 08:57:32 -05:00
|
|
|
static listForApi (options: {
|
2020-01-31 09:56:52 -06:00
|
|
|
start: number
|
|
|
|
count: number
|
|
|
|
videoPlaylistId: number
|
|
|
|
serverAccount: AccountModel
|
2019-08-15 04:53:26 -05:00
|
|
|
user?: MUserAccountId
|
2019-07-31 08:57:32 -05:00
|
|
|
}) {
|
|
|
|
const accountIds = [ options.serverAccount.id ]
|
|
|
|
const videoScope: (ScopeOptions | string)[] = [
|
|
|
|
VideoScopeNames.WITH_BLACKLISTED
|
|
|
|
]
|
|
|
|
|
|
|
|
if (options.user) {
|
|
|
|
accountIds.push(options.user.Account.id)
|
|
|
|
videoScope.push({ method: [ VideoScopeNames.WITH_USER_HISTORY, options.user.id ] })
|
|
|
|
}
|
|
|
|
|
|
|
|
const forApiOptions: ForAPIOptions = { withAccountBlockerIds: accountIds }
|
|
|
|
videoScope.push({
|
|
|
|
method: [
|
|
|
|
VideoScopeNames.FOR_API, forApiOptions
|
|
|
|
]
|
|
|
|
})
|
|
|
|
|
|
|
|
const findQuery = {
|
|
|
|
offset: options.start,
|
|
|
|
limit: options.count,
|
|
|
|
order: getSort('position'),
|
|
|
|
where: {
|
|
|
|
videoPlaylistId: options.videoPlaylistId
|
|
|
|
},
|
|
|
|
include: [
|
|
|
|
{
|
|
|
|
model: VideoModel.scope(videoScope),
|
|
|
|
required: false
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
const countQuery = {
|
|
|
|
where: {
|
|
|
|
videoPlaylistId: options.videoPlaylistId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.all([
|
|
|
|
VideoPlaylistElementModel.count(countQuery),
|
|
|
|
VideoPlaylistElementModel.findAll(findQuery)
|
|
|
|
]).then(([ total, data ]) => ({ total, data }))
|
|
|
|
}
|
|
|
|
|
2020-12-08 07:30:29 -06:00
|
|
|
static loadByPlaylistAndVideo (videoPlaylistId: number, videoId: number): Promise<MVideoPlaylistElement> {
|
2019-02-26 03:55:40 -06:00
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
videoPlaylistId,
|
|
|
|
videoId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return VideoPlaylistElementModel.findOne(query)
|
|
|
|
}
|
|
|
|
|
2020-12-08 07:30:29 -06:00
|
|
|
static loadById (playlistElementId: number | string): Promise<MVideoPlaylistElement> {
|
2019-07-31 08:57:32 -05:00
|
|
|
return VideoPlaylistElementModel.findByPk(playlistElementId)
|
|
|
|
}
|
|
|
|
|
2020-08-17 09:39:32 -05:00
|
|
|
static loadByPlaylistAndElementIdForAP (
|
2019-08-21 07:31:57 -05:00
|
|
|
playlistId: number | string,
|
2020-08-17 09:39:32 -05:00
|
|
|
playlistElementId: number
|
2020-12-08 07:30:29 -06:00
|
|
|
): Promise<MVideoPlaylistElementVideoUrlPlaylistPrivacy> {
|
2022-11-15 07:41:55 -06:00
|
|
|
const playlistWhere = validator.isUUID('' + playlistId)
|
|
|
|
? { uuid: playlistId }
|
|
|
|
: { id: playlistId }
|
2019-02-26 03:55:40 -06:00
|
|
|
|
|
|
|
const query = {
|
|
|
|
include: [
|
|
|
|
{
|
|
|
|
attributes: [ 'privacy' ],
|
|
|
|
model: VideoPlaylistModel.unscoped(),
|
|
|
|
where: playlistWhere
|
|
|
|
},
|
|
|
|
{
|
|
|
|
attributes: [ 'url' ],
|
2020-08-17 09:39:32 -05:00
|
|
|
model: VideoModel.unscoped()
|
2019-02-26 03:55:40 -06:00
|
|
|
}
|
2020-08-17 09:39:32 -05:00
|
|
|
],
|
|
|
|
where: {
|
|
|
|
id: playlistElementId
|
|
|
|
}
|
2019-02-26 03:55:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return VideoPlaylistElementModel.findOne(query)
|
|
|
|
}
|
|
|
|
|
2019-04-18 04:28:17 -05:00
|
|
|
static listUrlsOfForAP (videoPlaylistId: number, start: number, count: number, t?: Transaction) {
|
2022-02-28 01:34:43 -06:00
|
|
|
const getQuery = (forCount: boolean) => {
|
|
|
|
return {
|
|
|
|
attributes: forCount
|
|
|
|
? []
|
|
|
|
: [ 'url' ],
|
|
|
|
offset: start,
|
|
|
|
limit: count,
|
|
|
|
order: getSort('position'),
|
|
|
|
where: {
|
|
|
|
videoPlaylistId
|
|
|
|
},
|
|
|
|
transaction: t
|
|
|
|
}
|
2019-02-26 03:55:40 -06:00
|
|
|
}
|
|
|
|
|
2022-02-28 01:34:43 -06:00
|
|
|
return Promise.all([
|
|
|
|
VideoPlaylistElementModel.count(getQuery(true)),
|
|
|
|
VideoPlaylistElementModel.findAll(getQuery(false))
|
|
|
|
]).then(([ total, rows ]) => ({
|
|
|
|
total,
|
|
|
|
data: rows.map(e => e.url)
|
|
|
|
}))
|
2019-02-26 03:55:40 -06:00
|
|
|
}
|
|
|
|
|
2020-12-08 07:30:29 -06:00
|
|
|
static loadFirstElementWithVideoThumbnail (videoPlaylistId: number): Promise<MVideoPlaylistVideoThumbnail> {
|
2019-08-01 09:54:24 -05:00
|
|
|
const query = {
|
|
|
|
order: getSort('position'),
|
|
|
|
where: {
|
|
|
|
videoPlaylistId
|
|
|
|
},
|
|
|
|
include: [
|
|
|
|
{
|
|
|
|
model: VideoModel.scope(VideoScopeNames.WITH_THUMBNAILS),
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
return VideoPlaylistElementModel
|
|
|
|
.findOne(query)
|
|
|
|
}
|
|
|
|
|
2019-04-18 04:28:17 -05:00
|
|
|
static getNextPositionOf (videoPlaylistId: number, transaction?: Transaction) {
|
|
|
|
const query: AggregateOptions<number> = {
|
2019-02-26 03:55:40 -06:00
|
|
|
where: {
|
|
|
|
videoPlaylistId
|
|
|
|
},
|
|
|
|
transaction
|
|
|
|
}
|
|
|
|
|
|
|
|
return VideoPlaylistElementModel.max('position', query)
|
|
|
|
.then(position => position ? position + 1 : 1)
|
|
|
|
}
|
|
|
|
|
2022-11-15 07:41:55 -06:00
|
|
|
static reassignPositionOf (options: {
|
|
|
|
videoPlaylistId: number
|
|
|
|
firstPosition: number
|
|
|
|
endPosition: number
|
|
|
|
newPosition: number
|
2019-04-18 04:28:17 -05:00
|
|
|
transaction?: Transaction
|
2022-11-15 07:41:55 -06:00
|
|
|
}) {
|
|
|
|
const { videoPlaylistId, firstPosition, endPosition, newPosition, transaction } = options
|
|
|
|
|
2019-02-26 03:55:40 -06:00
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
videoPlaylistId,
|
|
|
|
position: {
|
2019-04-18 04:28:17 -05:00
|
|
|
[Op.gte]: firstPosition,
|
|
|
|
[Op.lte]: endPosition
|
2019-02-26 03:55:40 -06:00
|
|
|
}
|
|
|
|
},
|
2019-02-28 04:14:26 -06:00
|
|
|
transaction,
|
|
|
|
validate: false // We use a literal to update the position
|
2019-02-26 03:55:40 -06:00
|
|
|
}
|
|
|
|
|
2022-11-15 07:41:55 -06:00
|
|
|
const positionQuery = Sequelize.literal(`${forceNumber(newPosition)} + "position" - ${forceNumber(firstPosition)}`)
|
2021-12-08 04:07:19 -06:00
|
|
|
return VideoPlaylistElementModel.update({ position: positionQuery }, query)
|
2019-02-26 03:55:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static increasePositionOf (
|
|
|
|
videoPlaylistId: number,
|
|
|
|
fromPosition: number,
|
|
|
|
by = 1,
|
2019-04-18 04:28:17 -05:00
|
|
|
transaction?: Transaction
|
2019-02-26 03:55:40 -06:00
|
|
|
) {
|
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
videoPlaylistId,
|
|
|
|
position: {
|
2019-04-18 04:28:17 -05:00
|
|
|
[Op.gte]: fromPosition
|
2019-02-26 03:55:40 -06:00
|
|
|
}
|
|
|
|
},
|
|
|
|
transaction
|
|
|
|
}
|
|
|
|
|
|
|
|
return VideoPlaylistElementModel.increment({ position: by }, query)
|
|
|
|
}
|
|
|
|
|
2023-01-19 08:04:10 -06:00
|
|
|
toFormattedJSON (
|
|
|
|
this: MVideoPlaylistElementFormattable,
|
|
|
|
options: { accountId?: number } = {}
|
|
|
|
): VideoPlaylistElement {
|
|
|
|
return {
|
|
|
|
id: this.id,
|
|
|
|
position: this.position,
|
|
|
|
startTimestamp: this.startTimestamp,
|
|
|
|
stopTimestamp: this.stopTimestamp,
|
|
|
|
|
|
|
|
type: this.getType(options.accountId),
|
|
|
|
|
|
|
|
video: this.getVideoElement(options.accountId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getType (this: MVideoPlaylistElementFormattable, accountId?: number) {
|
2019-07-31 08:57:32 -05:00
|
|
|
const video = this.Video
|
|
|
|
|
|
|
|
if (!video) return VideoPlaylistElementType.DELETED
|
|
|
|
|
|
|
|
// Owned video, don't filter it
|
|
|
|
if (accountId && video.VideoChannel.Account.id === accountId) return VideoPlaylistElementType.REGULAR
|
|
|
|
|
2020-03-20 03:55:57 -05:00
|
|
|
// Internal video?
|
|
|
|
if (video.privacy === VideoPrivacy.INTERNAL && accountId) return VideoPlaylistElementType.REGULAR
|
|
|
|
|
2023-06-29 02:48:55 -05:00
|
|
|
// Private, internal and password protected videos cannot be read without appropriate access (ownership, internal)
|
|
|
|
if (new Set([ VideoPrivacy.PRIVATE, VideoPrivacy.INTERNAL, VideoPrivacy.PASSWORD_PROTECTED ]).has(video.privacy)) {
|
|
|
|
return VideoPlaylistElementType.PRIVATE
|
|
|
|
}
|
2019-07-31 08:57:32 -05:00
|
|
|
|
|
|
|
if (video.isBlacklisted() || video.isBlocked()) return VideoPlaylistElementType.UNAVAILABLE
|
|
|
|
|
|
|
|
return VideoPlaylistElementType.REGULAR
|
|
|
|
}
|
|
|
|
|
2023-01-19 08:04:10 -06:00
|
|
|
getVideoElement (this: MVideoPlaylistElementFormattable, accountId?: number) {
|
2019-07-31 08:57:32 -05:00
|
|
|
if (!this.Video) return null
|
2023-01-19 08:04:10 -06:00
|
|
|
if (this.getType(accountId) !== VideoPlaylistElementType.REGULAR) return null
|
2019-07-31 08:57:32 -05:00
|
|
|
|
|
|
|
return this.Video.toFormattedJSON()
|
|
|
|
}
|
|
|
|
|
2019-08-21 07:31:57 -05:00
|
|
|
toActivityPubObject (this: MVideoPlaylistElementAP): PlaylistElementObject {
|
2019-02-26 03:55:40 -06:00
|
|
|
const base: PlaylistElementObject = {
|
|
|
|
id: this.url,
|
|
|
|
type: 'PlaylistElement',
|
|
|
|
|
2022-06-21 01:59:46 -05:00
|
|
|
url: this.Video?.url || null,
|
2019-02-26 03:55:40 -06:00
|
|
|
position: this.position
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.startTimestamp) base.startTimestamp = this.startTimestamp
|
|
|
|
if (this.stopTimestamp) base.stopTimestamp = this.stopTimestamp
|
|
|
|
|
|
|
|
return base
|
|
|
|
}
|
|
|
|
}
|