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