diff options
author | Chocobozzz <me@florianbigard.com> | 2019-07-10 16:59:53 +0200 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2019-07-24 10:58:16 +0200 |
commit | ad91e7006e41f8ee5b8dcefee30f99e8ca44133a (patch) | |
tree | d860f20e05b036fa1a96e049c74deffd7f5d2b00 /server/models | |
parent | ffb321bedca46d6987c7b31dd58e5dea96ea2ea2 (diff) | |
download | PeerTube-ad91e7006e41f8ee5b8dcefee30f99e8ca44133a.tar.gz PeerTube-ad91e7006e41f8ee5b8dcefee30f99e8ca44133a.tar.zst PeerTube-ad91e7006e41f8ee5b8dcefee30f99e8ca44133a.zip |
WIP plugins: plugin settings on server side
Diffstat (limited to 'server/models')
-rw-r--r-- | server/models/server/plugin.ts | 78 |
1 files changed, 74 insertions, 4 deletions
diff --git a/server/models/server/plugin.ts b/server/models/server/plugin.ts index b3b8276df..059a442de 100644 --- a/server/models/server/plugin.ts +++ b/server/models/server/plugin.ts | |||
@@ -1,11 +1,20 @@ | |||
1 | import { AllowNull, Column, CreatedAt, DataType, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' | 1 | import { AllowNull, Column, CreatedAt, DataType, DefaultScope, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' |
2 | import { throwIfNotValid } from '../utils' | 2 | import { getSort, throwIfNotValid } from '../utils' |
3 | import { | 3 | import { |
4 | isPluginDescriptionValid, | 4 | isPluginDescriptionValid, |
5 | isPluginNameValid, | 5 | isPluginNameValid, |
6 | isPluginTypeValid, | 6 | isPluginTypeValid, |
7 | isPluginVersionValid | 7 | isPluginVersionValid |
8 | } from '../../helpers/custom-validators/plugins' | 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 | })) | ||
9 | 18 | ||
10 | @Table({ | 19 | @Table({ |
11 | tableName: 'plugin', | 20 | tableName: 'plugin', |
@@ -85,14 +94,75 @@ export class PluginModel extends Model<PluginModel> { | |||
85 | return PluginModel.findOne(query) | 94 | return PluginModel.findOne(query) |
86 | } | 95 | } |
87 | 96 | ||
88 | static uninstall (pluginName: string) { | 97 | static getSetting (pluginName: string, settingName: string) { |
98 | const query = { | ||
99 | attributes: [ 'settings' ], | ||
100 | where: { | ||
101 | name: pluginName | ||
102 | } | ||
103 | } | ||
104 | |||
105 | return PluginModel.findOne(query) | ||
106 | .then(p => p.settings) | ||
107 | .then(settings => { | ||
108 | if (!settings) return undefined | ||
109 | |||
110 | return settings[settingName] | ||
111 | }) | ||
112 | } | ||
113 | |||
114 | static setSetting (pluginName: string, settingName: string, settingValue: string) { | ||
89 | const query = { | 115 | const query = { |
90 | where: { | 116 | where: { |
91 | name: pluginName | 117 | name: pluginName |
92 | } | 118 | } |
93 | } | 119 | } |
94 | 120 | ||
95 | return PluginModel.update({ enabled: false, uninstalled: true }, query) | 121 | const toSave = { |
122 | [`settings.${settingName}`]: settingValue | ||
123 | } | ||
124 | |||
125 | return PluginModel.update(toSave, query) | ||
126 | .then(() => undefined) | ||
127 | } | ||
128 | |||
129 | static listForApi (options: { | ||
130 | type?: PluginType, | ||
131 | uninstalled?: boolean, | ||
132 | start: number, | ||
133 | count: number, | ||
134 | sort: string | ||
135 | }) { | ||
136 | const query: FindAndCountOptions = { | ||
137 | offset: options.start, | ||
138 | limit: options.count, | ||
139 | order: getSort(options.sort), | ||
140 | where: {} | ||
141 | } | ||
142 | |||
143 | if (options.type) query.where['type'] = options.type | ||
144 | if (options.uninstalled) query.where['uninstalled'] = options.uninstalled | ||
145 | |||
146 | return PluginModel | ||
147 | .findAndCountAll(query) | ||
148 | .then(({ rows, count }) => { | ||
149 | return { total: count, data: rows } | ||
150 | }) | ||
151 | } | ||
152 | |||
153 | toFormattedJSON (): PeerTubePlugin { | ||
154 | return { | ||
155 | name: this.name, | ||
156 | type: this.type, | ||
157 | version: this.version, | ||
158 | enabled: this.enabled, | ||
159 | uninstalled: this.uninstalled, | ||
160 | peertubeEngine: this.peertubeEngine, | ||
161 | description: this.description, | ||
162 | settings: this.settings, | ||
163 | createdAt: this.createdAt, | ||
164 | updatedAt: this.updatedAt | ||
165 | } | ||
96 | } | 166 | } |
97 | 167 | ||
98 | } | 168 | } |