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