17 } from 'sequelize-typescript'
18 import { VideoPlaylistPrivacy } from '../../../shared/models/videos/playlist/video-playlist-privacy.model'
19 import { buildServerIdsFollowedBy, buildWhereIdOrUUID, getSort, isOutdated, throwIfNotValid } from '../utils'
21 isVideoPlaylistDescriptionValid,
22 isVideoPlaylistNameValid,
23 isVideoPlaylistPrivacyValid
24 } from '../../helpers/custom-validators/video-playlists'
25 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
31 VIDEO_PLAYLIST_PRIVACIES,
34 } from '../../initializers/constants'
35 import { VideoPlaylist } from '../../../shared/models/videos/playlist/video-playlist.model'
36 import { AccountModel, ScopeNames as AccountScopeNames } from '../account/account'
37 import { ScopeNames as VideoChannelScopeNames, VideoChannelModel } from './video-channel'
38 import { join } from 'path'
39 import { VideoPlaylistElementModel } from './video-playlist-element'
40 import { PlaylistObject } from '../../../shared/models/activitypub/objects/playlist-object'
41 import { activityPubCollectionPagination } from '../../helpers/activitypub'
42 import { VideoPlaylistType } from '../../../shared/models/videos/playlist/video-playlist-type.model'
43 import { ThumbnailModel } from './thumbnail'
44 import { ActivityIconObject } from '../../../shared/models/activitypub/objects'
45 import { FindOptions, literal, Op, ScopeOptions, Transaction, WhereOptions } from 'sequelize'
48 AVAILABLE_FOR_LIST = 'AVAILABLE_FOR_LIST',
49 WITH_VIDEOS_LENGTH = 'WITH_VIDEOS_LENGTH',
50 WITH_ACCOUNT_AND_CHANNEL_SUMMARY = 'WITH_ACCOUNT_AND_CHANNEL_SUMMARY',
51 WITH_ACCOUNT = 'WITH_ACCOUNT',
52 WITH_THUMBNAIL = 'WITH_THUMBNAIL',
53 WITH_ACCOUNT_AND_CHANNEL = 'WITH_ACCOUNT_AND_CHANNEL'
56 type AvailableForListOptions = {
57 followerActorId: number
58 type?: VideoPlaylistType
60 videoChannelId?: number
61 privateAndUnlisted?: boolean
65 [ ScopeNames.WITH_THUMBNAIL ]: {
68 model: ThumbnailModel,
73 [ ScopeNames.WITH_VIDEOS_LENGTH ]: {
77 literal('(SELECT COUNT("id") FROM "videoPlaylistElement" WHERE "videoPlaylistId" = "VideoPlaylistModel"."id")'),
83 [ ScopeNames.WITH_ACCOUNT ]: {
91 [ ScopeNames.WITH_ACCOUNT_AND_CHANNEL_SUMMARY ]: {
94 model: AccountModel.scope(AccountScopeNames.SUMMARY),
98 model: VideoChannelModel.scope(VideoChannelScopeNames.SUMMARY),
103 [ ScopeNames.WITH_ACCOUNT_AND_CHANNEL ]: {
110 model: VideoChannelModel,
115 [ ScopeNames.AVAILABLE_FOR_LIST ]: (options: AvailableForListOptions) => {
116 // Only list local playlists OR playlists that are on an instance followed by actorId
117 const inQueryInstanceFollow = buildServerIdsFollowedBy(options.followerActorId)
125 [ Op.in ]: literal(inQueryInstanceFollow)
131 const whereAnd: WhereOptions[] = []
133 if (options.privateAndUnlisted !== true) {
135 privacy: VideoPlaylistPrivacy.PUBLIC
139 if (options.accountId) {
141 ownerAccountId: options.accountId
145 if (options.videoChannelId) {
147 videoChannelId: options.videoChannelId
161 const accountScope = {
162 method: [ AccountScopeNames.SUMMARY, actorWhere ]
169 model: AccountModel.scope(accountScope),
173 model: VideoChannelModel.scope(VideoChannelScopeNames.SUMMARY),
182 tableName: 'videoPlaylist',
185 fields: [ 'ownerAccountId' ]
188 fields: [ 'videoChannelId' ]
196 export class VideoPlaylistModel extends Model<VideoPlaylistModel> {
204 @Is('VideoPlaylistName', value => throwIfNotValid(value, isVideoPlaylistNameValid, 'name'))
209 @Is('VideoPlaylistDescription', value => throwIfNotValid(value, isVideoPlaylistDescriptionValid, 'description', true))
214 @Is('VideoPlaylistPrivacy', value => throwIfNotValid(value, isVideoPlaylistPrivacyValid, 'privacy'))
216 privacy: VideoPlaylistPrivacy
219 @Is('VideoPlaylistUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
220 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_PLAYLISTS.URL.max))
224 @Default(DataType.UUIDV4)
226 @Column(DataType.UUID)
230 @Default(VideoPlaylistType.REGULAR)
232 type: VideoPlaylistType
234 @ForeignKey(() => AccountModel)
236 ownerAccountId: number
238 @BelongsTo(() => AccountModel, {
244 OwnerAccount: AccountModel
246 @ForeignKey(() => VideoChannelModel)
248 videoChannelId: number
250 @BelongsTo(() => VideoChannelModel, {
256 VideoChannel: VideoChannelModel
258 @HasMany(() => VideoPlaylistElementModel, {
260 name: 'videoPlaylistId',
265 VideoPlaylistElements: VideoPlaylistElementModel[]
267 @HasOne(() => ThumbnailModel, {
270 name: 'videoPlaylistId',
276 Thumbnail: ThumbnailModel
278 static listForApi (options: {
279 followerActorId: number
283 type?: VideoPlaylistType,
285 videoChannelId?: number,
286 privateAndUnlisted?: boolean
289 offset: options.start,
290 limit: options.count,
291 order: getSort(options.sort)
294 const scopes: (string | ScopeOptions)[] = [
297 ScopeNames.AVAILABLE_FOR_LIST,
300 followerActorId: options.followerActorId,
301 accountId: options.accountId,
302 videoChannelId: options.videoChannelId,
303 privateAndUnlisted: options.privateAndUnlisted
304 } as AvailableForListOptions
307 ScopeNames.WITH_VIDEOS_LENGTH,
308 ScopeNames.WITH_THUMBNAIL
311 return VideoPlaylistModel
313 .findAndCountAll(query)
314 .then(({ rows, count }) => {
315 return { total: count, data: rows }
319 static listPublicUrlsOfForAP (accountId: number, start: number, count: number) {
321 attributes: [ 'url' ],
325 ownerAccountId: accountId,
326 privacy: VideoPlaylistPrivacy.PUBLIC
330 return VideoPlaylistModel.findAndCountAll(query)
331 .then(({ rows, count }) => {
332 return { total: count, data: rows.map(p => p.url) }
336 static listPlaylistIdsOf (accountId: number, videoIds: number[]) {
338 attributes: [ 'id' ],
340 ownerAccountId: accountId
344 attributes: [ 'videoId', 'startTimestamp', 'stopTimestamp' ],
345 model: VideoPlaylistElementModel.unscoped(),
348 [Op.in]: videoIds // FIXME: sequelize ANY seems broken
356 return VideoPlaylistModel.findAll(query)
359 static doesPlaylistExist (url: string) {
367 return VideoPlaylistModel
372 static loadWithAccountAndChannelSummary (id: number | string, transaction: Transaction) {
373 const where = buildWhereIdOrUUID(id)
380 return VideoPlaylistModel
381 .scope([ ScopeNames.WITH_ACCOUNT_AND_CHANNEL_SUMMARY, ScopeNames.WITH_VIDEOS_LENGTH, ScopeNames.WITH_THUMBNAIL ])
385 static loadWithAccountAndChannel (id: number | string, transaction: Transaction) {
386 const where = buildWhereIdOrUUID(id)
393 return VideoPlaylistModel
394 .scope([ ScopeNames.WITH_ACCOUNT_AND_CHANNEL, ScopeNames.WITH_VIDEOS_LENGTH, ScopeNames.WITH_THUMBNAIL ])
398 static loadByUrlAndPopulateAccount (url: string) {
405 return VideoPlaylistModel.scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_THUMBNAIL ]).findOne(query)
408 static getPrivacyLabel (privacy: VideoPlaylistPrivacy) {
409 return VIDEO_PLAYLIST_PRIVACIES[privacy] || 'Unknown'
412 static getTypeLabel (type: VideoPlaylistType) {
413 return VIDEO_PLAYLIST_TYPES[type] || 'Unknown'
416 static resetPlaylistsOfChannel (videoChannelId: number, transaction: Transaction) {
424 return VideoPlaylistModel.update({ privacy: VideoPlaylistPrivacy.PRIVATE, videoChannelId: null }, query)
427 async setAndSaveThumbnail (thumbnail: ThumbnailModel, t: Transaction) {
428 thumbnail.videoPlaylistId = this.id
430 this.Thumbnail = await thumbnail.save({ transaction: t })
434 return !!this.Thumbnail
437 generateThumbnailName () {
438 const extension = '.jpg'
440 return 'playlist-' + this.uuid + extension
444 if (!this.hasThumbnail()) return null
446 return WEBSERVER.URL + STATIC_PATHS.THUMBNAILS + this.Thumbnail.filename
449 getThumbnailStaticPath () {
450 if (!this.hasThumbnail()) return null
452 return join(STATIC_PATHS.THUMBNAILS, this.Thumbnail.filename)
456 this.changed('updatedAt', true)
462 return this.OwnerAccount.isOwned()
466 if (this.isOwned()) return false
468 return isOutdated(this, ACTIVITY_PUB.VIDEO_PLAYLIST_REFRESH_INTERVAL)
471 toFormattedJSON (): VideoPlaylist {
475 isLocal: this.isOwned(),
477 displayName: this.name,
478 description: this.description,
481 label: VideoPlaylistModel.getPrivacyLabel(this.privacy)
484 thumbnailPath: this.getThumbnailStaticPath(),
488 label: VideoPlaylistModel.getTypeLabel(this.type)
491 videosLength: this.get('videosLength') as number,
493 createdAt: this.createdAt,
494 updatedAt: this.updatedAt,
496 ownerAccount: this.OwnerAccount.toFormattedSummaryJSON(),
497 videoChannel: this.VideoChannel ? this.VideoChannel.toFormattedSummaryJSON() : null
501 toActivityPubObject (page: number, t: Transaction): Promise<PlaylistObject> {
502 const handler = (start: number, count: number) => {
503 return VideoPlaylistElementModel.listUrlsOfForAP(this.id, start, count, t)
506 let icon: ActivityIconObject
507 if (this.hasThumbnail()) {
509 type: 'Image' as 'Image',
510 url: this.getThumbnailUrl(),
511 mediaType: 'image/jpeg' as 'image/jpeg',
512 width: THUMBNAILS_SIZE.width,
513 height: THUMBNAILS_SIZE.height
517 return activityPubCollectionPagination(this.url, handler, page)
519 return Object.assign(o, {
520 type: 'Playlist' as 'Playlist',
522 content: this.description,
524 published: this.createdAt.toISOString(),
525 updated: this.updatedAt.toISOString(),
526 attributedTo: this.VideoChannel ? [ this.VideoChannel.Actor.url ] : [],