15 } from 'sequelize-typescript'
16 import { ForAPIOptions, ScopeNames as VideoScopeNames, VideoModel } from './video'
17 import { VideoPlaylistModel } from './video-playlist'
18 import { getSort, throwIfNotValid } from '../utils'
19 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
20 import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
21 import { PlaylistElementObject } from '../../../shared/models/activitypub/objects/playlist-element-object'
22 import validator from 'validator'
23 import { AggregateOptions, Op, ScopeOptions, Sequelize, Transaction } from 'sequelize'
24 import { VideoPlaylistElement, VideoPlaylistElementType } from '../../../shared/models/videos/playlist/video-playlist-element.model'
25 import { AccountModel } from '../account/account'
26 import { VideoPrivacy } from '../../../shared/models/videos'
27 import * as Bluebird from 'bluebird'
29 MVideoPlaylistElement,
30 MVideoPlaylistElementAP,
31 MVideoPlaylistElementFormattable,
32 MVideoPlaylistElementVideoUrlPlaylistPrivacy,
33 MVideoPlaylistVideoThumbnail
34 } from '@server/typings/models/video/video-playlist-element'
35 import { MUserAccountId } from '@server/typings/models'
38 tableName: 'videoPlaylistElement',
41 fields: [ 'videoPlaylistId' ]
47 fields: [ 'videoPlaylistId', 'videoId' ],
56 export class VideoPlaylistElementModel extends Model<VideoPlaylistElementModel> {
64 @Is('VideoPlaylistUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
65 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_PLAYLISTS.URL.max))
79 startTimestamp: number
87 @ForeignKey(() => VideoPlaylistModel)
89 videoPlaylistId: number
91 @BelongsTo(() => VideoPlaylistModel, {
97 VideoPlaylist: VideoPlaylistModel
99 @ForeignKey(() => VideoModel)
103 @BelongsTo(() => VideoModel, {
111 static deleteAllOf (videoPlaylistId: number, transaction?: Transaction) {
119 return VideoPlaylistElementModel.destroy(query)
122 static listForApi (options: {
125 videoPlaylistId: number
126 serverAccount: AccountModel
127 user?: MUserAccountId
129 const accountIds = [ options.serverAccount.id ]
130 const videoScope: (ScopeOptions | string)[] = [
131 VideoScopeNames.WITH_BLACKLISTED
135 accountIds.push(options.user.Account.id)
136 videoScope.push({ method: [ VideoScopeNames.WITH_USER_HISTORY, options.user.id ] })
139 const forApiOptions: ForAPIOptions = { withAccountBlockerIds: accountIds }
142 VideoScopeNames.FOR_API, forApiOptions
147 offset: options.start,
148 limit: options.count,
149 order: getSort('position'),
151 videoPlaylistId: options.videoPlaylistId
155 model: VideoModel.scope(videoScope),
163 videoPlaylistId: options.videoPlaylistId
168 VideoPlaylistElementModel.count(countQuery),
169 VideoPlaylistElementModel.findAll(findQuery)
170 ]).then(([ total, data ]) => ({ total, data }))
173 static loadByPlaylistAndVideo (videoPlaylistId: number, videoId: number): Bluebird<MVideoPlaylistElement> {
181 return VideoPlaylistElementModel.findOne(query)
184 static loadById (playlistElementId: number | string): Bluebird<MVideoPlaylistElement> {
185 return VideoPlaylistElementModel.findByPk(playlistElementId)
188 static loadByPlaylistAndVideoForAP (
189 playlistId: number | string,
190 videoId: number | string
191 ): Bluebird<MVideoPlaylistElementVideoUrlPlaylistPrivacy> {
192 const playlistWhere = validator.isUUID('' + playlistId) ? { uuid: playlistId } : { id: playlistId }
193 const videoWhere = validator.isUUID('' + videoId) ? { uuid: videoId } : { id: videoId }
198 attributes: [ 'privacy' ],
199 model: VideoPlaylistModel.unscoped(),
203 attributes: [ 'url' ],
204 model: VideoModel.unscoped(),
210 return VideoPlaylistElementModel.findOne(query)
213 static listUrlsOfForAP (videoPlaylistId: number, start: number, count: number, t?: Transaction) {
215 attributes: [ 'url' ],
218 order: getSort('position'),
225 return VideoPlaylistElementModel
226 .findAndCountAll(query)
227 .then(({ rows, count }) => {
228 return { total: count, data: rows.map(e => e.url) }
232 static loadFirstElementWithVideoThumbnail (videoPlaylistId: number): Bluebird<MVideoPlaylistVideoThumbnail> {
234 order: getSort('position'),
240 model: VideoModel.scope(VideoScopeNames.WITH_THUMBNAILS),
246 return VideoPlaylistElementModel
250 static getNextPositionOf (videoPlaylistId: number, transaction?: Transaction) {
251 const query: AggregateOptions<number> = {
258 return VideoPlaylistElementModel.max('position', query)
259 .then(position => position ? position + 1 : 1)
262 static reassignPositionOf (
263 videoPlaylistId: number,
264 firstPosition: number,
267 transaction?: Transaction
273 [Op.gte]: firstPosition,
274 [Op.lte]: endPosition
278 validate: false // We use a literal to update the position
281 return VideoPlaylistElementModel.update({ position: Sequelize.literal(`${newPosition} + "position" - ${firstPosition}`) }, query)
284 static increasePositionOf (
285 videoPlaylistId: number,
286 fromPosition: number,
289 transaction?: Transaction
295 [Op.gte]: fromPosition
301 return VideoPlaylistElementModel.increment({ position: by }, query)
304 getType (this: MVideoPlaylistElementFormattable, displayNSFW?: boolean, accountId?: number) {
305 const video = this.Video
307 if (!video) return VideoPlaylistElementType.DELETED
309 // Owned video, don't filter it
310 if (accountId && video.VideoChannel.Account.id === accountId) return VideoPlaylistElementType.REGULAR
312 if (video.privacy === VideoPrivacy.PRIVATE) return VideoPlaylistElementType.PRIVATE
314 if (video.isBlacklisted() || video.isBlocked()) return VideoPlaylistElementType.UNAVAILABLE
315 if (video.nsfw === true && displayNSFW === false) return VideoPlaylistElementType.UNAVAILABLE
317 return VideoPlaylistElementType.REGULAR
320 getVideoElement (this: MVideoPlaylistElementFormattable, displayNSFW?: boolean, accountId?: number) {
321 if (!this.Video) return null
322 if (this.getType(displayNSFW, accountId) !== VideoPlaylistElementType.REGULAR) return null
324 return this.Video.toFormattedJSON()
328 this: MVideoPlaylistElementFormattable,
329 options: { displayNSFW?: boolean, accountId?: number } = {}
330 ): VideoPlaylistElement {
333 position: this.position,
334 startTimestamp: this.startTimestamp,
335 stopTimestamp: this.stopTimestamp,
337 type: this.getType(options.displayNSFW, options.accountId),
339 video: this.getVideoElement(options.displayNSFW, options.accountId)
343 toActivityPubObject (this: MVideoPlaylistElementAP): PlaylistElementObject {
344 const base: PlaylistElementObject = {
346 type: 'PlaylistElement',
349 position: this.position
352 if (this.startTimestamp) base.startTimestamp = this.startTimestamp
353 if (this.stopTimestamp) base.stopTimestamp = this.stopTimestamp