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