]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/server/plugin.ts
Fix socket notification with multiple user tabs
[github/Chocobozzz/PeerTube.git] / server / models / server / plugin.ts
index 340d49f3b251f68bfd5bfd85dd1f01baa5e1db12..a15f9a7e28c905dd94bc3519a6028846baf2d41d 100644 (file)
@@ -9,7 +9,8 @@ import {
 } from '../../helpers/custom-validators/plugins'
 import { PluginType } from '../../../shared/models/plugins/plugin.type'
 import { PeerTubePlugin } from '../../../shared/models/plugins/peertube-plugin.model'
-import { FindAndCountOptions } from 'sequelize'
+import { FindAndCountOptions, json } from 'sequelize'
+import { RegisterServerSettingOptions } from '../../../shared/models/plugins/register-server-setting.model'
 
 @DefaultScope(() => ({
   attributes: {
@@ -142,8 +143,51 @@ export class PluginModel extends Model<PluginModel> {
       .then(() => undefined)
   }
 
+  static getData (pluginName: string, pluginType: PluginType, key: string) {
+    const query = {
+      raw: true,
+      attributes: [ [ json('storage.' + key), 'value' ] as any ], // FIXME: typings
+      where: {
+        name: pluginName,
+        type: pluginType
+      }
+    }
+
+    return PluginModel.findOne(query)
+      .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
+      })
+  }
+
+  static storeData (pluginName: string, pluginType: PluginType, key: string, data: any) {
+    const query = {
+      where: {
+        name: pluginName,
+        type: pluginType
+      }
+    }
+
+    const toSave = {
+      [`storage.${key}`]: data
+    }
+
+    return PluginModel.update(toSave, query)
+                      .then(() => undefined)
+  }
+
   static listForApi (options: {
-    type?: PluginType,
+    pluginType?: PluginType,
     uninstalled?: boolean,
     start: number,
     count: number,
@@ -159,7 +203,7 @@ 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)
@@ -168,8 +212,18 @@ export class PluginModel extends Model<PluginModel> {
       })
   }
 
-  static normalizePluginName (name: string) {
-    return name.replace(/^peertube-((theme)|(plugin))-/, '')
+  static listInstalled () {
+    const query = {
+      where: {
+        uninstalled: false
+      }
+    }
+
+    return PluginModel.findAll(query)
+  }
+
+  static normalizePluginName (npmName: string) {
+    return npmName.replace(/^peertube-((theme)|(plugin))-/, '')
   }
 
   static getTypeFromNpmName (npmName: string) {
@@ -184,6 +238,19 @@ export class PluginModel extends Model<PluginModel> {
     return 'peertube-plugin-' + name
   }
 
+  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 (): PeerTubePlugin {
     return {
       name: this.name,