17 } from 'sequelize-typescript'
18 import * as Sequelize from 'sequelize'
19 import { VideoPlaylistPrivacy } from '../../../shared/models/videos/playlist/video-playlist-privacy.model'
20 import { buildServerIdsFollowedBy, buildWhereIdOrUUID, getSort, throwIfNotValid } from '../utils'
22 isVideoPlaylistDescriptionValid,
23 isVideoPlaylistNameValid,
24 isVideoPlaylistPrivacyValid
25 } from '../../helpers/custom-validators/video-playlists'
26 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
32 VIDEO_PLAYLIST_PRIVACIES,
34 } from '../../initializers'
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 { remove } from 'fs-extra'
43 import { logger } from '../../helpers/logger'
44 import { VideoPlaylistType } from '../../../shared/models/videos/playlist/video-playlist-type.model'
47 AVAILABLE_FOR_LIST = 'AVAILABLE_FOR_LIST',
48 WITH_VIDEOS_LENGTH = 'WITH_VIDEOS_LENGTH',
49 WITH_ACCOUNT_AND_CHANNEL_SUMMARY = 'WITH_ACCOUNT_AND_CHANNEL_SUMMARY',
50 WITH_ACCOUNT = 'WITH_ACCOUNT'
53 type AvailableForListOptions = {
54 followerActorId: number
55 type?: VideoPlaylistType
57 videoChannelId?: number
58 privateAndUnlisted?: boolean
62 [ ScopeNames.WITH_VIDEOS_LENGTH ]: {
66 Sequelize.literal('(SELECT COUNT("id") FROM "videoPlaylistElement" WHERE "videoPlaylistId" = "VideoPlaylistModel"."id")'),
72 [ ScopeNames.WITH_ACCOUNT ]: {
75 model: () => AccountModel,
80 [ ScopeNames.WITH_ACCOUNT_AND_CHANNEL_SUMMARY ]: {
83 model: () => AccountModel.scope(AccountScopeNames.SUMMARY),
87 model: () => VideoChannelModel.scope(VideoChannelScopeNames.SUMMARY),
92 [ ScopeNames.AVAILABLE_FOR_LIST ]: (options: AvailableForListOptions) => {
93 // Only list local playlists OR playlists that are on an instance followed by actorId
94 const inQueryInstanceFollow = buildServerIdsFollowedBy(options.followerActorId)
96 [ Sequelize.Op.or ]: [
102 [ Sequelize.Op.in ]: Sequelize.literal(inQueryInstanceFollow)
108 const whereAnd: any[] = []
110 if (options.privateAndUnlisted !== true) {
112 privacy: VideoPlaylistPrivacy.PUBLIC
116 if (options.accountId) {
118 ownerAccountId: options.accountId
122 if (options.videoChannelId) {
124 videoChannelId: options.videoChannelId
135 [Sequelize.Op.and]: whereAnd
138 const accountScope = {
139 method: [ AccountScopeNames.SUMMARY, actorWhere ]
146 model: AccountModel.scope(accountScope),
150 model: VideoChannelModel.scope(VideoChannelScopeNames.SUMMARY),
159 tableName: 'videoPlaylist',
162 fields: [ 'ownerAccountId' ]
165 fields: [ 'videoChannelId' ]
173 export class VideoPlaylistModel extends Model<VideoPlaylistModel> {
181 @Is('VideoPlaylistName', value => throwIfNotValid(value, isVideoPlaylistNameValid, 'name'))
186 @Is('VideoPlaylistDescription', value => throwIfNotValid(value, isVideoPlaylistDescriptionValid, 'description'))
191 @Is('VideoPlaylistPrivacy', value => throwIfNotValid(value, isVideoPlaylistPrivacyValid, 'privacy'))
193 privacy: VideoPlaylistPrivacy
196 @Is('VideoPlaylistUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
197 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_PLAYLISTS.URL.max))
201 @Default(DataType.UUIDV4)
203 @Column(DataType.UUID)
207 @Default(VideoPlaylistType.REGULAR)
209 type: VideoPlaylistType
211 @ForeignKey(() => AccountModel)
213 ownerAccountId: number
215 @BelongsTo(() => AccountModel, {
221 OwnerAccount: AccountModel
223 @ForeignKey(() => VideoChannelModel)
225 videoChannelId: number
227 @BelongsTo(() => VideoChannelModel, {
233 VideoChannel: VideoChannelModel
235 @HasMany(() => VideoPlaylistElementModel, {
237 name: 'videoPlaylistId',
242 VideoPlaylistElements: VideoPlaylistElementModel[]
245 static async removeFiles (instance: VideoPlaylistModel) {
246 logger.info('Removing files of video playlist %s.', instance.url)
248 return instance.removeThumbnail()
251 static listForApi (options: {
252 followerActorId: number
256 type?: VideoPlaylistType,
258 videoChannelId?: number,
259 privateAndUnlisted?: boolean
262 offset: options.start,
263 limit: options.count,
264 order: getSort(options.sort)
270 ScopeNames.AVAILABLE_FOR_LIST,
273 followerActorId: options.followerActorId,
274 accountId: options.accountId,
275 videoChannelId: options.videoChannelId,
276 privateAndUnlisted: options.privateAndUnlisted
277 } as AvailableForListOptions
279 } as any, // FIXME: typings
280 ScopeNames.WITH_VIDEOS_LENGTH
283 return VideoPlaylistModel
285 .findAndCountAll(query)
286 .then(({ rows, count }) => {
287 return { total: count, data: rows }
291 static listUrlsOfForAP (accountId: number, start: number, count: number) {
293 attributes: [ 'url' ],
297 ownerAccountId: accountId
301 return VideoPlaylistModel.findAndCountAll(query)
302 .then(({ rows, count }) => {
303 return { total: count, data: rows.map(p => p.url) }
307 static doesPlaylistExist (url: string) {
315 return VideoPlaylistModel
320 static loadWithAccountAndChannel (id: number | string, transaction: Sequelize.Transaction) {
321 const where = buildWhereIdOrUUID(id)
328 return VideoPlaylistModel
329 .scope([ ScopeNames.WITH_ACCOUNT_AND_CHANNEL_SUMMARY, ScopeNames.WITH_VIDEOS_LENGTH ])
333 static loadByUrlAndPopulateAccount (url: string) {
340 return VideoPlaylistModel.scope(ScopeNames.WITH_ACCOUNT).findOne(query)
343 static getPrivacyLabel (privacy: VideoPlaylistPrivacy) {
344 return VIDEO_PLAYLIST_PRIVACIES[privacy] || 'Unknown'
347 static getTypeLabel (type: VideoPlaylistType) {
348 return VIDEO_PLAYLIST_TYPES[type] || 'Unknown'
351 static resetPlaylistsOfChannel (videoChannelId: number, transaction: Sequelize.Transaction) {
359 return VideoPlaylistModel.update({ privacy: VideoPlaylistPrivacy.PRIVATE, videoChannelId: null }, query)
362 getThumbnailName () {
363 const extension = '.jpg'
365 return 'playlist-' + this.uuid + extension
369 return CONFIG.WEBSERVER.URL + STATIC_PATHS.THUMBNAILS + this.getThumbnailName()
372 getThumbnailStaticPath () {
373 return join(STATIC_PATHS.THUMBNAILS, this.getThumbnailName())
377 const thumbnailPath = join(CONFIG.STORAGE.THUMBNAILS_DIR, this.getThumbnailName())
378 return remove(thumbnailPath)
379 .catch(err => logger.warn('Cannot delete thumbnail %s.', thumbnailPath, { err }))
383 return this.OwnerAccount.isOwned()
386 toFormattedJSON (): VideoPlaylist {
390 isLocal: this.isOwned(),
392 displayName: this.name,
393 description: this.description,
396 label: VideoPlaylistModel.getPrivacyLabel(this.privacy)
399 thumbnailPath: this.getThumbnailStaticPath(),
403 label: VideoPlaylistModel.getTypeLabel(this.type)
406 videosLength: this.get('videosLength'),
408 createdAt: this.createdAt,
409 updatedAt: this.updatedAt,
411 ownerAccount: this.OwnerAccount.toFormattedSummaryJSON(),
412 videoChannel: this.VideoChannel ? this.VideoChannel.toFormattedSummaryJSON() : null
416 toActivityPubObject (page: number, t: Sequelize.Transaction): Promise<PlaylistObject> {
417 const handler = (start: number, count: number) => {
418 return VideoPlaylistElementModel.listUrlsOfForAP(this.id, start, count, t)
421 return activityPubCollectionPagination(this.url, handler, page)
423 return Object.assign(o, {
424 type: 'Playlist' as 'Playlist',
426 content: this.description,
428 published: this.createdAt.toISOString(),
429 updated: this.updatedAt.toISOString(),
430 attributedTo: this.VideoChannel ? [ this.VideoChannel.Actor.url ] : [],
432 type: 'Image' as 'Image',
433 url: this.getThumbnailUrl(),
434 mediaType: 'image/jpeg' as 'image/jpeg',
435 width: THUMBNAILS_SIZE.width,
436 height: THUMBNAILS_SIZE.height