Add upload/import/go live video attributes hooks

This commit is contained in:
Chocobozzz 2021-12-10 13:49:19 +01:00
parent 8cf43a6524
commit d17d743051
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
6 changed files with 98 additions and 6 deletions

View File

@ -38,6 +38,7 @@ import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, videoIm
import { VideoModel } from '../../../models/video/video'
import { VideoCaptionModel } from '../../../models/video/video-caption'
import { VideoImportModel } from '../../../models/video/video-import'
import { Hooks } from '@server/lib/plugins/hooks'
const auditLogger = auditLoggerFactory('video-imports')
const videoImportsRouter = express.Router()
@ -94,7 +95,7 @@ async function addTorrentImport (req: express.Request, res: express.Response, to
videoName = result.name
}
const video = buildVideo(res.locals.videoChannel.id, body, { name: videoName })
const video = await buildVideo(res.locals.videoChannel.id, body, { name: videoName })
const thumbnailModel = await processThumbnail(req, video)
const previewModel = await processPreview(req, video)
@ -151,7 +152,7 @@ async function addYoutubeDLImport (req: express.Request, res: express.Response)
})
}
const video = buildVideo(res.locals.videoChannel.id, body, youtubeDLInfo)
const video = await buildVideo(res.locals.videoChannel.id, body, youtubeDLInfo)
// Process video thumbnail from request.files
let thumbnailModel = await processThumbnail(req, video)
@ -210,8 +211,8 @@ async function addYoutubeDLImport (req: express.Request, res: express.Response)
return res.json(videoImport.toFormattedJSON()).end()
}
function buildVideo (channelId: number, body: VideoImportCreate, importData: YoutubeDLInfo): MVideoThumbnail {
const videoData = {
async function buildVideo (channelId: number, body: VideoImportCreate, importData: YoutubeDLInfo): Promise<MVideoThumbnail> {
let videoData = {
name: body.name || importData.name || 'Unknown name',
remote: false,
category: body.category || importData.category,
@ -231,6 +232,14 @@ function buildVideo (channelId: number, body: VideoImportCreate, importData: You
? new Date(body.originallyPublishedAt)
: importData.originallyPublishedAt
}
videoData = await Hooks.wrapObject(
videoData,
body.targetUrl
? 'filter:api.video.import-url.video-attribute.result'
: 'filter:api.video.import-torrent.video-attribute.result'
)
const video = new VideoModel(videoData)
video.url = getLocalVideoActivityPubUrl(video)

View File

@ -83,7 +83,9 @@ async function addLiveVideo (req: express.Request, res: express.Response) {
const videoInfo: LiveVideoCreate = req.body
// Prepare data so we don't block the transaction
const videoData = buildLocalVideoFromReq(videoInfo, res.locals.videoChannel.id)
let videoData = buildLocalVideoFromReq(videoInfo, res.locals.videoChannel.id)
videoData = await Hooks.wrapObject(videoData, 'filter:api.video.live.video-attribute.result')
videoData.isLive = true
videoData.state = VideoState.WAITING_FOR_LIVE
videoData.duration = 0

View File

@ -153,7 +153,8 @@ async function addVideo (options: {
const videoChannel = res.locals.videoChannel
const user = res.locals.oauth.token.User
const videoData = buildLocalVideoFromReq(videoInfo, videoChannel.id)
let videoData = buildLocalVideoFromReq(videoInfo, videoChannel.id)
videoData = await Hooks.wrapObject(videoData, 'filter:api.video.upload.video-attribute.result')
videoData.state = buildNextVideoState()
videoData.duration = videoPhysicalFile.duration // duration was added by a previous middleware

View File

@ -240,6 +240,21 @@ async function register ({ registerHook, registerSetting, settingsManager, stora
}
})
// Upload/import/live attributes
for (const target of [
'filter:api.video.upload.video-attribute.result',
'filter:api.video.import-url.video-attribute.result',
'filter:api.video.import-torrent.video-attribute.result',
'filter:api.video.live.video-attribute.result'
]) {
registerHook({
target,
handler: (result) => {
return { ...result, description: result.description + ' - ' + target }
}
})
}
{
const filterHooks = [
'filter:api.search.videos.local.list.params',

View File

@ -537,6 +537,65 @@ describe('Test plugin filter hooks', function () {
})
})
describe('Upload/import/live attributes filters', function () {
before(async function () {
await servers[0].config.enableLive({ transcoding: false, allowReplay: false })
await servers[0].config.enableImports()
await servers[0].config.disableTranscoding()
})
it('Should run filter:api.video.upload.video-attribute.result', async function () {
for (const mode of [ 'legacy' as 'legacy', 'resumable' as 'resumable' ]) {
const { id } = await servers[0].videos.upload({ attributes: { name: 'video', description: 'upload' }, mode })
const video = await servers[0].videos.get({ id })
expect(video.description).to.equal('upload - filter:api.video.upload.video-attribute.result')
}
})
it('Should run filter:api.video.import-url.video-attribute.result', async function () {
const attributes = {
name: 'video',
description: 'import url',
channelId: servers[0].store.channel.id,
targetUrl: FIXTURE_URLS.goodVideo,
privacy: VideoPrivacy.PUBLIC
}
const { video: { id } } = await servers[0].imports.importVideo({ attributes })
const video = await servers[0].videos.get({ id })
expect(video.description).to.equal('import url - filter:api.video.import-url.video-attribute.result')
})
it('Should run filter:api.video.import-torrent.video-attribute.result', async function () {
const attributes = {
name: 'video',
description: 'import torrent',
channelId: servers[0].store.channel.id,
magnetUri: FIXTURE_URLS.magnet,
privacy: VideoPrivacy.PUBLIC
}
const { video: { id } } = await servers[0].imports.importVideo({ attributes })
const video = await servers[0].videos.get({ id })
expect(video.description).to.equal('import torrent - filter:api.video.import-torrent.video-attribute.result')
})
it('Should run filter:api.video.live.video-attribute.result', async function () {
const fields = {
name: 'live',
description: 'live',
channelId: servers[0].store.channel.id,
privacy: VideoPrivacy.PUBLIC
}
const { id } = await servers[0].live.create({ fields })
const video = await servers[0].videos.get({ id })
expect(video.description).to.equal('live - filter:api.video.live.video-attribute.result')
})
})
describe('Stats filters', function () {
it('Should run filter:api.server.stats.get.result', async function () {

View File

@ -53,6 +53,12 @@ export const serverFilterHookObject = {
'filter:api.video-thread.create.accept.result': true,
'filter:api.video-comment-reply.create.accept.result': true,
// Filter attributes when creating video object
'filter:api.video.upload.video-attribute.result': true,
'filter:api.video.import-url.video-attribute.result': true,
'filter:api.video.import-torrent.video-attribute.result': true,
'filter:api.video.live.video-attribute.result': true,
// Filter params/result used to list threads of a specific video
// (used by the video watch page)
'filter:api.video-threads.list.params': true,