From a4d2ca071514b4838544f2fd7229d2e8cb9da0d4 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 20 Jan 2021 15:28:34 +0100 Subject: Add user video list hooks --- server/controllers/api/users/me.ts | 22 +++++++++++++++------- server/models/video/video.ts | 14 ++++++++------ server/tests/fixtures/peertube-plugin-test/main.js | 10 ++++++++++ server/tests/plugins/filter-hooks.ts | 15 +++++++++++++++ 4 files changed, 48 insertions(+), 13 deletions(-) (limited to 'server') diff --git a/server/controllers/api/users/me.ts b/server/controllers/api/users/me.ts index 009cf42b7..c4e8d8130 100644 --- a/server/controllers/api/users/me.ts +++ b/server/controllers/api/users/me.ts @@ -1,7 +1,8 @@ import 'multer' import * as express from 'express' import { auditLoggerFactory, getAuditIdFromRes, UserAuditView } from '@server/helpers/audit-logger' -import { UserUpdateMe, UserVideoRate as FormattedUserVideoRate, VideoSortField } from '../../../../shared' +import { Hooks } from '@server/lib/plugins/hooks' +import { UserUpdateMe, UserVideoRate as FormattedUserVideoRate } from '../../../../shared' import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' import { UserVideoQuota } from '../../../../shared/models/users/user-video-quota.model' import { createReqFiles } from '../../../helpers/express-utils' @@ -104,12 +105,19 @@ export { async function getUserVideos (req: express.Request, res: express.Response) { const user = res.locals.oauth.token.User - const resultList = await VideoModel.listUserVideosForApi( - user.Account.id, - req.query.start as number, - req.query.count as number, - req.query.sort as VideoSortField, - req.query.search as string + + const apiOptions = await Hooks.wrapObject({ + accountId: user.Account.id, + start: req.query.start, + count: req.query.count, + sort: req.query.sort, + search: req.query.search + }, 'filter:api.user.me.videos.list.params') + + const resultList = await Hooks.wrapPromiseFun( + VideoModel.listUserVideosForApi, + apiOptions, + 'filter:api.user.me.videos.list.result' ) const additionalAttributes = { diff --git a/server/models/video/video.ts b/server/models/video/video.ts index 5027e980d..3db6549ae 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts @@ -1004,13 +1004,15 @@ export class VideoModel extends Model { return result.map(v => v.id) } - static listUserVideosForApi ( - accountId: number, - start: number, - count: number, - sort: string, + static listUserVideosForApi (options: { + accountId: number + start: number + count: number + sort: string search?: string - ) { + }) { + const { accountId, start, count, sort, search } = options + function buildBaseQuery (): FindOptions { let baseQuery = { offset: start, diff --git a/server/tests/fixtures/peertube-plugin-test/main.js b/server/tests/fixtures/peertube-plugin-test/main.js index e5a9f8df5..305d92002 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.user.me.videos.list.params', + handler: obj => addToCount(obj, 4) + }) + + registerHook({ + target: 'filter:api.user.me.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 cdde6cd30..d88170201 100644 --- a/server/tests/plugins/filter-hooks.ts +++ b/server/tests/plugins/filter-hooks.ts @@ -10,6 +10,7 @@ import { doubleFollow, getAccountVideos, getConfig, + getMyVideos, getPluginTestPath, getVideo, getVideoChannelVideos, @@ -121,6 +122,20 @@ describe('Test plugin filter hooks', function () { expect(res.body.total).to.equal(13) }) + it('Should run filter:api.user.me.videos.list.params', async function () { + const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 2) + + // 1 plugin do +4 to the count parameter + expect(res.body.data).to.have.lengthOf(6) + }) + + it('Should run filter:api.user.me.videos.list.result', async function () { + const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 2) + + // Plugin do +4 to the total result + expect(res.body.total).to.equal(14) + }) + it('Should run filter:api.video.get.result', async function () { const res = await getVideo(servers[0].url, videoUUID) -- cgit v1.2.3