]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
Add SQL query support in plugins
authorChocobozzz <me@florianbigard.com>
Thu, 9 Apr 2020 09:00:30 +0000 (11:00 +0200)
committerChocobozzz <me@florianbigard.com>
Thu, 9 Apr 2020 09:00:30 +0000 (11:00 +0200)
scripts/parse-log.ts
server/helpers/audit-logger.ts
server/lib/plugins/plugin-helpers.ts
server/lib/plugins/register-helpers.ts
server/tests/fixtures/peertube-plugin-test-four/main.js [new file with mode: 0644]
server/tests/fixtures/peertube-plugin-test-four/package.json [new file with mode: 0644]
server/tests/plugins/index.ts
server/tests/plugins/plugin-helpers.ts [new file with mode: 0644]
server/typings/plugins/register-server-option.model.ts
shared/extra-utils/server/servers.ts

index 9e6653ca3ed73d42712b289153902a10343b9b58..26049b54d993e9f24753ed26bfc9e7dbe1ec8d95 100755 (executable)
@@ -40,7 +40,7 @@ const logger = winston.createLogger({
       stderrLevels: [],
       format: winston.format.combine(
         winston.format.splat(),
-        labelFormatter,
+        labelFormatter(),
         winston.format.colorize(),
         loggerFormat
       )
index a4cfeef76c11cb0e0a50e48c29056f6ece7746a4..0bbfbc753e559a3ba89465ff18d43aac6e4268a7 100644 (file)
@@ -36,7 +36,7 @@ const auditLogger = winston.createLogger({
       maxFiles: 5,
       format: winston.format.combine(
         winston.format.timestamp(),
-        labelFormatter,
+        labelFormatter(),
         winston.format.splat(),
         jsonLoggerFormat
       )
index 36d08d84a3cee6efae837f4cc1ca8d11994eeb80..a1493c7df7d2e4ff44a5671dd868effb5457a19e 100644 (file)
@@ -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)
+  }
+}
index 58bc96f04429cba90303b35c314392c3ef428305..4c0935a05b76a9c3e9685148ae1876eee056edfb 100644 (file)
@@ -35,7 +35,7 @@ function buildRegisterHelpers (npmName: string, plugin: PluginModel): Omit<Regis
   const videoCategoryManager = buildVideoCategoryManager(npmName)
   const videoLicenceManager = buildVideoLicenceManager(npmName)
 
-  const peertubeHelpers = buildPluginHelpers(npmName, plugin)
+  const peertubeHelpers = buildPluginHelpers(npmName)
 
   return {
     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 (file)
index 0000000..9abb736
--- /dev/null
@@ -0,0 +1,27 @@
+async function register ({
+  peertubeHelpers
+}) {
+  peertubeHelpers.logger.info('Hello world from plugin four')
+
+  const username = 'root'
+  const results = await peertubeHelpers.database.query(
+    'SELECT "email" from "user" WHERE "username" = $username',
+    {
+      type: 'SELECT',
+      bind: { username }
+    }
+  )
+
+  peertubeHelpers.logger.info('root email is ' + results[0]['email'])
+}
+
+async function unregister () {
+  return
+}
+
+module.exports = {
+  register,
+  unregister
+}
+
+// ###########################################################################
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 (file)
index 0000000..dda3c7f
--- /dev/null
@@ -0,0 +1,20 @@
+{
+  "name": "peertube-plugin-test-four",
+  "version": "0.0.1",
+  "description": "Plugin test 4",
+  "engine": {
+    "peertube": ">=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": {}
+}
index f41708055f6c040c133aae6c936885781acaa1dd..9c9499a79ebad31aa570fd407cc4591772b8641b 100644 (file)
@@ -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 (file)
index 0000000..0592827
--- /dev/null
@@ -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 ])
+  })
+})
index 76ac3cb9a7fe2ca1078fbeb2537f13ad3494e726..6f2ba7cd699b241822b6b0771012df4a148ce65f 100644 (file)
@@ -9,6 +9,10 @@ import { Logger } from 'winston'
 
 export type PeerTubeHelpers = {
   logger: Logger
+
+  database: {
+    query: Function
+  }
 }
 
 export type RegisterServerOptions = {
index a0f0ce9c9792f11885e0a8ff5cd0814ab057bb65..0f883d839c08bf3eedc71cf19a020dcbf6350228 100644 (file)
@@ -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)
   }