+ const toSave = {
+ [`settings.${settingName}`]: settingValue
+ }
+
+ return PluginModel.update(toSave, query)
+ .then(() => undefined)
+ }
+
+ static getData (pluginName: string, pluginType: PluginType, key: string) {
+ const query = {
+ raw: true,
+ attributes: [ [ json('storage.' + key), 'value' ] as any ], // FIXME: typings
+ where: {
+ name: pluginName,
+ type: pluginType
+ }
+ }
+
+ return PluginModel.findOne(query)
+ .then((c: any) => {
+ if (!c) return undefined
+ const value = c.value
+
+ try {
+ return JSON.parse(value)
+ } catch {
+ return value
+ }
+ })
+ }
+
+ static storeData (pluginName: string, pluginType: PluginType, key: string, data: any) {
+ const query = 'UPDATE "plugin" SET "storage" = jsonb_set(coalesce("storage", \'{}\'), :key, :data::jsonb) ' +
+ 'WHERE "name" = :pluginName AND "type" = :pluginType'
+
+ const jsonPath = '{' + key + '}'
+
+ const options = {
+ replacements: { pluginName, pluginType, key: jsonPath, data: JSON.stringify(data) },
+ type: QueryTypes.UPDATE
+ }
+
+ return PluginModel.sequelize.query(query, options)
+ .then(() => undefined)
+ }
+
+ static listForApi (options: {
+ pluginType?: PluginType
+ uninstalled?: boolean
+ start: number
+ count: number
+ sort: string
+ }) {
+ const { uninstalled = false } = options
+ const query: FindAndCountOptions = {
+ offset: options.start,
+ limit: options.count,
+ order: getSort(options.sort),
+ where: {
+ uninstalled
+ }
+ }
+
+ if (options.pluginType) query.where['type'] = options.pluginType
+
+ return Promise.all([
+ PluginModel.count(query),
+ PluginModel.findAll<MPlugin>(query)
+ ]).then(([ total, data ]) => ({ total, data }))
+ }
+
+ static listInstalled (): Promise<MPlugin[]> {
+ const query = {
+ where: {
+ uninstalled: false
+ }
+ }
+
+ return PluginModel.findAll(query)
+ }
+
+ static normalizePluginName (npmName: string) {
+ return npmName.replace(/^peertube-((theme)|(plugin))-/, '')
+ }
+
+ static getTypeFromNpmName (npmName: string) {
+ return npmName.startsWith('peertube-plugin-')
+ ? PluginType.PLUGIN
+ : PluginType.THEME
+ }
+
+ static buildNpmName (name: string, type: PluginType) {
+ if (type === PluginType.THEME) return 'peertube-theme-' + name
+
+ return 'peertube-plugin-' + name
+ }
+
+ getPublicSettings (registeredSettings: RegisterServerSettingOptions[]) {
+ const result: SettingEntries = {}
+ const settings = this.settings || {}
+
+ for (const r of registeredSettings) {
+ if (r.private !== false) continue
+
+ result[r.name] = settings[r.name] ?? r.default ?? null
+ }
+
+ return result
+ }
+
+ toFormattedJSON (this: MPluginFormattable): PeerTubePlugin {
+ return {
+ name: this.name,
+ type: this.type,
+ version: this.version,
+ latestVersion: this.latestVersion,
+ enabled: this.enabled,
+ uninstalled: this.uninstalled,
+ peertubeEngine: this.peertubeEngine,
+ description: this.description,
+ homepage: this.homepage,
+ settings: this.settings,
+ createdAt: this.createdAt,
+ updatedAt: this.updatedAt
+ }