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'
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, { whereActor } as SummaryOptions ]
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, {
269 name: 'videoPlaylistId',
275 Thumbnail: ThumbnailModel
277 static listForApi (options: {
278 followerActorId: number
282 type?: VideoPlaylistType,
284 videoChannelId?: number,
285 privateAndUnlisted?: boolean
288 offset: options.start,
289 limit: options.count,
290 order: getSort(options.sort)
293 const scopes: (string | ScopeOptions)[] = [
296 ScopeNames.AVAILABLE_FOR_LIST,
299 followerActorId: options.followerActorId,
300 accountId: options.accountId,
301 videoChannelId: options.videoChannelId,
302 privateAndUnlisted: options.privateAndUnlisted
303 } as AvailableForListOptions
306 ScopeNames.WITH_VIDEOS_LENGTH,
307 ScopeNames.WITH_THUMBNAIL
310 return VideoPlaylistModel
312 .findAndCountAll(query)
313 .then(({ rows, count }) => {
314 return { total: count, data: rows }
318 static listPublicUrlsOfForAP (accountId: number, start: number, count: number) {
320 attributes: [ 'url' ],
324 ownerAccountId: accountId,
325 privacy: VideoPlaylistPrivacy.PUBLIC
329 return VideoPlaylistModel.findAndCountAll(query)
330 .then(({ rows, count }) => {
331 return { total: count, data: rows.map(p => p.url) }
335 static listPlaylistIdsOf (accountId: number, videoIds: number[]) {
337 attributes: [ 'id' ],
339 ownerAccountId: accountId
343 attributes: [ 'id', 'videoId', 'startTimestamp', 'stopTimestamp' ],
344 model: VideoPlaylistElementModel.unscoped(),
347 [Op.in]: videoIds // FIXME: sequelize ANY seems broken
355 return VideoPlaylistModel.findAll(query)
358 static doesPlaylistExist (url: string) {
366 return VideoPlaylistModel
371 static loadWithAccountAndChannelSummary (id: number | string, transaction: Transaction) {
372 const where = buildWhereIdOrUUID(id)
379 return VideoPlaylistModel
380 .scope([ ScopeNames.WITH_ACCOUNT_AND_CHANNEL_SUMMARY, ScopeNames.WITH_VIDEOS_LENGTH, ScopeNames.WITH_THUMBNAIL ])
384 static loadWithAccountAndChannel (id: number | string, transaction: Transaction) {
385 const where = buildWhereIdOrUUID(id)
392 return VideoPlaylistModel
393 .scope([ ScopeNames.WITH_ACCOUNT_AND_CHANNEL, ScopeNames.WITH_VIDEOS_LENGTH, ScopeNames.WITH_THUMBNAIL ])
397 static loadByUrlAndPopulateAccount (url: string) {
404 return VideoPlaylistModel.scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_THUMBNAIL ]).findOne(query)
407 static getPrivacyLabel (privacy: VideoPlaylistPrivacy) {
408 return VIDEO_PLAYLIST_PRIVACIES[privacy] || 'Unknown'
411 static getTypeLabel (type: VideoPlaylistType) {
412 return VIDEO_PLAYLIST_TYPES[type] || 'Unknown'
415 static resetPlaylistsOfChannel (videoChannelId: number, transaction: Transaction) {
423 return VideoPlaylistModel.update({ privacy: VideoPlaylistPrivacy.PRIVATE, videoChannelId: null }, query)
426 async setAndSaveThumbnail (thumbnail: ThumbnailModel, t: Transaction) {
427 thumbnail.videoPlaylistId = this.id
429 this.Thumbnail = await thumbnail.save({ transaction: t })
433 return !!this.Thumbnail
436 hasGeneratedThumbnail () {
437 return this.hasThumbnail() && this.Thumbnail.automaticallyGenerated === true
440 generateThumbnailName () {
441 const extension = '.jpg'
443 return 'playlist-' + this.uuid + extension
447 if (!this.hasThumbnail()) return null
449 return WEBSERVER.URL + STATIC_PATHS.THUMBNAILS + this.Thumbnail.filename
452 getThumbnailStaticPath () {
453 if (!this.hasThumbnail()) return null
455 return join(STATIC_PATHS.THUMBNAILS, this.Thumbnail.filename)
459 this.changed('updatedAt', true)
465 return this.OwnerAccount.isOwned()
469 if (this.isOwned()) return false
471 return isOutdated(this, ACTIVITY_PUB.VIDEO_PLAYLIST_REFRESH_INTERVAL)
474 toFormattedJSON (): VideoPlaylist {
478 isLocal: this.isOwned(),
480 displayName: this.name,
481 description: this.description,
484 label: VideoPlaylistModel.getPrivacyLabel(this.privacy)
487 thumbnailPath: this.getThumbnailStaticPath(),
491 label: VideoPlaylistModel.getTypeLabel(this.type)
494 videosLength: this.get('videosLength') as number,
496 createdAt: this.createdAt,
497 updatedAt: this.updatedAt,
499 ownerAccount: this.OwnerAccount.toFormattedSummaryJSON(),
500 videoChannel: this.VideoChannel ? this.VideoChannel.toFormattedSummaryJSON() : null
504 toActivityPubObject (page: number, t: Transaction): Promise<PlaylistObject> {
505 const handler = (start: number, count: number) => {
506 return VideoPlaylistElementModel.listUrlsOfForAP(this.id, start, count, t)
509 let icon: ActivityIconObject
510 if (this.hasThumbnail()) {
512 type: 'Image' as 'Image',
513 url: this.getThumbnailUrl(),
514 mediaType: 'image/jpeg' as 'image/jpeg',
515 width: THUMBNAILS_SIZE.width,
516 height: THUMBNAILS_SIZE.height
520 return activityPubCollectionPagination(this.url, handler, page)
522 return Object.assign(o, {
523 type: 'Playlist' as 'Playlist',
525 content: this.description,
527 published: this.createdAt.toISOString(),
528 updated: this.updatedAt.toISOString(),
529 attributedTo: this.VideoChannel ? [ this.VideoChannel.Actor.url ] : [],