X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fvideo%2Fvideo-playlist.ts;h=4ca17ebec0126f49ace0f97be84c932f5f2e19ea;hb=f409f0c3b91d85c66b4841d72ea65b5fbe1483d8;hp=073609c24baaacc7347a6ace6c2fe0aeb54c5364;hpb=e8bafea35bc930cb8ac5b2d521a188642a1adffe;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/video/video-playlist.ts b/server/models/video/video-playlist.ts index 073609c24..4ca17ebec 100644 --- a/server/models/video/video-playlist.ts +++ b/server/models/video/video-playlist.ts @@ -15,7 +15,6 @@ import { Table, UpdatedAt } from 'sequelize-typescript' -import * as Sequelize from 'sequelize' import { VideoPlaylistPrivacy } from '../../../shared/models/videos/playlist/video-playlist-privacy.model' import { buildServerIdsFollowedBy, buildWhereIdOrUUID, getSort, isOutdated, throwIfNotValid } from '../utils' import { @@ -34,7 +33,7 @@ import { WEBSERVER } from '../../initializers/constants' import { VideoPlaylist } from '../../../shared/models/videos/playlist/video-playlist.model' -import { AccountModel, ScopeNames as AccountScopeNames } from '../account/account' +import { AccountModel, ScopeNames as AccountScopeNames, SummaryOptions } from '../account/account' import { ScopeNames as VideoChannelScopeNames, VideoChannelModel } from './video-channel' import { join } from 'path' import { VideoPlaylistElementModel } from './video-playlist-element' @@ -43,6 +42,17 @@ import { activityPubCollectionPagination } from '../../helpers/activitypub' import { VideoPlaylistType } from '../../../shared/models/videos/playlist/video-playlist-type.model' import { ThumbnailModel } from './thumbnail' import { ActivityIconObject } from '../../../shared/models/activitypub/objects' +import { FindOptions, literal, Op, ScopeOptions, Transaction, WhereOptions } from 'sequelize' +import * as Bluebird from 'bluebird' +import { + MVideoPlaylistAccountThumbnail, + MVideoPlaylistAP, + MVideoPlaylistFormattable, + MVideoPlaylistFull, + MVideoPlaylistFullSummary, + MVideoPlaylistIdWithElements +} from '../../typings/models/video/video-playlist' +import { MThumbnail } from '../../typings/models/video/thumbnail' enum ScopeNames { AVAILABLE_FOR_LIST = 'AVAILABLE_FOR_LIST', @@ -58,82 +68,87 @@ type AvailableForListOptions = { type?: VideoPlaylistType accountId?: number videoChannelId?: number - privateAndUnlisted?: boolean + listMyPlaylists?: boolean + search?: string } -@Scopes({ - [ ScopeNames.WITH_THUMBNAIL ]: { +@Scopes(() => ({ + [ScopeNames.WITH_THUMBNAIL]: { include: [ { - model: () => ThumbnailModel, + model: ThumbnailModel, required: false } ] }, - [ ScopeNames.WITH_VIDEOS_LENGTH ]: { + [ScopeNames.WITH_VIDEOS_LENGTH]: { attributes: { include: [ [ - Sequelize.literal('(SELECT COUNT("id") FROM "videoPlaylistElement" WHERE "videoPlaylistId" = "VideoPlaylistModel"."id")'), + literal('(SELECT COUNT("id") FROM "videoPlaylistElement" WHERE "videoPlaylistId" = "VideoPlaylistModel"."id")'), 'videosLength' ] ] } - }, - [ ScopeNames.WITH_ACCOUNT ]: { + } as FindOptions, + [ScopeNames.WITH_ACCOUNT]: { include: [ { - model: () => AccountModel, + model: AccountModel, required: true } ] }, - [ ScopeNames.WITH_ACCOUNT_AND_CHANNEL_SUMMARY ]: { + [ScopeNames.WITH_ACCOUNT_AND_CHANNEL_SUMMARY]: { include: [ { - model: () => AccountModel.scope(AccountScopeNames.SUMMARY), + model: AccountModel.scope(AccountScopeNames.SUMMARY), required: true }, { - model: () => VideoChannelModel.scope(VideoChannelScopeNames.SUMMARY), + model: VideoChannelModel.scope(VideoChannelScopeNames.SUMMARY), required: false } ] }, - [ ScopeNames.WITH_ACCOUNT_AND_CHANNEL ]: { + [ScopeNames.WITH_ACCOUNT_AND_CHANNEL]: { include: [ { - model: () => AccountModel, + model: AccountModel, required: true }, { - model: () => VideoChannelModel, + model: VideoChannelModel, required: false } ] }, - [ ScopeNames.AVAILABLE_FOR_LIST ]: (options: AvailableForListOptions) => { - // Only list local playlists OR playlists that are on an instance followed by actorId - const inQueryInstanceFollow = buildServerIdsFollowedBy(options.followerActorId) - const actorWhere = { - [ Sequelize.Op.or ]: [ - { - serverId: null - }, - { - serverId: { - [ Sequelize.Op.in ]: Sequelize.literal(inQueryInstanceFollow) - } - } - ] - } + [ScopeNames.AVAILABLE_FOR_LIST]: (options: AvailableForListOptions) => { + + let whereActor: WhereOptions = {} - const whereAnd: any[] = [] + const whereAnd: WhereOptions[] = [] - if (options.privateAndUnlisted !== true) { + if (options.listMyPlaylists !== true) { whereAnd.push({ privacy: VideoPlaylistPrivacy.PUBLIC }) + + // Only list local playlists OR playlists that are on an instance followed by actorId + const inQueryInstanceFollow = buildServerIdsFollowedBy(options.followerActorId) + + whereActor = { + [Op.or]: [ + { + serverId: null + }, + { + serverId: { + [Op.in]: literal(inQueryInstanceFollow) + } + } + ] + } } if (options.accountId) { @@ -154,12 +169,20 @@ type AvailableForListOptions = { }) } + if (options.search) { + whereAnd.push({ + name: { + [Op.iLike]: '%' + options.search + '%' + } + }) + } + const where = { - [Sequelize.Op.and]: whereAnd + [Op.and]: whereAnd } const accountScope = { - method: [ AccountScopeNames.SUMMARY, actorWhere ] + method: [ AccountScopeNames.SUMMARY, { whereActor } as SummaryOptions ] } return { @@ -174,9 +197,9 @@ type AvailableForListOptions = { required: false } ] - } + } as FindOptions } -}) +})) @Table({ tableName: 'videoPlaylist', @@ -206,7 +229,7 @@ export class VideoPlaylistModel extends Model { name: string @AllowNull(true) - @Is('VideoPlaylistDescription', value => throwIfNotValid(value, isVideoPlaylistDescriptionValid, 'description')) + @Is('VideoPlaylistDescription', value => throwIfNotValid(value, isVideoPlaylistDescriptionValid, 'description', true)) @Column description: string @@ -276,13 +299,14 @@ export class VideoPlaylistModel extends Model { static listForApi (options: { followerActorId: number - start: number, - count: number, - sort: string, - type?: VideoPlaylistType, - accountId?: number, - videoChannelId?: number, - privateAndUnlisted?: boolean + start: number + count: number + sort: string + type?: VideoPlaylistType + accountId?: number + videoChannelId?: number + listMyPlaylists?: boolean + search?: string }) { const query = { offset: options.start, @@ -290,7 +314,7 @@ export class VideoPlaylistModel extends Model { order: getSort(options.sort) } - const scopes = [ + const scopes: (string | ScopeOptions)[] = [ { method: [ ScopeNames.AVAILABLE_FOR_LIST, @@ -299,10 +323,11 @@ export class VideoPlaylistModel extends Model { followerActorId: options.followerActorId, accountId: options.accountId, videoChannelId: options.videoChannelId, - privateAndUnlisted: options.privateAndUnlisted + listMyPlaylists: options.listMyPlaylists, + search: options.search } as AvailableForListOptions ] - } as any, // FIXME: typings + }, ScopeNames.WITH_VIDEOS_LENGTH, ScopeNames.WITH_THUMBNAIL ] @@ -332,7 +357,7 @@ export class VideoPlaylistModel extends Model { }) } - static listPlaylistIdsOf (accountId: number, videoIds: number[]) { + static listPlaylistIdsOf (accountId: number, videoIds: number[]): Bluebird { const query = { attributes: [ 'id' ], where: { @@ -340,11 +365,11 @@ export class VideoPlaylistModel extends Model { }, include: [ { - attributes: [ 'videoId', 'startTimestamp', 'stopTimestamp' ], + attributes: [ 'id', 'videoId', 'startTimestamp', 'stopTimestamp' ], model: VideoPlaylistElementModel.unscoped(), where: { videoId: { - [Sequelize.Op.any]: videoIds + [Op.in]: videoIds } }, required: true @@ -368,7 +393,7 @@ export class VideoPlaylistModel extends Model { .then(e => !!e) } - static loadWithAccountAndChannelSummary (id: number | string, transaction: Sequelize.Transaction) { + static loadWithAccountAndChannelSummary (id: number | string, transaction: Transaction): Bluebird { const where = buildWhereIdOrUUID(id) const query = { @@ -381,7 +406,7 @@ export class VideoPlaylistModel extends Model { .findOne(query) } - static loadWithAccountAndChannel (id: number | string, transaction: Sequelize.Transaction) { + static loadWithAccountAndChannel (id: number | string, transaction: Transaction): Bluebird { const where = buildWhereIdOrUUID(id) const query = { @@ -394,7 +419,7 @@ export class VideoPlaylistModel extends Model { .findOne(query) } - static loadByUrlAndPopulateAccount (url: string) { + static loadByUrlAndPopulateAccount (url: string): Bluebird { const query = { where: { url @@ -412,7 +437,7 @@ export class VideoPlaylistModel extends Model { return VIDEO_PLAYLIST_TYPES[type] || 'Unknown' } - static resetPlaylistsOfChannel (videoChannelId: number, transaction: Sequelize.Transaction) { + static resetPlaylistsOfChannel (videoChannelId: number, transaction: Transaction) { const query = { where: { videoChannelId @@ -423,18 +448,20 @@ export class VideoPlaylistModel extends Model { return VideoPlaylistModel.update({ privacy: VideoPlaylistPrivacy.PRIVATE, videoChannelId: null }, query) } - setThumbnail (thumbnail: ThumbnailModel) { - this.Thumbnail = thumbnail - } + async setAndSaveThumbnail (thumbnail: MThumbnail, t: Transaction) { + thumbnail.videoPlaylistId = this.id - getThumbnail () { - return this.Thumbnail + this.Thumbnail = await thumbnail.save({ transaction: t }) } hasThumbnail () { return !!this.Thumbnail } + hasGeneratedThumbnail () { + return this.hasThumbnail() && this.Thumbnail.automaticallyGenerated === true + } + generateThumbnailName () { const extension = '.jpg' @@ -444,13 +471,13 @@ export class VideoPlaylistModel extends Model { getThumbnailUrl () { if (!this.hasThumbnail()) return null - return WEBSERVER.URL + STATIC_PATHS.THUMBNAILS + this.getThumbnail().filename + return WEBSERVER.URL + STATIC_PATHS.THUMBNAILS + this.Thumbnail.filename } getThumbnailStaticPath () { if (!this.hasThumbnail()) return null - return join(STATIC_PATHS.THUMBNAILS, this.getThumbnail().filename) + return join(STATIC_PATHS.THUMBNAILS, this.Thumbnail.filename) } setAsRefreshed () { @@ -469,7 +496,7 @@ export class VideoPlaylistModel extends Model { return isOutdated(this, ACTIVITY_PUB.VIDEO_PLAYLIST_REFRESH_INTERVAL) } - toFormattedJSON (): VideoPlaylist { + toFormattedJSON (this: MVideoPlaylistFormattable): VideoPlaylist { return { id: this.id, uuid: this.uuid, @@ -489,7 +516,7 @@ export class VideoPlaylistModel extends Model { label: VideoPlaylistModel.getTypeLabel(this.type) }, - videosLength: this.get('videosLength'), + videosLength: this.get('videosLength') as number, createdAt: this.createdAt, updatedAt: this.updatedAt, @@ -499,7 +526,7 @@ export class VideoPlaylistModel extends Model { } } - toActivityPubObject (page: number, t: Sequelize.Transaction): Promise { + toActivityPubObject (this: MVideoPlaylistAP, page: number, t: Transaction): Promise { const handler = (start: number, count: number) => { return VideoPlaylistElementModel.listUrlsOfForAP(this.id, start, count, t) }