X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fvideo%2Fvideo-playlist.ts;h=00cca054981db54e435fb12f90dace0df39fbf38;hb=2b621ac0ebe83693bba6354b3482a03ba58143e7;hp=72ba474b40bb80c626b28679983fb532fba715dd;hpb=29837f8885eb37fa300e4b80c90a6d03ab337084;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/video/video-playlist.ts b/server/models/video/video-playlist.ts index 72ba474b4..00cca0549 100644 --- a/server/models/video/video-playlist.ts +++ b/server/models/video/video-playlist.ts @@ -1,5 +1,5 @@ import { join } from 'path' -import { FindOptions, literal, Op, ScopeOptions, Sequelize, Transaction, WhereOptions } from 'sequelize' +import { FindOptions, Includeable, literal, Op, ScopeOptions, Sequelize, Transaction, WhereOptions } from 'sequelize' import { AllowNull, BelongsTo, @@ -17,16 +17,16 @@ import { Table, UpdatedAt } from 'sequelize-typescript' -import { setAsUpdated } from '@server/helpers/database-utils' -import { buildUUID, uuidToShort } from '@server/helpers/uuid' +import { activityPubCollectionPagination } from '@server/lib/activitypub/collection' import { MAccountId, MChannelId } from '@server/types/models' -import { AttributesOnly, buildPlaylistEmbedPath, buildPlaylistWatchPath } from '@shared/core-utils' +import { buildPlaylistEmbedPath, buildPlaylistWatchPath, pick } from '@shared/core-utils' +import { buildUUID, uuidToShort } from '@shared/extra-utils' +import { AttributesOnly } from '@shared/typescript-utils' import { ActivityIconObject } from '../../../shared/models/activitypub/objects' import { PlaylistObject } from '../../../shared/models/activitypub/objects/playlist-object' import { VideoPlaylistPrivacy } from '../../../shared/models/videos/playlist/video-playlist-privacy.model' import { VideoPlaylistType } from '../../../shared/models/videos/playlist/video-playlist-type.model' import { VideoPlaylist } from '../../../shared/models/videos/playlist/video-playlist.model' -import { activityPubCollectionPagination } from '../../helpers/activitypub' import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' import { isVideoPlaylistDescriptionValid, @@ -53,6 +53,7 @@ import { } from '../../types/models/video/video-playlist' import { AccountModel, ScopeNames as AccountScopeNames, SummaryOptions } from '../account/account' import { ActorModel } from '../actor/actor' +import { setAsUpdated } from '../shared' import { buildServerIdsFollowedBy, buildTrigramSearchIndex, @@ -82,7 +83,10 @@ type AvailableForListOptions = { videoChannelId?: number listMyPlaylists?: boolean search?: string + host?: string + uuids?: string[] withVideos?: boolean + forCount?: boolean } function getVideoLengthSelect () { @@ -141,9 +145,19 @@ function getVideoLengthSelect () { ] }, [ScopeNames.AVAILABLE_FOR_LIST]: (options: AvailableForListOptions) => { + const whereAnd: WhereOptions[] = [] + + const whereServer = options.host && options.host !== WEBSERVER.HOST + ? { host: options.host } + : undefined + let whereActor: WhereOptions = {} - const whereAnd: WhereOptions[] = [] + if (options.host === WEBSERVER.HOST) { + whereActor = { + [Op.and]: [ { serverId: null } ] + } + } if (options.listMyPlaylists !== true) { whereAnd.push({ @@ -168,9 +182,7 @@ function getVideoLengthSelect () { }) } - whereActor = { - [Op.or]: whereActorOr - } + Object.assign(whereActor, { [Op.or]: whereActorOr }) } if (options.accountId) { @@ -191,18 +203,26 @@ function getVideoLengthSelect () { }) } + if (options.uuids) { + whereAnd.push({ + uuid: { + [Op.in]: options.uuids + } + }) + } + if (options.withVideos === true) { whereAnd.push( literal(`(${getVideoLengthSelect()}) != 0`) ) } - const attributesInclude = [] + let attributesInclude: any[] = [ literal('0 as similarity') ] if (options.search) { const escapedSearch = VideoPlaylistModel.sequelize.escape(options.search) const escapedLikeSearch = VideoPlaylistModel.sequelize.escape('%' + options.search + '%') - attributesInclude.push(createSimilarityAttribute('VideoPlaylistModel.name', options.search)) + attributesInclude = [ createSimilarityAttribute('VideoPlaylistModel.name', options.search) ] whereAnd.push({ [Op.or]: [ @@ -220,23 +240,28 @@ function getVideoLengthSelect () { [Op.and]: whereAnd } + const include: Includeable[] = [ + { + model: AccountModel.scope({ + method: [ AccountScopeNames.SUMMARY, { whereActor, whereServer, forCount: options.forCount } as SummaryOptions ] + }), + required: true + } + ] + + if (options.forCount !== true) { + include.push({ + model: VideoChannelModel.scope(VideoChannelScopeNames.SUMMARY), + required: false + }) + } + return { attributes: { include: attributesInclude }, where, - include: [ - { - model: AccountModel.scope({ - method: [ AccountScopeNames.SUMMARY, { whereActor } as SummaryOptions ] - }), - required: true - }, - { - model: VideoChannelModel.scope(VideoChannelScopeNames.SUMMARY), - required: false - } - ] + include } as FindOptions } })) @@ -339,17 +364,10 @@ export class VideoPlaylistModel extends Model { - return { total: count, data: rows } - }) + const scopesCount: (string | ScopeOptions)[] = [ + { + method: [ + ScopeNames.AVAILABLE_FOR_LIST, + + { + ...commonAvailableForListOptions, + + withVideos: options.withVideos || false, + forCount: true + } as AvailableForListOptions + ] + }, + ScopeNames.WITH_VIDEOS_LENGTH + ] + + return Promise.all([ + VideoPlaylistModel.scope(scopesCount).count(), + VideoPlaylistModel.scope(scopesFind).findAll(query) + ]).then(([ count, rows ]) => ({ total: count, data: rows })) } - static searchForApi (options: { - followerActorId: number + static searchForApi (options: Pick & { start: number count: number sort: string - search?: string }) { return VideoPlaylistModel.listForApi({ ...options, + type: VideoPlaylistType.REGULAR, listMyPlaylists: false, withVideos: true @@ -412,17 +450,24 @@ export class VideoPlaylistModel extends Model { + return { + attributes: forCount === true + ? [] + : [ 'url' ], + offset: start, + limit: count, + where + } } - return VideoPlaylistModel.findAndCountAll(query) - .then(({ rows, count }) => { - return { total: count, data: rows.map(p => p.url) } - }) + return Promise.all([ + VideoPlaylistModel.count(getQuery(true)), + VideoPlaylistModel.findAll(getQuery(false)) + ]).then(([ total, rows ]) => ({ + total, + data: rows.map(p => p.url) + })) } static listPlaylistIdsOf (accountId: number, videoIds: number[]): Promise { @@ -572,11 +617,11 @@ export class VideoPlaylistModel extends Model