]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/server/plugin.ts
Merge branch 'release/3.2.0' into develop
[github/Chocobozzz/PeerTube.git] / server / models / server / plugin.ts
index 95774a4674634bf8609cedc5bdd5ff619054180e..a8de64dd4854b079d7d41d7d8a18a8807cff1975 100644 (file)
@@ -1,5 +1,8 @@
+import { FindAndCountOptions, json, QueryTypes } from 'sequelize'
 import { AllowNull, Column, CreatedAt, DataType, DefaultScope, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
-import { getSort, throwIfNotValid } from '../utils'
+import { MPlugin, MPluginFormattable } from '@server/types/models'
+import { AttributesOnly } from '@shared/core-utils'
+import { PeerTubePlugin, PluginType, RegisterServerSettingOptions } from '../../../shared/models'
 import {
   isPluginDescriptionValid,
   isPluginHomepage,
@@ -7,12 +10,7 @@ import {
   isPluginTypeValid,
   isPluginVersionValid
 } from '../../helpers/custom-validators/plugins'
-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'
+import { getSort, throwIfNotValid } from '../utils'
 
 @DefaultScope(() => ({
   attributes: {
@@ -29,7 +27,7 @@ import { MPlugin, MPluginFormattable } from '@server/typings/models'
     }
   ]
 })
-export class PluginModel extends Model<PluginModel> {
+export class PluginModel extends Model<Partial<AttributesOnly<PluginModel>>> {
 
   @AllowNull(false)
   @Is('PluginName', value => throwIfNotValid(value, isPluginNameValid, 'name'))
@@ -87,7 +85,7 @@ export class PluginModel extends Model<PluginModel> {
   @UpdatedAt
   updatedAt: Date
 
-  static listEnabledPluginsAndThemes (): Bluebird<MPlugin[]> {
+  static listEnabledPluginsAndThemes (): Promise<MPlugin[]> {
     const query = {
       where: {
         enabled: true,
@@ -98,7 +96,7 @@ export class PluginModel extends Model<PluginModel> {
     return PluginModel.findAll(query)
   }
 
-  static loadByNpmName (npmName: string): Bluebird<MPlugin> {
+  static loadByNpmName (npmName: string): Promise<MPlugin> {
     const name = this.normalizePluginName(npmName)
     const type = this.getTypeFromNpmName(npmName)
 
@@ -112,7 +110,7 @@ export class PluginModel extends Model<PluginModel> {
     return PluginModel.findOne(query)
   }
 
-  static getSetting (pluginName: string, pluginType: PluginType, settingName: string) {
+  static getSetting (pluginName: string, pluginType: PluginType, settingName: string, registeredSettings: RegisterServerSettingOptions[]) {
     const query = {
       attributes: [ 'settings' ],
       where: {
@@ -123,12 +121,51 @@ export class PluginModel extends Model<PluginModel> {
 
     return PluginModel.findOne(query)
       .then(p => {
-        if (!p || !p.settings) return undefined
+        if (!p || !p.settings || p.settings === undefined) {
+          const registered = registeredSettings.find(s => s.name === settingName)
+          if (!registered || registered.default === undefined) return undefined
+
+          return registered.default
+        }
 
         return p.settings[settingName]
       })
   }
 
+  static getSettings (
+    pluginName: string,
+    pluginType: PluginType,
+    settingNames: string[],
+    registeredSettings: RegisterServerSettingOptions[]
+  ) {
+    const query = {
+      attributes: [ 'settings' ],
+      where: {
+        name: pluginName,
+        type: pluginType
+      }
+    }
+
+    return PluginModel.findOne(query)
+      .then(p => {
+        const result: { [settingName: string ]: string | boolean } = {}
+
+        for (const name of settingNames) {
+          if (!p || !p.settings || p.settings[name] === undefined) {
+            const registered = registeredSettings.find(s => s.name === name)
+
+            if (registered?.default !== undefined) {
+              result[name] = registered.default
+            }
+          } else {
+            result[name] = p.settings[name]
+          }
+        }
+
+        return result
+      })
+  }
+
   static setSetting (pluginName: string, pluginType: PluginType, settingName: string, settingValue: string) {
     const query = {
       where: {
@@ -173,18 +210,17 @@ export class PluginModel extends Model<PluginModel> {
   }
 
   static storeData (pluginName: string, pluginType: PluginType, key: string, data: any) {
-    const query = {
-      where: {
-        name: pluginName,
-        type: pluginType
-      }
-    }
+    const query = 'UPDATE "plugin" SET "storage" = jsonb_set(coalesce("storage", \'{}\'), :key, :data::jsonb) ' +
+    'WHERE "name" = :pluginName AND "type" = :pluginType'
 
-    const toSave = {
-      [`storage.${key}`]: data
+    const jsonPath = '{' + key + '}'
+
+    const options = {
+      replacements: { pluginName, pluginType, key: jsonPath, data: JSON.stringify(data) },
+      type: QueryTypes.UPDATE
     }
 
-    return PluginModel.update(toSave, query)
+    return PluginModel.sequelize.query(query, options)
                       .then(() => undefined)
   }
 
@@ -214,7 +250,7 @@ export class PluginModel extends Model<PluginModel> {
       })
   }
 
-  static listInstalled (): Bluebird<MPlugin[]> {
+  static listInstalled (): Promise<MPlugin[]> {
     const query = {
       where: {
         uninstalled: false
@@ -247,7 +283,7 @@ export class PluginModel extends Model<PluginModel> {
     for (const r of registeredSettings) {
       if (r.private !== false) continue
 
-      result[r.name] = settings[r.name] || r.default || null
+      result[r.name] = settings[r.name] ?? r.default ?? null
     }
 
     return result