Skip to content

Commit

Permalink
Add server hooks for listing subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
wickloww authored and Chocobozzz committed Feb 25, 2023
1 parent d41f4a6 commit c0687c9
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
11 changes: 9 additions & 2 deletions server/controllers/api/users/my-subscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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: {
Expand All @@ -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)))
}
10 changes: 10 additions & 0 deletions server/tests/fixtures/peertube-plugin-test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
17 changes: 17 additions & 0 deletions server/tests/plugins/filter-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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')
Expand Down
4 changes: 4 additions & 0 deletions shared/models/plugins/server/server-hook.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit c0687c9

Please sign in to comment.