PeerTube/server/models/video/video-playlist-element.ts

361 lines
9.2 KiB
TypeScript
Raw Normal View History

2019-02-26 10:55:40 +01:00
import {
AllowNull,
BelongsTo,
Column,
CreatedAt,
DataType,
Default,
ForeignKey,
Is,
IsInt,
Min,
Model,
Table,
UpdatedAt
} from 'sequelize-typescript'
2019-07-31 15:57:32 +02:00
import { ForAPIOptions, ScopeNames as VideoScopeNames, VideoModel } from './video'
2019-02-26 10:55:40 +01:00
import { VideoPlaylistModel } from './video-playlist'
import { getSort, throwIfNotValid } from '../utils'
import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
2019-02-26 10:55:40 +01:00
import { PlaylistElementObject } from '../../../shared/models/activitypub/objects/playlist-element-object'
2020-01-07 14:56:07 +01:00
import validator from 'validator'
2019-07-31 15:57:32 +02:00
import { AggregateOptions, Op, ScopeOptions, Sequelize, Transaction } from 'sequelize'
import { VideoPlaylistElement, VideoPlaylistElementType } from '../../../shared/models/videos/playlist/video-playlist-element.model'
import { AccountModel } from '../account/account'
import { VideoPrivacy } from '../../../shared/models/videos'
2019-08-15 11:53:26 +02:00
import * as Bluebird from 'bluebird'
2019-08-20 19:05:31 +02:00
import {
MVideoPlaylistElement,
MVideoPlaylistElementAP,
MVideoPlaylistElementFormattable,
2019-08-21 14:31:57 +02:00
MVideoPlaylistElementVideoUrlPlaylistPrivacy,
2019-08-20 19:05:31 +02:00
MVideoPlaylistVideoThumbnail
2020-06-18 10:45:25 +02:00
} from '@server/types/models/video/video-playlist-element'
import { MUserAccountId } from '@server/types/models'
2019-02-26 10:55:40 +01:00
@Table({
tableName: 'videoPlaylistElement',
indexes: [
{
fields: [ 'videoPlaylistId' ]
},
{
fields: [ 'videoId' ]
},
{
fields: [ 'videoPlaylistId', 'videoId' ],
unique: true
},
{
fields: [ 'url' ],
unique: true
}
]
})
export class VideoPlaylistElementModel extends Model<VideoPlaylistElementModel> {
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
@AllowNull(false)
@Is('VideoPlaylistUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
@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 15:57:32 +02:00
allowNull: true
2019-02-26 10:55:40 +01:00
},
2019-07-31 15:57:32 +02:00
onDelete: 'set null'
2019-02-26 10:55:40 +01:00
})
Video: VideoModel
2019-04-18 11:28:17 +02:00
static deleteAllOf (videoPlaylistId: number, transaction?: Transaction) {
2019-02-26 10:55:40 +01:00
const query = {
where: {
videoPlaylistId
},
transaction
}
return VideoPlaylistElementModel.destroy(query)
}
2019-07-31 15:57:32 +02:00
static listForApi (options: {
2020-01-31 16:56:52 +01:00
start: number
count: number
videoPlaylistId: number
serverAccount: AccountModel
2019-08-15 11:53:26 +02:00
user?: MUserAccountId
2019-07-31 15:57:32 +02: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 }))
}
2019-08-15 11:53:26 +02:00
static loadByPlaylistAndVideo (videoPlaylistId: number, videoId: number): Bluebird<MVideoPlaylistElement> {
2019-02-26 10:55:40 +01:00
const query = {
where: {
videoPlaylistId,
videoId
}
}
return VideoPlaylistElementModel.findOne(query)
}
2019-10-21 14:50:55 +02:00
static loadById (playlistElementId: number | string): Bluebird<MVideoPlaylistElement> {
2019-07-31 15:57:32 +02:00
return VideoPlaylistElementModel.findByPk(playlistElementId)
}
2019-08-21 14:31:57 +02:00
static loadByPlaylistAndVideoForAP (
playlistId: number | string,
videoId: number | string
): Bluebird<MVideoPlaylistElementVideoUrlPlaylistPrivacy> {
2019-02-26 10:55:40 +01:00
const playlistWhere = validator.isUUID('' + playlistId) ? { uuid: playlistId } : { id: playlistId }
const videoWhere = validator.isUUID('' + videoId) ? { uuid: videoId } : { id: videoId }
const query = {
include: [
{
attributes: [ 'privacy' ],
model: VideoPlaylistModel.unscoped(),
where: playlistWhere
},
{
attributes: [ 'url' ],
model: VideoModel.unscoped(),
where: videoWhere
}
]
}
return VideoPlaylistElementModel.findOne(query)
}
2019-04-18 11:28:17 +02:00
static listUrlsOfForAP (videoPlaylistId: number, start: number, count: number, t?: Transaction) {
2019-02-26 10:55:40 +01:00
const query = {
attributes: [ 'url' ],
offset: start,
limit: count,
order: getSort('position'),
where: {
videoPlaylistId
2019-03-05 10:58:44 +01:00
},
transaction: t
2019-02-26 10:55:40 +01:00
}
return VideoPlaylistElementModel
.findAndCountAll(query)
.then(({ rows, count }) => {
return { total: count, data: rows.map(e => e.url) }
})
}
2019-08-15 11:53:26 +02:00
static loadFirstElementWithVideoThumbnail (videoPlaylistId: number): Bluebird<MVideoPlaylistVideoThumbnail> {
const query = {
order: getSort('position'),
where: {
videoPlaylistId
},
include: [
{
model: VideoModel.scope(VideoScopeNames.WITH_THUMBNAILS),
required: true
}
]
}
return VideoPlaylistElementModel
.findOne(query)
}
2019-04-18 11:28:17 +02:00
static getNextPositionOf (videoPlaylistId: number, transaction?: Transaction) {
const query: AggregateOptions<number> = {
2019-02-26 10:55:40 +01:00
where: {
videoPlaylistId
},
transaction
}
return VideoPlaylistElementModel.max('position', query)
.then(position => position ? position + 1 : 1)
}
static reassignPositionOf (
videoPlaylistId: number,
firstPosition: number,
endPosition: number,
newPosition: number,
2019-04-18 11:28:17 +02:00
transaction?: Transaction
2019-02-26 10:55:40 +01:00
) {
const query = {
where: {
videoPlaylistId,
position: {
2019-04-18 11:28:17 +02:00
[Op.gte]: firstPosition,
[Op.lte]: endPosition
2019-02-26 10:55:40 +01:00
}
},
2019-02-28 11:14:26 +01:00
transaction,
validate: false // We use a literal to update the position
2019-02-26 10:55:40 +01:00
}
return VideoPlaylistElementModel.update({ position: Sequelize.literal(`${newPosition} + "position" - ${firstPosition}`) }, query)
}
static increasePositionOf (
videoPlaylistId: number,
fromPosition: number,
toPosition?: number,
by = 1,
2019-04-18 11:28:17 +02:00
transaction?: Transaction
2019-02-26 10:55:40 +01:00
) {
const query = {
where: {
videoPlaylistId,
position: {
2019-04-18 11:28:17 +02:00
[Op.gte]: fromPosition
2019-02-26 10:55:40 +01:00
}
},
transaction
}
return VideoPlaylistElementModel.increment({ position: by }, query)
}
2019-08-20 19:05:31 +02:00
getType (this: MVideoPlaylistElementFormattable, displayNSFW?: boolean, accountId?: number) {
2019-07-31 15:57:32 +02: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 09:55:57 +01:00
// Internal video?
if (video.privacy === VideoPrivacy.INTERNAL && accountId) return VideoPlaylistElementType.REGULAR
if (video.privacy === VideoPrivacy.PRIVATE || video.privacy === VideoPrivacy.INTERNAL) return VideoPlaylistElementType.PRIVATE
2019-07-31 15:57:32 +02:00
if (video.isBlacklisted() || video.isBlocked()) return VideoPlaylistElementType.UNAVAILABLE
if (video.nsfw === true && displayNSFW === false) return VideoPlaylistElementType.UNAVAILABLE
return VideoPlaylistElementType.REGULAR
}
2019-08-20 19:05:31 +02:00
getVideoElement (this: MVideoPlaylistElementFormattable, displayNSFW?: boolean, accountId?: number) {
2019-07-31 15:57:32 +02:00
if (!this.Video) return null
if (this.getType(displayNSFW, accountId) !== VideoPlaylistElementType.REGULAR) return null
return this.Video.toFormattedJSON()
}
2019-08-20 19:05:31 +02:00
toFormattedJSON (
this: MVideoPlaylistElementFormattable,
options: { displayNSFW?: boolean, accountId?: number } = {}
): VideoPlaylistElement {
2019-07-31 15:57:32 +02:00
return {
id: this.id,
position: this.position,
startTimestamp: this.startTimestamp,
stopTimestamp: this.stopTimestamp,
type: this.getType(options.displayNSFW, options.accountId),
video: this.getVideoElement(options.displayNSFW, options.accountId)
}
}
2019-08-21 14:31:57 +02:00
toActivityPubObject (this: MVideoPlaylistElementAP): PlaylistElementObject {
2019-02-26 10:55:40 +01:00
const base: PlaylistElementObject = {
id: this.url,
type: 'PlaylistElement',
url: this.Video.url,
position: this.position
}
if (this.startTimestamp) base.startTimestamp = this.startTimestamp
if (this.stopTimestamp) base.stopTimestamp = this.stopTimestamp
return base
}
}