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