mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2025-02-25 18:55:32 -06:00
Add server plugin filter hooks for import with torrent and url (#2621)
* Add server plugin filter hooks for import with torrent and url * WIP: pre and post-import filter hooks * Rebased * Cleanup filters to accept imports Co-authored-by: Chocobozzz <me@florianbigard.com>
This commit is contained in:
@@ -50,7 +50,47 @@ async function register ({ registerHook, registerSetting, settingsManager, stora
|
||||
target: 'filter:api.video.upload.accept.result',
|
||||
handler: ({ accepted }, { videoBody }) => {
|
||||
if (!accepted) return { accepted: false }
|
||||
if (videoBody.name.indexOf('bad word') !== -1) return { accepted: false, errorMessage: 'bad word '}
|
||||
if (videoBody.name.indexOf('bad word') !== -1) return { accepted: false, errorMessage: 'bad word' }
|
||||
|
||||
return { accepted: true }
|
||||
}
|
||||
})
|
||||
|
||||
registerHook({
|
||||
target: 'filter:api.video.pre-import-url.accept.result',
|
||||
handler: ({ accepted }, { videoImportBody }) => {
|
||||
if (!accepted) return { accepted: false }
|
||||
if (videoImportBody.targetUrl.includes('bad')) return { accepted: false, errorMessage: 'bad target url' }
|
||||
|
||||
return { accepted: true }
|
||||
}
|
||||
})
|
||||
|
||||
registerHook({
|
||||
target: 'filter:api.video.pre-import-torrent.accept.result',
|
||||
handler: ({ accepted }, { videoImportBody }) => {
|
||||
if (!accepted) return { accepted: false }
|
||||
if (videoImportBody.name.includes('bad torrent')) return { accepted: false, errorMessage: 'bad torrent' }
|
||||
|
||||
return { accepted: true }
|
||||
}
|
||||
})
|
||||
|
||||
registerHook({
|
||||
target: 'filter:api.video.post-import-url.accept.result',
|
||||
handler: ({ accepted }, { video }) => {
|
||||
if (!accepted) return { accepted: false }
|
||||
if (video.name.includes('bad word')) return { accepted: false, errorMessage: 'bad word' }
|
||||
|
||||
return { accepted: true }
|
||||
}
|
||||
})
|
||||
|
||||
registerHook({
|
||||
target: 'filter:api.video.post-import-torrent.accept.result',
|
||||
handler: ({ accepted }, { video }) => {
|
||||
if (!accepted) return { accepted: false }
|
||||
if (video.name.includes('bad word')) return { accepted: false, errorMessage: 'bad word' }
|
||||
|
||||
return { accepted: true }
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
|
||||
|
||||
import * as chai from 'chai'
|
||||
import 'mocha'
|
||||
import { cleanupTests, flushAndRunMultipleServers, ServerInfo } from '../../../shared/extra-utils/server/servers'
|
||||
import * as chai from 'chai'
|
||||
import { ServerConfig } from '@shared/models'
|
||||
import {
|
||||
addVideoCommentReply,
|
||||
addVideoCommentThread,
|
||||
@@ -23,10 +23,10 @@ import {
|
||||
uploadVideo,
|
||||
waitJobs
|
||||
} from '../../../shared/extra-utils'
|
||||
import { cleanupTests, flushAndRunMultipleServers, ServerInfo } from '../../../shared/extra-utils/server/servers'
|
||||
import { getMyVideoImports, getYoutubeVideoUrl, importVideo } from '../../../shared/extra-utils/videos/video-imports'
|
||||
import { VideoDetails, VideoImport, VideoImportState, VideoPrivacy } from '../../../shared/models/videos'
|
||||
import { VideoCommentThreadTree } from '../../../shared/models/videos/video-comment.model'
|
||||
import { VideoDetails } from '../../../shared/models/videos'
|
||||
import { getYoutubeVideoUrl, importVideo } from '../../../shared/extra-utils/videos/video-imports'
|
||||
import { ServerConfig } from '@shared/models'
|
||||
|
||||
const expect = chai.expect
|
||||
|
||||
@@ -87,6 +87,84 @@ describe('Test plugin filter hooks', function () {
|
||||
await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video with bad word' }, 403)
|
||||
})
|
||||
|
||||
it('Should run filter:api.video.pre-import-url.accept.result', async function () {
|
||||
const baseAttributes = {
|
||||
name: 'normal title',
|
||||
privacy: VideoPrivacy.PUBLIC,
|
||||
channelId: servers[0].videoChannel.id,
|
||||
targetUrl: getYoutubeVideoUrl() + 'bad'
|
||||
}
|
||||
await importVideo(servers[0].url, servers[0].accessToken, baseAttributes, 403)
|
||||
})
|
||||
|
||||
it('Should run filter:api.video.pre-import-torrent.accept.result', async function () {
|
||||
const baseAttributes = {
|
||||
name: 'bad torrent',
|
||||
privacy: VideoPrivacy.PUBLIC,
|
||||
channelId: servers[0].videoChannel.id,
|
||||
torrentfile: 'video-720p.torrent' as any
|
||||
}
|
||||
await importVideo(servers[0].url, servers[0].accessToken, baseAttributes, 403)
|
||||
})
|
||||
|
||||
it('Should run filter:api.video.post-import-url.accept.result', async function () {
|
||||
this.timeout(60000)
|
||||
|
||||
let videoImportId: number
|
||||
|
||||
{
|
||||
const baseAttributes = {
|
||||
name: 'title with bad word',
|
||||
privacy: VideoPrivacy.PUBLIC,
|
||||
channelId: servers[0].videoChannel.id,
|
||||
targetUrl: getYoutubeVideoUrl()
|
||||
}
|
||||
const res = await importVideo(servers[0].url, servers[0].accessToken, baseAttributes)
|
||||
videoImportId = res.body.id
|
||||
}
|
||||
|
||||
await waitJobs(servers)
|
||||
|
||||
{
|
||||
const res = await getMyVideoImports(servers[0].url, servers[0].accessToken)
|
||||
const videoImports = res.body.data as VideoImport[]
|
||||
|
||||
const videoImport = videoImports.find(i => i.id === videoImportId)
|
||||
|
||||
expect(videoImport.state.id).to.equal(VideoImportState.REJECTED)
|
||||
expect(videoImport.state.label).to.equal('Rejected')
|
||||
}
|
||||
})
|
||||
|
||||
it('Should run filter:api.video.post-import-torrent.accept.result', async function () {
|
||||
this.timeout(60000)
|
||||
|
||||
let videoImportId: number
|
||||
|
||||
{
|
||||
const baseAttributes = {
|
||||
name: 'title with bad word',
|
||||
privacy: VideoPrivacy.PUBLIC,
|
||||
channelId: servers[0].videoChannel.id,
|
||||
torrentfile: 'video-720p.torrent' as any
|
||||
}
|
||||
const res = await importVideo(servers[0].url, servers[0].accessToken, baseAttributes)
|
||||
videoImportId = res.body.id
|
||||
}
|
||||
|
||||
await waitJobs(servers)
|
||||
|
||||
{
|
||||
const res = await getMyVideoImports(servers[0].url, servers[0].accessToken)
|
||||
const videoImports = res.body.data as VideoImport[]
|
||||
|
||||
const videoImport = videoImports.find(i => i.id === videoImportId)
|
||||
|
||||
expect(videoImport.state.id).to.equal(VideoImportState.REJECTED)
|
||||
expect(videoImport.state.label).to.equal('Rejected')
|
||||
}
|
||||
})
|
||||
|
||||
it('Should run filter:api.video-thread.create.accept.result', async function () {
|
||||
await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoUUID, 'comment with bad word', 403)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user