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