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 '../../types/models/video/video-playlist'
55 import { MThumbnail } from '../../types/models/video/thumbnail'
56 import { MAccountId, MChannelId } from '@server/types/models'
59 AVAILABLE_FOR_LIST = 'AVAILABLE_FOR_LIST',
60 WITH_VIDEOS_LENGTH = 'WITH_VIDEOS_LENGTH',
61 WITH_ACCOUNT_AND_CHANNEL_SUMMARY = 'WITH_ACCOUNT_AND_CHANNEL_SUMMARY',
62 WITH_ACCOUNT = 'WITH_ACCOUNT',
63 WITH_THUMBNAIL = 'WITH_THUMBNAIL',
64 WITH_ACCOUNT_AND_CHANNEL = 'WITH_ACCOUNT_AND_CHANNEL'
67 type AvailableForListOptions = {
68 followerActorId: number
69 type?: VideoPlaylistType
71 videoChannelId?: number
72 listMyPlaylists?: boolean
77 [ScopeNames.WITH_THUMBNAIL]: {
80 model: ThumbnailModel,
85 [ScopeNames.WITH_VIDEOS_LENGTH]: {
89 literal('(SELECT COUNT("id") FROM "videoPlaylistElement" WHERE "videoPlaylistId" = "VideoPlaylistModel"."id")'),
95 [ScopeNames.WITH_ACCOUNT]: {
103 [ScopeNames.WITH_ACCOUNT_AND_CHANNEL_SUMMARY]: {
106 model: AccountModel.scope(AccountScopeNames.SUMMARY),
110 model: VideoChannelModel.scope(VideoChannelScopeNames.SUMMARY),
115 [ScopeNames.WITH_ACCOUNT_AND_CHANNEL]: {
122 model: VideoChannelModel,
127 [ScopeNames.AVAILABLE_FOR_LIST]: (options: AvailableForListOptions) => {
129 let whereActor: WhereOptions = {}
131 const whereAnd: WhereOptions[] = []
133 if (options.listMyPlaylists !== true) {
135 privacy: VideoPlaylistPrivacy.PUBLIC
138 // Only list local playlists OR playlists that are on an instance followed by actorId
139 const inQueryInstanceFollow = buildServerIdsFollowedBy(options.followerActorId)
148 [Op.in]: literal(inQueryInstanceFollow)
155 if (options.accountId) {
157 ownerAccountId: options.accountId
161 if (options.videoChannelId) {
163 videoChannelId: options.videoChannelId
173 if (options.search) {
176 [Op.iLike]: '%' + options.search + '%'
185 const accountScope = {
186 method: [ AccountScopeNames.SUMMARY, { whereActor } as SummaryOptions ]
193 model: AccountModel.scope(accountScope),
197 model: VideoChannelModel.scope(VideoChannelScopeNames.SUMMARY),
206 tableName: 'videoPlaylist',
209 fields: [ 'ownerAccountId' ]
212 fields: [ 'videoChannelId' ]
220 export class VideoPlaylistModel extends Model<VideoPlaylistModel> {
228 @Is('VideoPlaylistName', value => throwIfNotValid(value, isVideoPlaylistNameValid, 'name'))
233 @Is('VideoPlaylistDescription', value => throwIfNotValid(value, isVideoPlaylistDescriptionValid, 'description', true))
234 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_PLAYLISTS.DESCRIPTION.max))
238 @Is('VideoPlaylistPrivacy', value => throwIfNotValid(value, isVideoPlaylistPrivacyValid, 'privacy'))
240 privacy: VideoPlaylistPrivacy
243 @Is('VideoPlaylistUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
244 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_PLAYLISTS.URL.max))
248 @Default(DataType.UUIDV4)
250 @Column(DataType.UUID)
254 @Default(VideoPlaylistType.REGULAR)
256 type: VideoPlaylistType
258 @ForeignKey(() => AccountModel)
260 ownerAccountId: number
262 @BelongsTo(() => AccountModel, {
268 OwnerAccount: AccountModel
270 @ForeignKey(() => VideoChannelModel)
272 videoChannelId: number
274 @BelongsTo(() => VideoChannelModel, {
280 VideoChannel: VideoChannelModel
282 @HasMany(() => VideoPlaylistElementModel, {
284 name: 'videoPlaylistId',
289 VideoPlaylistElements: VideoPlaylistElementModel[]
291 @HasOne(() => ThumbnailModel, {
293 name: 'videoPlaylistId',
299 Thumbnail: ThumbnailModel
301 static listForApi (options: {
302 followerActorId: number
306 type?: VideoPlaylistType
308 videoChannelId?: number
309 listMyPlaylists?: boolean
313 offset: options.start,
314 limit: options.count,
315 order: getSort(options.sort)
318 const scopes: (string | ScopeOptions)[] = [
321 ScopeNames.AVAILABLE_FOR_LIST,
324 followerActorId: options.followerActorId,
325 accountId: options.accountId,
326 videoChannelId: options.videoChannelId,
327 listMyPlaylists: options.listMyPlaylists,
328 search: options.search
329 } as AvailableForListOptions
332 ScopeNames.WITH_VIDEOS_LENGTH,
333 ScopeNames.WITH_THUMBNAIL
336 return VideoPlaylistModel
338 .findAndCountAll(query)
339 .then(({ rows, count }) => {
340 return { total: count, data: rows }
344 static listPublicUrlsOfForAP (options: { account?: MAccountId, channel?: MChannelId }, start: number, count: number) {
346 privacy: VideoPlaylistPrivacy.PUBLIC
349 if (options.account) {
350 Object.assign(where, { ownerAccountId: options.account.id })
353 if (options.channel) {
354 Object.assign(where, { videoChannelId: options.channel.id })
358 attributes: [ 'url' ],
364 return VideoPlaylistModel.findAndCountAll(query)
365 .then(({ rows, count }) => {
366 return { total: count, data: rows.map(p => p.url) }
370 static listPlaylistIdsOf (accountId: number, videoIds: number[]): Bluebird<MVideoPlaylistIdWithElements[]> {
372 attributes: [ 'id' ],
374 ownerAccountId: accountId
378 attributes: [ 'id', 'videoId', 'startTimestamp', 'stopTimestamp' ],
379 model: VideoPlaylistElementModel.unscoped(),
390 return VideoPlaylistModel.findAll(query)
393 static doesPlaylistExist (url: string) {
401 return VideoPlaylistModel
406 static loadWithAccountAndChannelSummary (id: number | string, transaction: Transaction): Bluebird<MVideoPlaylistFullSummary> {
407 const where = buildWhereIdOrUUID(id)
414 return VideoPlaylistModel
415 .scope([ ScopeNames.WITH_ACCOUNT_AND_CHANNEL_SUMMARY, ScopeNames.WITH_VIDEOS_LENGTH, ScopeNames.WITH_THUMBNAIL ])
419 static loadWithAccountAndChannel (id: number | string, transaction: Transaction): Bluebird<MVideoPlaylistFull> {
420 const where = buildWhereIdOrUUID(id)
427 return VideoPlaylistModel
428 .scope([ ScopeNames.WITH_ACCOUNT_AND_CHANNEL, ScopeNames.WITH_VIDEOS_LENGTH, ScopeNames.WITH_THUMBNAIL ])
432 static loadByUrlAndPopulateAccount (url: string): Bluebird<MVideoPlaylistAccountThumbnail> {
439 return VideoPlaylistModel.scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_THUMBNAIL ]).findOne(query)
442 static getPrivacyLabel (privacy: VideoPlaylistPrivacy) {
443 return VIDEO_PLAYLIST_PRIVACIES[privacy] || 'Unknown'
446 static getTypeLabel (type: VideoPlaylistType) {
447 return VIDEO_PLAYLIST_TYPES[type] || 'Unknown'
450 static resetPlaylistsOfChannel (videoChannelId: number, transaction: Transaction) {
458 return VideoPlaylistModel.update({ privacy: VideoPlaylistPrivacy.PRIVATE, videoChannelId: null }, query)
461 async setAndSaveThumbnail (thumbnail: MThumbnail, t: Transaction) {
462 thumbnail.videoPlaylistId = this.id
464 this.Thumbnail = await thumbnail.save({ transaction: t })
468 return !!this.Thumbnail
471 hasGeneratedThumbnail () {
472 return this.hasThumbnail() && this.Thumbnail.automaticallyGenerated === true
475 generateThumbnailName () {
476 const extension = '.jpg'
478 return 'playlist-' + this.uuid + extension
482 if (!this.hasThumbnail()) return null
484 return WEBSERVER.URL + STATIC_PATHS.THUMBNAILS + this.Thumbnail.filename
487 getThumbnailStaticPath () {
488 if (!this.hasThumbnail()) return null
490 return join(STATIC_PATHS.THUMBNAILS, this.Thumbnail.filename)
494 this.changed('updatedAt', true)
500 return this.OwnerAccount.isOwned()
504 if (this.isOwned()) return false
506 return isOutdated(this, ACTIVITY_PUB.VIDEO_PLAYLIST_REFRESH_INTERVAL)
509 toFormattedJSON (this: MVideoPlaylistFormattable): VideoPlaylist {
513 isLocal: this.isOwned(),
515 displayName: this.name,
516 description: this.description,
519 label: VideoPlaylistModel.getPrivacyLabel(this.privacy)
522 thumbnailPath: this.getThumbnailStaticPath(),
526 label: VideoPlaylistModel.getTypeLabel(this.type)
529 videosLength: this.get('videosLength') as number,
531 createdAt: this.createdAt,
532 updatedAt: this.updatedAt,
534 ownerAccount: this.OwnerAccount.toFormattedSummaryJSON(),
535 videoChannel: this.VideoChannel
536 ? this.VideoChannel.toFormattedSummaryJSON()
541 toActivityPubObject (this: MVideoPlaylistAP, page: number, t: Transaction): Promise<PlaylistObject> {
542 const handler = (start: number, count: number) => {
543 return VideoPlaylistElementModel.listUrlsOfForAP(this.id, start, count, t)
546 let icon: ActivityIconObject
547 if (this.hasThumbnail()) {
549 type: 'Image' as 'Image',
550 url: this.getThumbnailUrl(),
551 mediaType: 'image/jpeg' as 'image/jpeg',
552 width: THUMBNAILS_SIZE.width,
553 height: THUMBNAILS_SIZE.height
557 return activityPubCollectionPagination(this.url, handler, page)
559 return Object.assign(o, {
560 type: 'Playlist' as 'Playlist',
562 content: this.description,
564 published: this.createdAt.toISOString(),
565 updated: this.updatedAt.toISOString(),
566 attributedTo: this.VideoChannel ? [ this.VideoChannel.Actor.url ] : [],