From 1b05d82d861f42c27766e9f24d8d55e68b0cf098 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 9 Apr 2020 11:00:30 +0200 Subject: [PATCH] Add SQL query support in plugins --- scripts/parse-log.ts | 2 +- server/helpers/audit-logger.ts | 2 +- server/lib/plugins/plugin-helpers.ts | 20 +++++++--- server/lib/plugins/register-helpers.ts | 2 +- .../peertube-plugin-test-four/main.js | 27 +++++++++++++ .../peertube-plugin-test-four/package.json | 20 ++++++++++ server/tests/plugins/index.ts | 1 + server/tests/plugins/plugin-helpers.ts | 38 +++++++++++++++++++ .../plugins/register-server-option.model.ts | 4 ++ shared/extra-utils/server/servers.ts | 3 +- 10 files changed, 110 insertions(+), 9 deletions(-) create mode 100644 server/tests/fixtures/peertube-plugin-test-four/main.js create mode 100644 server/tests/fixtures/peertube-plugin-test-four/package.json create mode 100644 server/tests/plugins/plugin-helpers.ts diff --git a/scripts/parse-log.ts b/scripts/parse-log.ts index 9e6653ca3..26049b54d 100755 --- a/scripts/parse-log.ts +++ b/scripts/parse-log.ts @@ -40,7 +40,7 @@ const logger = winston.createLogger({ stderrLevels: [], format: winston.format.combine( winston.format.splat(), - labelFormatter, + labelFormatter(), winston.format.colorize(), loggerFormat ) 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({ maxFiles: 5, format: winston.format.combine( winston.format.timestamp(), - labelFormatter, + labelFormatter(), winston.format.splat(), jsonLoggerFormat ) 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 @@ -import { PluginModel } from '@server/models/server/plugin' import { PeerTubeHelpers } from '@server/typings/plugins' +import { sequelizeTypescript } from '@server/initializers/database' +import { buildLogger } from '@server/helpers/logger' -function buildPluginHelpers (npmName: string, plugin: PluginModel): PeerTubeHelpers { - const logger = buildLogger(npmName) +function buildPluginHelpers (npmName: string): PeerTubeHelpers { + const logger = buildPluginLogger(npmName) + + const database = buildDatabaseHelpers() return { - logger + logger, + database } } @@ -15,6 +19,12 @@ export { // --------------------------------------------------------------------------- -function buildLogger (npmName: string) { +function buildPluginLogger (npmName: string) { return buildLogger(npmName) } + +function buildDatabaseHelpers () { + return { + query: sequelizeTypescript.query.bind(sequelizeTypescript) + } +} 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=1.3.0" + }, + "keywords": [ + "peertube", + "plugin" + ], + "homepage": "https://github.com/Chocobozzz/PeerTube", + "author": "Chocobozzz", + "bugs": "https://github.com/Chocobozzz/PeerTube/issues", + "library": "./main.js", + "staticDirs": {}, + "css": [], + "clientScripts": [], + "translations": {} +} 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' import './filter-hooks' import './translations' import './video-constants' +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 @@ +/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ + +import * as chai from 'chai' +import 'mocha' +import { cleanupTests, flushAndRunServer, ServerInfo, waitUntilLog } from '../../../shared/extra-utils/server/servers' +import { getPluginTestPath, installPlugin, setAccessTokensToServers } from '../../../shared/extra-utils' + +const expect = chai.expect + +describe('Test plugin helpers', function () { + let server: ServerInfo + + before(async function () { + this.timeout(30000) + + server = await flushAndRunServer(1) + await setAccessTokensToServers([ server ]) + + await installPlugin({ + url: server.url, + accessToken: server.accessToken, + path: getPluginTestPath('-four') + }) + }) + + it('Should have logged things', async function () { + await waitUntilLog(server, 'localhost:' + server.port + ' peertube-plugin-test-four', 1, false) + await waitUntilLog(server, 'Hello world from plugin four', 1) + }) + + it('Should have made a query', async function () { + await waitUntilLog(server, `root email is admin${server.internalServerNumber}@example.com`, 1) + }) + + after(async function () { + await cleanupTests([ server ]) + }) +}) 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' export type PeerTubeHelpers = { logger: Logger + + database: { + query: Function + } } export type RegisterServerOptions = { diff --git a/shared/extra-utils/server/servers.ts b/shared/extra-utils/server/servers.ts index a0f0ce9c9..0f883d839 100644 --- a/shared/extra-utils/server/servers.ts +++ b/shared/extra-utils/server/servers.ts @@ -285,7 +285,7 @@ function cleanupTests (servers: ServerInfo[]) { return Promise.all(p) } -async function waitUntilLog (server: ServerInfo, str: string, count = 1) { +async function waitUntilLog (server: ServerInfo, str: string, count = 1, strictCount = true) { const logfile = join(root(), 'test' + server.internalServerNumber, 'logs/peertube.log') while (true) { @@ -293,6 +293,7 @@ async function waitUntilLog (server: ServerInfo, str: string, count = 1) { const matches = buf.toString().match(new RegExp(str, 'g')) if (matches && matches.length === count) return + if (matches && strictCount === false && matches.length >= count) return await wait(1000) } -- 2.41.0