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