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