WIP plugins: install/uninstall
[oweals/peertube.git] / server / models / server / plugin.ts
1 import { AllowNull, Column, CreatedAt, DataType, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
2 import { throwIfNotValid } from '../utils'
3 import {
4   isPluginDescriptionValid,
5   isPluginNameValid,
6   isPluginTypeValid,
7   isPluginVersionValid
8 } from '../../helpers/custom-validators/plugins'
9
10 @Table({
11   tableName: 'plugin',
12   indexes: [
13     {
14       fields: [ 'name' ],
15       unique: true
16     }
17   ]
18 })
19 export class PluginModel extends Model<PluginModel> {
20
21   @AllowNull(false)
22   @Is('PluginName', value => throwIfNotValid(value, isPluginNameValid, 'name'))
23   @Column
24   name: string
25
26   @AllowNull(false)
27   @Is('PluginType', value => throwIfNotValid(value, isPluginTypeValid, 'type'))
28   @Column
29   type: number
30
31   @AllowNull(false)
32   @Is('PluginVersion', value => throwIfNotValid(value, isPluginVersionValid, 'version'))
33   @Column
34   version: string
35
36   @AllowNull(false)
37   @Column
38   enabled: boolean
39
40   @AllowNull(false)
41   @Column
42   uninstalled: boolean
43
44   @AllowNull(false)
45   @Column
46   peertubeEngine: string
47
48   @AllowNull(true)
49   @Is('PluginDescription', value => throwIfNotValid(value, isPluginDescriptionValid, 'description'))
50   @Column
51   description: string
52
53   @AllowNull(true)
54   @Column(DataType.JSONB)
55   settings: any
56
57   @AllowNull(true)
58   @Column(DataType.JSONB)
59   storage: any
60
61   @CreatedAt
62   createdAt: Date
63
64   @UpdatedAt
65   updatedAt: Date
66
67   static listEnabledPluginsAndThemes () {
68     const query = {
69       where: {
70         enabled: true,
71         uninstalled: false
72       }
73     }
74
75     return PluginModel.findAll(query)
76   }
77
78   static uninstall (pluginName: string) {
79     const query = {
80       where: {
81         name: pluginName
82       }
83     }
84
85     return PluginModel.update({ enabled: false, uninstalled: true }, query)
86   }
87
88 }