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