]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/server/plugin.ts
Type toFormattedJSON
[github/Chocobozzz/PeerTube.git] / server / models / server / plugin.ts
index bd3d7a81ec46443ad6e11149a98febc55d9998e9..d094da1f560084b9d51d39e93364941d92a0e823 100644 (file)
@@ -10,6 +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 { RegisterServerSettingOptions } from '../../../shared/models/plugins/register-server-setting.model'
+import * as Bluebird from 'bluebird'
+import { MPlugin, MPluginFormattable } from '@server/typings/models'
 
 @DefaultScope(() => ({
   attributes: {
@@ -84,7 +87,7 @@ export class PluginModel extends Model<PluginModel> {
   @UpdatedAt
   updatedAt: Date
 
-  static listEnabledPluginsAndThemes () {
+  static listEnabledPluginsAndThemes (): Bluebird<MPlugin[]> {
     const query = {
       where: {
         enabled: true,
@@ -95,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)
 
@@ -153,8 +156,17 @@ export class PluginModel extends Model<PluginModel> {
     }
 
     return PluginModel.findOne(query)
-      .then((c: any) =>  {
+      .then((c: any) => {
         if (!c) return undefined
+        const value = c.value
+
+        if (typeof value === 'string' && value.startsWith('{')) {
+          try {
+            return JSON.parse(value)
+          } catch {
+            return value
+          }
+        }
 
         return c.value
       })
@@ -177,7 +189,7 @@ export class PluginModel extends Model<PluginModel> {
   }
 
   static listForApi (options: {
-    type?: PluginType,
+    pluginType?: PluginType,
     uninstalled?: boolean,
     start: number,
     count: number,
@@ -193,17 +205,27 @@ export class PluginModel extends Model<PluginModel> {
       }
     }
 
-    if (options.type) query.where['type'] = options.type
+    if (options.pluginType) query.where['type'] = options.pluginType
 
     return PluginModel
-      .findAndCountAll(query)
+      .findAndCountAll<MPlugin>(query)
       .then(({ rows, count }) => {
         return { total: count, data: rows }
       })
   }
 
-  static normalizePluginName (name: string) {
-    return name.replace(/^peertube-((theme)|(plugin))-/, '')
+  static listInstalled (): Bluebird<MPlugin[]> {
+    const query = {
+      where: {
+        uninstalled: false
+      }
+    }
+
+    return PluginModel.findAll(query)
+  }
+
+  static normalizePluginName (npmName: string) {
+    return npmName.replace(/^peertube-((theme)|(plugin))-/, '')
   }
 
   static getTypeFromNpmName (npmName: string) {
@@ -218,7 +240,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,