From 650580504cf14a87bd4025eec9673eb5642dc71d Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 9 Dec 2021 14:05:21 +0100 Subject: [PATCH] Add filter:api.server.stats.get.result hook --- server/controllers/api/server/stats.ts | 4 +++- server/tests/fixtures/peertube-plugin-test/main.js | 7 +++++++ server/tests/plugins/external-auth.ts | 2 +- server/tests/plugins/filter-hooks.ts | 10 ++++++++++ shared/models/plugins/server/server-hook.model.ts | 3 +++ 5 files changed, 24 insertions(+), 2 deletions(-) diff --git a/server/controllers/api/server/stats.ts b/server/controllers/api/server/stats.ts index d661144ca..2ab398f4d 100644 --- a/server/controllers/api/server/stats.ts +++ b/server/controllers/api/server/stats.ts @@ -3,6 +3,7 @@ import { StatsManager } from '@server/lib/stat-manager' import { ROUTE_CACHE_LIFETIME } from '../../../initializers/constants' import { asyncMiddleware } from '../../../middlewares' import { cacheRoute } from '../../../middlewares/cache/cache' +import { Hooks } from '@server/lib/plugins/hooks' const statsRouter = express.Router() @@ -12,7 +13,8 @@ statsRouter.get('/stats', ) async function getStats (_req: express.Request, res: express.Response) { - const data = await StatsManager.Instance.getStats() + let data = await StatsManager.Instance.getStats() + data = await Hooks.wrapObject(data, 'filter:api.server.stats.get.result') return res.json(data) } diff --git a/server/tests/fixtures/peertube-plugin-test/main.js b/server/tests/fixtures/peertube-plugin-test/main.js index db405ff31..aba415d1e 100644 --- a/server/tests/fixtures/peertube-plugin-test/main.js +++ b/server/tests/fixtures/peertube-plugin-test/main.js @@ -233,6 +233,13 @@ async function register ({ registerHook, registerSetting, settingsManager, stora } }) + registerHook({ + target: 'filter:api.server.stats.get.result', + handler: (result) => { + return { ...result, customStats: 14 } + } + }) + { const filterHooks = [ 'filter:api.search.videos.local.list.params', diff --git a/server/tests/plugins/external-auth.ts b/server/tests/plugins/external-auth.ts index f3e018d43..25b25bfee 100644 --- a/server/tests/plugins/external-auth.ts +++ b/server/tests/plugins/external-auth.ts @@ -125,7 +125,7 @@ describe('Test external auth plugins', function () { expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) - await server.servers.waitUntilLog('expired external auth token', 2) + await server.servers.waitUntilLog('expired external auth token', 4) }) it('Should auto login Cyan, create the user and use the token', async function () { diff --git a/server/tests/plugins/filter-hooks.ts b/server/tests/plugins/filter-hooks.ts index 02915f08c..80014566b 100644 --- a/server/tests/plugins/filter-hooks.ts +++ b/server/tests/plugins/filter-hooks.ts @@ -537,6 +537,16 @@ describe('Test plugin filter hooks', function () { }) }) + describe('Stats filters', function () { + + it('Should run filter:api.server.stats.get.result', async function () { + const data = await servers[0].stats.get() + + expect((data as any).customStats).to.equal(14) + }) + + }) + after(async function () { await cleanupTests(servers) }) diff --git a/shared/models/plugins/server/server-hook.model.ts b/shared/models/plugins/server/server-hook.model.ts index 3ab910197..7e344e003 100644 --- a/shared/models/plugins/server/server-hook.model.ts +++ b/shared/models/plugins/server/server-hook.model.ts @@ -63,6 +63,9 @@ export const serverFilterHookObject = { 'filter:api.video-thread-comments.list.params': true, 'filter:api.video-thread-comments.list.result': true, + // Filter get stats result + 'filter:api.server.stats.get.result': true, + // Filter result used to check if we need to auto blacklist a video // (fired when a local or remote video is created or updated) 'filter:video.auto-blacklist.result': true,