]>
Commit | Line | Data |
---|---|---|
418d092a C |
1 | import { |
2 | AllowNull, | |
418d092a C |
3 | BelongsTo, |
4 | Column, | |
5 | CreatedAt, | |
6 | DataType, | |
7 | Default, | |
8 | ForeignKey, | |
9 | HasMany, | |
e8bafea3 | 10 | HasOne, |
418d092a C |
11 | Is, |
12 | IsUUID, | |
13 | Model, | |
14 | Scopes, | |
15 | Table, | |
822c7e61 | 16 | UpdatedAt |
418d092a | 17 | } from 'sequelize-typescript' |
418d092a | 18 | import { VideoPlaylistPrivacy } from '../../../shared/models/videos/playlist/video-playlist-privacy.model' |
822c7e61 | 19 | import { buildServerIdsFollowedBy, buildWhereIdOrUUID, getSort, isOutdated, throwIfNotValid } from '../utils' |
418d092a C |
20 | import { |
21 | isVideoPlaylistDescriptionValid, | |
22 | isVideoPlaylistNameValid, | |
23 | isVideoPlaylistPrivacyValid | |
24 | } from '../../helpers/custom-validators/video-playlists' | |
25 | import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' | |
df0b219d | 26 | import { |
9f79ade6 | 27 | ACTIVITY_PUB, |
df0b219d C |
28 | CONSTRAINTS_FIELDS, |
29 | STATIC_PATHS, | |
30 | THUMBNAILS_SIZE, | |
31 | VIDEO_PLAYLIST_PRIVACIES, | |
6dd9de95 C |
32 | VIDEO_PLAYLIST_TYPES, |
33 | WEBSERVER | |
74dc3bca | 34 | } from '../../initializers/constants' |
418d092a | 35 | import { VideoPlaylist } from '../../../shared/models/videos/playlist/video-playlist.model' |
bfbd9128 | 36 | import { AccountModel, ScopeNames as AccountScopeNames, SummaryOptions } from '../account/account' |
418d092a C |
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' | |
df0b219d | 42 | import { VideoPlaylistType } from '../../../shared/models/videos/playlist/video-playlist-type.model' |
e8bafea3 C |
43 | import { ThumbnailModel } from './thumbnail' |
44 | import { ActivityIconObject } from '../../../shared/models/activitypub/objects' | |
3acc5084 | 45 | import { FindOptions, literal, Op, ScopeOptions, Transaction, WhereOptions } from 'sequelize' |
453e83ea C |
46 | import * as Bluebird from 'bluebird' |
47 | import { | |
822c7e61 C |
48 | MVideoPlaylistAccountThumbnail, |
49 | MVideoPlaylistAP, | |
1ca9f7c3 | 50 | MVideoPlaylistFormattable, |
453e83ea C |
51 | MVideoPlaylistFull, |
52 | MVideoPlaylistFullSummary, | |
53 | MVideoPlaylistIdWithElements | |
54 | } from '../../typings/models/video/video-playlist' | |
55 | import { MThumbnail } from '../../typings/models/video/thumbnail' | |
418d092a C |
56 | |
57 | enum ScopeNames { | |
58 | AVAILABLE_FOR_LIST = 'AVAILABLE_FOR_LIST', | |
59 | WITH_VIDEOS_LENGTH = 'WITH_VIDEOS_LENGTH', | |
df0b219d | 60 | WITH_ACCOUNT_AND_CHANNEL_SUMMARY = 'WITH_ACCOUNT_AND_CHANNEL_SUMMARY', |
09979f89 | 61 | WITH_ACCOUNT = 'WITH_ACCOUNT', |
e8bafea3 | 62 | WITH_THUMBNAIL = 'WITH_THUMBNAIL', |
09979f89 | 63 | WITH_ACCOUNT_AND_CHANNEL = 'WITH_ACCOUNT_AND_CHANNEL' |
418d092a C |
64 | } |
65 | ||
66 | type AvailableForListOptions = { | |
67 | followerActorId: number | |
df0b219d C |
68 | type?: VideoPlaylistType |
69 | accountId?: number | |
418d092a | 70 | videoChannelId?: number |
a1587156 | 71 | listMyPlaylists?: boolean |
c06af501 | 72 | search?: string |
418d092a C |
73 | } |
74 | ||
3acc5084 | 75 | @Scopes(() => ({ |
a1587156 | 76 | [ScopeNames.WITH_THUMBNAIL]: { |
e8bafea3 C |
77 | include: [ |
78 | { | |
3acc5084 | 79 | model: ThumbnailModel, |
e8bafea3 C |
80 | required: false |
81 | } | |
82 | ] | |
83 | }, | |
a1587156 | 84 | [ScopeNames.WITH_VIDEOS_LENGTH]: { |
418d092a C |
85 | attributes: { |
86 | include: [ | |
1735c825 C |
87 | [ |
88 | literal('(SELECT COUNT("id") FROM "videoPlaylistElement" WHERE "videoPlaylistId" = "VideoPlaylistModel"."id")'), | |
418d092a C |
89 | 'videosLength' |
90 | ] | |
91 | ] | |
92 | } | |
3acc5084 | 93 | } as FindOptions, |
a1587156 | 94 | [ScopeNames.WITH_ACCOUNT]: { |
df0b219d C |
95 | include: [ |
96 | { | |
3acc5084 | 97 | model: AccountModel, |
df0b219d C |
98 | required: true |
99 | } | |
100 | ] | |
101 | }, | |
a1587156 | 102 | [ScopeNames.WITH_ACCOUNT_AND_CHANNEL_SUMMARY]: { |
418d092a C |
103 | include: [ |
104 | { | |
3acc5084 | 105 | model: AccountModel.scope(AccountScopeNames.SUMMARY), |
418d092a C |
106 | required: true |
107 | }, | |
108 | { | |
3acc5084 | 109 | model: VideoChannelModel.scope(VideoChannelScopeNames.SUMMARY), |
418d092a C |
110 | required: false |
111 | } | |
112 | ] | |
113 | }, | |
a1587156 | 114 | [ScopeNames.WITH_ACCOUNT_AND_CHANNEL]: { |
09979f89 C |
115 | include: [ |
116 | { | |
3acc5084 | 117 | model: AccountModel, |
09979f89 C |
118 | required: true |
119 | }, | |
120 | { | |
3acc5084 | 121 | model: VideoChannelModel, |
09979f89 C |
122 | required: false |
123 | } | |
124 | ] | |
125 | }, | |
a1587156 | 126 | [ScopeNames.AVAILABLE_FOR_LIST]: (options: AvailableForListOptions) => { |
6b0c3c7c C |
127 | |
128 | let whereActor: WhereOptions = {} | |
418d092a | 129 | |
3acc5084 | 130 | const whereAnd: WhereOptions[] = [] |
418d092a | 131 | |
6b0c3c7c | 132 | if (options.listMyPlaylists !== true) { |
418d092a C |
133 | whereAnd.push({ |
134 | privacy: VideoPlaylistPrivacy.PUBLIC | |
135 | }) | |
6b0c3c7c C |
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 = { | |
a1587156 | 141 | [Op.or]: [ |
6b0c3c7c C |
142 | { |
143 | serverId: null | |
144 | }, | |
145 | { | |
146 | serverId: { | |
a1587156 | 147 | [Op.in]: literal(inQueryInstanceFollow) |
6b0c3c7c C |
148 | } |
149 | } | |
150 | ] | |
151 | } | |
418d092a C |
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 | ||
df0b219d C |
166 | if (options.type) { |
167 | whereAnd.push({ | |
168 | type: options.type | |
169 | }) | |
170 | } | |
171 | ||
c06af501 | 172 | if (options.search) { |
c06af501 | 173 | whereAnd.push({ |
822c7e61 | 174 | name: { |
a1587156 | 175 | [Op.iLike]: '%' + options.search + '%' |
c06af501 RK |
176 | } |
177 | }) | |
178 | } | |
179 | ||
418d092a | 180 | const where = { |
1735c825 | 181 | [Op.and]: whereAnd |
418d092a C |
182 | } |
183 | ||
184 | const accountScope = { | |
bfbd9128 | 185 | method: [ AccountScopeNames.SUMMARY, { whereActor } as SummaryOptions ] |
418d092a C |
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 | ] | |
3acc5084 | 200 | } as FindOptions |
418d092a | 201 | } |
3acc5084 | 202 | })) |
418d092a C |
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) | |
1735c825 | 232 | @Is('VideoPlaylistDescription', value => throwIfNotValid(value, isVideoPlaylistDescriptionValid, 'description', true)) |
418d092a C |
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 | ||
df0b219d C |
252 | @AllowNull(false) |
253 | @Default(VideoPlaylistType.REGULAR) | |
254 | @Column | |
255 | type: VideoPlaylistType | |
256 | ||
418d092a C |
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: { | |
07b1a18a | 275 | allowNull: true |
418d092a C |
276 | }, |
277 | onDelete: 'CASCADE' | |
278 | }) | |
279 | VideoChannel: VideoChannelModel | |
280 | ||
281 | @HasMany(() => VideoPlaylistElementModel, { | |
282 | foreignKey: { | |
283 | name: 'videoPlaylistId', | |
284 | allowNull: false | |
285 | }, | |
df0b219d | 286 | onDelete: 'CASCADE' |
418d092a C |
287 | }) |
288 | VideoPlaylistElements: VideoPlaylistElementModel[] | |
289 | ||
e8bafea3 C |
290 | @HasOne(() => ThumbnailModel, { |
291 | foreignKey: { | |
292 | name: 'videoPlaylistId', | |
293 | allowNull: true | |
294 | }, | |
295 | onDelete: 'CASCADE', | |
296 | hooks: true | |
297 | }) | |
298 | Thumbnail: ThumbnailModel | |
418d092a C |
299 | |
300 | static listForApi (options: { | |
301 | followerActorId: number | |
a1587156 C |
302 | start: number |
303 | count: number | |
304 | sort: string | |
305 | type?: VideoPlaylistType | |
306 | accountId?: number | |
307 | videoChannelId?: number | |
308 | listMyPlaylists?: boolean | |
c06af501 | 309 | search?: string |
418d092a C |
310 | }) { |
311 | const query = { | |
312 | offset: options.start, | |
313 | limit: options.count, | |
314 | order: getSort(options.sort) | |
315 | } | |
316 | ||
3acc5084 | 317 | const scopes: (string | ScopeOptions)[] = [ |
418d092a C |
318 | { |
319 | method: [ | |
320 | ScopeNames.AVAILABLE_FOR_LIST, | |
321 | { | |
df0b219d | 322 | type: options.type, |
418d092a C |
323 | followerActorId: options.followerActorId, |
324 | accountId: options.accountId, | |
325 | videoChannelId: options.videoChannelId, | |
6b0c3c7c | 326 | listMyPlaylists: options.listMyPlaylists, |
c06af501 | 327 | search: options.search |
418d092a C |
328 | } as AvailableForListOptions |
329 | ] | |
3acc5084 | 330 | }, |
e8bafea3 C |
331 | ScopeNames.WITH_VIDEOS_LENGTH, |
332 | ScopeNames.WITH_THUMBNAIL | |
418d092a C |
333 | ] |
334 | ||
335 | return VideoPlaylistModel | |
336 | .scope(scopes) | |
337 | .findAndCountAll(query) | |
338 | .then(({ rows, count }) => { | |
339 | return { total: count, data: rows } | |
340 | }) | |
341 | } | |
342 | ||
0b16f5f2 | 343 | static listPublicUrlsOfForAP (accountId: number, start: number, count: number) { |
418d092a C |
344 | const query = { |
345 | attributes: [ 'url' ], | |
346 | offset: start, | |
347 | limit: count, | |
348 | where: { | |
0b16f5f2 C |
349 | ownerAccountId: accountId, |
350 | privacy: VideoPlaylistPrivacy.PUBLIC | |
418d092a C |
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 | ||
453e83ea | 360 | static listPlaylistIdsOf (accountId: number, videoIds: number[]): Bluebird<MVideoPlaylistIdWithElements[]> { |
f0a39880 C |
361 | const query = { |
362 | attributes: [ 'id' ], | |
363 | where: { | |
364 | ownerAccountId: accountId | |
365 | }, | |
366 | include: [ | |
367 | { | |
bfbd9128 | 368 | attributes: [ 'id', 'videoId', 'startTimestamp', 'stopTimestamp' ], |
f0a39880 C |
369 | model: VideoPlaylistElementModel.unscoped(), |
370 | where: { | |
371 | videoId: { | |
0374b6b5 | 372 | [Op.in]: videoIds |
f0a39880 C |
373 | } |
374 | }, | |
375 | required: true | |
376 | } | |
377 | ] | |
378 | } | |
379 | ||
380 | return VideoPlaylistModel.findAll(query) | |
381 | } | |
382 | ||
418d092a C |
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 | ||
453e83ea | 396 | static loadWithAccountAndChannelSummary (id: number | string, transaction: Transaction): Bluebird<MVideoPlaylistFullSummary> { |
418d092a C |
397 | const where = buildWhereIdOrUUID(id) |
398 | ||
399 | const query = { | |
400 | where, | |
401 | transaction | |
402 | } | |
403 | ||
404 | return VideoPlaylistModel | |
e8bafea3 | 405 | .scope([ ScopeNames.WITH_ACCOUNT_AND_CHANNEL_SUMMARY, ScopeNames.WITH_VIDEOS_LENGTH, ScopeNames.WITH_THUMBNAIL ]) |
09979f89 C |
406 | .findOne(query) |
407 | } | |
408 | ||
453e83ea | 409 | static loadWithAccountAndChannel (id: number | string, transaction: Transaction): Bluebird<MVideoPlaylistFull> { |
09979f89 C |
410 | const where = buildWhereIdOrUUID(id) |
411 | ||
412 | const query = { | |
413 | where, | |
414 | transaction | |
415 | } | |
416 | ||
417 | return VideoPlaylistModel | |
e8bafea3 | 418 | .scope([ ScopeNames.WITH_ACCOUNT_AND_CHANNEL, ScopeNames.WITH_VIDEOS_LENGTH, ScopeNames.WITH_THUMBNAIL ]) |
418d092a C |
419 | .findOne(query) |
420 | } | |
421 | ||
453e83ea | 422 | static loadByUrlAndPopulateAccount (url: string): Bluebird<MVideoPlaylistAccountThumbnail> { |
df0b219d C |
423 | const query = { |
424 | where: { | |
425 | url | |
426 | } | |
427 | } | |
428 | ||
e8bafea3 | 429 | return VideoPlaylistModel.scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_THUMBNAIL ]).findOne(query) |
df0b219d C |
430 | } |
431 | ||
418d092a C |
432 | static getPrivacyLabel (privacy: VideoPlaylistPrivacy) { |
433 | return VIDEO_PLAYLIST_PRIVACIES[privacy] || 'Unknown' | |
434 | } | |
435 | ||
df0b219d C |
436 | static getTypeLabel (type: VideoPlaylistType) { |
437 | return VIDEO_PLAYLIST_TYPES[type] || 'Unknown' | |
438 | } | |
439 | ||
1735c825 | 440 | static resetPlaylistsOfChannel (videoChannelId: number, transaction: Transaction) { |
df0b219d C |
441 | const query = { |
442 | where: { | |
443 | videoChannelId | |
444 | }, | |
445 | transaction | |
446 | } | |
447 | ||
448 | return VideoPlaylistModel.update({ privacy: VideoPlaylistPrivacy.PRIVATE, videoChannelId: null }, query) | |
449 | } | |
450 | ||
453e83ea | 451 | async setAndSaveThumbnail (thumbnail: MThumbnail, t: Transaction) { |
3acc5084 | 452 | thumbnail.videoPlaylistId = this.id |
e8bafea3 | 453 | |
3acc5084 | 454 | this.Thumbnail = await thumbnail.save({ transaction: t }) |
e8bafea3 C |
455 | } |
456 | ||
457 | hasThumbnail () { | |
458 | return !!this.Thumbnail | |
459 | } | |
460 | ||
65af03a2 C |
461 | hasGeneratedThumbnail () { |
462 | return this.hasThumbnail() && this.Thumbnail.automaticallyGenerated === true | |
463 | } | |
464 | ||
e8bafea3 | 465 | generateThumbnailName () { |
418d092a C |
466 | const extension = '.jpg' |
467 | ||
468 | return 'playlist-' + this.uuid + extension | |
469 | } | |
470 | ||
471 | getThumbnailUrl () { | |
e8bafea3 C |
472 | if (!this.hasThumbnail()) return null |
473 | ||
3acc5084 | 474 | return WEBSERVER.URL + STATIC_PATHS.THUMBNAILS + this.Thumbnail.filename |
418d092a C |
475 | } |
476 | ||
477 | getThumbnailStaticPath () { | |
e8bafea3 | 478 | if (!this.hasThumbnail()) return null |
418d092a | 479 | |
3acc5084 | 480 | return join(STATIC_PATHS.THUMBNAILS, this.Thumbnail.filename) |
418d092a C |
481 | } |
482 | ||
9f79ade6 C |
483 | setAsRefreshed () { |
484 | this.changed('updatedAt', true) | |
485 | ||
486 | return this.save() | |
487 | } | |
488 | ||
418d092a C |
489 | isOwned () { |
490 | return this.OwnerAccount.isOwned() | |
491 | } | |
492 | ||
9f79ade6 C |
493 | isOutdated () { |
494 | if (this.isOwned()) return false | |
495 | ||
496 | return isOutdated(this, ACTIVITY_PUB.VIDEO_PLAYLIST_REFRESH_INTERVAL) | |
497 | } | |
498 | ||
1ca9f7c3 | 499 | toFormattedJSON (this: MVideoPlaylistFormattable): VideoPlaylist { |
418d092a C |
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 | ||
df0b219d C |
514 | type: { |
515 | id: this.type, | |
516 | label: VideoPlaylistModel.getTypeLabel(this.type) | |
517 | }, | |
518 | ||
1735c825 | 519 | videosLength: this.get('videosLength') as number, |
418d092a C |
520 | |
521 | createdAt: this.createdAt, | |
522 | updatedAt: this.updatedAt, | |
523 | ||
524 | ownerAccount: this.OwnerAccount.toFormattedSummaryJSON(), | |
07b1a18a | 525 | videoChannel: this.VideoChannel ? this.VideoChannel.toFormattedSummaryJSON() : null |
418d092a C |
526 | } |
527 | } | |
528 | ||
b5fecbf4 | 529 | toActivityPubObject (this: MVideoPlaylistAP, page: number, t: Transaction): Promise<PlaylistObject> { |
418d092a | 530 | const handler = (start: number, count: number) => { |
df0b219d | 531 | return VideoPlaylistElementModel.listUrlsOfForAP(this.id, start, count, t) |
418d092a C |
532 | } |
533 | ||
e8bafea3 C |
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 | ||
df0b219d | 545 | return activityPubCollectionPagination(this.url, handler, page) |
418d092a C |
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, | |
df0b219d C |
552 | published: this.createdAt.toISOString(), |
553 | updated: this.updatedAt.toISOString(), | |
418d092a | 554 | attributedTo: this.VideoChannel ? [ this.VideoChannel.Actor.url ] : [], |
e8bafea3 | 555 | icon |
418d092a C |
556 | }) |
557 | }) | |
558 | } | |
559 | } |