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