]>
Commit | Line | Data |
---|---|---|
2650d6d4 | 1 | import { join } from 'path' |
37a44fc9 | 2 | import { FindOptions, literal, Op, ScopeOptions, Sequelize, Transaction, WhereOptions } from 'sequelize' |
418d092a C |
3 | import { |
4 | AllowNull, | |
418d092a C |
5 | BelongsTo, |
6 | Column, | |
7 | CreatedAt, | |
8 | DataType, | |
9 | Default, | |
10 | ForeignKey, | |
11 | HasMany, | |
e8bafea3 | 12 | HasOne, |
418d092a C |
13 | Is, |
14 | IsUUID, | |
15 | Model, | |
16 | Scopes, | |
17 | Table, | |
822c7e61 | 18 | UpdatedAt |
418d092a | 19 | } from 'sequelize-typescript' |
d4a8e7a6 | 20 | import { buildUUID, uuidToShort } from '@server/helpers/uuid' |
2650d6d4 | 21 | import { MAccountId, MChannelId } from '@server/types/models' |
29837f88 | 22 | import { AttributesOnly, buildPlaylistEmbedPath, buildPlaylistWatchPath } from '@shared/core-utils' |
2650d6d4 C |
23 | import { ActivityIconObject } from '../../../shared/models/activitypub/objects' |
24 | import { PlaylistObject } from '../../../shared/models/activitypub/objects/playlist-object' | |
418d092a | 25 | import { VideoPlaylistPrivacy } from '../../../shared/models/videos/playlist/video-playlist-privacy.model' |
2650d6d4 C |
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' | |
418d092a C |
30 | import { |
31 | isVideoPlaylistDescriptionValid, | |
32 | isVideoPlaylistNameValid, | |
33 | isVideoPlaylistPrivacyValid | |
34 | } from '../../helpers/custom-validators/video-playlists' | |
df0b219d | 35 | import { |
9f79ade6 | 36 | ACTIVITY_PUB, |
df0b219d C |
37 | CONSTRAINTS_FIELDS, |
38 | STATIC_PATHS, | |
39 | THUMBNAILS_SIZE, | |
40 | VIDEO_PLAYLIST_PRIVACIES, | |
6dd9de95 C |
41 | VIDEO_PLAYLIST_TYPES, |
42 | WEBSERVER | |
74dc3bca | 43 | } from '../../initializers/constants' |
2650d6d4 | 44 | import { MThumbnail } from '../../types/models/video/thumbnail' |
453e83ea | 45 | import { |
822c7e61 C |
46 | MVideoPlaylistAccountThumbnail, |
47 | MVideoPlaylistAP, | |
1ca9f7c3 | 48 | MVideoPlaylistFormattable, |
453e83ea C |
49 | MVideoPlaylistFull, |
50 | MVideoPlaylistFullSummary, | |
51 | MVideoPlaylistIdWithElements | |
26d6bf65 | 52 | } from '../../types/models/video/video-playlist' |
2650d6d4 | 53 | import { AccountModel, ScopeNames as AccountScopeNames, SummaryOptions } from '../account/account' |
7d9ba5c0 | 54 | import { ActorModel } from '../actor/actor' |
fa47956e | 55 | import { setAsUpdated } from '../shared' |
37a44fc9 C |
56 | import { |
57 | buildServerIdsFollowedBy, | |
58 | buildTrigramSearchIndex, | |
59 | buildWhereIdOrUUID, | |
60 | createSimilarityAttribute, | |
61 | getPlaylistSort, | |
62 | isOutdated, | |
63 | throwIfNotValid | |
64 | } from '../utils' | |
2650d6d4 C |
65 | import { ThumbnailModel } from './thumbnail' |
66 | import { ScopeNames as VideoChannelScopeNames, VideoChannelModel } from './video-channel' | |
67 | import { VideoPlaylistElementModel } from './video-playlist-element' | |
418d092a C |
68 | |
69 | enum ScopeNames { | |
70 | AVAILABLE_FOR_LIST = 'AVAILABLE_FOR_LIST', | |
71 | WITH_VIDEOS_LENGTH = 'WITH_VIDEOS_LENGTH', | |
df0b219d | 72 | WITH_ACCOUNT_AND_CHANNEL_SUMMARY = 'WITH_ACCOUNT_AND_CHANNEL_SUMMARY', |
09979f89 | 73 | WITH_ACCOUNT = 'WITH_ACCOUNT', |
e8bafea3 | 74 | WITH_THUMBNAIL = 'WITH_THUMBNAIL', |
09979f89 | 75 | WITH_ACCOUNT_AND_CHANNEL = 'WITH_ACCOUNT_AND_CHANNEL' |
418d092a C |
76 | } |
77 | ||
78 | type AvailableForListOptions = { | |
fe19f600 | 79 | followerActorId?: number |
df0b219d C |
80 | type?: VideoPlaylistType |
81 | accountId?: number | |
418d092a | 82 | videoChannelId?: number |
a1587156 | 83 | listMyPlaylists?: boolean |
c06af501 | 84 | search?: string |
fa47956e | 85 | host?: string |
37a44fc9 C |
86 | withVideos?: boolean |
87 | } | |
88 | ||
89 | function getVideoLengthSelect () { | |
90 | return 'SELECT COUNT("id") FROM "videoPlaylistElement" WHERE "videoPlaylistId" = "VideoPlaylistModel"."id"' | |
418d092a C |
91 | } |
92 | ||
3acc5084 | 93 | @Scopes(() => ({ |
a1587156 | 94 | [ScopeNames.WITH_THUMBNAIL]: { |
e8bafea3 C |
95 | include: [ |
96 | { | |
3acc5084 | 97 | model: ThumbnailModel, |
e8bafea3 C |
98 | required: false |
99 | } | |
100 | ] | |
101 | }, | |
a1587156 | 102 | [ScopeNames.WITH_VIDEOS_LENGTH]: { |
418d092a C |
103 | attributes: { |
104 | include: [ | |
1735c825 | 105 | [ |
37a44fc9 | 106 | literal(`(${getVideoLengthSelect()})`), |
418d092a C |
107 | 'videosLength' |
108 | ] | |
109 | ] | |
110 | } | |
3acc5084 | 111 | } as FindOptions, |
a1587156 | 112 | [ScopeNames.WITH_ACCOUNT]: { |
df0b219d C |
113 | include: [ |
114 | { | |
3acc5084 | 115 | model: AccountModel, |
df0b219d C |
116 | required: true |
117 | } | |
118 | ] | |
119 | }, | |
a1587156 | 120 | [ScopeNames.WITH_ACCOUNT_AND_CHANNEL_SUMMARY]: { |
418d092a C |
121 | include: [ |
122 | { | |
3acc5084 | 123 | model: AccountModel.scope(AccountScopeNames.SUMMARY), |
418d092a C |
124 | required: true |
125 | }, | |
126 | { | |
3acc5084 | 127 | model: VideoChannelModel.scope(VideoChannelScopeNames.SUMMARY), |
418d092a C |
128 | required: false |
129 | } | |
130 | ] | |
131 | }, | |
a1587156 | 132 | [ScopeNames.WITH_ACCOUNT_AND_CHANNEL]: { |
09979f89 C |
133 | include: [ |
134 | { | |
3acc5084 | 135 | model: AccountModel, |
09979f89 C |
136 | required: true |
137 | }, | |
138 | { | |
3acc5084 | 139 | model: VideoChannelModel, |
09979f89 C |
140 | required: false |
141 | } | |
142 | ] | |
143 | }, | |
a1587156 | 144 | [ScopeNames.AVAILABLE_FOR_LIST]: (options: AvailableForListOptions) => { |
fa47956e C |
145 | const whereAnd: WhereOptions[] = [] |
146 | ||
147 | const whereServer = options.host && options.host !== WEBSERVER.HOST | |
148 | ? { host: options.host } | |
149 | : undefined | |
150 | ||
6b0c3c7c | 151 | let whereActor: WhereOptions = {} |
418d092a | 152 | |
fa47956e C |
153 | if (options.host === WEBSERVER.HOST) { |
154 | whereActor = { | |
155 | [Op.and]: [ { serverId: null } ] | |
156 | } | |
157 | } | |
418d092a | 158 | |
6b0c3c7c | 159 | if (options.listMyPlaylists !== true) { |
418d092a C |
160 | whereAnd.push({ |
161 | privacy: VideoPlaylistPrivacy.PUBLIC | |
162 | }) | |
6b0c3c7c | 163 | |
fe19f600 RK |
164 | // Only list local playlists |
165 | const whereActorOr: WhereOptions[] = [ | |
166 | { | |
167 | serverId: null | |
168 | } | |
169 | ] | |
6b0c3c7c | 170 | |
fe19f600 RK |
171 | // … OR playlists that are on an instance followed by actorId |
172 | if (options.followerActorId) { | |
173 | const inQueryInstanceFollow = buildServerIdsFollowedBy(options.followerActorId) | |
174 | ||
175 | whereActorOr.push({ | |
176 | serverId: { | |
177 | [Op.in]: literal(inQueryInstanceFollow) | |
6b0c3c7c | 178 | } |
fe19f600 RK |
179 | }) |
180 | } | |
181 | ||
fa47956e | 182 | Object.assign(whereActor, { [Op.or]: whereActorOr }) |
418d092a C |
183 | } |
184 | ||
185 | if (options.accountId) { | |
186 | whereAnd.push({ | |
187 | ownerAccountId: options.accountId | |
188 | }) | |
189 | } | |
190 | ||
191 | if (options.videoChannelId) { | |
192 | whereAnd.push({ | |
193 | videoChannelId: options.videoChannelId | |
194 | }) | |
195 | } | |
196 | ||
df0b219d C |
197 | if (options.type) { |
198 | whereAnd.push({ | |
199 | type: options.type | |
200 | }) | |
201 | } | |
202 | ||
37a44fc9 C |
203 | if (options.withVideos === true) { |
204 | whereAnd.push( | |
205 | literal(`(${getVideoLengthSelect()}) != 0`) | |
206 | ) | |
207 | } | |
208 | ||
209 | const attributesInclude = [] | |
210 | ||
c06af501 | 211 | if (options.search) { |
37a44fc9 C |
212 | const escapedSearch = VideoPlaylistModel.sequelize.escape(options.search) |
213 | const escapedLikeSearch = VideoPlaylistModel.sequelize.escape('%' + options.search + '%') | |
214 | attributesInclude.push(createSimilarityAttribute('VideoPlaylistModel.name', options.search)) | |
215 | ||
c06af501 | 216 | whereAnd.push({ |
37a44fc9 C |
217 | [Op.or]: [ |
218 | Sequelize.literal( | |
219 | 'lower(immutable_unaccent("VideoPlaylistModel"."name")) % lower(immutable_unaccent(' + escapedSearch + '))' | |
220 | ), | |
221 | Sequelize.literal( | |
222 | 'lower(immutable_unaccent("VideoPlaylistModel"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))' | |
223 | ) | |
224 | ] | |
c06af501 RK |
225 | }) |
226 | } | |
227 | ||
418d092a | 228 | const where = { |
1735c825 | 229 | [Op.and]: whereAnd |
418d092a C |
230 | } |
231 | ||
418d092a | 232 | return { |
37a44fc9 C |
233 | attributes: { |
234 | include: attributesInclude | |
235 | }, | |
418d092a C |
236 | where, |
237 | include: [ | |
238 | { | |
b49f22d8 | 239 | model: AccountModel.scope({ |
fa47956e | 240 | method: [ AccountScopeNames.SUMMARY, { whereActor, whereServer } as SummaryOptions ] |
b49f22d8 | 241 | }), |
418d092a C |
242 | required: true |
243 | }, | |
244 | { | |
245 | model: VideoChannelModel.scope(VideoChannelScopeNames.SUMMARY), | |
246 | required: false | |
247 | } | |
248 | ] | |
3acc5084 | 249 | } as FindOptions |
418d092a | 250 | } |
3acc5084 | 251 | })) |
418d092a C |
252 | |
253 | @Table({ | |
254 | tableName: 'videoPlaylist', | |
255 | indexes: [ | |
37a44fc9 C |
256 | buildTrigramSearchIndex('video_playlist_name_trigram', 'name'), |
257 | ||
418d092a C |
258 | { |
259 | fields: [ 'ownerAccountId' ] | |
260 | }, | |
261 | { | |
262 | fields: [ 'videoChannelId' ] | |
263 | }, | |
264 | { | |
265 | fields: [ 'url' ], | |
266 | unique: true | |
267 | } | |
268 | ] | |
269 | }) | |
16c016e8 | 270 | export class VideoPlaylistModel extends Model<Partial<AttributesOnly<VideoPlaylistModel>>> { |
418d092a C |
271 | @CreatedAt |
272 | createdAt: Date | |
273 | ||
274 | @UpdatedAt | |
275 | updatedAt: Date | |
276 | ||
277 | @AllowNull(false) | |
278 | @Is('VideoPlaylistName', value => throwIfNotValid(value, isVideoPlaylistNameValid, 'name')) | |
279 | @Column | |
280 | name: string | |
281 | ||
282 | @AllowNull(true) | |
1735c825 | 283 | @Is('VideoPlaylistDescription', value => throwIfNotValid(value, isVideoPlaylistDescriptionValid, 'description', true)) |
1c320673 | 284 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_PLAYLISTS.DESCRIPTION.max)) |
418d092a C |
285 | description: string |
286 | ||
287 | @AllowNull(false) | |
288 | @Is('VideoPlaylistPrivacy', value => throwIfNotValid(value, isVideoPlaylistPrivacyValid, 'privacy')) | |
289 | @Column | |
290 | privacy: VideoPlaylistPrivacy | |
291 | ||
292 | @AllowNull(false) | |
293 | @Is('VideoPlaylistUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url')) | |
294 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_PLAYLISTS.URL.max)) | |
295 | url: string | |
296 | ||
297 | @AllowNull(false) | |
298 | @Default(DataType.UUIDV4) | |
299 | @IsUUID(4) | |
300 | @Column(DataType.UUID) | |
301 | uuid: string | |
302 | ||
df0b219d C |
303 | @AllowNull(false) |
304 | @Default(VideoPlaylistType.REGULAR) | |
305 | @Column | |
306 | type: VideoPlaylistType | |
307 | ||
418d092a C |
308 | @ForeignKey(() => AccountModel) |
309 | @Column | |
310 | ownerAccountId: number | |
311 | ||
312 | @BelongsTo(() => AccountModel, { | |
313 | foreignKey: { | |
314 | allowNull: false | |
315 | }, | |
316 | onDelete: 'CASCADE' | |
317 | }) | |
318 | OwnerAccount: AccountModel | |
319 | ||
320 | @ForeignKey(() => VideoChannelModel) | |
321 | @Column | |
322 | videoChannelId: number | |
323 | ||
324 | @BelongsTo(() => VideoChannelModel, { | |
325 | foreignKey: { | |
07b1a18a | 326 | allowNull: true |
418d092a C |
327 | }, |
328 | onDelete: 'CASCADE' | |
329 | }) | |
330 | VideoChannel: VideoChannelModel | |
331 | ||
332 | @HasMany(() => VideoPlaylistElementModel, { | |
333 | foreignKey: { | |
334 | name: 'videoPlaylistId', | |
335 | allowNull: false | |
336 | }, | |
df0b219d | 337 | onDelete: 'CASCADE' |
418d092a C |
338 | }) |
339 | VideoPlaylistElements: VideoPlaylistElementModel[] | |
340 | ||
e8bafea3 C |
341 | @HasOne(() => ThumbnailModel, { |
342 | foreignKey: { | |
343 | name: 'videoPlaylistId', | |
344 | allowNull: true | |
345 | }, | |
346 | onDelete: 'CASCADE', | |
347 | hooks: true | |
348 | }) | |
349 | Thumbnail: ThumbnailModel | |
418d092a C |
350 | |
351 | static listForApi (options: { | |
352 | followerActorId: number | |
a1587156 C |
353 | start: number |
354 | count: number | |
355 | sort: string | |
356 | type?: VideoPlaylistType | |
357 | accountId?: number | |
358 | videoChannelId?: number | |
359 | listMyPlaylists?: boolean | |
c06af501 | 360 | search?: string |
fa47956e | 361 | host?: string |
37a44fc9 | 362 | withVideos?: boolean // false by default |
418d092a C |
363 | }) { |
364 | const query = { | |
365 | offset: options.start, | |
366 | limit: options.count, | |
49cff3a4 | 367 | order: getPlaylistSort(options.sort) |
418d092a C |
368 | } |
369 | ||
3acc5084 | 370 | const scopes: (string | ScopeOptions)[] = [ |
418d092a C |
371 | { |
372 | method: [ | |
373 | ScopeNames.AVAILABLE_FOR_LIST, | |
374 | { | |
df0b219d | 375 | type: options.type, |
418d092a C |
376 | followerActorId: options.followerActorId, |
377 | accountId: options.accountId, | |
378 | videoChannelId: options.videoChannelId, | |
6b0c3c7c | 379 | listMyPlaylists: options.listMyPlaylists, |
37a44fc9 | 380 | search: options.search, |
fa47956e | 381 | host: options.host, |
37a44fc9 | 382 | withVideos: options.withVideos || false |
418d092a C |
383 | } as AvailableForListOptions |
384 | ] | |
3acc5084 | 385 | }, |
e8bafea3 C |
386 | ScopeNames.WITH_VIDEOS_LENGTH, |
387 | ScopeNames.WITH_THUMBNAIL | |
418d092a C |
388 | ] |
389 | ||
390 | return VideoPlaylistModel | |
391 | .scope(scopes) | |
392 | .findAndCountAll(query) | |
393 | .then(({ rows, count }) => { | |
394 | return { total: count, data: rows } | |
395 | }) | |
396 | } | |
397 | ||
37a44fc9 C |
398 | static searchForApi (options: { |
399 | followerActorId: number | |
400 | start: number | |
401 | count: number | |
402 | sort: string | |
403 | search?: string | |
fa47956e | 404 | host?: string |
37a44fc9 C |
405 | }) { |
406 | return VideoPlaylistModel.listForApi({ | |
407 | ...options, | |
408 | type: VideoPlaylistType.REGULAR, | |
409 | listMyPlaylists: false, | |
410 | withVideos: true | |
411 | }) | |
412 | } | |
413 | ||
7405b6ba C |
414 | static listPublicUrlsOfForAP (options: { account?: MAccountId, channel?: MChannelId }, start: number, count: number) { |
415 | const where = { | |
416 | privacy: VideoPlaylistPrivacy.PUBLIC | |
417 | } | |
418 | ||
419 | if (options.account) { | |
420 | Object.assign(where, { ownerAccountId: options.account.id }) | |
421 | } | |
422 | ||
423 | if (options.channel) { | |
424 | Object.assign(where, { videoChannelId: options.channel.id }) | |
425 | } | |
426 | ||
418d092a C |
427 | const query = { |
428 | attributes: [ 'url' ], | |
429 | offset: start, | |
430 | limit: count, | |
7405b6ba | 431 | where |
418d092a C |
432 | } |
433 | ||
434 | return VideoPlaylistModel.findAndCountAll(query) | |
435 | .then(({ rows, count }) => { | |
436 | return { total: count, data: rows.map(p => p.url) } | |
437 | }) | |
438 | } | |
439 | ||
b49f22d8 | 440 | static listPlaylistIdsOf (accountId: number, videoIds: number[]): Promise<MVideoPlaylistIdWithElements[]> { |
f0a39880 C |
441 | const query = { |
442 | attributes: [ 'id' ], | |
443 | where: { | |
444 | ownerAccountId: accountId | |
445 | }, | |
446 | include: [ | |
447 | { | |
bfbd9128 | 448 | attributes: [ 'id', 'videoId', 'startTimestamp', 'stopTimestamp' ], |
f0a39880 C |
449 | model: VideoPlaylistElementModel.unscoped(), |
450 | where: { | |
451 | videoId: { | |
0374b6b5 | 452 | [Op.in]: videoIds |
f0a39880 C |
453 | } |
454 | }, | |
455 | required: true | |
456 | } | |
457 | ] | |
458 | } | |
459 | ||
460 | return VideoPlaylistModel.findAll(query) | |
461 | } | |
462 | ||
418d092a C |
463 | static doesPlaylistExist (url: string) { |
464 | const query = { | |
b49f22d8 | 465 | attributes: [ 'id' ], |
418d092a C |
466 | where: { |
467 | url | |
468 | } | |
469 | } | |
470 | ||
471 | return VideoPlaylistModel | |
472 | .findOne(query) | |
473 | .then(e => !!e) | |
474 | } | |
475 | ||
b49f22d8 | 476 | static loadWithAccountAndChannelSummary (id: number | string, transaction: Transaction): Promise<MVideoPlaylistFullSummary> { |
418d092a C |
477 | const where = buildWhereIdOrUUID(id) |
478 | ||
479 | const query = { | |
480 | where, | |
481 | transaction | |
482 | } | |
483 | ||
484 | return VideoPlaylistModel | |
e8bafea3 | 485 | .scope([ ScopeNames.WITH_ACCOUNT_AND_CHANNEL_SUMMARY, ScopeNames.WITH_VIDEOS_LENGTH, ScopeNames.WITH_THUMBNAIL ]) |
09979f89 C |
486 | .findOne(query) |
487 | } | |
488 | ||
b49f22d8 | 489 | static loadWithAccountAndChannel (id: number | string, transaction: Transaction): Promise<MVideoPlaylistFull> { |
09979f89 C |
490 | const where = buildWhereIdOrUUID(id) |
491 | ||
492 | const query = { | |
493 | where, | |
494 | transaction | |
495 | } | |
496 | ||
497 | return VideoPlaylistModel | |
e8bafea3 | 498 | .scope([ ScopeNames.WITH_ACCOUNT_AND_CHANNEL, ScopeNames.WITH_VIDEOS_LENGTH, ScopeNames.WITH_THUMBNAIL ]) |
418d092a C |
499 | .findOne(query) |
500 | } | |
501 | ||
b49f22d8 | 502 | static loadByUrlAndPopulateAccount (url: string): Promise<MVideoPlaylistAccountThumbnail> { |
df0b219d C |
503 | const query = { |
504 | where: { | |
505 | url | |
506 | } | |
507 | } | |
508 | ||
e8bafea3 | 509 | return VideoPlaylistModel.scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_THUMBNAIL ]).findOne(query) |
df0b219d C |
510 | } |
511 | ||
37a44fc9 C |
512 | static loadByUrlWithAccountAndChannelSummary (url: string): Promise<MVideoPlaylistFullSummary> { |
513 | const query = { | |
514 | where: { | |
515 | url | |
516 | } | |
517 | } | |
518 | ||
519 | return VideoPlaylistModel | |
520 | .scope([ ScopeNames.WITH_ACCOUNT_AND_CHANNEL_SUMMARY, ScopeNames.WITH_VIDEOS_LENGTH, ScopeNames.WITH_THUMBNAIL ]) | |
521 | .findOne(query) | |
522 | } | |
523 | ||
418d092a C |
524 | static getPrivacyLabel (privacy: VideoPlaylistPrivacy) { |
525 | return VIDEO_PLAYLIST_PRIVACIES[privacy] || 'Unknown' | |
526 | } | |
527 | ||
df0b219d C |
528 | static getTypeLabel (type: VideoPlaylistType) { |
529 | return VIDEO_PLAYLIST_TYPES[type] || 'Unknown' | |
530 | } | |
531 | ||
1735c825 | 532 | static resetPlaylistsOfChannel (videoChannelId: number, transaction: Transaction) { |
df0b219d C |
533 | const query = { |
534 | where: { | |
535 | videoChannelId | |
536 | }, | |
537 | transaction | |
538 | } | |
539 | ||
540 | return VideoPlaylistModel.update({ privacy: VideoPlaylistPrivacy.PRIVATE, videoChannelId: null }, query) | |
541 | } | |
542 | ||
453e83ea | 543 | async setAndSaveThumbnail (thumbnail: MThumbnail, t: Transaction) { |
3acc5084 | 544 | thumbnail.videoPlaylistId = this.id |
e8bafea3 | 545 | |
3acc5084 | 546 | this.Thumbnail = await thumbnail.save({ transaction: t }) |
e8bafea3 C |
547 | } |
548 | ||
549 | hasThumbnail () { | |
550 | return !!this.Thumbnail | |
551 | } | |
552 | ||
65af03a2 C |
553 | hasGeneratedThumbnail () { |
554 | return this.hasThumbnail() && this.Thumbnail.automaticallyGenerated === true | |
555 | } | |
556 | ||
e8bafea3 | 557 | generateThumbnailName () { |
418d092a C |
558 | const extension = '.jpg' |
559 | ||
d4a8e7a6 | 560 | return 'playlist-' + buildUUID() + extension |
418d092a C |
561 | } |
562 | ||
563 | getThumbnailUrl () { | |
e8bafea3 C |
564 | if (!this.hasThumbnail()) return null |
565 | ||
3acc5084 | 566 | return WEBSERVER.URL + STATIC_PATHS.THUMBNAILS + this.Thumbnail.filename |
418d092a C |
567 | } |
568 | ||
569 | getThumbnailStaticPath () { | |
e8bafea3 | 570 | if (!this.hasThumbnail()) return null |
418d092a | 571 | |
3acc5084 | 572 | return join(STATIC_PATHS.THUMBNAILS, this.Thumbnail.filename) |
418d092a C |
573 | } |
574 | ||
15a7eafb C |
575 | getWatchStaticPath () { |
576 | return buildPlaylistWatchPath({ shortUUID: uuidToShort(this.uuid) }) | |
8d987ec6 K |
577 | } |
578 | ||
6fad8e51 | 579 | getEmbedStaticPath () { |
15a7eafb | 580 | return buildPlaylistEmbedPath(this) |
6fad8e51 C |
581 | } |
582 | ||
fe19f600 RK |
583 | static async getStats () { |
584 | const totalLocalPlaylists = await VideoPlaylistModel.count({ | |
585 | include: [ | |
586 | { | |
587 | model: AccountModel, | |
588 | required: true, | |
589 | include: [ | |
590 | { | |
591 | model: ActorModel, | |
592 | required: true, | |
593 | where: { | |
594 | serverId: null | |
595 | } | |
596 | } | |
597 | ] | |
598 | } | |
599 | ], | |
600 | where: { | |
601 | privacy: VideoPlaylistPrivacy.PUBLIC | |
602 | } | |
603 | }) | |
604 | ||
605 | return { | |
606 | totalLocalPlaylists | |
607 | } | |
608 | } | |
609 | ||
9f79ade6 | 610 | setAsRefreshed () { |
49af5ac8 | 611 | return setAsUpdated('videoPlaylist', this.id) |
9f79ade6 C |
612 | } |
613 | ||
37a44fc9 C |
614 | setVideosLength (videosLength: number) { |
615 | this.set('videosLength' as any, videosLength, { raw: true }) | |
616 | } | |
617 | ||
418d092a C |
618 | isOwned () { |
619 | return this.OwnerAccount.isOwned() | |
620 | } | |
621 | ||
9f79ade6 C |
622 | isOutdated () { |
623 | if (this.isOwned()) return false | |
624 | ||
625 | return isOutdated(this, ACTIVITY_PUB.VIDEO_PLAYLIST_REFRESH_INTERVAL) | |
626 | } | |
627 | ||
1ca9f7c3 | 628 | toFormattedJSON (this: MVideoPlaylistFormattable): VideoPlaylist { |
418d092a C |
629 | return { |
630 | id: this.id, | |
631 | uuid: this.uuid, | |
d4a8e7a6 C |
632 | shortUUID: uuidToShort(this.uuid), |
633 | ||
418d092a C |
634 | isLocal: this.isOwned(), |
635 | ||
37a44fc9 C |
636 | url: this.url, |
637 | ||
418d092a C |
638 | displayName: this.name, |
639 | description: this.description, | |
640 | privacy: { | |
641 | id: this.privacy, | |
642 | label: VideoPlaylistModel.getPrivacyLabel(this.privacy) | |
643 | }, | |
644 | ||
645 | thumbnailPath: this.getThumbnailStaticPath(), | |
951b582f | 646 | embedPath: this.getEmbedStaticPath(), |
418d092a | 647 | |
df0b219d C |
648 | type: { |
649 | id: this.type, | |
650 | label: VideoPlaylistModel.getTypeLabel(this.type) | |
651 | }, | |
652 | ||
1735c825 | 653 | videosLength: this.get('videosLength') as number, |
418d092a C |
654 | |
655 | createdAt: this.createdAt, | |
656 | updatedAt: this.updatedAt, | |
657 | ||
658 | ownerAccount: this.OwnerAccount.toFormattedSummaryJSON(), | |
c1843150 C |
659 | videoChannel: this.VideoChannel |
660 | ? this.VideoChannel.toFormattedSummaryJSON() | |
661 | : null | |
418d092a C |
662 | } |
663 | } | |
664 | ||
b5fecbf4 | 665 | toActivityPubObject (this: MVideoPlaylistAP, page: number, t: Transaction): Promise<PlaylistObject> { |
418d092a | 666 | const handler = (start: number, count: number) => { |
df0b219d | 667 | return VideoPlaylistElementModel.listUrlsOfForAP(this.id, start, count, t) |
418d092a C |
668 | } |
669 | ||
e8bafea3 C |
670 | let icon: ActivityIconObject |
671 | if (this.hasThumbnail()) { | |
672 | icon = { | |
673 | type: 'Image' as 'Image', | |
674 | url: this.getThumbnailUrl(), | |
675 | mediaType: 'image/jpeg' as 'image/jpeg', | |
676 | width: THUMBNAILS_SIZE.width, | |
677 | height: THUMBNAILS_SIZE.height | |
678 | } | |
679 | } | |
680 | ||
df0b219d | 681 | return activityPubCollectionPagination(this.url, handler, page) |
418d092a C |
682 | .then(o => { |
683 | return Object.assign(o, { | |
684 | type: 'Playlist' as 'Playlist', | |
685 | name: this.name, | |
686 | content: this.description, | |
687 | uuid: this.uuid, | |
df0b219d C |
688 | published: this.createdAt.toISOString(), |
689 | updated: this.updatedAt.toISOString(), | |
418d092a | 690 | attributedTo: this.VideoChannel ? [ this.VideoChannel.Actor.url ] : [], |
e8bafea3 | 691 | icon |
418d092a C |
692 | }) |
693 | }) | |
694 | } | |
695 | } |