1 import { AggregateOptions, Op, ScopeOptions, Sequelize, Transaction } from 'sequelize'
16 } from 'sequelize-typescript'
17 import validator from 'validator'
18 import { MUserAccountId } from '@server/types/models'
20 MVideoPlaylistElement,
21 MVideoPlaylistElementAP,
22 MVideoPlaylistElementFormattable,
23 MVideoPlaylistElementVideoUrlPlaylistPrivacy,
24 MVideoPlaylistVideoThumbnail
25 } from '@server/types/models/video/video-playlist-element'
26 import { PlaylistElementObject } from '../../../shared/models/activitypub/objects/playlist-element-object'
27 import { VideoPrivacy } from '../../../shared/models/videos'
28 import { VideoPlaylistElement, VideoPlaylistElementType } from '../../../shared/models/videos/playlist/video-playlist-element.model'
29 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
30 import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
31 import { AccountModel } from '../account/account'
32 import { getSort, throwIfNotValid } from '../utils'
33 import { ForAPIOptions, ScopeNames as VideoScopeNames, VideoModel } from './video'
34 import { VideoPlaylistModel } from './video-playlist'
35 import { AttributesOnly } from '@shared/core-utils'
38 tableName: 'videoPlaylistElement',
41 fields: [ 'videoPlaylistId' ]
52 export class VideoPlaylistElementModel extends Model<Partial<AttributesOnly<VideoPlaylistElementModel>>> {
60 @Is('VideoPlaylistUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url', true))
61 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_PLAYLISTS.URL.max))
75 startTimestamp: number
83 @ForeignKey(() => VideoPlaylistModel)
85 videoPlaylistId: number
87 @BelongsTo(() => VideoPlaylistModel, {
93 VideoPlaylist: VideoPlaylistModel
95 @ForeignKey(() => VideoModel)
99 @BelongsTo(() => VideoModel, {
107 static deleteAllOf (videoPlaylistId: number, transaction?: Transaction) {
115 return VideoPlaylistElementModel.destroy(query)
118 static listForApi (options: {
121 videoPlaylistId: number
122 serverAccount: AccountModel
123 user?: MUserAccountId
125 const accountIds = [ options.serverAccount.id ]
126 const videoScope: (ScopeOptions | string)[] = [
127 VideoScopeNames.WITH_BLACKLISTED
131 accountIds.push(options.user.Account.id)
132 videoScope.push({ method: [ VideoScopeNames.WITH_USER_HISTORY, options.user.id ] })
135 const forApiOptions: ForAPIOptions = { withAccountBlockerIds: accountIds }
138 VideoScopeNames.FOR_API, forApiOptions
143 offset: options.start,
144 limit: options.count,
145 order: getSort('position'),
147 videoPlaylistId: options.videoPlaylistId
151 model: VideoModel.scope(videoScope),
159 videoPlaylistId: options.videoPlaylistId
164 VideoPlaylistElementModel.count(countQuery),
165 VideoPlaylistElementModel.findAll(findQuery)
166 ]).then(([ total, data ]) => ({ total, data }))
169 static loadByPlaylistAndVideo (videoPlaylistId: number, videoId: number): Promise<MVideoPlaylistElement> {
177 return VideoPlaylistElementModel.findOne(query)
180 static loadById (playlistElementId: number | string): Promise<MVideoPlaylistElement> {
181 return VideoPlaylistElementModel.findByPk(playlistElementId)
184 static loadByPlaylistAndElementIdForAP (
185 playlistId: number | string,
186 playlistElementId: number
187 ): Promise<MVideoPlaylistElementVideoUrlPlaylistPrivacy> {
188 const playlistWhere = validator.isUUID('' + playlistId) ? { uuid: playlistId } : { id: playlistId }
193 attributes: [ 'privacy' ],
194 model: VideoPlaylistModel.unscoped(),
198 attributes: [ 'url' ],
199 model: VideoModel.unscoped()
203 id: playlistElementId
207 return VideoPlaylistElementModel.findOne(query)
210 static listUrlsOfForAP (videoPlaylistId: number, start: number, count: number, t?: Transaction) {
212 attributes: [ 'url' ],
215 order: getSort('position'),
222 return VideoPlaylistElementModel
223 .findAndCountAll(query)
224 .then(({ rows, count }) => {
225 return { total: count, data: rows.map(e => e.url) }
229 static loadFirstElementWithVideoThumbnail (videoPlaylistId: number): Promise<MVideoPlaylistVideoThumbnail> {
231 order: getSort('position'),
237 model: VideoModel.scope(VideoScopeNames.WITH_THUMBNAILS),
243 return VideoPlaylistElementModel
247 static getNextPositionOf (videoPlaylistId: number, transaction?: Transaction) {
248 const query: AggregateOptions<number> = {
255 return VideoPlaylistElementModel.max('position', query)
256 .then(position => position ? position + 1 : 1)
259 static reassignPositionOf (
260 videoPlaylistId: number,
261 firstPosition: number,
264 transaction?: Transaction
270 [Op.gte]: firstPosition,
271 [Op.lte]: endPosition
275 validate: false // We use a literal to update the position
278 const positionQuery = Sequelize.literal(`${newPosition} + "position" - ${firstPosition}`)
279 return VideoPlaylistElementModel.update({ position: positionQuery as any }, query)
282 static increasePositionOf (
283 videoPlaylistId: number,
284 fromPosition: number,
287 transaction?: Transaction
293 [Op.gte]: fromPosition
299 return VideoPlaylistElementModel.increment({ position: by }, query)
302 getType (this: MVideoPlaylistElementFormattable, displayNSFW?: boolean, accountId?: number) {
303 const video = this.Video
305 if (!video) return VideoPlaylistElementType.DELETED
307 // Owned video, don't filter it
308 if (accountId && video.VideoChannel.Account.id === accountId) return VideoPlaylistElementType.REGULAR
311 if (video.privacy === VideoPrivacy.INTERNAL && accountId) return VideoPlaylistElementType.REGULAR
313 if (video.privacy === VideoPrivacy.PRIVATE || video.privacy === VideoPrivacy.INTERNAL) return VideoPlaylistElementType.PRIVATE
315 if (video.isBlacklisted() || video.isBlocked()) return VideoPlaylistElementType.UNAVAILABLE
316 if (video.nsfw === true && displayNSFW === false) return VideoPlaylistElementType.UNAVAILABLE
318 return VideoPlaylistElementType.REGULAR
321 getVideoElement (this: MVideoPlaylistElementFormattable, displayNSFW?: boolean, accountId?: number) {
322 if (!this.Video) return null
323 if (this.getType(displayNSFW, accountId) !== VideoPlaylistElementType.REGULAR) return null
325 return this.Video.toFormattedJSON()
329 this: MVideoPlaylistElementFormattable,
330 options: { displayNSFW?: boolean, accountId?: number } = {}
331 ): VideoPlaylistElement {
334 position: this.position,
335 startTimestamp: this.startTimestamp,
336 stopTimestamp: this.stopTimestamp,
338 type: this.getType(options.displayNSFW, options.accountId),
340 video: this.getVideoElement(options.displayNSFW, options.accountId)
344 toActivityPubObject (this: MVideoPlaylistElementAP): PlaylistElementObject {
345 const base: PlaylistElementObject = {
347 type: 'PlaylistElement',
350 position: this.position
353 if (this.startTimestamp) base.startTimestamp = this.startTimestamp
354 if (this.stopTimestamp) base.stopTimestamp = this.stopTimestamp