diff options
author | Chocobozzz <me@florianbigard.com> | 2020-04-09 11:00:30 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2020-04-09 11:00:30 +0200 |
commit | 1b05d82d861f42c27766e9f24d8d55e68b0cf098 (patch) | |
tree | bbf14abc606dd3f8f0c13625afbdf3b17fea3a23 /server | |
parent | bc0d801bb7a0cc503c1637f4a07bb51d68d85608 (diff) | |
download | PeerTube-1b05d82d861f42c27766e9f24d8d55e68b0cf098.tar.gz PeerTube-1b05d82d861f42c27766e9f24d8d55e68b0cf098.tar.zst PeerTube-1b05d82d861f42c27766e9f24d8d55e68b0cf098.zip |
Add SQL query support in plugins
Diffstat (limited to 'server')
-rw-r--r-- | server/helpers/audit-logger.ts | 2 | ||||
-rw-r--r-- | server/lib/plugins/plugin-helpers.ts | 20 | ||||
-rw-r--r-- | server/lib/plugins/register-helpers.ts | 2 | ||||
-rw-r--r-- | server/tests/fixtures/peertube-plugin-test-four/main.js | 27 | ||||
-rw-r--r-- | server/tests/fixtures/peertube-plugin-test-four/package.json | 20 | ||||
-rw-r--r-- | server/tests/plugins/index.ts | 1 | ||||
-rw-r--r-- | server/tests/plugins/plugin-helpers.ts | 38 | ||||
-rw-r--r-- | server/typings/plugins/register-server-option.model.ts | 4 |
8 files changed, 107 insertions, 7 deletions
diff --git a/server/helpers/audit-logger.ts b/server/helpers/audit-logger.ts index a4cfeef76..0bbfbc753 100644 --- a/server/helpers/audit-logger.ts +++ b/server/helpers/audit-logger.ts | |||
@@ -36,7 +36,7 @@ const auditLogger = winston.createLogger({ | |||
36 | maxFiles: 5, | 36 | maxFiles: 5, |
37 | format: winston.format.combine( | 37 | format: winston.format.combine( |
38 | winston.format.timestamp(), | 38 | winston.format.timestamp(), |
39 | labelFormatter, | 39 | labelFormatter(), |
40 | winston.format.splat(), | 40 | winston.format.splat(), |
41 | jsonLoggerFormat | 41 | jsonLoggerFormat |
42 | ) | 42 | ) |
diff --git a/server/lib/plugins/plugin-helpers.ts b/server/lib/plugins/plugin-helpers.ts index 36d08d84a..a1493c7df 100644 --- a/server/lib/plugins/plugin-helpers.ts +++ b/server/lib/plugins/plugin-helpers.ts | |||
@@ -1,11 +1,15 @@ | |||
1 | import { PluginModel } from '@server/models/server/plugin' | ||
2 | import { PeerTubeHelpers } from '@server/typings/plugins' | 1 | import { PeerTubeHelpers } from '@server/typings/plugins' |
2 | import { sequelizeTypescript } from '@server/initializers/database' | ||
3 | import { buildLogger } from '@server/helpers/logger' | ||
3 | 4 | ||
4 | function buildPluginHelpers (npmName: string, plugin: PluginModel): PeerTubeHelpers { | 5 | function buildPluginHelpers (npmName: string): PeerTubeHelpers { |
5 | const logger = buildLogger(npmName) | 6 | const logger = buildPluginLogger(npmName) |
7 | |||
8 | const database = buildDatabaseHelpers() | ||
6 | 9 | ||
7 | return { | 10 | return { |
8 | logger | 11 | logger, |
12 | database | ||
9 | } | 13 | } |
10 | } | 14 | } |
11 | 15 | ||
@@ -15,6 +19,12 @@ export { | |||
15 | 19 | ||
16 | // --------------------------------------------------------------------------- | 20 | // --------------------------------------------------------------------------- |
17 | 21 | ||
18 | function buildLogger (npmName: string) { | 22 | function buildPluginLogger (npmName: string) { |
19 | return buildLogger(npmName) | 23 | return buildLogger(npmName) |
20 | } | 24 | } |
25 | |||
26 | function buildDatabaseHelpers () { | ||
27 | return { | ||
28 | query: sequelizeTypescript.query.bind(sequelizeTypescript) | ||
29 | } | ||
30 | } | ||
diff --git a/server/lib/plugins/register-helpers.ts b/server/lib/plugins/register-helpers.ts index 58bc96f04..4c0935a05 100644 --- a/server/lib/plugins/register-helpers.ts +++ b/server/lib/plugins/register-helpers.ts | |||
@@ -35,7 +35,7 @@ function buildRegisterHelpers (npmName: string, plugin: PluginModel): Omit<Regis | |||
35 | const videoCategoryManager = buildVideoCategoryManager(npmName) | 35 | const videoCategoryManager = buildVideoCategoryManager(npmName) |
36 | const videoLicenceManager = buildVideoLicenceManager(npmName) | 36 | const videoLicenceManager = buildVideoLicenceManager(npmName) |
37 | 37 | ||
38 | const peertubeHelpers = buildPluginHelpers(npmName, plugin) | 38 | const peertubeHelpers = buildPluginHelpers(npmName) |
39 | 39 | ||
40 | return { | 40 | return { |
41 | settingsManager, | 41 | settingsManager, |
diff --git a/server/tests/fixtures/peertube-plugin-test-four/main.js b/server/tests/fixtures/peertube-plugin-test-four/main.js new file mode 100644 index 000000000..9abb73646 --- /dev/null +++ b/server/tests/fixtures/peertube-plugin-test-four/main.js | |||
@@ -0,0 +1,27 @@ | |||
1 | async function register ({ | ||
2 | peertubeHelpers | ||
3 | }) { | ||
4 | peertubeHelpers.logger.info('Hello world from plugin four') | ||
5 | |||
6 | const username = 'root' | ||
7 | const results = await peertubeHelpers.database.query( | ||
8 | 'SELECT "email" from "user" WHERE "username" = $username', | ||
9 | { | ||
10 | type: 'SELECT', | ||
11 | bind: { username } | ||
12 | } | ||
13 | ) | ||
14 | |||
15 | peertubeHelpers.logger.info('root email is ' + results[0]['email']) | ||
16 | } | ||
17 | |||
18 | async function unregister () { | ||
19 | return | ||
20 | } | ||
21 | |||
22 | module.exports = { | ||
23 | register, | ||
24 | unregister | ||
25 | } | ||
26 | |||
27 | // ########################################################################### | ||
diff --git a/server/tests/fixtures/peertube-plugin-test-four/package.json b/server/tests/fixtures/peertube-plugin-test-four/package.json new file mode 100644 index 000000000..dda3c7f37 --- /dev/null +++ b/server/tests/fixtures/peertube-plugin-test-four/package.json | |||
@@ -0,0 +1,20 @@ | |||
1 | { | ||
2 | "name": "peertube-plugin-test-four", | ||
3 | "version": "0.0.1", | ||
4 | "description": "Plugin test 4", | ||
5 | "engine": { | ||
6 | "peertube": ">=1.3.0" | ||
7 | }, | ||
8 | "keywords": [ | ||
9 | "peertube", | ||
10 | "plugin" | ||
11 | ], | ||
12 | "homepage": "https://github.com/Chocobozzz/PeerTube", | ||
13 | "author": "Chocobozzz", | ||
14 | "bugs": "https://github.com/Chocobozzz/PeerTube/issues", | ||
15 | "library": "./main.js", | ||
16 | "staticDirs": {}, | ||
17 | "css": [], | ||
18 | "clientScripts": [], | ||
19 | "translations": {} | ||
20 | } | ||
diff --git a/server/tests/plugins/index.ts b/server/tests/plugins/index.ts index f41708055..9c9499a79 100644 --- a/server/tests/plugins/index.ts +++ b/server/tests/plugins/index.ts | |||
@@ -2,3 +2,4 @@ import './action-hooks' | |||
2 | import './filter-hooks' | 2 | import './filter-hooks' |
3 | import './translations' | 3 | import './translations' |
4 | import './video-constants' | 4 | import './video-constants' |
5 | import './plugin-helpers' | ||
diff --git a/server/tests/plugins/plugin-helpers.ts b/server/tests/plugins/plugin-helpers.ts new file mode 100644 index 000000000..05928273f --- /dev/null +++ b/server/tests/plugins/plugin-helpers.ts | |||
@@ -0,0 +1,38 @@ | |||
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ | ||
2 | |||
3 | import * as chai from 'chai' | ||
4 | import 'mocha' | ||
5 | import { cleanupTests, flushAndRunServer, ServerInfo, waitUntilLog } from '../../../shared/extra-utils/server/servers' | ||
6 | import { getPluginTestPath, installPlugin, setAccessTokensToServers } from '../../../shared/extra-utils' | ||
7 | |||
8 | const expect = chai.expect | ||
9 | |||
10 | describe('Test plugin helpers', function () { | ||
11 | let server: ServerInfo | ||
12 | |||
13 | before(async function () { | ||
14 | this.timeout(30000) | ||
15 | |||
16 | server = await flushAndRunServer(1) | ||
17 | await setAccessTokensToServers([ server ]) | ||
18 | |||
19 | await installPlugin({ | ||
20 | url: server.url, | ||
21 | accessToken: server.accessToken, | ||
22 | path: getPluginTestPath('-four') | ||
23 | }) | ||
24 | }) | ||
25 | |||
26 | it('Should have logged things', async function () { | ||
27 | await waitUntilLog(server, 'localhost:' + server.port + ' peertube-plugin-test-four', 1, false) | ||
28 | await waitUntilLog(server, 'Hello world from plugin four', 1) | ||
29 | }) | ||
30 | |||
31 | it('Should have made a query', async function () { | ||
32 | await waitUntilLog(server, `root email is admin${server.internalServerNumber}@example.com`, 1) | ||
33 | }) | ||
34 | |||
35 | after(async function () { | ||
36 | await cleanupTests([ server ]) | ||
37 | }) | ||
38 | }) | ||
diff --git a/server/typings/plugins/register-server-option.model.ts b/server/typings/plugins/register-server-option.model.ts index 76ac3cb9a..6f2ba7cd6 100644 --- a/server/typings/plugins/register-server-option.model.ts +++ b/server/typings/plugins/register-server-option.model.ts | |||
@@ -9,6 +9,10 @@ import { Logger } from 'winston' | |||
9 | 9 | ||
10 | export type PeerTubeHelpers = { | 10 | export type PeerTubeHelpers = { |
11 | logger: Logger | 11 | logger: Logger |
12 | |||
13 | database: { | ||
14 | query: Function | ||
15 | } | ||
12 | } | 16 | } |
13 | 17 | ||
14 | export type RegisterServerOptions = { | 18 | export type RegisterServerOptions = { |