diff options
author | Chocobozzz <me@florianbigard.com> | 2020-04-09 11:35:29 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2020-04-09 11:37:44 +0200 |
commit | ab3ead3a6f080e6768b898e699c8de92703d93c6 (patch) | |
tree | d335ad83d47341b43ca842c319f21eaefdcc78e4 | |
parent | 1b05d82d861f42c27766e9f24d8d55e68b0cf098 (diff) | |
download | PeerTube-ab3ead3a6f080e6768b898e699c8de92703d93c6.tar.gz PeerTube-ab3ead3a6f080e6768b898e699c8de92703d93c6.tar.zst PeerTube-ab3ead3a6f080e6768b898e699c8de92703d93c6.zip |
Add ability to remove a video from a plugin
-rw-r--r-- | server/lib/plugins/hooks.ts | 2 | ||||
-rw-r--r-- | server/lib/plugins/plugin-helpers.ts | 17 | ||||
-rw-r--r-- | server/tests/fixtures/peertube-plugin-test-four/main.js | 18 | ||||
-rw-r--r-- | server/tests/plugins/plugin-helpers.ts | 36 | ||||
-rw-r--r-- | server/typings/plugins/register-server-option.model.ts | 4 |
5 files changed, 68 insertions, 9 deletions
diff --git a/server/lib/plugins/hooks.ts b/server/lib/plugins/hooks.ts index bcc8c674e..aa92f03cc 100644 --- a/server/lib/plugins/hooks.ts +++ b/server/lib/plugins/hooks.ts | |||
@@ -25,7 +25,7 @@ const Hooks = { | |||
25 | }, | 25 | }, |
26 | 26 | ||
27 | runAction: <T, U extends ServerActionHookName>(hookName: U, params?: T) => { | 27 | runAction: <T, U extends ServerActionHookName>(hookName: U, params?: T) => { |
28 | PluginManager.Instance.runHook(hookName, params) | 28 | PluginManager.Instance.runHook(hookName, undefined, params) |
29 | .catch(err => logger.error('Fatal hook error.', { err })) | 29 | .catch(err => logger.error('Fatal hook error.', { err })) |
30 | } | 30 | } |
31 | } | 31 | } |
diff --git a/server/lib/plugins/plugin-helpers.ts b/server/lib/plugins/plugin-helpers.ts index a1493c7df..e91beffba 100644 --- a/server/lib/plugins/plugin-helpers.ts +++ b/server/lib/plugins/plugin-helpers.ts | |||
@@ -1,15 +1,18 @@ | |||
1 | import { PeerTubeHelpers } from '@server/typings/plugins' | 1 | import { PeerTubeHelpers } from '@server/typings/plugins' |
2 | import { sequelizeTypescript } from '@server/initializers/database' | 2 | import { sequelizeTypescript } from '@server/initializers/database' |
3 | import { buildLogger } from '@server/helpers/logger' | 3 | import { buildLogger } from '@server/helpers/logger' |
4 | import { VideoModel } from '@server/models/video/video' | ||
4 | 5 | ||
5 | function buildPluginHelpers (npmName: string): PeerTubeHelpers { | 6 | function buildPluginHelpers (npmName: string): PeerTubeHelpers { |
6 | const logger = buildPluginLogger(npmName) | 7 | const logger = buildPluginLogger(npmName) |
7 | 8 | ||
8 | const database = buildDatabaseHelpers() | 9 | const database = buildDatabaseHelpers() |
10 | const videos = buildVideosHelpers() | ||
9 | 11 | ||
10 | return { | 12 | return { |
11 | logger, | 13 | logger, |
12 | database | 14 | database, |
15 | videos | ||
13 | } | 16 | } |
14 | } | 17 | } |
15 | 18 | ||
@@ -28,3 +31,15 @@ function buildDatabaseHelpers () { | |||
28 | query: sequelizeTypescript.query.bind(sequelizeTypescript) | 31 | query: sequelizeTypescript.query.bind(sequelizeTypescript) |
29 | } | 32 | } |
30 | } | 33 | } |
34 | |||
35 | function buildVideosHelpers () { | ||
36 | return { | ||
37 | removeVideo: (id: number) => { | ||
38 | return sequelizeTypescript.transaction(async t => { | ||
39 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(id, t) | ||
40 | |||
41 | await video.destroy({ transaction: t }) | ||
42 | }) | ||
43 | } | ||
44 | } | ||
45 | } | ||
diff --git a/server/tests/fixtures/peertube-plugin-test-four/main.js b/server/tests/fixtures/peertube-plugin-test-four/main.js index 9abb73646..2e81550c1 100644 --- a/server/tests/fixtures/peertube-plugin-test-four/main.js +++ b/server/tests/fixtures/peertube-plugin-test-four/main.js | |||
@@ -1,7 +1,10 @@ | |||
1 | async function register ({ | 1 | async function register ({ |
2 | peertubeHelpers | 2 | peertubeHelpers, |
3 | registerHook | ||
3 | }) { | 4 | }) { |
4 | peertubeHelpers.logger.info('Hello world from plugin four') | 5 | const logger = peertubeHelpers.logger |
6 | |||
7 | logger.info('Hello world from plugin four') | ||
5 | 8 | ||
6 | const username = 'root' | 9 | const username = 'root' |
7 | const results = await peertubeHelpers.database.query( | 10 | const results = await peertubeHelpers.database.query( |
@@ -12,7 +15,16 @@ async function register ({ | |||
12 | } | 15 | } |
13 | ) | 16 | ) |
14 | 17 | ||
15 | peertubeHelpers.logger.info('root email is ' + results[0]['email']) | 18 | logger.info('root email is ' + results[0]['email']) |
19 | |||
20 | registerHook({ | ||
21 | target: 'action:api.video.viewed', | ||
22 | handler: async ({ video }) => { | ||
23 | await peertubeHelpers.videos.removeVideo(video.id) | ||
24 | |||
25 | logger.info('Video deleted by plugin four.') | ||
26 | } | ||
27 | }) | ||
16 | } | 28 | } |
17 | 29 | ||
18 | async function unregister () { | 30 | async function unregister () { |
diff --git a/server/tests/plugins/plugin-helpers.ts b/server/tests/plugins/plugin-helpers.ts index 05928273f..dfe8ebe55 100644 --- a/server/tests/plugins/plugin-helpers.ts +++ b/server/tests/plugins/plugin-helpers.ts | |||
@@ -1,11 +1,16 @@ | |||
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ | 1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ |
2 | 2 | ||
3 | import * as chai from 'chai' | ||
4 | import 'mocha' | 3 | import 'mocha' |
5 | import { cleanupTests, flushAndRunServer, ServerInfo, waitUntilLog } from '../../../shared/extra-utils/server/servers' | 4 | import { cleanupTests, flushAndRunServer, ServerInfo, waitUntilLog } from '../../../shared/extra-utils/server/servers' |
6 | import { getPluginTestPath, installPlugin, setAccessTokensToServers } from '../../../shared/extra-utils' | 5 | import { |
7 | 6 | checkVideoFilesWereRemoved, | |
8 | const expect = chai.expect | 7 | getPluginTestPath, |
8 | getVideo, | ||
9 | installPlugin, | ||
10 | setAccessTokensToServers, | ||
11 | uploadVideoAndGetId, | ||
12 | viewVideo | ||
13 | } from '../../../shared/extra-utils' | ||
9 | 14 | ||
10 | describe('Test plugin helpers', function () { | 15 | describe('Test plugin helpers', function () { |
11 | let server: ServerInfo | 16 | let server: ServerInfo |
@@ -32,6 +37,29 @@ describe('Test plugin helpers', function () { | |||
32 | await waitUntilLog(server, `root email is admin${server.internalServerNumber}@example.com`, 1) | 37 | await waitUntilLog(server, `root email is admin${server.internalServerNumber}@example.com`, 1) |
33 | }) | 38 | }) |
34 | 39 | ||
40 | it('Should remove a video after a view', async function () { | ||
41 | this.timeout(20000) | ||
42 | |||
43 | const videoUUID = (await uploadVideoAndGetId({ server: server, videoName: 'video1' })).uuid | ||
44 | |||
45 | // Should not throw -> video exists | ||
46 | await getVideo(server.url, videoUUID) | ||
47 | // Should delete the video | ||
48 | await viewVideo(server.url, videoUUID) | ||
49 | |||
50 | await waitUntilLog(server, 'Video deleted by plugin four.', 1) | ||
51 | |||
52 | try { | ||
53 | // Should throw because the video should have been deleted | ||
54 | await getVideo(server.url, videoUUID) | ||
55 | throw new Error('Video exists') | ||
56 | } catch (err) { | ||
57 | if (err.message.includes('exists')) throw err | ||
58 | } | ||
59 | |||
60 | await checkVideoFilesWereRemoved(videoUUID, server.internalServerNumber) | ||
61 | }) | ||
62 | |||
35 | after(async function () { | 63 | after(async function () { |
36 | await cleanupTests([ server ]) | 64 | await cleanupTests([ server ]) |
37 | }) | 65 | }) |
diff --git a/server/typings/plugins/register-server-option.model.ts b/server/typings/plugins/register-server-option.model.ts index 6f2ba7cd6..fda9afb11 100644 --- a/server/typings/plugins/register-server-option.model.ts +++ b/server/typings/plugins/register-server-option.model.ts | |||
@@ -13,6 +13,10 @@ export type PeerTubeHelpers = { | |||
13 | database: { | 13 | database: { |
14 | query: Function | 14 | query: Function |
15 | } | 15 | } |
16 | |||
17 | videos: { | ||
18 | removeVideo: (videoId: number) => Promise<void> | ||
19 | } | ||
16 | } | 20 | } |
17 | 21 | ||
18 | export type RegisterServerOptions = { | 22 | export type RegisterServerOptions = { |