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