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