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,
50 MVideoPlaylistFormattable,
52 MVideoPlaylistFullSummary,
53 MVideoPlaylistIdWithElements
54 } from '../../typings/models/video/video-playlist'
55 import { MThumbnail } from '../../typings/models/video/thumbnail'
58 AVAILABLE_FOR_LIST = 'AVAILABLE_FOR_LIST',
59 WITH_VIDEOS_LENGTH = 'WITH_VIDEOS_LENGTH',
60 WITH_ACCOUNT_AND_CHANNEL_SUMMARY = 'WITH_ACCOUNT_AND_CHANNEL_SUMMARY',
61 WITH_ACCOUNT = 'WITH_ACCOUNT',
62 WITH_THUMBNAIL = 'WITH_THUMBNAIL',
63 WITH_ACCOUNT_AND_CHANNEL = 'WITH_ACCOUNT_AND_CHANNEL'
66 type AvailableForListOptions = {
67 followerActorId: number
68 type?: VideoPlaylistType
70 videoChannelId?: number
71 privateAndUnlisted?: boolean,
76 [ ScopeNames.WITH_THUMBNAIL ]: {
79 model: ThumbnailModel,
84 [ ScopeNames.WITH_VIDEOS_LENGTH ]: {
88 literal('(SELECT COUNT("id") FROM "videoPlaylistElement" WHERE "videoPlaylistId" = "VideoPlaylistModel"."id")'),
94 [ ScopeNames.WITH_ACCOUNT ]: {
102 [ ScopeNames.WITH_ACCOUNT_AND_CHANNEL_SUMMARY ]: {
105 model: AccountModel.scope(AccountScopeNames.SUMMARY),
109 model: VideoChannelModel.scope(VideoChannelScopeNames.SUMMARY),
114 [ ScopeNames.WITH_ACCOUNT_AND_CHANNEL ]: {
121 model: VideoChannelModel,
126 [ ScopeNames.AVAILABLE_FOR_LIST ]: (options: AvailableForListOptions) => {
127 // Only list local playlists OR playlists that are on an instance followed by actorId
128 const inQueryInstanceFollow = buildServerIdsFollowedBy(options.followerActorId)
136 [ Op.in ]: literal(inQueryInstanceFollow)
142 const whereAnd: WhereOptions[] = []
144 if (options.privateAndUnlisted !== true) {
146 privacy: VideoPlaylistPrivacy.PUBLIC
150 if (options.accountId) {
152 ownerAccountId: options.accountId
156 if (options.videoChannelId) {
158 videoChannelId: options.videoChannelId
168 if (options.search) {
171 [ Op.iLike ]: '%' + options.search + '%'
180 const accountScope = {
181 method: [ AccountScopeNames.SUMMARY, { whereActor } as SummaryOptions ]
188 model: AccountModel.scope(accountScope),
192 model: VideoChannelModel.scope(VideoChannelScopeNames.SUMMARY),
201 tableName: 'videoPlaylist',
204 fields: [ 'ownerAccountId' ]
207 fields: [ 'videoChannelId' ]
215 export class VideoPlaylistModel extends Model<VideoPlaylistModel> {
223 @Is('VideoPlaylistName', value => throwIfNotValid(value, isVideoPlaylistNameValid, 'name'))
228 @Is('VideoPlaylistDescription', value => throwIfNotValid(value, isVideoPlaylistDescriptionValid, 'description', true))
233 @Is('VideoPlaylistPrivacy', value => throwIfNotValid(value, isVideoPlaylistPrivacyValid, 'privacy'))
235 privacy: VideoPlaylistPrivacy
238 @Is('VideoPlaylistUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
239 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_PLAYLISTS.URL.max))
243 @Default(DataType.UUIDV4)
245 @Column(DataType.UUID)
249 @Default(VideoPlaylistType.REGULAR)
251 type: VideoPlaylistType
253 @ForeignKey(() => AccountModel)
255 ownerAccountId: number
257 @BelongsTo(() => AccountModel, {
263 OwnerAccount: AccountModel
265 @ForeignKey(() => VideoChannelModel)
267 videoChannelId: number
269 @BelongsTo(() => VideoChannelModel, {
275 VideoChannel: VideoChannelModel
277 @HasMany(() => VideoPlaylistElementModel, {
279 name: 'videoPlaylistId',
284 VideoPlaylistElements: VideoPlaylistElementModel[]
286 @HasOne(() => ThumbnailModel, {
288 name: 'videoPlaylistId',
294 Thumbnail: ThumbnailModel
296 static listForApi (options: {
297 followerActorId: number
301 type?: VideoPlaylistType,
303 videoChannelId?: number,
304 privateAndUnlisted?: boolean,
308 offset: options.start,
309 limit: options.count,
310 order: getSort(options.sort)
313 const scopes: (string | ScopeOptions)[] = [
316 ScopeNames.AVAILABLE_FOR_LIST,
319 followerActorId: options.followerActorId,
320 accountId: options.accountId,
321 videoChannelId: options.videoChannelId,
322 privateAndUnlisted: options.privateAndUnlisted,
323 search: options.search
324 } as AvailableForListOptions
327 ScopeNames.WITH_VIDEOS_LENGTH,
328 ScopeNames.WITH_THUMBNAIL
331 return VideoPlaylistModel
333 .findAndCountAll(query)
334 .then(({ rows, count }) => {
335 return { total: count, data: rows }
339 static listPublicUrlsOfForAP (accountId: number, start: number, count: number) {
341 attributes: [ 'url' ],
345 ownerAccountId: accountId,
346 privacy: VideoPlaylistPrivacy.PUBLIC
350 return VideoPlaylistModel.findAndCountAll(query)
351 .then(({ rows, count }) => {
352 return { total: count, data: rows.map(p => p.url) }
356 static listPlaylistIdsOf (accountId: number, videoIds: number[]): Bluebird<MVideoPlaylistIdWithElements[]> {
358 attributes: [ 'id' ],
360 ownerAccountId: accountId
364 attributes: [ 'id', 'videoId', 'startTimestamp', 'stopTimestamp' ],
365 model: VideoPlaylistElementModel.unscoped(),
368 [Op.in]: videoIds // FIXME: sequelize ANY seems broken
376 return VideoPlaylistModel.findAll(query)
379 static doesPlaylistExist (url: string) {
387 return VideoPlaylistModel
392 static loadWithAccountAndChannelSummary (id: number | string, transaction: Transaction): Bluebird<MVideoPlaylistFullSummary> {
393 const where = buildWhereIdOrUUID(id)
400 return VideoPlaylistModel
401 .scope([ ScopeNames.WITH_ACCOUNT_AND_CHANNEL_SUMMARY, ScopeNames.WITH_VIDEOS_LENGTH, ScopeNames.WITH_THUMBNAIL ])
405 static loadWithAccountAndChannel (id: number | string, transaction: Transaction): Bluebird<MVideoPlaylistFull> {
406 const where = buildWhereIdOrUUID(id)
413 return VideoPlaylistModel
414 .scope([ ScopeNames.WITH_ACCOUNT_AND_CHANNEL, ScopeNames.WITH_VIDEOS_LENGTH, ScopeNames.WITH_THUMBNAIL ])
418 static loadByUrlAndPopulateAccount (url: string): Bluebird<MVideoPlaylistAccountThumbnail> {
425 return VideoPlaylistModel.scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_THUMBNAIL ]).findOne(query)
428 static getPrivacyLabel (privacy: VideoPlaylistPrivacy) {
429 return VIDEO_PLAYLIST_PRIVACIES[privacy] || 'Unknown'
432 static getTypeLabel (type: VideoPlaylistType) {
433 return VIDEO_PLAYLIST_TYPES[type] || 'Unknown'
436 static resetPlaylistsOfChannel (videoChannelId: number, transaction: Transaction) {
444 return VideoPlaylistModel.update({ privacy: VideoPlaylistPrivacy.PRIVATE, videoChannelId: null }, query)
447 async setAndSaveThumbnail (thumbnail: MThumbnail, t: Transaction) {
448 thumbnail.videoPlaylistId = this.id
450 this.Thumbnail = await thumbnail.save({ transaction: t })
454 return !!this.Thumbnail
457 hasGeneratedThumbnail () {
458 return this.hasThumbnail() && this.Thumbnail.automaticallyGenerated === true
461 generateThumbnailName () {
462 const extension = '.jpg'
464 return 'playlist-' + this.uuid + extension
468 if (!this.hasThumbnail()) return null
470 return WEBSERVER.URL + STATIC_PATHS.THUMBNAILS + this.Thumbnail.filename
473 getThumbnailStaticPath () {
474 if (!this.hasThumbnail()) return null
476 return join(STATIC_PATHS.THUMBNAILS, this.Thumbnail.filename)
480 this.changed('updatedAt', true)
486 return this.OwnerAccount.isOwned()
490 if (this.isOwned()) return false
492 return isOutdated(this, ACTIVITY_PUB.VIDEO_PLAYLIST_REFRESH_INTERVAL)
495 toFormattedJSON (this: MVideoPlaylistFormattable): VideoPlaylist {
499 isLocal: this.isOwned(),
501 displayName: this.name,
502 description: this.description,
505 label: VideoPlaylistModel.getPrivacyLabel(this.privacy)
508 thumbnailPath: this.getThumbnailStaticPath(),
512 label: VideoPlaylistModel.getTypeLabel(this.type)
515 videosLength: this.get('videosLength') as number,
517 createdAt: this.createdAt,
518 updatedAt: this.updatedAt,
520 ownerAccount: this.OwnerAccount.toFormattedSummaryJSON(),
521 videoChannel: this.VideoChannel ? this.VideoChannel.toFormattedSummaryJSON() : null
525 toActivityPubObject (this: MVideoPlaylistAP, page: number, t: Transaction): Promise<PlaylistObject> {
526 const handler = (start: number, count: number) => {
527 return VideoPlaylistElementModel.listUrlsOfForAP(this.id, start, count, t)
530 let icon: ActivityIconObject
531 if (this.hasThumbnail()) {
533 type: 'Image' as 'Image',
534 url: this.getThumbnailUrl(),
535 mediaType: 'image/jpeg' as 'image/jpeg',
536 width: THUMBNAILS_SIZE.width,
537 height: THUMBNAILS_SIZE.height
541 return activityPubCollectionPagination(this.url, handler, page)
543 return Object.assign(o, {
544 type: 'Playlist' as 'Playlist',
546 content: this.description,
548 published: this.createdAt.toISOString(),
549 updated: this.updatedAt.toISOString(),
550 attributedTo: this.VideoChannel ? [ this.VideoChannel.Actor.url ] : [],