]>
Commit | Line | Data |
---|---|---|
418d092a C |
1 | import { |
2 | AllowNull, | |
418d092a C |
3 | BelongsTo, |
4 | Column, | |
5 | CreatedAt, | |
6 | DataType, | |
7 | Default, | |
8 | ForeignKey, | |
9 | HasMany, | |
e8bafea3 | 10 | HasOne, |
418d092a C |
11 | Is, |
12 | IsUUID, | |
13 | Model, | |
14 | Scopes, | |
15 | Table, | |
16 | UpdatedAt | |
17 | } from 'sequelize-typescript' | |
418d092a | 18 | import { VideoPlaylistPrivacy } from '../../../shared/models/videos/playlist/video-playlist-privacy.model' |
9f79ade6 | 19 | import { buildServerIdsFollowedBy, buildWhereIdOrUUID, getSort, isOutdated, throwIfNotValid } from '../utils' |
418d092a C |
20 | import { |
21 | isVideoPlaylistDescriptionValid, | |
22 | isVideoPlaylistNameValid, | |
23 | isVideoPlaylistPrivacyValid | |
24 | } from '../../helpers/custom-validators/video-playlists' | |
25 | import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' | |
df0b219d | 26 | import { |
9f79ade6 | 27 | ACTIVITY_PUB, |
df0b219d C |
28 | CONSTRAINTS_FIELDS, |
29 | STATIC_PATHS, | |
30 | THUMBNAILS_SIZE, | |
31 | VIDEO_PLAYLIST_PRIVACIES, | |
6dd9de95 C |
32 | VIDEO_PLAYLIST_TYPES, |
33 | WEBSERVER | |
74dc3bca | 34 | } from '../../initializers/constants' |
418d092a | 35 | import { VideoPlaylist } from '../../../shared/models/videos/playlist/video-playlist.model' |
bfbd9128 | 36 | import { AccountModel, ScopeNames as AccountScopeNames, SummaryOptions } from '../account/account' |
418d092a C |
37 | import { ScopeNames as VideoChannelScopeNames, VideoChannelModel } from './video-channel' |
38 | import { join } from 'path' | |
39 | import { VideoPlaylistElementModel } from './video-playlist-element' | |
40 | import { PlaylistObject } from '../../../shared/models/activitypub/objects/playlist-object' | |
41 | import { activityPubCollectionPagination } from '../../helpers/activitypub' | |
df0b219d | 42 | import { VideoPlaylistType } from '../../../shared/models/videos/playlist/video-playlist-type.model' |
e8bafea3 C |
43 | import { ThumbnailModel } from './thumbnail' |
44 | import { ActivityIconObject } from '../../../shared/models/activitypub/objects' | |
3acc5084 | 45 | import { FindOptions, literal, Op, ScopeOptions, Transaction, WhereOptions } from 'sequelize' |
453e83ea C |
46 | import * as Bluebird from 'bluebird' |
47 | import { | |
b5fecbf4 | 48 | MVideoPlaylistAccountThumbnail, MVideoPlaylistAP, |
1ca9f7c3 | 49 | MVideoPlaylistFormattable, |
453e83ea C |
50 | MVideoPlaylistFull, |
51 | MVideoPlaylistFullSummary, | |
52 | MVideoPlaylistIdWithElements | |
53 | } from '../../typings/models/video/video-playlist' | |
54 | import { MThumbnail } from '../../typings/models/video/thumbnail' | |
418d092a C |
55 | |
56 | enum ScopeNames { | |
57 | AVAILABLE_FOR_LIST = 'AVAILABLE_FOR_LIST', | |
58 | WITH_VIDEOS_LENGTH = 'WITH_VIDEOS_LENGTH', | |
df0b219d | 59 | WITH_ACCOUNT_AND_CHANNEL_SUMMARY = 'WITH_ACCOUNT_AND_CHANNEL_SUMMARY', |
09979f89 | 60 | WITH_ACCOUNT = 'WITH_ACCOUNT', |
e8bafea3 | 61 | WITH_THUMBNAIL = 'WITH_THUMBNAIL', |
09979f89 | 62 | WITH_ACCOUNT_AND_CHANNEL = 'WITH_ACCOUNT_AND_CHANNEL' |
418d092a C |
63 | } |
64 | ||
65 | type AvailableForListOptions = { | |
66 | followerActorId: number | |
df0b219d C |
67 | type?: VideoPlaylistType |
68 | accountId?: number | |
418d092a C |
69 | videoChannelId?: number |
70 | privateAndUnlisted?: boolean | |
71 | } | |
72 | ||
3acc5084 | 73 | @Scopes(() => ({ |
e8bafea3 C |
74 | [ ScopeNames.WITH_THUMBNAIL ]: { |
75 | include: [ | |
76 | { | |
3acc5084 | 77 | model: ThumbnailModel, |
e8bafea3 C |
78 | required: false |
79 | } | |
80 | ] | |
81 | }, | |
df0b219d | 82 | [ ScopeNames.WITH_VIDEOS_LENGTH ]: { |
418d092a C |
83 | attributes: { |
84 | include: [ | |
1735c825 C |
85 | [ |
86 | literal('(SELECT COUNT("id") FROM "videoPlaylistElement" WHERE "videoPlaylistId" = "VideoPlaylistModel"."id")'), | |
418d092a C |
87 | 'videosLength' |
88 | ] | |
89 | ] | |
90 | } | |
3acc5084 | 91 | } as FindOptions, |
df0b219d C |
92 | [ ScopeNames.WITH_ACCOUNT ]: { |
93 | include: [ | |
94 | { | |
3acc5084 | 95 | model: AccountModel, |
df0b219d C |
96 | required: true |
97 | } | |
98 | ] | |
99 | }, | |
100 | [ ScopeNames.WITH_ACCOUNT_AND_CHANNEL_SUMMARY ]: { | |
418d092a C |
101 | include: [ |
102 | { | |
3acc5084 | 103 | model: AccountModel.scope(AccountScopeNames.SUMMARY), |
418d092a C |
104 | required: true |
105 | }, | |
106 | { | |
3acc5084 | 107 | model: VideoChannelModel.scope(VideoChannelScopeNames.SUMMARY), |
418d092a C |
108 | required: false |
109 | } | |
110 | ] | |
111 | }, | |
09979f89 C |
112 | [ ScopeNames.WITH_ACCOUNT_AND_CHANNEL ]: { |
113 | include: [ | |
114 | { | |
3acc5084 | 115 | model: AccountModel, |
09979f89 C |
116 | required: true |
117 | }, | |
118 | { | |
3acc5084 | 119 | model: VideoChannelModel, |
09979f89 C |
120 | required: false |
121 | } | |
122 | ] | |
123 | }, | |
df0b219d | 124 | [ ScopeNames.AVAILABLE_FOR_LIST ]: (options: AvailableForListOptions) => { |
418d092a C |
125 | // Only list local playlists OR playlists that are on an instance followed by actorId |
126 | const inQueryInstanceFollow = buildServerIdsFollowedBy(options.followerActorId) | |
bfbd9128 | 127 | const whereActor = { |
1735c825 | 128 | [ Op.or ]: [ |
418d092a C |
129 | { |
130 | serverId: null | |
131 | }, | |
132 | { | |
133 | serverId: { | |
1735c825 | 134 | [ Op.in ]: literal(inQueryInstanceFollow) |
418d092a C |
135 | } |
136 | } | |
137 | ] | |
138 | } | |
139 | ||
3acc5084 | 140 | const whereAnd: WhereOptions[] = [] |
418d092a C |
141 | |
142 | if (options.privateAndUnlisted !== true) { | |
143 | whereAnd.push({ | |
144 | privacy: VideoPlaylistPrivacy.PUBLIC | |
145 | }) | |
146 | } | |
147 | ||
148 | if (options.accountId) { | |
149 | whereAnd.push({ | |
150 | ownerAccountId: options.accountId | |
151 | }) | |
152 | } | |
153 | ||
154 | if (options.videoChannelId) { | |
155 | whereAnd.push({ | |
156 | videoChannelId: options.videoChannelId | |
157 | }) | |
158 | } | |
159 | ||
df0b219d C |
160 | if (options.type) { |
161 | whereAnd.push({ | |
162 | type: options.type | |
163 | }) | |
164 | } | |
165 | ||
418d092a | 166 | const where = { |
1735c825 | 167 | [Op.and]: whereAnd |
418d092a C |
168 | } |
169 | ||
170 | const accountScope = { | |
bfbd9128 | 171 | method: [ AccountScopeNames.SUMMARY, { whereActor } as SummaryOptions ] |
418d092a C |
172 | } |
173 | ||
174 | return { | |
175 | where, | |
176 | include: [ | |
177 | { | |
178 | model: AccountModel.scope(accountScope), | |
179 | required: true | |
180 | }, | |
181 | { | |
182 | model: VideoChannelModel.scope(VideoChannelScopeNames.SUMMARY), | |
183 | required: false | |
184 | } | |
185 | ] | |
3acc5084 | 186 | } as FindOptions |
418d092a | 187 | } |
3acc5084 | 188 | })) |
418d092a C |
189 | |
190 | @Table({ | |
191 | tableName: 'videoPlaylist', | |
192 | indexes: [ | |
193 | { | |
194 | fields: [ 'ownerAccountId' ] | |
195 | }, | |
196 | { | |
197 | fields: [ 'videoChannelId' ] | |
198 | }, | |
199 | { | |
200 | fields: [ 'url' ], | |
201 | unique: true | |
202 | } | |
203 | ] | |
204 | }) | |
205 | export class VideoPlaylistModel extends Model<VideoPlaylistModel> { | |
206 | @CreatedAt | |
207 | createdAt: Date | |
208 | ||
209 | @UpdatedAt | |
210 | updatedAt: Date | |
211 | ||
212 | @AllowNull(false) | |
213 | @Is('VideoPlaylistName', value => throwIfNotValid(value, isVideoPlaylistNameValid, 'name')) | |
214 | @Column | |
215 | name: string | |
216 | ||
217 | @AllowNull(true) | |
1735c825 | 218 | @Is('VideoPlaylistDescription', value => throwIfNotValid(value, isVideoPlaylistDescriptionValid, 'description', true)) |
418d092a C |
219 | @Column |
220 | description: string | |
221 | ||
222 | @AllowNull(false) | |
223 | @Is('VideoPlaylistPrivacy', value => throwIfNotValid(value, isVideoPlaylistPrivacyValid, 'privacy')) | |
224 | @Column | |
225 | privacy: VideoPlaylistPrivacy | |
226 | ||
227 | @AllowNull(false) | |
228 | @Is('VideoPlaylistUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url')) | |
229 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_PLAYLISTS.URL.max)) | |
230 | url: string | |
231 | ||
232 | @AllowNull(false) | |
233 | @Default(DataType.UUIDV4) | |
234 | @IsUUID(4) | |
235 | @Column(DataType.UUID) | |
236 | uuid: string | |
237 | ||
df0b219d C |
238 | @AllowNull(false) |
239 | @Default(VideoPlaylistType.REGULAR) | |
240 | @Column | |
241 | type: VideoPlaylistType | |
242 | ||
418d092a C |
243 | @ForeignKey(() => AccountModel) |
244 | @Column | |
245 | ownerAccountId: number | |
246 | ||
247 | @BelongsTo(() => AccountModel, { | |
248 | foreignKey: { | |
249 | allowNull: false | |
250 | }, | |
251 | onDelete: 'CASCADE' | |
252 | }) | |
253 | OwnerAccount: AccountModel | |
254 | ||
255 | @ForeignKey(() => VideoChannelModel) | |
256 | @Column | |
257 | videoChannelId: number | |
258 | ||
259 | @BelongsTo(() => VideoChannelModel, { | |
260 | foreignKey: { | |
07b1a18a | 261 | allowNull: true |
418d092a C |
262 | }, |
263 | onDelete: 'CASCADE' | |
264 | }) | |
265 | VideoChannel: VideoChannelModel | |
266 | ||
267 | @HasMany(() => VideoPlaylistElementModel, { | |
268 | foreignKey: { | |
269 | name: 'videoPlaylistId', | |
270 | allowNull: false | |
271 | }, | |
df0b219d | 272 | onDelete: 'CASCADE' |
418d092a C |
273 | }) |
274 | VideoPlaylistElements: VideoPlaylistElementModel[] | |
275 | ||
e8bafea3 C |
276 | @HasOne(() => ThumbnailModel, { |
277 | foreignKey: { | |
278 | name: 'videoPlaylistId', | |
279 | allowNull: true | |
280 | }, | |
281 | onDelete: 'CASCADE', | |
282 | hooks: true | |
283 | }) | |
284 | Thumbnail: ThumbnailModel | |
418d092a C |
285 | |
286 | static listForApi (options: { | |
287 | followerActorId: number | |
288 | start: number, | |
289 | count: number, | |
290 | sort: string, | |
df0b219d | 291 | type?: VideoPlaylistType, |
418d092a C |
292 | accountId?: number, |
293 | videoChannelId?: number, | |
294 | privateAndUnlisted?: boolean | |
295 | }) { | |
296 | const query = { | |
297 | offset: options.start, | |
298 | limit: options.count, | |
299 | order: getSort(options.sort) | |
300 | } | |
301 | ||
3acc5084 | 302 | const scopes: (string | ScopeOptions)[] = [ |
418d092a C |
303 | { |
304 | method: [ | |
305 | ScopeNames.AVAILABLE_FOR_LIST, | |
306 | { | |
df0b219d | 307 | type: options.type, |
418d092a C |
308 | followerActorId: options.followerActorId, |
309 | accountId: options.accountId, | |
310 | videoChannelId: options.videoChannelId, | |
311 | privateAndUnlisted: options.privateAndUnlisted | |
312 | } as AvailableForListOptions | |
313 | ] | |
3acc5084 | 314 | }, |
e8bafea3 C |
315 | ScopeNames.WITH_VIDEOS_LENGTH, |
316 | ScopeNames.WITH_THUMBNAIL | |
418d092a C |
317 | ] |
318 | ||
319 | return VideoPlaylistModel | |
320 | .scope(scopes) | |
321 | .findAndCountAll(query) | |
322 | .then(({ rows, count }) => { | |
323 | return { total: count, data: rows } | |
324 | }) | |
325 | } | |
326 | ||
0b16f5f2 | 327 | static listPublicUrlsOfForAP (accountId: number, start: number, count: number) { |
418d092a C |
328 | const query = { |
329 | attributes: [ 'url' ], | |
330 | offset: start, | |
331 | limit: count, | |
332 | where: { | |
0b16f5f2 C |
333 | ownerAccountId: accountId, |
334 | privacy: VideoPlaylistPrivacy.PUBLIC | |
418d092a C |
335 | } |
336 | } | |
337 | ||
338 | return VideoPlaylistModel.findAndCountAll(query) | |
339 | .then(({ rows, count }) => { | |
340 | return { total: count, data: rows.map(p => p.url) } | |
341 | }) | |
342 | } | |
343 | ||
453e83ea | 344 | static listPlaylistIdsOf (accountId: number, videoIds: number[]): Bluebird<MVideoPlaylistIdWithElements[]> { |
f0a39880 C |
345 | const query = { |
346 | attributes: [ 'id' ], | |
347 | where: { | |
348 | ownerAccountId: accountId | |
349 | }, | |
350 | include: [ | |
351 | { | |
bfbd9128 | 352 | attributes: [ 'id', 'videoId', 'startTimestamp', 'stopTimestamp' ], |
f0a39880 C |
353 | model: VideoPlaylistElementModel.unscoped(), |
354 | where: { | |
355 | videoId: { | |
3acc5084 | 356 | [Op.in]: videoIds // FIXME: sequelize ANY seems broken |
f0a39880 C |
357 | } |
358 | }, | |
359 | required: true | |
360 | } | |
361 | ] | |
362 | } | |
363 | ||
364 | return VideoPlaylistModel.findAll(query) | |
365 | } | |
366 | ||
418d092a C |
367 | static doesPlaylistExist (url: string) { |
368 | const query = { | |
369 | attributes: [], | |
370 | where: { | |
371 | url | |
372 | } | |
373 | } | |
374 | ||
375 | return VideoPlaylistModel | |
376 | .findOne(query) | |
377 | .then(e => !!e) | |
378 | } | |
379 | ||
453e83ea | 380 | static loadWithAccountAndChannelSummary (id: number | string, transaction: Transaction): Bluebird<MVideoPlaylistFullSummary> { |
418d092a C |
381 | const where = buildWhereIdOrUUID(id) |
382 | ||
383 | const query = { | |
384 | where, | |
385 | transaction | |
386 | } | |
387 | ||
388 | return VideoPlaylistModel | |
e8bafea3 | 389 | .scope([ ScopeNames.WITH_ACCOUNT_AND_CHANNEL_SUMMARY, ScopeNames.WITH_VIDEOS_LENGTH, ScopeNames.WITH_THUMBNAIL ]) |
09979f89 C |
390 | .findOne(query) |
391 | } | |
392 | ||
453e83ea | 393 | static loadWithAccountAndChannel (id: number | string, transaction: Transaction): Bluebird<MVideoPlaylistFull> { |
09979f89 C |
394 | const where = buildWhereIdOrUUID(id) |
395 | ||
396 | const query = { | |
397 | where, | |
398 | transaction | |
399 | } | |
400 | ||
401 | return VideoPlaylistModel | |
e8bafea3 | 402 | .scope([ ScopeNames.WITH_ACCOUNT_AND_CHANNEL, ScopeNames.WITH_VIDEOS_LENGTH, ScopeNames.WITH_THUMBNAIL ]) |
418d092a C |
403 | .findOne(query) |
404 | } | |
405 | ||
453e83ea | 406 | static loadByUrlAndPopulateAccount (url: string): Bluebird<MVideoPlaylistAccountThumbnail> { |
df0b219d C |
407 | const query = { |
408 | where: { | |
409 | url | |
410 | } | |
411 | } | |
412 | ||
e8bafea3 | 413 | return VideoPlaylistModel.scope([ ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_THUMBNAIL ]).findOne(query) |
df0b219d C |
414 | } |
415 | ||
418d092a C |
416 | static getPrivacyLabel (privacy: VideoPlaylistPrivacy) { |
417 | return VIDEO_PLAYLIST_PRIVACIES[privacy] || 'Unknown' | |
418 | } | |
419 | ||
df0b219d C |
420 | static getTypeLabel (type: VideoPlaylistType) { |
421 | return VIDEO_PLAYLIST_TYPES[type] || 'Unknown' | |
422 | } | |
423 | ||
1735c825 | 424 | static resetPlaylistsOfChannel (videoChannelId: number, transaction: Transaction) { |
df0b219d C |
425 | const query = { |
426 | where: { | |
427 | videoChannelId | |
428 | }, | |
429 | transaction | |
430 | } | |
431 | ||
432 | return VideoPlaylistModel.update({ privacy: VideoPlaylistPrivacy.PRIVATE, videoChannelId: null }, query) | |
433 | } | |
434 | ||
453e83ea | 435 | async setAndSaveThumbnail (thumbnail: MThumbnail, t: Transaction) { |
3acc5084 | 436 | thumbnail.videoPlaylistId = this.id |
e8bafea3 | 437 | |
3acc5084 | 438 | this.Thumbnail = await thumbnail.save({ transaction: t }) |
e8bafea3 C |
439 | } |
440 | ||
441 | hasThumbnail () { | |
442 | return !!this.Thumbnail | |
443 | } | |
444 | ||
65af03a2 C |
445 | hasGeneratedThumbnail () { |
446 | return this.hasThumbnail() && this.Thumbnail.automaticallyGenerated === true | |
447 | } | |
448 | ||
e8bafea3 | 449 | generateThumbnailName () { |
418d092a C |
450 | const extension = '.jpg' |
451 | ||
452 | return 'playlist-' + this.uuid + extension | |
453 | } | |
454 | ||
455 | getThumbnailUrl () { | |
e8bafea3 C |
456 | if (!this.hasThumbnail()) return null |
457 | ||
3acc5084 | 458 | return WEBSERVER.URL + STATIC_PATHS.THUMBNAILS + this.Thumbnail.filename |
418d092a C |
459 | } |
460 | ||
461 | getThumbnailStaticPath () { | |
e8bafea3 | 462 | if (!this.hasThumbnail()) return null |
418d092a | 463 | |
3acc5084 | 464 | return join(STATIC_PATHS.THUMBNAILS, this.Thumbnail.filename) |
418d092a C |
465 | } |
466 | ||
9f79ade6 C |
467 | setAsRefreshed () { |
468 | this.changed('updatedAt', true) | |
469 | ||
470 | return this.save() | |
471 | } | |
472 | ||
418d092a C |
473 | isOwned () { |
474 | return this.OwnerAccount.isOwned() | |
475 | } | |
476 | ||
9f79ade6 C |
477 | isOutdated () { |
478 | if (this.isOwned()) return false | |
479 | ||
480 | return isOutdated(this, ACTIVITY_PUB.VIDEO_PLAYLIST_REFRESH_INTERVAL) | |
481 | } | |
482 | ||
1ca9f7c3 | 483 | toFormattedJSON (this: MVideoPlaylistFormattable): VideoPlaylist { |
418d092a C |
484 | return { |
485 | id: this.id, | |
486 | uuid: this.uuid, | |
487 | isLocal: this.isOwned(), | |
488 | ||
489 | displayName: this.name, | |
490 | description: this.description, | |
491 | privacy: { | |
492 | id: this.privacy, | |
493 | label: VideoPlaylistModel.getPrivacyLabel(this.privacy) | |
494 | }, | |
495 | ||
496 | thumbnailPath: this.getThumbnailStaticPath(), | |
497 | ||
df0b219d C |
498 | type: { |
499 | id: this.type, | |
500 | label: VideoPlaylistModel.getTypeLabel(this.type) | |
501 | }, | |
502 | ||
1735c825 | 503 | videosLength: this.get('videosLength') as number, |
418d092a C |
504 | |
505 | createdAt: this.createdAt, | |
506 | updatedAt: this.updatedAt, | |
507 | ||
508 | ownerAccount: this.OwnerAccount.toFormattedSummaryJSON(), | |
07b1a18a | 509 | videoChannel: this.VideoChannel ? this.VideoChannel.toFormattedSummaryJSON() : null |
418d092a C |
510 | } |
511 | } | |
512 | ||
b5fecbf4 | 513 | toActivityPubObject (this: MVideoPlaylistAP, page: number, t: Transaction): Promise<PlaylistObject> { |
418d092a | 514 | const handler = (start: number, count: number) => { |
df0b219d | 515 | return VideoPlaylistElementModel.listUrlsOfForAP(this.id, start, count, t) |
418d092a C |
516 | } |
517 | ||
e8bafea3 C |
518 | let icon: ActivityIconObject |
519 | if (this.hasThumbnail()) { | |
520 | icon = { | |
521 | type: 'Image' as 'Image', | |
522 | url: this.getThumbnailUrl(), | |
523 | mediaType: 'image/jpeg' as 'image/jpeg', | |
524 | width: THUMBNAILS_SIZE.width, | |
525 | height: THUMBNAILS_SIZE.height | |
526 | } | |
527 | } | |
528 | ||
df0b219d | 529 | return activityPubCollectionPagination(this.url, handler, page) |
418d092a C |
530 | .then(o => { |
531 | return Object.assign(o, { | |
532 | type: 'Playlist' as 'Playlist', | |
533 | name: this.name, | |
534 | content: this.description, | |
535 | uuid: this.uuid, | |
df0b219d C |
536 | published: this.createdAt.toISOString(), |
537 | updated: this.updatedAt.toISOString(), | |
418d092a | 538 | attributedTo: this.VideoChannel ? [ this.VideoChannel.Actor.url ] : [], |
e8bafea3 | 539 | icon |
418d092a C |
540 | }) |
541 | }) | |
542 | } | |
543 | } |