]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/server/plugin.ts
WIP plugins: add plugin settings/uninstall in client
[github/Chocobozzz/PeerTube.git] / server / models / server / plugin.ts
1 import { AllowNull, Column, CreatedAt, DataType, DefaultScope, Is, Model, Table, UpdatedAt } from 'sequelize-typescript'
2 import { getSort, throwIfNotValid } from '../utils'
3 import {
4 isPluginDescriptionValid, isPluginHomepage,
5 isPluginNameValid,
6 isPluginTypeValid,
7 isPluginVersionValid
8 } from '../../helpers/custom-validators/plugins'
9 import { PluginType } from '../../../shared/models/plugins/plugin.type'
10 import { PeerTubePlugin } from '../../../shared/models/plugins/peertube-plugin.model'
11 import { FindAndCountOptions } from 'sequelize'
12
13 @DefaultScope(() => ({
14 attributes: {
15 exclude: [ 'storage' ]
16 }
17 }))
18
19 @Table({
20 tableName: 'plugin',
21 indexes: [
22 {
23 fields: [ 'name', 'type' ],
24 unique: true
25 }
26 ]
27 })
28 export class PluginModel extends Model<PluginModel> {
29
30 @AllowNull(false)
31 @Is('PluginName', value => throwIfNotValid(value, isPluginNameValid, 'name'))
32 @Column
33 name: string
34
35 @AllowNull(false)
36 @Is('PluginType', value => throwIfNotValid(value, isPluginTypeValid, 'type'))
37 @Column
38 type: number
39
40 @AllowNull(false)
41 @Is('PluginVersion', value => throwIfNotValid(value, isPluginVersionValid, 'version'))
42 @Column
43 version: string
44
45 @AllowNull(false)
46 @Column
47 enabled: boolean
48
49 @AllowNull(false)
50 @Column
51 uninstalled: boolean
52
53 @AllowNull(false)
54 @Column
55 peertubeEngine: string
56
57 @AllowNull(true)
58 @Is('PluginDescription', value => throwIfNotValid(value, isPluginDescriptionValid, 'description'))
59 @Column
60 description: string
61
62 @AllowNull(false)
63 @Is('PluginHomepage', value => throwIfNotValid(value, isPluginHomepage, 'homepage'))
64 @Column
65 homepage: string
66
67 @AllowNull(true)
68 @Column(DataType.JSONB)
69 settings: any
70
71 @AllowNull(true)
72 @Column(DataType.JSONB)
73 storage: any
74
75 @CreatedAt
76 createdAt: Date
77
78 @UpdatedAt
79 updatedAt: Date
80
81 static listEnabledPluginsAndThemes () {
82 const query = {
83 where: {
84 enabled: true,
85 uninstalled: false
86 }
87 }
88
89 return PluginModel.findAll(query)
90 }
91
92 static loadByNpmName (npmName: string) {
93 const name = this.normalizePluginName(npmName)
94 const type = this.getTypeFromNpmName(npmName)
95
96 const query = {
97 where: {
98 name,
99 type
100 }
101 }
102
103 return PluginModel.findOne(query)
104 }
105
106 static getSetting (pluginName: string, settingName: string) {
107 const query = {
108 attributes: [ 'settings' ],
109 where: {
110 name: pluginName
111 }
112 }
113
114 return PluginModel.findOne(query)
115 .then(p => p.settings)
116 .then(settings => {
117 if (!settings) return undefined
118
119 return settings[settingName]
120 })
121 }
122
123 static setSetting (pluginName: string, settingName: string, settingValue: string) {
124 const query = {
125 where: {
126 name: pluginName
127 }
128 }
129
130 const toSave = {
131 [`settings.${settingName}`]: settingValue
132 }
133
134 return PluginModel.update(toSave, query)
135 .then(() => undefined)
136 }
137
138 static listForApi (options: {
139 type?: PluginType,
140 uninstalled?: boolean,
141 start: number,
142 count: number,
143 sort: string
144 }) {
145 const query: FindAndCountOptions = {
146 offset: options.start,
147 limit: options.count,
148 order: getSort(options.sort),
149 where: {}
150 }
151
152 if (options.type) query.where['type'] = options.type
153 if (options.uninstalled) query.where['uninstalled'] = options.uninstalled
154
155 return PluginModel
156 .findAndCountAll(query)
157 .then(({ rows, count }) => {
158 return { total: count, data: rows }
159 })
160 }
161
162 static normalizePluginName (name: string) {
163 return name.replace(/^peertube-((theme)|(plugin))-/, '')
164 }
165
166 static getTypeFromNpmName (npmName: string) {
167 return npmName.startsWith('peertube-plugin-')
168 ? PluginType.PLUGIN
169 : PluginType.THEME
170 }
171
172 toFormattedJSON (): PeerTubePlugin {
173 return {
174 name: this.name,
175 type: this.type,
176 version: this.version,
177 enabled: this.enabled,
178 uninstalled: this.uninstalled,
179 peertubeEngine: this.peertubeEngine,
180 description: this.description,
181 homepage: this.homepage,
182 settings: this.settings,
183 createdAt: this.createdAt,
184 updatedAt: this.updatedAt
185 }
186 }
187
188 }