]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/video-playlist.ts
8bbe54c49531ba91650c8d7ffbbaea75dd2e3e86
[github/Chocobozzz/PeerTube.git] / server / models / video / video-playlist.ts
1 import { join } from 'path'
2 import { FindOptions, Includeable, literal, Op, ScopeOptions, Sequelize, Transaction, WhereOptions } from 'sequelize'
3 import {
4 AllowNull,
5 BelongsTo,
6 Column,
7 CreatedAt,
8 DataType,
9 Default,
10 ForeignKey,
11 HasMany,
12 HasOne,
13 Is,
14 IsUUID,
15 Model,
16 Scopes,
17 Table,
18 UpdatedAt
19 } from 'sequelize-typescript'
20 import { activityPubCollectionPagination } from '@server/lib/activitypub/collection'
21 import { MAccountId, MChannelId } from '@server/types/models'
22 import { buildPlaylistEmbedPath, buildPlaylistWatchPath, pick } from '@shared/core-utils'
23 import { buildUUID, uuidToShort } from '@shared/extra-utils'
24 import { AttributesOnly } from '@shared/typescript-utils'
25 import { ActivityIconObject } from '../../../shared/models/activitypub/objects'
26 import { PlaylistObject } from '../../../shared/models/activitypub/objects/playlist-object'
27 import { VideoPlaylistPrivacy } from '../../../shared/models/videos/playlist/video-playlist-privacy.model'
28 import { VideoPlaylistType } from '../../../shared/models/videos/playlist/video-playlist-type.model'
29 import { VideoPlaylist } from '../../../shared/models/videos/playlist/video-playlist.model'
30 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
31 import {
32 isVideoPlaylistDescriptionValid,
33 isVideoPlaylistNameValid,
34 isVideoPlaylistPrivacyValid
35 } from '../../helpers/custom-validators/video-playlists'
36 import {
37 ACTIVITY_PUB,
38 CONSTRAINTS_FIELDS,
39 STATIC_PATHS,
40 THUMBNAILS_SIZE,
41 VIDEO_PLAYLIST_PRIVACIES,
42 VIDEO_PLAYLIST_TYPES,
43 WEBSERVER
44 } from '../../initializers/constants'
45 import { MThumbnail } from '../../types/models/video/thumbnail'
46 import {
47 MVideoPlaylistAccountThumbnail,
48 MVideoPlaylistAP,
49 MVideoPlaylistFormattable,
50 MVideoPlaylistFull,
51 MVideoPlaylistFullSummary,
52 MVideoPlaylistSummaryWithElements
53 } from '../../types/models/video/video-playlist'
54 import { AccountModel, ScopeNames as AccountScopeNames, SummaryOptions } from '../account/account'
55 import { ActorModel } from '../actor/actor'
56 import { setAsUpdated } from '../shared'
57 import {
58 buildServerIdsFollowedBy,
59 buildTrigramSearchIndex,
60 buildWhereIdOrUUID,
61 createSimilarityAttribute,
62 getPlaylistSort,
63 isOutdated,
64 throwIfNotValid
65 } from '../utils'
66 import { ThumbnailModel } from './thumbnail'
67 import { ScopeNames as VideoChannelScopeNames, VideoChannelModel } from './video-channel'
68 import { VideoPlaylistElementModel } from './video-playlist-element'
69
70 enum ScopeNames {
71 AVAILABLE_FOR_LIST = 'AVAILABLE_FOR_LIST',
72 WITH_VIDEOS_LENGTH = 'WITH_VIDEOS_LENGTH',
73 WITH_ACCOUNT_AND_CHANNEL_SUMMARY = 'WITH_ACCOUNT_AND_CHANNEL_SUMMARY',
74 WITH_ACCOUNT = 'WITH_ACCOUNT',
75 WITH_THUMBNAIL = 'WITH_THUMBNAIL',
76 WITH_ACCOUNT_AND_CHANNEL = 'WITH_ACCOUNT_AND_CHANNEL'
77 }
78
79 type AvailableForListOptions = {
80 followerActorId?: number
81 type?: VideoPlaylistType
82 accountId?: number
83 videoChannelId?: number
84 listMyPlaylists?: boolean
85 search?: string
86 host?: string
87 uuids?: string[]
88 withVideos?: boolean
89 forCount?: boolean
90 }
91
92 function getVideoLengthSelect () {
93 return 'SELECT COUNT("id") FROM "videoPlaylistElement" WHERE "videoPlaylistId" = "VideoPlaylistModel"."id"'
94 }
95
96 @Scopes(() => ({
97 [ScopeNames.WITH_THUMBNAIL]: {
98 include: [
99 {
100 model: ThumbnailModel,
101 required: false
102 }
103 ]
104 },
105 [ScopeNames.WITH_VIDEOS_LENGTH]: {
106 attributes: {
107 include: [
108 [
109 literal(`(${getVideoLengthSelect()})`),
110 'videosLength'
111 ]
112 ]
113 }
114 } as FindOptions,
115 [ScopeNames.WITH_ACCOUNT]: {
116 include: [
117 {
118 model: AccountModel,
119 required: true
120 }
121 ]
122 },
123 [ScopeNames.WITH_ACCOUNT_AND_CHANNEL_SUMMARY]: {
124 include: [
125 {
126 model: AccountModel.scope(AccountScopeNames.SUMMARY),
127 required: true
128 },
129 {
130 model: VideoChannelModel.scope(VideoChannelScopeNames.SUMMARY),
131 required: false
132 }
133 ]
134 },
135 [ScopeNames.WITH_ACCOUNT_AND_CHANNEL]: {
136 include: [
137 {
138 model: AccountModel,
139 required: true
140 },
141 {
142 model: VideoChannelModel,
143 required: false
144 }
145 ]
146 },
147 [ScopeNames.AVAILABLE_FOR_LIST]: (options: AvailableForListOptions) => {
148 const whereAnd: WhereOptions[] = []
149
150 const whereServer = options.host && options.host !== WEBSERVER.HOST
151 ? { host: options.host }
152 : undefined
153
154 let whereActor: WhereOptions = {}
155
156 if (options.host === WEBSERVER.HOST) {
157 whereActor = {
158 [Op.and]: [ { serverId: null } ]
159 }
160 }
161
162 if (options.listMyPlaylists !== true) {
163 whereAnd.push({
164 privacy: VideoPlaylistPrivacy.PUBLIC
165 })
166
167 // Only list local playlists
168 const whereActorOr: WhereOptions[] = [
169 {
170 serverId: null
171 }
172 ]
173
174 // … OR playlists that are on an instance followed by actorId
175 if (options.followerActorId) {
176 const inQueryInstanceFollow = buildServerIdsFollowedBy(options.followerActorId)
177
178 whereActorOr.push({
179 serverId: {
180 [Op.in]: literal(inQueryInstanceFollow)
181 }
182 })
183 }
184
185 Object.assign(whereActor, { [Op.or]: whereActorOr })
186 }
187
188 if (options.accountId) {
189 whereAnd.push({
190 ownerAccountId: options.accountId
191 })
192 }
193
194 if (options.videoChannelId) {
195 whereAnd.push({
196 videoChannelId: options.videoChannelId
197 })
198 }
199
200 if (options.type) {
201 whereAnd.push({
202 type: options.type
203 })
204 }
205
206 if (options.uuids) {
207 whereAnd.push({
208 uuid: {
209 [Op.in]: options.uuids
210 }
211 })
212 }
213
214 if (options.withVideos === true) {
215 whereAnd.push(
216 literal(`(${getVideoLengthSelect()}) != 0`)
217 )
218 }
219
220 let attributesInclude: any[] = [ literal('0 as similarity') ]
221
222 if (options.search) {
223 const escapedSearch = VideoPlaylistModel.sequelize.escape(options.search)
224 const escapedLikeSearch = VideoPlaylistModel.sequelize.escape('%' + options.search + '%')
225 attributesInclude = [ createSimilarityAttribute('VideoPlaylistModel.name', options.search) ]
226
227 whereAnd.push({
228 [Op.or]: [
229 Sequelize.literal(
230 'lower(immutable_unaccent("VideoPlaylistModel"."name")) % lower(immutable_unaccent(' + escapedSearch + '))'
231 ),
232 Sequelize.literal(
233 'lower(immutable_unaccent("VideoPlaylistModel"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))'
234 )
235 ]
236 })
237 }
238
239 const where = {
240 [Op.and]: whereAnd
241 }
242
243 const include: Includeable[] = [
244 {
245 model: AccountModel.scope({
246 method: [ AccountScopeNames.SUMMARY, { whereActor, whereServer, forCount: options.forCount } as SummaryOptions ]
247 }),
248 required: true
249 }
250 ]
251
252 if (options.forCount !== true) {
253 include.push({
254 model: VideoChannelModel.scope(VideoChannelScopeNames.SUMMARY),
255 required: false
256 })
257 }
258
259 return {
260 attributes: {
261 include: attributesInclude
262 },
263 where,
264 include
265 } as FindOptions
266 }
267 }))
268
269 @Table({
270 tableName: 'videoPlaylist',
271 indexes: [
272 buildTrigramSearchIndex('video_playlist_name_trigram', 'name'),
273
274 {
275 fields: [ 'ownerAccountId' ]
276 },
277 {
278 fields: [ 'videoChannelId' ]
279 },
280 {
281 fields: [ 'url' ],
282 unique: true
283 }
284 ]
285 })
286 export class VideoPlaylistModel extends Model<Partial<AttributesOnly<VideoPlaylistModel>>> {
287 @CreatedAt
288 createdAt: Date
289
290 @UpdatedAt
291 updatedAt: Date
292
293 @AllowNull(false)
294 @Is('VideoPlaylistName', value => throwIfNotValid(value, isVideoPlaylistNameValid, 'name'))
295 @Column
296 name: string
297
298 @AllowNull(true)
299 @Is('VideoPlaylistDescription', value => throwIfNotValid(value, isVideoPlaylistDescriptionValid, 'description', true))
300 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_PLAYLISTS.DESCRIPTION.max))
301 description: string
302
303 @AllowNull(false)
304 @Is('VideoPlaylistPrivacy', value => throwIfNotValid(value, isVideoPlaylistPrivacyValid, 'privacy'))
305 @Column
306 privacy: VideoPlaylistPrivacy
307
308 @AllowNull(false)
309 @Is('VideoPlaylistUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
310 @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_PLAYLISTS.URL.max))
311 url: string
312
313 @AllowNull(false)
314 @Default(DataType.UUIDV4)
315 @IsUUID(4)
316 @Column(DataType.UUID)
317 uuid: string
318
319 @AllowNull(false)
320 @Default(VideoPlaylistType.REGULAR)
321 @Column
322 type: VideoPlaylistType
323
324 @ForeignKey(() => AccountModel)
325 @Column
326 ownerAccountId: number
327
328 @BelongsTo(() => AccountModel, {
329 foreignKey: {
330 allowNull: false
331 },
332 onDelete: 'CASCADE'
333 })
334 OwnerAccount: AccountModel
335
336 @ForeignKey(() => VideoChannelModel)
337 @Column
338 videoChannelId: number
339
340 @BelongsTo(() => VideoChannelModel, {
341 foreignKey: {
342 allowNull: true
343 },
344 onDelete: 'CASCADE'
345 })
346 VideoChannel: VideoChannelModel
347
348 @HasMany(() => VideoPlaylistElementModel, {
349 foreignKey: {
350 name: 'videoPlaylistId',
351 allowNull: false
352 },
353 onDelete: 'CASCADE'
354 })
355 VideoPlaylistElements: VideoPlaylistElementModel[]
356
357 @HasOne(() => ThumbnailModel, {
358 foreignKey: {
359 name: 'videoPlaylistId',
360 allowNull: true
361 },
362 onDelete: 'CASCADE',
363 hooks: true
364 })
365 Thumbnail: ThumbnailModel
366
367 static listForApi (options: AvailableForListOptions & {
368 start: number
369 count: number
370 sort: string
371 }) {
372 const query = {
373 offset: options.start,
374 limit: options.count,
375 order: getPlaylistSort(options.sort)
376 }
377
378 const commonAvailableForListOptions = pick(options, [
379 'type',
380 'followerActorId',
381 'accountId',
382 'videoChannelId',
383 'listMyPlaylists',
384 'search',
385 'host',
386 'uuids'
387 ])
388
389 const scopesFind: (string | ScopeOptions)[] = [
390 {
391 method: [
392 ScopeNames.AVAILABLE_FOR_LIST,
393 {
394 ...commonAvailableForListOptions,
395
396 withVideos: options.withVideos || false
397 } as AvailableForListOptions
398 ]
399 },
400 ScopeNames.WITH_VIDEOS_LENGTH,
401 ScopeNames.WITH_THUMBNAIL
402 ]
403
404 const scopesCount: (string | ScopeOptions)[] = [
405 {
406 method: [
407 ScopeNames.AVAILABLE_FOR_LIST,
408
409 {
410 ...commonAvailableForListOptions,
411
412 withVideos: options.withVideos || false,
413 forCount: true
414 } as AvailableForListOptions
415 ]
416 },
417 ScopeNames.WITH_VIDEOS_LENGTH
418 ]
419
420 return Promise.all([
421 VideoPlaylistModel.scope(scopesCount).count(),
422 VideoPlaylistModel.scope(scopesFind).findAll(query)
423 ]).then(([ count, rows ]) => ({ total: count, data: rows }))
424 }
425
426 static searchForApi (options: Pick<AvailableForListOptions, 'followerActorId' | 'search' | 'host' | 'uuids'> & {
427 start: number
428 count: number
429 sort: string
430 }) {
431 return VideoPlaylistModel.listForApi({
432 ...options,
433
434 type: VideoPlaylistType.REGULAR,
435 listMyPlaylists: false,
436 withVideos: true
437 })
438 }
439
440 static listPublicUrlsOfForAP (options: { account?: MAccountId, channel?: MChannelId }, start: number, count: number) {
441 const where = {
442 privacy: VideoPlaylistPrivacy.PUBLIC
443 }
444
445 if (options.account) {
446 Object.assign(where, { ownerAccountId: options.account.id })
447 }
448
449 if (options.channel) {
450 Object.assign(where, { videoChannelId: options.channel.id })
451 }
452
453 const getQuery = (forCount: boolean) => {
454 return {
455 attributes: forCount === true
456 ? []
457 : [ 'url' ],
458 offset: start,
459 limit: count,
460 where
461 }
462 }
463
464 return Promise.all([
465 VideoPlaylistModel.count(getQuery(true)),
466 VideoPlaylistModel.findAll(getQuery(false))
467 ]).then(([ total, rows ]) => ({
468 total,
469 data: rows.map(p => p.url)
470 }))
471 }
472
473 static listPlaylistSummariesOf (accountId: number, videoIds: number[]): Promise<MVideoPlaylistSummaryWithElements[]> {
474 const query = {
475 attributes: [ 'id', 'name', 'uuid' ],
476 where: {
477 ownerAccountId: accountId
478 },
479 include: [
480 {
481 attributes: [ 'id', 'videoId', 'startTimestamp', 'stopTimestamp' ],
482 model: VideoPlaylistElementModel.unscoped(),
483 where: {
484 videoId: {
485 [Op.in]: videoIds
486 }
487 },
488 required: true
489 }
490 ]
491 }
492
493 return VideoPlaylistModel.findAll(query)
494 }
495
496 static doesPlaylistExist (url: string) {
497 const query = {
498 attributes: [ 'id' ],
499 where: {
500 url
501 }
502 }
503
504 return VideoPlaylistModel
505 .findOne(query)
506 .then(e => !!e)
507 }
508
509 static loadWithAccountAndChannelSummary (id: number | string, transaction: Transaction): Promise<MVideoPlaylistFullSummary> {
510 const where = buildWhereIdOrUUID(id)
511
512 const query = {
513 where,
514 transaction
515 }
516
517 return VideoPlaylistModel
518 .scope([ ScopeNames.WITH_ACCOUNT_AND_CHANNEL_SUMMARY, ScopeNames.WITH_VIDEOS_LENGTH, ScopeNames.WITH_THUMBNAIL ])
519 .findOne(query)
520 }
521
522 static loadWithAccountAndChannel (id: number | string, transaction: Transaction): Promise<MVideoPlaylistFull> {
523 const where = buildWhereIdOrUUID(id)
524
525 const query = {
526 where,
527 transaction
528 }
529
530 return VideoPlaylistModel
531 .scope([ ScopeNames.WITH_ACCOUNT_AND_CHANNEL, ScopeNames.WITH_VIDEOS_LENGTH, ScopeNames.WITH_THUMBNAIL ])
532 .findOne(query)
533 }
534
535 static loadByUrlAndPopulateAccount (url: string): Promise<MVideoPlaylistAccountThumbnail> {
536 const query = {
537 where: {
538 url
539 }
540 }
541
542 return VideoPlaylistModel.scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_THUMBNAIL ]).findOne(query)
543 }
544
545 static loadByUrlWithAccountAndChannelSummary (url: string): Promise<MVideoPlaylistFullSummary> {
546 const query = {
547 where: {
548 url
549 }
550 }
551
552 return VideoPlaylistModel
553 .scope([ ScopeNames.WITH_ACCOUNT_AND_CHANNEL_SUMMARY, ScopeNames.WITH_VIDEOS_LENGTH, ScopeNames.WITH_THUMBNAIL ])
554 .findOne(query)
555 }
556
557 static getPrivacyLabel (privacy: VideoPlaylistPrivacy) {
558 return VIDEO_PLAYLIST_PRIVACIES[privacy] || 'Unknown'
559 }
560
561 static getTypeLabel (type: VideoPlaylistType) {
562 return VIDEO_PLAYLIST_TYPES[type] || 'Unknown'
563 }
564
565 static resetPlaylistsOfChannel (videoChannelId: number, transaction: Transaction) {
566 const query = {
567 where: {
568 videoChannelId
569 },
570 transaction
571 }
572
573 return VideoPlaylistModel.update({ privacy: VideoPlaylistPrivacy.PRIVATE, videoChannelId: null }, query)
574 }
575
576 async setAndSaveThumbnail (thumbnail: MThumbnail, t: Transaction) {
577 thumbnail.videoPlaylistId = this.id
578
579 this.Thumbnail = await thumbnail.save({ transaction: t })
580 }
581
582 hasThumbnail () {
583 return !!this.Thumbnail
584 }
585
586 hasGeneratedThumbnail () {
587 return this.hasThumbnail() && this.Thumbnail.automaticallyGenerated === true
588 }
589
590 generateThumbnailName () {
591 const extension = '.jpg'
592
593 return 'playlist-' + buildUUID() + extension
594 }
595
596 getThumbnailUrl () {
597 if (!this.hasThumbnail()) return null
598
599 return WEBSERVER.URL + STATIC_PATHS.THUMBNAILS + this.Thumbnail.filename
600 }
601
602 getThumbnailStaticPath () {
603 if (!this.hasThumbnail()) return null
604
605 return join(STATIC_PATHS.THUMBNAILS, this.Thumbnail.filename)
606 }
607
608 getWatchStaticPath () {
609 return buildPlaylistWatchPath({ shortUUID: uuidToShort(this.uuid) })
610 }
611
612 getEmbedStaticPath () {
613 return buildPlaylistEmbedPath(this)
614 }
615
616 static async getStats () {
617 const totalLocalPlaylists = await VideoPlaylistModel.count({
618 include: [
619 {
620 model: AccountModel.unscoped(),
621 required: true,
622 include: [
623 {
624 model: ActorModel.unscoped(),
625 required: true,
626 where: {
627 serverId: null
628 }
629 }
630 ]
631 }
632 ],
633 where: {
634 privacy: VideoPlaylistPrivacy.PUBLIC
635 }
636 })
637
638 return {
639 totalLocalPlaylists
640 }
641 }
642
643 setAsRefreshed () {
644 return setAsUpdated('videoPlaylist', this.id)
645 }
646
647 setVideosLength (videosLength: number) {
648 this.set('videosLength' as any, videosLength, { raw: true })
649 }
650
651 isOwned () {
652 return this.OwnerAccount.isOwned()
653 }
654
655 isOutdated () {
656 if (this.isOwned()) return false
657
658 return isOutdated(this, ACTIVITY_PUB.VIDEO_PLAYLIST_REFRESH_INTERVAL)
659 }
660
661 toFormattedJSON (this: MVideoPlaylistFormattable): VideoPlaylist {
662 return {
663 id: this.id,
664 uuid: this.uuid,
665 shortUUID: uuidToShort(this.uuid),
666
667 isLocal: this.isOwned(),
668
669 url: this.url,
670
671 displayName: this.name,
672 description: this.description,
673 privacy: {
674 id: this.privacy,
675 label: VideoPlaylistModel.getPrivacyLabel(this.privacy)
676 },
677
678 thumbnailPath: this.getThumbnailStaticPath(),
679 embedPath: this.getEmbedStaticPath(),
680
681 type: {
682 id: this.type,
683 label: VideoPlaylistModel.getTypeLabel(this.type)
684 },
685
686 videosLength: this.get('videosLength') as number,
687
688 createdAt: this.createdAt,
689 updatedAt: this.updatedAt,
690
691 ownerAccount: this.OwnerAccount.toFormattedSummaryJSON(),
692 videoChannel: this.VideoChannel
693 ? this.VideoChannel.toFormattedSummaryJSON()
694 : null
695 }
696 }
697
698 toActivityPubObject (this: MVideoPlaylistAP, page: number, t: Transaction): Promise<PlaylistObject> {
699 const handler = (start: number, count: number) => {
700 return VideoPlaylistElementModel.listUrlsOfForAP(this.id, start, count, t)
701 }
702
703 let icon: ActivityIconObject
704 if (this.hasThumbnail()) {
705 icon = {
706 type: 'Image' as 'Image',
707 url: this.getThumbnailUrl(),
708 mediaType: 'image/jpeg' as 'image/jpeg',
709 width: THUMBNAILS_SIZE.width,
710 height: THUMBNAILS_SIZE.height
711 }
712 }
713
714 return activityPubCollectionPagination(this.url, handler, page)
715 .then(o => {
716 return Object.assign(o, {
717 type: 'Playlist' as 'Playlist',
718 name: this.name,
719 content: this.description,
720 mediaType: 'text/markdown' as 'text/markdown',
721 uuid: this.uuid,
722 published: this.createdAt.toISOString(),
723 updated: this.updatedAt.toISOString(),
724 attributedTo: this.VideoChannel ? [ this.VideoChannel.Actor.url ] : [],
725 icon
726 })
727 })
728 }
729 }