aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2020-12-08 10:30:33 +0100
committerChocobozzz <chocobozzz@cpy.re>2020-12-08 10:33:23 +0100
commit38267c0c8aa80f974a2c0b4c2bd33c8d8008ac94 (patch)
treedd99242abdd5a02de272be0f06ecfe03edadc575
parent1bfc07e4cca1464c87c317060eb86742344467a6 (diff)
downloadPeerTube-38267c0c8aa80f974a2c0b4c2bd33c8d8008ac94.tar.gz
PeerTube-38267c0c8aa80f974a2c0b4c2bd33c8d8008ac94.tar.zst
PeerTube-38267c0c8aa80f974a2c0b4c2bd33c8d8008ac94.zip
Use dedicated hooks for account/channel videos
-rw-r--r--server/controllers/api/accounts.ts4
-rw-r--r--server/controllers/api/video-channel.ts11
-rw-r--r--server/tests/fixtures/peertube-plugin-test/main.js28
-rw-r--r--server/tests/plugins/filter-hooks.ts30
-rw-r--r--shared/models/plugins/server-hook.model.ts9
5 files changed, 74 insertions, 8 deletions
diff --git a/server/controllers/api/accounts.ts b/server/controllers/api/accounts.ts
index 9eb29d330..e807b4f44 100644
--- a/server/controllers/api/accounts.ts
+++ b/server/controllers/api/accounts.ts
@@ -176,12 +176,12 @@ async function listAccountVideos (req: express.Request, res: express.Response) {
176 accountId: account.id, 176 accountId: account.id,
177 user: res.locals.oauth ? res.locals.oauth.token.User : undefined, 177 user: res.locals.oauth ? res.locals.oauth.token.User : undefined,
178 countVideos 178 countVideos
179 }, 'filter:api.videos.list.params') 179 }, 'filter:api.accounts.videos.list.params')
180 180
181 const resultList = await Hooks.wrapPromiseFun( 181 const resultList = await Hooks.wrapPromiseFun(
182 VideoModel.listForApi, 182 VideoModel.listForApi,
183 apiOptions, 183 apiOptions,
184 'filter:api.videos.list.result' 184 'filter:api.accounts.videos.list.result'
185 ) 185 )
186 186
187 return res.json(getFormattedObjects(resultList.data, resultList.total)) 187 return res.json(getFormattedObjects(resultList.data, resultList.total))
diff --git a/server/controllers/api/video-channel.ts b/server/controllers/api/video-channel.ts
index 5c96950c5..c48e00232 100644
--- a/server/controllers/api/video-channel.ts
+++ b/server/controllers/api/video-channel.ts
@@ -1,4 +1,5 @@
1import * as express from 'express' 1import * as express from 'express'
2import { Hooks } from '@server/lib/plugins/hooks'
2import { getServerActor } from '@server/models/application/application' 3import { getServerActor } from '@server/models/application/application'
3import { MChannelAccountDefault } from '@server/types/models' 4import { MChannelAccountDefault } from '@server/types/models'
4import { VideoChannelCreate, VideoChannelUpdate } from '../../../shared' 5import { VideoChannelCreate, VideoChannelUpdate } from '../../../shared'
@@ -266,7 +267,7 @@ async function listVideoChannelVideos (req: express.Request, res: express.Respon
266 const followerActorId = isUserAbleToSearchRemoteURI(res) ? null : undefined 267 const followerActorId = isUserAbleToSearchRemoteURI(res) ? null : undefined
267 const countVideos = getCountVideos(req) 268 const countVideos = getCountVideos(req)
268 269
269 const resultList = await VideoModel.listForApi({ 270 const apiOptions = await Hooks.wrapObject({
270 followerActorId, 271 followerActorId,
271 start: req.query.start, 272 start: req.query.start,
272 count: req.query.count, 273 count: req.query.count,
@@ -283,7 +284,13 @@ async function listVideoChannelVideos (req: express.Request, res: express.Respon
283 videoChannelId: videoChannelInstance.id, 284 videoChannelId: videoChannelInstance.id,
284 user: res.locals.oauth ? res.locals.oauth.token.User : undefined, 285 user: res.locals.oauth ? res.locals.oauth.token.User : undefined,
285 countVideos 286 countVideos
286 }) 287 }, 'filter:api.video-channels.videos.list.params')
288
289 const resultList = await Hooks.wrapPromiseFun(
290 VideoModel.listForApi,
291 apiOptions,
292 'filter:api.video-channels.videos.list.result'
293 )
287 294
288 return res.json(getFormattedObjects(resultList.data, resultList.total)) 295 return res.json(getFormattedObjects(resultList.data, resultList.total))
289} 296}
diff --git a/server/tests/fixtures/peertube-plugin-test/main.js b/server/tests/fixtures/peertube-plugin-test/main.js
index 322c0610c..e5a9f8df5 100644
--- a/server/tests/fixtures/peertube-plugin-test/main.js
+++ b/server/tests/fixtures/peertube-plugin-test/main.js
@@ -40,6 +40,26 @@ async function register ({ registerHook, registerSetting, settingsManager, stora
40 }) 40 })
41 41
42 registerHook({ 42 registerHook({
43 target: 'filter:api.accounts.videos.list.params',
44 handler: obj => addToCount(obj)
45 })
46
47 registerHook({
48 target: 'filter:api.accounts.videos.list.result',
49 handler: obj => addToTotal(obj, 2)
50 })
51
52 registerHook({
53 target: 'filter:api.video-channels.videos.list.params',
54 handler: obj => addToCount(obj, 3)
55 })
56
57 registerHook({
58 target: 'filter:api.video-channels.videos.list.result',
59 handler: obj => addToTotal(obj, 3)
60 })
61
62 registerHook({
43 target: 'filter:api.video.get.result', 63 target: 'filter:api.video.get.result',
44 handler: video => { 64 handler: video => {
45 video.name += ' <3' 65 video.name += ' <3'
@@ -167,14 +187,14 @@ module.exports = {
167 187
168// ############################################################################ 188// ############################################################################
169 189
170function addToCount (obj) { 190function addToCount (obj, amount = 1) {
171 return Object.assign({}, obj, { count: obj.count + 1 }) 191 return Object.assign({}, obj, { count: obj.count + amount })
172} 192}
173 193
174function addToTotal (result) { 194function addToTotal (result, amount = 1) {
175 return { 195 return {
176 data: result.data, 196 data: result.data,
177 total: result.total + 1 197 total: result.total + amount
178 } 198 }
179} 199}
180 200
diff --git a/server/tests/plugins/filter-hooks.ts b/server/tests/plugins/filter-hooks.ts
index 9939b8e6e..3a5c7aa62 100644
--- a/server/tests/plugins/filter-hooks.ts
+++ b/server/tests/plugins/filter-hooks.ts
@@ -8,9 +8,11 @@ import {
8 addVideoCommentThread, 8 addVideoCommentThread,
9 createLive, 9 createLive,
10 doubleFollow, 10 doubleFollow,
11 getAccountVideos,
11 getConfig, 12 getConfig,
12 getPluginTestPath, 13 getPluginTestPath,
13 getVideo, 14 getVideo,
15 getVideoChannelVideos,
14 getVideoCommentThreads, 16 getVideoCommentThreads,
15 getVideosList, 17 getVideosList,
16 getVideosListPagination, 18 getVideosListPagination,
@@ -90,6 +92,34 @@ describe('Test plugin filter hooks', function () {
90 expect(res.body.total).to.equal(11) 92 expect(res.body.total).to.equal(11)
91 }) 93 })
92 94
95 it('Should run filter:api.accounts.videos.list.params', async function () {
96 const res = await getAccountVideos(servers[0].url, servers[0].accessToken, 'root', 0, 2)
97
98 // 1 plugin do +1 to the count parameter
99 expect(res.body.data).to.have.lengthOf(3)
100 })
101
102 it('Should run filter:api.accounts.videos.list.result', async function () {
103 const res = await getAccountVideos(servers[0].url, servers[0].accessToken, 'root', 0, 2)
104
105 // Plugin do +2 to the total result
106 expect(res.body.total).to.equal(12)
107 })
108
109 it('Should run filter:api.video-channels.videos.list.params', async function () {
110 const res = await getVideoChannelVideos(servers[0].url, servers[0].accessToken, 'root_channel', 0, 2)
111
112 // 1 plugin do +3 to the count parameter
113 expect(res.body.data).to.have.lengthOf(5)
114 })
115
116 it('Should run filter:api.video-channels.videos.list.result', async function () {
117 const res = await getAccountVideos(servers[0].url, servers[0].accessToken, 'root_channel', 0, 2)
118
119 // Plugin do +3 to the total result
120 expect(res.body.total).to.equal(13)
121 })
122
93 it('Should run filter:api.video.get.result', async function () { 123 it('Should run filter:api.video.get.result', async function () {
94 const res = await getVideo(servers[0].url, videoUUID) 124 const res = await getVideo(servers[0].url, videoUUID)
95 125
diff --git a/shared/models/plugins/server-hook.model.ts b/shared/models/plugins/server-hook.model.ts
index 6609bc893..64d63f8f5 100644
--- a/shared/models/plugins/server-hook.model.ts
+++ b/shared/models/plugins/server-hook.model.ts
@@ -5,6 +5,15 @@ export const serverFilterHookObject = {
5 // (used by the trending page, recently-added page, local page etc) 5 // (used by the trending page, recently-added page, local page etc)
6 'filter:api.videos.list.params': true, 6 'filter:api.videos.list.params': true,
7 'filter:api.videos.list.result': true, 7 'filter:api.videos.list.result': true,
8
9 // Filter params/result used to list account videos for the REST API
10 'filter:api.accounts.videos.list.params': true,
11 'filter:api.accounts.videos.list.result': true,
12
13 // Filter params/result used to list account videos for the REST API
14 'filter:api.video-channels.videos.list.params': true,
15 'filter:api.video-channels.videos.list.result': true,
16
8 // Filter the result of the get function 17 // Filter the result of the get function
9 // Used to get detailed video information (video watch page for example) 18 // Used to get detailed video information (video watch page for example)
10 'filter:api.video.get.result': true, 19 'filter:api.video.get.result': true,