]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/server/plugin.ts
Rename studio to editor
[github/Chocobozzz/PeerTube.git] / server / models / server / plugin.ts
index 53b6227d71708eac64c6866636cd977b2bc4d8ee..fa5b4cc4b3c51d9b99316712d57801ec8ea1b4dd 100644 (file)
@@ -1,10 +1,8 @@
-import * as Bluebird from 'bluebird'
 import { FindAndCountOptions, json, QueryTypes } from 'sequelize'
 import { AllowNull, Column, CreatedAt, DataType, DefaultScope, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
-import { MPlugin, MPluginFormattable } from '@server/typings/models'
-import { PeerTubePlugin } from '../../../shared/models/plugins/peertube-plugin.model'
-import { PluginType } from '../../../shared/models/plugins/plugin.type'
-import { RegisterServerSettingOptions } from '../../../shared/models/plugins/register-server-setting.model'
+import { MPlugin, MPluginFormattable } from '@server/types/models'
+import { AttributesOnly } from '@shared/typescript-utils'
+import { PeerTubePlugin, PluginType, RegisterServerSettingOptions, SettingEntries, SettingValue } from '../../../shared/models'
 import {
   isPluginDescriptionValid,
   isPluginHomepage,
@@ -29,7 +27,7 @@ import { getSort, throwIfNotValid } from '../utils'
     }
   ]
 })
-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)
 
@@ -150,7 +148,7 @@ export class PluginModel extends Model<PluginModel> {
 
     return PluginModel.findOne(query)
       .then(p => {
-        const result: { [settingName: string ]: string | boolean } = {}
+        const result: SettingEntries = {}
 
         for (const name of settingNames) {
           if (!p || !p.settings || p.settings[name] === undefined) {
@@ -168,7 +166,7 @@ export class PluginModel extends Model<PluginModel> {
       })
   }
 
-  static setSetting (pluginName: string, pluginType: PluginType, settingName: string, settingValue: string) {
+  static setSetting (pluginName: string, pluginType: PluginType, settingName: string, settingValue: SettingValue) {
     const query = {
       where: {
         name: pluginName,
@@ -199,15 +197,11 @@ export class PluginModel extends Model<PluginModel> {
         if (!c) return undefined
         const value = c.value
 
-        if (typeof value === 'string' && value.startsWith('{')) {
-          try {
-            return JSON.parse(value)
-          } catch {
-            return value
-          }
+        try {
+          return JSON.parse(value)
+        } catch {
+          return value
         }
-
-        return c.value
       })
   }
 
@@ -245,14 +239,13 @@ export class PluginModel extends Model<PluginModel> {
 
     if (options.pluginType) query.where['type'] = options.pluginType
 
-    return PluginModel
-      .findAndCountAll<MPlugin>(query)
-      .then(({ rows, count }) => {
-        return { total: count, data: rows }
-      })
+    return Promise.all([
+      PluginModel.count(query),
+      PluginModel.findAll<MPlugin>(query)
+    ]).then(([ total, data ]) => ({ total, data }))
   }
 
-  static listInstalled (): Bluebird<MPlugin[]> {
+  static listInstalled (): Promise<MPlugin[]> {
     const query = {
       where: {
         uninstalled: false
@@ -279,13 +272,13 @@ export class PluginModel extends Model<PluginModel> {
   }
 
   getPublicSettings (registeredSettings: RegisterServerSettingOptions[]) {
-    const result: { [ name: string ]: string } = {}
+    const result: SettingEntries = {}
     const settings = this.settings || {}
 
     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