]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/server/plugin.ts
WIP plugins: add plugin settings/uninstall in client
[github/Chocobozzz/PeerTube.git] / server / models / server / plugin.ts
index b3b8276dffe87b7fa15b97ae7953fa1fa887cad8..60abaec6592bc0de89c79bcc2d124179e7cd98b7 100644 (file)
@@ -1,17 +1,26 @@
-import { AllowNull, Column, CreatedAt, DataType, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
-import { throwIfNotValid } from '../utils'
+import { AllowNull, Column, CreatedAt, DataType, DefaultScope, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
+import { getSort, throwIfNotValid } from '../utils'
 import {
-  isPluginDescriptionValid,
+  isPluginDescriptionValid, isPluginHomepage,
   isPluginNameValid,
   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 } from 'sequelize'
+
+@DefaultScope(() => ({
+  attributes: {
+    exclude: [ 'storage' ]
+  }
+}))
 
 @Table({
   tableName: 'plugin',
   indexes: [
     {
-      fields: [ 'name' ],
+      fields: [ 'name', 'type' ],
       unique: true
     }
   ]
@@ -50,6 +59,11 @@ export class PluginModel extends Model<PluginModel> {
   @Column
   description: string
 
+  @AllowNull(false)
+  @Is('PluginHomepage', value => throwIfNotValid(value, isPluginHomepage, 'homepage'))
+  @Column
+  homepage: string
+
   @AllowNull(true)
   @Column(DataType.JSONB)
   settings: any
@@ -75,24 +89,100 @@ export class PluginModel extends Model<PluginModel> {
     return PluginModel.findAll(query)
   }
 
-  static load (pluginName: string) {
+  static loadByNpmName (npmName: string) {
+    const name = this.normalizePluginName(npmName)
+    const type = this.getTypeFromNpmName(npmName)
+
     const query = {
+      where: {
+        name,
+        type
+      }
+    }
+
+    return PluginModel.findOne(query)
+  }
+
+  static getSetting (pluginName: string, settingName: string) {
+    const query = {
+      attributes: [ 'settings' ],
       where: {
         name: pluginName
       }
     }
 
     return PluginModel.findOne(query)
+      .then(p => p.settings)
+      .then(settings => {
+        if (!settings) return undefined
+
+        return settings[settingName]
+      })
   }
 
-  static uninstall (pluginName: string) {
+  static setSetting (pluginName: string, settingName: string, settingValue: string) {
     const query = {
       where: {
         name: pluginName
       }
     }
 
-    return PluginModel.update({ enabled: false, uninstalled: true }, query)
+    const toSave = {
+      [`settings.${settingName}`]: settingValue
+    }
+
+    return PluginModel.update(toSave, query)
+      .then(() => undefined)
+  }
+
+  static listForApi (options: {
+    type?: PluginType,
+    uninstalled?: boolean,
+    start: number,
+    count: number,
+    sort: string
+  }) {
+    const query: FindAndCountOptions = {
+      offset: options.start,
+      limit: options.count,
+      order: getSort(options.sort),
+      where: {}
+    }
+
+    if (options.type) query.where['type'] = options.type
+    if (options.uninstalled) query.where['uninstalled'] = options.uninstalled
+
+    return PluginModel
+      .findAndCountAll(query)
+      .then(({ rows, count }) => {
+        return { total: count, data: rows }
+      })
+  }
+
+  static normalizePluginName (name: string) {
+    return name.replace(/^peertube-((theme)|(plugin))-/, '')
+  }
+
+  static getTypeFromNpmName (npmName: string) {
+    return npmName.startsWith('peertube-plugin-')
+      ? PluginType.PLUGIN
+      : PluginType.THEME
+  }
+
+  toFormattedJSON (): PeerTubePlugin {
+    return {
+      name: this.name,
+      type: this.type,
+      version: this.version,
+      enabled: this.enabled,
+      uninstalled: this.uninstalled,
+      peertubeEngine: this.peertubeEngine,
+      description: this.description,
+      homepage: this.homepage,
+      settings: this.settings,
+      createdAt: this.createdAt,
+      updatedAt: this.updatedAt
+    }
   }
 
 }