add playlists.videos.list.params/results hooks

closes #4192
This commit is contained in:
kontrollanten 2021-06-16 19:22:32 +02:00
parent 60520c7c85
commit ebd822ca0b
4 changed files with 53 additions and 2 deletions

View File

@ -43,6 +43,7 @@ import { AccountModel } from '../../models/account/account'
import { VideoPlaylistModel } from '../../models/video/video-playlist' import { VideoPlaylistModel } from '../../models/video/video-playlist'
import { VideoPlaylistElementModel } from '../../models/video/video-playlist-element' import { VideoPlaylistElementModel } from '../../models/video/video-playlist-element'
import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
import { Hooks } from '@server/lib/plugins/hooks'
const reqThumbnailFile = createReqFiles([ 'thumbnailfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT, { thumbnailfile: CONFIG.STORAGE.TMP_DIR }) const reqThumbnailFile = createReqFiles([ 'thumbnailfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT, { thumbnailfile: CONFIG.STORAGE.TMP_DIR })
@ -449,13 +450,19 @@ async function getVideoPlaylistVideos (req: express.Request, res: express.Respon
const user = res.locals.oauth ? res.locals.oauth.token.User : undefined const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
const server = await getServerActor() const server = await getServerActor()
const resultList = await VideoPlaylistElementModel.listForApi({ const apiOptions = await Hooks.wrapObject({
start: req.query.start, start: req.query.start,
count: req.query.count, count: req.query.count,
videoPlaylistId: videoPlaylistInstance.id, videoPlaylistId: videoPlaylistInstance.id,
serverAccount: server.Account, serverAccount: server.Account,
user user
}) }, 'filter:api.video-playlists.videos.list.params')
const resultList = await Hooks.wrapPromiseFun(
VideoPlaylistElementModel.listForApi,
apiOptions,
'filter:api.video-playlists.videos.list.result'
)
const options = { const options = {
displayNSFW: buildNSFWFilter(res, req.query.nsfw), displayNSFW: buildNSFWFilter(res, req.query.nsfw),

View File

@ -59,6 +59,16 @@ async function register ({ registerHook, registerSetting, settingsManager, stora
handler: obj => addToTotal(obj, 3) handler: obj => addToTotal(obj, 3)
}) })
registerHook({
target: 'filter:api.video-playlists.videos.list.params',
handle: obj => addToCount(obj, 4)
})
registerHook({
target: 'filter:api.video-playlists.videos.list.result',
handle: obj => addToTotal(obj, 7)
})
registerHook({ registerHook({
target: 'filter:api.user.me.videos.list.params', target: 'filter:api.user.me.videos.list.params',
handler: obj => addToCount(obj, 4) handler: obj => addToCount(obj, 4)

View File

@ -15,6 +15,7 @@ import {
getAccountVideos, getAccountVideos,
getConfig, getConfig,
getMyVideos, getMyVideos,
getPlaylistVideos,
getPluginTestPath, getPluginTestPath,
getVideo, getVideo,
getVideoChannelVideos, getVideoChannelVideos,
@ -53,6 +54,7 @@ describe('Test plugin filter hooks', function () {
let servers: ServerInfo[] let servers: ServerInfo[]
let videoUUID: string let videoUUID: string
let threadId: number let threadId: number
let playlistId: number
before(async function () { before(async function () {
this.timeout(60000) this.timeout(60000)
@ -78,6 +80,20 @@ describe('Test plugin filter hooks', function () {
await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'default video ' + i }) await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'default video ' + i })
} }
const createPlaylistRes = await createVideoPlaylist({
url: servers[0].url,
token: servers[0].accessToken,
playlistAttrs: {
displayName: 'my super playlist',
privacy: VideoPlaylistPrivacy.PUBLIC,
description: 'my super description',
thumbnailfile: 'thumbnail.jpg',
videoChannelId: servers[0].videoChannel.id
}
})
playlistId = createPlaylistRes.body.videoPlaylist.id
const res = await getVideosList(servers[0].url) const res = await getVideosList(servers[0].url)
videoUUID = res.body.data[0].uuid videoUUID = res.body.data[0].uuid
@ -135,6 +151,20 @@ describe('Test plugin filter hooks', function () {
expect(res.body.total).to.equal(13) expect(res.body.total).to.equal(13)
}) })
it('Should run filter:api.video-playlists.videos.list.params', async function () {
const res = await getPlaylistVideos(servers[0].url, servers[0].accessToken, playlistId, 0, 20)
// 1 plugin do +4 to the count parameter
expect(res.body.data).to.have.lengthOf(4)
})
it('Should run filter:api.video-playlists.videos.list.result', async function () {
const res = await getPlaylistVideos(servers[0].url, servers[0].accessToken, playlistId, 0, 20)
// Plugin do +7 to the total result
expect(res.body.total).to.equal(7)
})
it('Should run filter:api.user.me.videos.list.params', async function () { it('Should run filter:api.user.me.videos.list.params', async function () {
const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 2) const res = await getMyVideos(servers[0].url, servers[0].accessToken, 0, 2)

View File

@ -14,6 +14,10 @@ export const serverFilterHookObject = {
'filter:api.video-channels.videos.list.params': true, 'filter:api.video-channels.videos.list.params': true,
'filter:api.video-channels.videos.list.result': true, 'filter:api.video-channels.videos.list.result': true,
// Filter params/result used to list playlist videos for the REST API
'filter:api.video-playlists.videos.list.params': true,
'filter:api.video-playlists.videos.list.result': true,
// Filter params/result used to list my user videos for the REST API // Filter params/result used to list my user videos for the REST API
'filter:api.user.me.videos.list.params': true, 'filter:api.user.me.videos.list.params': true,
'filter:api.user.me.videos.list.result': true, 'filter:api.user.me.videos.list.result': true,