aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorWicklow <wicklow@framasoft.org>2023-02-24 16:21:26 +0100
committerChocobozzz <chocobozzz@cpy.re>2023-02-25 15:47:48 +0100
commitc0687c91b9cf185c36e477ab30266c779f792ee1 (patch)
tree5c917b6b87b38eb89620ed21b114ac67c96a71d9
parentd41f4a6dc69d098e9dc9173b7e1a586695ef7b97 (diff)
downloadPeerTube-c0687c91b9cf185c36e477ab30266c779f792ee1.tar.gz
PeerTube-c0687c91b9cf185c36e477ab30266c779f792ee1.tar.zst
PeerTube-c0687c91b9cf185c36e477ab30266c779f792ee1.zip
Add server hooks for listing subscription
-rw-r--r--server/controllers/api/users/my-subscriptions.ts11
-rw-r--r--server/tests/fixtures/peertube-plugin-test/main.js10
-rw-r--r--server/tests/plugins/filter-hooks.ts17
-rw-r--r--shared/models/plugins/server/server-hook.model.ts4
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 {
30} from '../../../middlewares/validators' 30} from '../../../middlewares/validators'
31import { ActorFollowModel } from '../../../models/actor/actor-follow' 31import { ActorFollowModel } from '../../../models/actor/actor-follow'
32import { VideoModel } from '../../../models/video/video' 32import { VideoModel } from '../../../models/video/video'
33import { Hooks } from '@server/lib/plugins/hooks'
33 34
34const mySubscriptionsRouter = express.Router() 35const mySubscriptionsRouter = express.Router()
35 36
@@ -170,7 +171,7 @@ async function getUserSubscriptionVideos (req: express.Request, res: express.Res
170 const countVideos = getCountVideos(req) 171 const countVideos = getCountVideos(req)
171 const query = pickCommonVideoQuery(req.query) 172 const query = pickCommonVideoQuery(req.query)
172 173
173 const resultList = await VideoModel.listForApi({ 174 const apiOptions = await Hooks.wrapObject({
174 ...query, 175 ...query,
175 176
176 displayOnlyForFollower: { 177 displayOnlyForFollower: {
@@ -180,7 +181,13 @@ async function getUserSubscriptionVideos (req: express.Request, res: express.Res
180 nsfw: buildNSFWFilter(res, query.nsfw), 181 nsfw: buildNSFWFilter(res, query.nsfw),
181 user, 182 user,
182 countVideos 183 countVideos
183 }) 184 }, 'filter:api.user.me.subscription-videos.list.params')
185
186 const resultList = await Hooks.wrapPromiseFun(
187 VideoModel.listForApi,
188 apiOptions,
189 'filter:api.user.me.subscription-videos.list.result'
190 )
184 191
185 return res.json(getFormattedObjects(resultList.data, resultList.total, guessAdditionalAttributesFromQuery(query))) 192 return res.json(getFormattedObjects(resultList.data, resultList.total, guessAdditionalAttributesFromQuery(query)))
186} 193}
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
@@ -90,6 +90,16 @@ async function register ({ registerHook, registerSetting, settingsManager, stora
90 }) 90 })
91 91
92 registerHook({ 92 registerHook({
93 target: 'filter:api.user.me.subscription-videos.list.params',
94 handler: obj => Object.assign({}, obj, { count: 1 })
95 })
96
97 registerHook({
98 target: 'filter:api.user.me.subscription-videos.list.result',
99 handler: obj => addToTotal(obj, 4)
100 })
101
102 registerHook({
93 target: 'filter:api.video.get.result', 103 target: 'filter:api.video.get.result',
94 handler: video => { 104 handler: video => {
95 video.name += ' <3' 105 video.name += ' <3'
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 () {
71 } 71 }
72 } 72 }
73 }) 73 })
74
75 // Root subscribes to itself
76 await servers[0].subscriptions.add({ targetUri: 'root_channel@' + servers[0].host })
74 }) 77 })
75 78
76 describe('Videos', function () { 79 describe('Videos', function () {
@@ -151,6 +154,20 @@ describe('Test plugin filter hooks', function () {
151 expect(total).to.equal(14) 154 expect(total).to.equal(14)
152 }) 155 })
153 156
157 it('Should run filter:api.user.me.subscription-videos.list.params', async function () {
158 const { data } = await servers[0].subscriptions.listVideos()
159
160 // 1 plugin set the count parameter to 1
161 expect(data).to.have.lengthOf(1)
162 })
163
164 it('Should run filter:api.user.me.subscription-videos.list.result', async function () {
165 const { total } = await servers[0].subscriptions.listVideos()
166
167 // Plugin do +4 to the total result
168 expect(total).to.equal(14)
169 })
170
154 it('Should run filter:api.video.get.result', async function () { 171 it('Should run filter:api.video.get.result', async function () {
155 const video = await servers[0].videos.get({ id: videoUUID }) 172 const video = await servers[0].videos.get({ id: videoUUID })
156 expect(video.name).to.contain('<3') 173 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 = {
27 'filter:api.overviews.videos.list.params': true, 27 'filter:api.overviews.videos.list.params': true,
28 'filter:api.overviews.videos.list.result': true, 28 'filter:api.overviews.videos.list.result': true,
29 29
30 // Filter params/result used to list subscription videos for the REST API
31 'filter:api.user.me.subscription-videos.list.params': true,
32 'filter:api.user.me.subscription-videos.list.result': true,
33
30 // Filter params/results to search videos/channels in the DB or on the remote index 34 // Filter params/results to search videos/channels in the DB or on the remote index
31 'filter:api.search.videos.local.list.params': true, 35 'filter:api.search.videos.local.list.params': true,
32 'filter:api.search.videos.local.list.result': true, 36 'filter:api.search.videos.local.list.result': true,