From ebd822ca0b769d88dbc5ae0062b286238cbced92 Mon Sep 17 00:00:00 2001 From: kontrollanten <6680299+kontrollanten@users.noreply.github.com> Date: Wed, 16 Jun 2021 19:22:32 +0200 Subject: [PATCH] add playlists.videos.list.params/results hooks closes #4192 --- server/controllers/api/video-playlist.ts | 11 +++++-- .../fixtures/peertube-plugin-test/main.js | 10 +++++++ server/tests/plugins/filter-hooks.ts | 30 +++++++++++++++++++ .../plugins/server/server-hook.model.ts | 4 +++ 4 files changed, 53 insertions(+), 2 deletions(-) diff --git a/server/controllers/api/video-playlist.ts b/server/controllers/api/video-playlist.ts index 7c816b93a..65431792f 100644 --- a/server/controllers/api/video-playlist.ts +++ b/server/controllers/api/video-playlist.ts @@ -43,6 +43,7 @@ import { AccountModel } from '../../models/account/account' import { VideoPlaylistModel } from '../../models/video/video-playlist' import { VideoPlaylistElementModel } from '../../models/video/video-playlist-element' import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' +import { Hooks } from '@server/lib/plugins/hooks' const reqThumbnailFile = createReqFiles([ 'thumbnailfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT, { thumbnailfile: CONFIG.STORAGE.TMP_DIR }) @@ -449,13 +450,19 @@ async function getVideoPlaylistVideos (req: express.Request, res: express.Respon const user = res.locals.oauth ? res.locals.oauth.token.User : undefined const server = await getServerActor() - const resultList = await VideoPlaylistElementModel.listForApi({ + const apiOptions = await Hooks.wrapObject({ start: req.query.start, count: req.query.count, videoPlaylistId: videoPlaylistInstance.id, serverAccount: server.Account, user - }) + }, 'filter:api.video-playlists.videos.list.params') + + const resultList = await Hooks.wrapPromiseFun( + VideoPlaylistElementModel.listForApi, + apiOptions, + 'filter:api.video-playlists.videos.list.result' + ) const options = { displayNSFW: buildNSFWFilter(res, req.query.nsfw), diff --git a/server/tests/fixtures/peertube-plugin-test/main.js b/server/tests/fixtures/peertube-plugin-test/main.js index ee0bc39f3..91f8e5e6f 100644 --- a/server/tests/fixtures/peertube-plugin-test/main.js +++ b/server/tests/fixtures/peertube-plugin-test/main.js @@ -59,6 +59,16 @@ async function register ({ registerHook, registerSetting, settingsManager, stora handler: obj => addToTotal(obj, 3) }) + registerHook({ + target: 'filter:api.video-playlists.videos.list.params', + handle: obj => addToCount(obj, 4) + }) + + registerHook({ + target: 'filter:api.video-playlists.videos.list.result', + handle: obj => addToTotal(obj, 7) + }) + registerHook({ target: 'filter:api.user.me.videos.list.params', handler: obj => addToCount(obj, 4) diff --git a/server/tests/plugins/filter-hooks.ts b/server/tests/plugins/filter-hooks.ts index a947283c2..8dac2d878 100644 --- a/server/tests/plugins/filter-hooks.ts +++ b/server/tests/plugins/filter-hooks.ts @@ -15,6 +15,7 @@ import { getAccountVideos, getConfig, getMyVideos, + getPlaylistVideos, getPluginTestPath, getVideo, getVideoChannelVideos, @@ -53,6 +54,7 @@ describe('Test plugin filter hooks', function () { let servers: ServerInfo[] let videoUUID: string let threadId: number + let playlistId: number before(async function () { this.timeout(60000) @@ -78,6 +80,20 @@ describe('Test plugin filter hooks', function () { await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'default video ' + i }) } + const createPlaylistRes = await createVideoPlaylist({ + url: servers[0].url, + token: servers[0].accessToken, + playlistAttrs: { + displayName: 'my super playlist', + privacy: VideoPlaylistPrivacy.PUBLIC, + description: 'my super description', + thumbnailfile: 'thumbnail.jpg', + videoChannelId: servers[0].videoChannel.id + } + }) + + playlistId = createPlaylistRes.body.videoPlaylist.id + const res = await getVideosList(servers[0].url) videoUUID = res.body.data[0].uuid @@ -135,6 +151,20 @@ describe('Test plugin filter hooks', function () { expect(res.body.total).to.equal(13) }) + it('Should run filter:api.video-playlists.videos.list.params', async function () { + const res = await getPlaylistVideos(servers[0].url, servers[0].accessToken, playlistId, 0, 20) + + // 1 plugin do +4 to the count parameter + expect(res.body.data).to.have.lengthOf(4) + }) + + it('Should run filter:api.video-playlists.videos.list.result', async function () { + const res = await getPlaylistVideos(servers[0].url, servers[0].accessToken, playlistId, 0, 20) + + // Plugin do +7 to the total result + expect(res.body.total).to.equal(7) + }) + it('Should run filter:api.user.me.videos.list.params', async function () { const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 2) diff --git a/shared/models/plugins/server/server-hook.model.ts b/shared/models/plugins/server/server-hook.model.ts index 88277af5a..554e92ffd 100644 --- a/shared/models/plugins/server/server-hook.model.ts +++ b/shared/models/plugins/server/server-hook.model.ts @@ -14,6 +14,10 @@ export const serverFilterHookObject = { 'filter:api.video-channels.videos.list.params': true, 'filter:api.video-channels.videos.list.result': true, + // Filter params/result used to list playlist videos for the REST API + 'filter:api.video-playlists.videos.list.params': true, + 'filter:api.video-playlists.videos.list.result': true, + // Filter params/result used to list my user videos for the REST API 'filter:api.user.me.videos.list.params': true, 'filter:api.user.me.videos.list.result': true,