15 } from 'sequelize-typescript'
16 import { VideoModel } from './video'
17 import { VideoPlaylistModel } from './video-playlist'
18 import * as Sequelize from 'sequelize'
19 import { getSort, throwIfNotValid } from '../utils'
20 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
21 import { CONSTRAINTS_FIELDS } from '../../initializers'
22 import { PlaylistElementObject } from '../../../shared/models/activitypub/objects/playlist-element-object'
25 tableName: 'videoPlaylistElement',
28 fields: [ 'videoPlaylistId' ]
34 fields: [ 'videoPlaylistId', 'videoId' ],
38 fields: [ 'videoPlaylistId', 'position' ],
47 export class VideoPlaylistElementModel extends Model<VideoPlaylistElementModel> {
55 @Is('VideoPlaylistUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
56 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_PLAYLISTS.URL.max))
70 startTimestamp: number
78 @ForeignKey(() => VideoPlaylistModel)
80 videoPlaylistId: number
82 @BelongsTo(() => VideoPlaylistModel, {
88 VideoPlaylist: VideoPlaylistModel
90 @ForeignKey(() => VideoModel)
94 @BelongsTo(() => VideoModel, {
102 static deleteAllOf (videoPlaylistId: number, transaction?: Sequelize.Transaction) {
110 return VideoPlaylistElementModel.destroy(query)
113 static loadByPlaylistAndVideo (videoPlaylistId: number, videoId: number) {
121 return VideoPlaylistElementModel.findOne(query)
124 static loadByPlaylistAndVideoForAP (playlistId: number | string, videoId: number | string) {
125 const playlistWhere = validator.isUUID('' + playlistId) ? { uuid: playlistId } : { id: playlistId }
126 const videoWhere = validator.isUUID('' + videoId) ? { uuid: videoId } : { id: videoId }
131 attributes: [ 'privacy' ],
132 model: VideoPlaylistModel.unscoped(),
136 attributes: [ 'url' ],
137 model: VideoModel.unscoped(),
143 return VideoPlaylistElementModel.findOne(query)
146 static listUrlsOfForAP (videoPlaylistId: number, start: number, count: number) {
148 attributes: [ 'url' ],
151 order: getSort('position'),
157 return VideoPlaylistElementModel
158 .findAndCountAll(query)
159 .then(({ rows, count }) => {
160 return { total: count, data: rows.map(e => e.url) }
164 static getNextPositionOf (videoPlaylistId: number, transaction?: Sequelize.Transaction) {
172 return VideoPlaylistElementModel.max('position', query)
173 .then(position => position ? position + 1 : 1)
176 static reassignPositionOf (
177 videoPlaylistId: number,
178 firstPosition: number,
181 transaction?: Sequelize.Transaction
187 [Sequelize.Op.gte]: firstPosition,
188 [Sequelize.Op.lte]: endPosition
192 validate: false // We use a literal to update the position
195 return VideoPlaylistElementModel.update({ position: Sequelize.literal(`${newPosition} + "position" - ${firstPosition}`) }, query)
198 static increasePositionOf (
199 videoPlaylistId: number,
200 fromPosition: number,
203 transaction?: Sequelize.Transaction
209 [Sequelize.Op.gte]: fromPosition
215 return VideoPlaylistElementModel.increment({ position: by }, query)
218 toActivityPubObject (): PlaylistElementObject {
219 const base: PlaylistElementObject = {
221 type: 'PlaylistElement',
224 position: this.position
227 if (this.startTimestamp) base.startTimestamp = this.startTimestamp
228 if (this.stopTimestamp) base.stopTimestamp = this.stopTimestamp