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