]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/server/plugin.ts
Add getSettings documentation
[github/Chocobozzz/PeerTube.git] / server / models / server / plugin.ts
index f39b97ef00fa9b1059f5bee30793e4a3bd338cc4..83c873c5bad8dabc86c476e0c868a87f835e874e 100644 (file)
@@ -10,7 +10,9 @@ import {
 import { PluginType } from '../../../shared/models/plugins/plugin.type'
 import { PeerTubePlugin } from '../../../shared/models/plugins/peertube-plugin.model'
 import { FindAndCountOptions, json } from 'sequelize'
-import { PeerTubePluginIndex } from '../../../shared/models/plugins/peertube-plugin-index.model'
+import { RegisterServerSettingOptions } from '../../../shared/models/plugins/register-server-setting.model'
+import * as Bluebird from 'bluebird'
+import { MPlugin, MPluginFormattable } from '@server/typings/models'
 
 @DefaultScope(() => ({
   attributes: {
@@ -85,7 +87,7 @@ export class PluginModel extends Model<PluginModel> {
   @UpdatedAt
   updatedAt: Date
 
-  static listEnabledPluginsAndThemes () {
+  static listEnabledPluginsAndThemes (): Bluebird<MPlugin[]> {
     const query = {
       where: {
         enabled: true,
@@ -96,7 +98,7 @@ export class PluginModel extends Model<PluginModel> {
     return PluginModel.findAll(query)
   }
 
-  static loadByNpmName (npmName: string) {
+  static loadByNpmName (npmName: string): Bluebird<MPlugin> {
     const name = this.normalizePluginName(npmName)
     const type = this.getTypeFromNpmName(npmName)
 
@@ -127,6 +129,31 @@ export class PluginModel extends Model<PluginModel> {
       })
   }
 
+  static getSettings (pluginName: string, pluginType: PluginType, settingNames: string[]) {
+    const query = {
+      attributes: [ 'settings' ],
+      where: {
+        name: pluginName,
+        type: pluginType
+      }
+    }
+
+    return PluginModel.findOne(query)
+      .then(p => {
+        if (!p || !p.settings) return {}
+
+        const result: { [settingName: string ]: string } = {}
+
+        for (const key of Object.keys(p.settings)) {
+          if (settingNames.includes(key)) {
+            result[key] = p.settings[key]
+          }
+        }
+
+        return result
+      })
+  }
+
   static setSetting (pluginName: string, pluginType: PluginType, settingName: string, settingValue: string) {
     const query = {
       where: {
@@ -187,10 +214,10 @@ export class PluginModel extends Model<PluginModel> {
   }
 
   static listForApi (options: {
-    pluginType?: PluginType,
-    uninstalled?: boolean,
-    start: number,
-    count: number,
+    pluginType?: PluginType
+    uninstalled?: boolean
+    start: number
+    count: number
     sort: string
   }) {
     const { uninstalled = false } = options
@@ -206,13 +233,13 @@ export class PluginModel extends Model<PluginModel> {
     if (options.pluginType) query.where['type'] = options.pluginType
 
     return PluginModel
-      .findAndCountAll(query)
+      .findAndCountAll<MPlugin>(query)
       .then(({ rows, count }) => {
         return { total: count, data: rows }
       })
   }
 
-  static listInstalled () {
+  static listInstalled (): Bluebird<MPlugin[]> {
     const query = {
       where: {
         uninstalled: false
@@ -238,7 +265,20 @@ export class PluginModel extends Model<PluginModel> {
     return 'peertube-plugin-' + name
   }
 
-  toFormattedJSON (): PeerTubePlugin {
+  getPublicSettings (registeredSettings: RegisterServerSettingOptions[]) {
+    const result: { [ name: string ]: string } = {}
+    const settings = this.settings || {}
+
+    for (const r of registeredSettings) {
+      if (r.private !== false) continue
+
+      result[r.name] = settings[r.name] || r.default || null
+    }
+
+    return result
+  }
+
+  toFormattedJSON (this: MPluginFormattable): PeerTubePlugin {
     return {
       name: this.name,
       type: this.type,