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, SummaryOptions } 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'
46 import * as Bluebird from 'bluebird'
48 MVideoPlaylistAccountThumbnail, MVideoPlaylistAP,
49 MVideoPlaylistFormattable,
51 MVideoPlaylistFullSummary,
52 MVideoPlaylistIdWithElements
53 } from '../../typings/models/video/video-playlist'
54 import { MThumbnail } from '../../typings/models/video/thumbnail'
57 AVAILABLE_FOR_LIST = 'AVAILABLE_FOR_LIST',
58 WITH_VIDEOS_LENGTH = 'WITH_VIDEOS_LENGTH',
59 WITH_ACCOUNT_AND_CHANNEL_SUMMARY = 'WITH_ACCOUNT_AND_CHANNEL_SUMMARY',
60 WITH_ACCOUNT = 'WITH_ACCOUNT',
61 WITH_THUMBNAIL = 'WITH_THUMBNAIL',
62 WITH_ACCOUNT_AND_CHANNEL = 'WITH_ACCOUNT_AND_CHANNEL'
65 type AvailableForListOptions = {
66 followerActorId: number
67 type?: VideoPlaylistType
69 videoChannelId?: number
70 privateAndUnlisted?: boolean
74 [ ScopeNames.WITH_THUMBNAIL ]: {
77 model: ThumbnailModel,
82 [ ScopeNames.WITH_VIDEOS_LENGTH ]: {
86 literal('(SELECT COUNT("id") FROM "videoPlaylistElement" WHERE "videoPlaylistId" = "VideoPlaylistModel"."id")'),
92 [ ScopeNames.WITH_ACCOUNT ]: {
100 [ ScopeNames.WITH_ACCOUNT_AND_CHANNEL_SUMMARY ]: {
103 model: AccountModel.scope(AccountScopeNames.SUMMARY),
107 model: VideoChannelModel.scope(VideoChannelScopeNames.SUMMARY),
112 [ ScopeNames.WITH_ACCOUNT_AND_CHANNEL ]: {
119 model: VideoChannelModel,
124 [ ScopeNames.AVAILABLE_FOR_LIST ]: (options: AvailableForListOptions) => {
125 // Only list local playlists OR playlists that are on an instance followed by actorId
126 const inQueryInstanceFollow = buildServerIdsFollowedBy(options.followerActorId)
134 [ Op.in ]: literal(inQueryInstanceFollow)
140 const whereAnd: WhereOptions[] = []
142 if (options.privateAndUnlisted !== true) {
144 privacy: VideoPlaylistPrivacy.PUBLIC
148 if (options.accountId) {
150 ownerAccountId: options.accountId
154 if (options.videoChannelId) {
156 videoChannelId: options.videoChannelId
170 const accountScope = {
171 method: [ AccountScopeNames.SUMMARY, { whereActor } as SummaryOptions ]
178 model: AccountModel.scope(accountScope),
182 model: VideoChannelModel.scope(VideoChannelScopeNames.SUMMARY),
191 tableName: 'videoPlaylist',
194 fields: [ 'ownerAccountId' ]
197 fields: [ 'videoChannelId' ]
205 export class VideoPlaylistModel extends Model<VideoPlaylistModel> {
213 @Is('VideoPlaylistName', value => throwIfNotValid(value, isVideoPlaylistNameValid, 'name'))
218 @Is('VideoPlaylistDescription', value => throwIfNotValid(value, isVideoPlaylistDescriptionValid, 'description', true))
223 @Is('VideoPlaylistPrivacy', value => throwIfNotValid(value, isVideoPlaylistPrivacyValid, 'privacy'))
225 privacy: VideoPlaylistPrivacy
228 @Is('VideoPlaylistUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
229 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_PLAYLISTS.URL.max))
233 @Default(DataType.UUIDV4)
235 @Column(DataType.UUID)
239 @Default(VideoPlaylistType.REGULAR)
241 type: VideoPlaylistType
243 @ForeignKey(() => AccountModel)
245 ownerAccountId: number
247 @BelongsTo(() => AccountModel, {
253 OwnerAccount: AccountModel
255 @ForeignKey(() => VideoChannelModel)
257 videoChannelId: number
259 @BelongsTo(() => VideoChannelModel, {
265 VideoChannel: VideoChannelModel
267 @HasMany(() => VideoPlaylistElementModel, {
269 name: 'videoPlaylistId',
274 VideoPlaylistElements: VideoPlaylistElementModel[]
276 @HasOne(() => ThumbnailModel, {
278 name: 'videoPlaylistId',
284 Thumbnail: ThumbnailModel
286 static listForApi (options: {
287 followerActorId: number
291 type?: VideoPlaylistType,
293 videoChannelId?: number,
294 privateAndUnlisted?: boolean
297 offset: options.start,
298 limit: options.count,
299 order: getSort(options.sort)
302 const scopes: (string | ScopeOptions)[] = [
305 ScopeNames.AVAILABLE_FOR_LIST,
308 followerActorId: options.followerActorId,
309 accountId: options.accountId,
310 videoChannelId: options.videoChannelId,
311 privateAndUnlisted: options.privateAndUnlisted
312 } as AvailableForListOptions
315 ScopeNames.WITH_VIDEOS_LENGTH,
316 ScopeNames.WITH_THUMBNAIL
319 return VideoPlaylistModel
321 .findAndCountAll(query)
322 .then(({ rows, count }) => {
323 return { total: count, data: rows }
327 static listPublicUrlsOfForAP (accountId: number, start: number, count: number) {
329 attributes: [ 'url' ],
333 ownerAccountId: accountId,
334 privacy: VideoPlaylistPrivacy.PUBLIC
338 return VideoPlaylistModel.findAndCountAll(query)
339 .then(({ rows, count }) => {
340 return { total: count, data: rows.map(p => p.url) }
344 static listPlaylistIdsOf (accountId: number, videoIds: number[]): Bluebird<MVideoPlaylistIdWithElements[]> {
346 attributes: [ 'id' ],
348 ownerAccountId: accountId
352 attributes: [ 'id', 'videoId', 'startTimestamp', 'stopTimestamp' ],
353 model: VideoPlaylistElementModel.unscoped(),
356 [Op.in]: videoIds // FIXME: sequelize ANY seems broken
364 return VideoPlaylistModel.findAll(query)
367 static doesPlaylistExist (url: string) {
375 return VideoPlaylistModel
380 static loadWithAccountAndChannelSummary (id: number | string, transaction: Transaction): Bluebird<MVideoPlaylistFullSummary> {
381 const where = buildWhereIdOrUUID(id)
388 return VideoPlaylistModel
389 .scope([ ScopeNames.WITH_ACCOUNT_AND_CHANNEL_SUMMARY, ScopeNames.WITH_VIDEOS_LENGTH, ScopeNames.WITH_THUMBNAIL ])
393 static loadWithAccountAndChannel (id: number | string, transaction: Transaction): Bluebird<MVideoPlaylistFull> {
394 const where = buildWhereIdOrUUID(id)
401 return VideoPlaylistModel
402 .scope([ ScopeNames.WITH_ACCOUNT_AND_CHANNEL, ScopeNames.WITH_VIDEOS_LENGTH, ScopeNames.WITH_THUMBNAIL ])
406 static loadByUrlAndPopulateAccount (url: string): Bluebird<MVideoPlaylistAccountThumbnail> {
413 return VideoPlaylistModel.scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_THUMBNAIL ]).findOne(query)
416 static getPrivacyLabel (privacy: VideoPlaylistPrivacy) {
417 return VIDEO_PLAYLIST_PRIVACIES[privacy] || 'Unknown'
420 static getTypeLabel (type: VideoPlaylistType) {
421 return VIDEO_PLAYLIST_TYPES[type] || 'Unknown'
424 static resetPlaylistsOfChannel (videoChannelId: number, transaction: Transaction) {
432 return VideoPlaylistModel.update({ privacy: VideoPlaylistPrivacy.PRIVATE, videoChannelId: null }, query)
435 async setAndSaveThumbnail (thumbnail: MThumbnail, t: Transaction) {
436 thumbnail.videoPlaylistId = this.id
438 this.Thumbnail = await thumbnail.save({ transaction: t })
442 return !!this.Thumbnail
445 hasGeneratedThumbnail () {
446 return this.hasThumbnail() && this.Thumbnail.automaticallyGenerated === true
449 generateThumbnailName () {
450 const extension = '.jpg'
452 return 'playlist-' + this.uuid + extension
456 if (!this.hasThumbnail()) return null
458 return WEBSERVER.URL + STATIC_PATHS.THUMBNAILS + this.Thumbnail.filename
461 getThumbnailStaticPath () {
462 if (!this.hasThumbnail()) return null
464 return join(STATIC_PATHS.THUMBNAILS, this.Thumbnail.filename)
468 this.changed('updatedAt', true)
474 return this.OwnerAccount.isOwned()
478 if (this.isOwned()) return false
480 return isOutdated(this, ACTIVITY_PUB.VIDEO_PLAYLIST_REFRESH_INTERVAL)
483 toFormattedJSON (this: MVideoPlaylistFormattable): VideoPlaylist {
487 isLocal: this.isOwned(),
489 displayName: this.name,
490 description: this.description,
493 label: VideoPlaylistModel.getPrivacyLabel(this.privacy)
496 thumbnailPath: this.getThumbnailStaticPath(),
500 label: VideoPlaylistModel.getTypeLabel(this.type)
503 videosLength: this.get('videosLength') as number,
505 createdAt: this.createdAt,
506 updatedAt: this.updatedAt,
508 ownerAccount: this.OwnerAccount.toFormattedSummaryJSON(),
509 videoChannel: this.VideoChannel ? this.VideoChannel.toFormattedSummaryJSON() : null
513 toActivityPubObject (this: MVideoPlaylistAP, page: number, t: Transaction): Promise<PlaylistObject> {
514 const handler = (start: number, count: number) => {
515 return VideoPlaylistElementModel.listUrlsOfForAP(this.id, start, count, t)
518 let icon: ActivityIconObject
519 if (this.hasThumbnail()) {
521 type: 'Image' as 'Image',
522 url: this.getThumbnailUrl(),
523 mediaType: 'image/jpeg' as 'image/jpeg',
524 width: THUMBNAILS_SIZE.width,
525 height: THUMBNAILS_SIZE.height
529 return activityPubCollectionPagination(this.url, handler, page)
531 return Object.assign(o, {
532 type: 'Playlist' as 'Playlist',
534 content: this.description,
536 published: this.createdAt.toISOString(),
537 updated: this.updatedAt.toISOString(),
538 attributedTo: this.VideoChannel ? [ this.VideoChannel.Actor.url ] : [],