]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/server/plugin.ts
Fix lint
[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,
5 isPluginHomepage,
6 isPluginNameValid,
7 isPluginTypeValid,
8 isPluginVersionValid
9 } from '../../helpers/custom-validators/plugins'
10 import { PluginType } from '../../../shared/models/plugins/plugin.type'
11 import { PeerTubePlugin } from '../../../shared/models/plugins/peertube-plugin.model'
12 import { FindAndCountOptions, json } from 'sequelize'
13 import { PeerTubePluginIndex } from '../../../shared/models/plugins/peertube-plugin-index.model'
14
15 @DefaultScope(() => ({
16 attributes: {
17 exclude: [ 'storage' ]
18 }
19 }))
20
21 @Table({
22 tableName: 'plugin',
23 indexes: [
24 {
25 fields: [ 'name', 'type' ],
26 unique: true
27 }
28 ]
29 })
30 export class PluginModel extends Model<PluginModel> {
31
32 @AllowNull(false)
33 @Is('PluginName', value => throwIfNotValid(value, isPluginNameValid, 'name'))
34 @Column
35 name: string
36
37 @AllowNull(false)
38 @Is('PluginType', value => throwIfNotValid(value, isPluginTypeValid, 'type'))
39 @Column
40 type: number
41
42 @AllowNull(false)
43 @Is('PluginVersion', value => throwIfNotValid(value, isPluginVersionValid, 'version'))
44 @Column
45 version: string
46
47 @AllowNull(true)
48 @Is('PluginLatestVersion', value => throwIfNotValid(value, isPluginVersionValid, 'version'))
49 @Column
50 latestVersion: string
51
52 @AllowNull(false)
53 @Column
54 enabled: boolean
55
56 @AllowNull(false)
57 @Column
58 uninstalled: boolean
59
60 @AllowNull(false)
61 @Column
62 peertubeEngine: string
63
64 @AllowNull(true)
65 @Is('PluginDescription', value => throwIfNotValid(value, isPluginDescriptionValid, 'description'))
66 @Column
67 description: string
68
69 @AllowNull(false)
70 @Is('PluginHomepage', value => throwIfNotValid(value, isPluginHomepage, 'homepage'))
71 @Column
72 homepage: string
73
74 @AllowNull(true)
75 @Column(DataType.JSONB)
76 settings: any
77
78 @AllowNull(true)
79 @Column(DataType.JSONB)
80 storage: any
81
82 @CreatedAt
83 createdAt: Date
84
85 @UpdatedAt
86 updatedAt: Date
87
88 static listEnabledPluginsAndThemes () {
89 const query = {
90 where: {
91 enabled: true,
92 uninstalled: false
93 }
94 }
95
96 return PluginModel.findAll(query)
97 }
98
99 static loadByNpmName (npmName: string) {
100 const name = this.normalizePluginName(npmName)
101 const type = this.getTypeFromNpmName(npmName)
102
103 const query = {
104 where: {
105 name,
106 type
107 }
108 }
109
110 return PluginModel.findOne(query)
111 }
112
113 static getSetting (pluginName: string, pluginType: PluginType, settingName: string) {
114 const query = {
115 attributes: [ 'settings' ],
116 where: {
117 name: pluginName,
118 type: pluginType
119 }
120 }
121
122 return PluginModel.findOne(query)
123 .then(p => {
124 if (!p || !p.settings) return undefined
125
126 return p.settings[settingName]
127 })
128 }
129
130 static setSetting (pluginName: string, pluginType: PluginType, settingName: string, settingValue: string) {
131 const query = {
132 where: {
133 name: pluginName,
134 type: pluginType
135 }
136 }
137
138 const toSave = {
139 [`settings.${settingName}`]: settingValue
140 }
141
142 return PluginModel.update(toSave, query)
143 .then(() => undefined)
144 }
145
146 static getData (pluginName: string, pluginType: PluginType, key: string) {
147 const query = {
148 raw: true,
149 attributes: [ [ json('storage.' + key), 'value' ] as any ], // FIXME: typings
150 where: {
151 name: pluginName,
152 type: pluginType
153 }
154 }
155
156 return PluginModel.findOne(query)
157 .then((c: any) => {
158 if (!c) return undefined
159
160 return c.value
161 })
162 }
163
164 static storeData (pluginName: string, pluginType: PluginType, key: string, data: any) {
165 const query = {
166 where: {
167 name: pluginName,
168 type: pluginType
169 }
170 }
171
172 const toSave = {
173 [`storage.${key}`]: data
174 }
175
176 return PluginModel.update(toSave, query)
177 .then(() => undefined)
178 }
179
180 static listForApi (options: {
181 pluginType?: PluginType,
182 uninstalled?: boolean,
183 start: number,
184 count: number,
185 sort: string
186 }) {
187 const { uninstalled = false } = options
188 const query: FindAndCountOptions = {
189 offset: options.start,
190 limit: options.count,
191 order: getSort(options.sort),
192 where: {
193 uninstalled
194 }
195 }
196
197 if (options.pluginType) query.where['type'] = options.pluginType
198
199 return PluginModel
200 .findAndCountAll(query)
201 .then(({ rows, count }) => {
202 return { total: count, data: rows }
203 })
204 }
205
206 static listInstalled () {
207 const query = {
208 where: {
209 uninstalled: false
210 }
211 }
212
213 return PluginModel.findAll(query)
214 }
215
216 static normalizePluginName (npmName: string) {
217 return npmName.replace(/^peertube-((theme)|(plugin))-/, '')
218 }
219
220 static getTypeFromNpmName (npmName: string) {
221 return npmName.startsWith('peertube-plugin-')
222 ? PluginType.PLUGIN
223 : PluginType.THEME
224 }
225
226 static buildNpmName (name: string, type: PluginType) {
227 if (type === PluginType.THEME) return 'peertube-theme-' + name
228
229 return 'peertube-plugin-' + name
230 }
231
232 toFormattedJSON (): PeerTubePlugin {
233 return {
234 name: this.name,
235 type: this.type,
236 version: this.version,
237 latestVersion: this.latestVersion,
238 enabled: this.enabled,
239 uninstalled: this.uninstalled,
240 peertubeEngine: this.peertubeEngine,
241 description: this.description,
242 homepage: this.homepage,
243 settings: this.settings,
244 createdAt: this.createdAt,
245 updatedAt: this.updatedAt
246 }
247 }
248
249 }