From c0687c91b9cf185c36e477ab30266c779f792ee1 Mon Sep 17 00:00:00 2001 From: Wicklow Date: Fri, 24 Feb 2023 16:21:26 +0100 Subject: [PATCH] Add server hooks for listing subscription --- .../controllers/api/users/my-subscriptions.ts | 11 +++++++++-- .../tests/fixtures/peertube-plugin-test/main.js | 10 ++++++++++ server/tests/plugins/filter-hooks.ts | 17 +++++++++++++++++ .../models/plugins/server/server-hook.model.ts | 4 ++++ 4 files changed, 40 insertions(+), 2 deletions(-) diff --git a/server/controllers/api/users/my-subscriptions.ts b/server/controllers/api/users/my-subscriptions.ts index 6c64e99a7..6ba8ba597 100644 --- a/server/controllers/api/users/my-subscriptions.ts +++ b/server/controllers/api/users/my-subscriptions.ts @@ -30,6 +30,7 @@ import { } from '../../../middlewares/validators' import { ActorFollowModel } from '../../../models/actor/actor-follow' import { VideoModel } from '../../../models/video/video' +import { Hooks } from '@server/lib/plugins/hooks' const mySubscriptionsRouter = express.Router() @@ -170,7 +171,7 @@ async function getUserSubscriptionVideos (req: express.Request, res: express.Res const countVideos = getCountVideos(req) const query = pickCommonVideoQuery(req.query) - const resultList = await VideoModel.listForApi({ + const apiOptions = await Hooks.wrapObject({ ...query, displayOnlyForFollower: { @@ -180,7 +181,13 @@ async function getUserSubscriptionVideos (req: express.Request, res: express.Res nsfw: buildNSFWFilter(res, query.nsfw), user, countVideos - }) + }, 'filter:api.user.me.subscription-videos.list.params') + + const resultList = await Hooks.wrapPromiseFun( + VideoModel.listForApi, + apiOptions, + 'filter:api.user.me.subscription-videos.list.result' + ) return res.json(getFormattedObjects(resultList.data, resultList.total, guessAdditionalAttributesFromQuery(query))) } diff --git a/server/tests/fixtures/peertube-plugin-test/main.js b/server/tests/fixtures/peertube-plugin-test/main.js index 5b4d34f15..8b918b634 100644 --- a/server/tests/fixtures/peertube-plugin-test/main.js +++ b/server/tests/fixtures/peertube-plugin-test/main.js @@ -89,6 +89,16 @@ async function register ({ registerHook, registerSetting, settingsManager, stora handler: obj => addToTotal(obj, 4) }) + registerHook({ + target: 'filter:api.user.me.subscription-videos.list.params', + handler: obj => Object.assign({}, obj, { count: 1 }) + }) + + registerHook({ + target: 'filter:api.user.me.subscription-videos.list.result', + handler: obj => addToTotal(obj, 4) + }) + registerHook({ target: 'filter:api.video.get.result', handler: video => { diff --git a/server/tests/plugins/filter-hooks.ts b/server/tests/plugins/filter-hooks.ts index 37eef6cf3..6bd38cf65 100644 --- a/server/tests/plugins/filter-hooks.ts +++ b/server/tests/plugins/filter-hooks.ts @@ -71,6 +71,9 @@ describe('Test plugin filter hooks', function () { } } }) + + // Root subscribes to itself + await servers[0].subscriptions.add({ targetUri: 'root_channel@' + servers[0].host }) }) describe('Videos', function () { @@ -151,6 +154,20 @@ describe('Test plugin filter hooks', function () { expect(total).to.equal(14) }) + it('Should run filter:api.user.me.subscription-videos.list.params', async function () { + const { data } = await servers[0].subscriptions.listVideos() + + // 1 plugin set the count parameter to 1 + expect(data).to.have.lengthOf(1) + }) + + it('Should run filter:api.user.me.subscription-videos.list.result', async function () { + const { total } = await servers[0].subscriptions.listVideos() + + // Plugin do +4 to the total result + expect(total).to.equal(14) + }) + it('Should run filter:api.video.get.result', async function () { const video = await servers[0].videos.get({ id: videoUUID }) expect(video.name).to.contain('<3') diff --git a/shared/models/plugins/server/server-hook.model.ts b/shared/models/plugins/server/server-hook.model.ts index dd9cc3ad6..bbd08365c 100644 --- a/shared/models/plugins/server/server-hook.model.ts +++ b/shared/models/plugins/server/server-hook.model.ts @@ -27,6 +27,10 @@ export const serverFilterHookObject = { 'filter:api.overviews.videos.list.params': true, 'filter:api.overviews.videos.list.result': true, + // Filter params/result used to list subscription videos for the REST API + 'filter:api.user.me.subscription-videos.list.params': true, + 'filter:api.user.me.subscription-videos.list.result': true, + // Filter params/results to search videos/channels in the DB or on the remote index 'filter:api.search.videos.local.list.params': true, 'filter:api.search.videos.local.list.result': true,