X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Fserver%2Fplugin.ts;h=9948c9f7ac27d3366acd5f6dbac41767d1c5144f;hb=5e47f6ab984a7d00782e4c7030afffa1ba480add;hp=3f88ac26d5b160ad79442fec107992cc2ff1e45b;hpb=bc90883f1a5e9c4ecb76ae358734b85be515af7f;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/server/plugin.ts b/server/models/server/plugin.ts index 3f88ac26d..9948c9f7a 100644 --- a/server/models/server/plugin.ts +++ b/server/models/server/plugin.ts @@ -1,18 +1,17 @@ -import * as Bluebird from 'bluebird' -import { FindAndCountOptions, json } from 'sequelize' +import { FindAndCountOptions, json, QueryTypes } from 'sequelize' import { AllowNull, Column, CreatedAt, DataType, DefaultScope, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' -import { MPlugin, MPluginFormattable } from '@server/typings/models' -import { PeerTubePlugin } from '../../../shared/models/plugins/peertube-plugin.model' -import { PluginType } from '../../../shared/models/plugins/plugin.type' -import { RegisterServerSettingOptions } from '../../../shared/models/plugins/register-server-setting.model' +import { MPlugin, MPluginFormattable } from '@server/types/models' +import { AttributesOnly } from '@shared/typescript-utils' +import { PeerTubePlugin, PluginType, RegisterServerSettingOptions, SettingEntries, SettingValue } from '../../../shared/models' import { isPluginDescriptionValid, isPluginHomepage, isPluginNameValid, - isPluginTypeValid, - isPluginVersionValid + isPluginStableOrUnstableVersionValid, + isPluginStableVersionValid, + isPluginTypeValid } from '../../helpers/custom-validators/plugins' -import { getSort, throwIfNotValid } from '../utils' +import { getSort, throwIfNotValid } from '../shared' @DefaultScope(() => ({ attributes: { @@ -29,7 +28,7 @@ import { getSort, throwIfNotValid } from '../utils' } ] }) -export class PluginModel extends Model { +export class PluginModel extends Model>> { @AllowNull(false) @Is('PluginName', value => throwIfNotValid(value, isPluginNameValid, 'name')) @@ -42,12 +41,12 @@ export class PluginModel extends Model { type: number @AllowNull(false) - @Is('PluginVersion', value => throwIfNotValid(value, isPluginVersionValid, 'version')) + @Is('PluginVersion', value => throwIfNotValid(value, isPluginStableOrUnstableVersionValid, 'version')) @Column version: string @AllowNull(true) - @Is('PluginLatestVersion', value => throwIfNotValid(value, isPluginVersionValid, 'version')) + @Is('PluginLatestVersion', value => throwIfNotValid(value, isPluginStableVersionValid, 'version')) @Column latestVersion: string @@ -87,7 +86,7 @@ export class PluginModel extends Model { @UpdatedAt updatedAt: Date - static listEnabledPluginsAndThemes (): Bluebird { + static listEnabledPluginsAndThemes (): Promise { const query = { where: { enabled: true, @@ -98,7 +97,7 @@ export class PluginModel extends Model { return PluginModel.findAll(query) } - static loadByNpmName (npmName: string): Bluebird { + static loadByNpmName (npmName: string): Promise { const name = this.normalizePluginName(npmName) const type = this.getTypeFromNpmName(npmName) @@ -123,7 +122,7 @@ export class PluginModel extends Model { return PluginModel.findOne(query) .then(p => { - if (!p || p.settings === undefined) { + if (!p?.settings || p.settings === undefined) { const registered = registeredSettings.find(s => s.name === settingName) if (!registered || registered.default === undefined) return undefined @@ -150,10 +149,10 @@ export class PluginModel extends Model { return PluginModel.findOne(query) .then(p => { - const result: { [settingName: string ]: string | boolean } = {} + const result: SettingEntries = {} for (const name of settingNames) { - if (!p || p.settings[name] === undefined) { + if (!p?.settings || p.settings[name] === undefined) { const registered = registeredSettings.find(s => s.name === name) if (registered?.default !== undefined) { @@ -168,7 +167,7 @@ export class PluginModel extends Model { }) } - static setSetting (pluginName: string, pluginType: PluginType, settingName: string, settingValue: string) { + static setSetting (pluginName: string, pluginType: PluginType, settingName: string, settingValue: SettingValue) { const query = { where: { name: pluginName, @@ -199,31 +198,26 @@ export class PluginModel extends Model { if (!c) return undefined const value = c.value - if (typeof value === 'string' && value.startsWith('{')) { - try { - return JSON.parse(value) - } catch { - return value - } + try { + return JSON.parse(value) + } catch { + return value } - - return c.value }) } static storeData (pluginName: string, pluginType: PluginType, key: string, data: any) { - const query = { - where: { - name: pluginName, - type: pluginType - } - } + const query = 'UPDATE "plugin" SET "storage" = jsonb_set(coalesce("storage", \'{}\'), :key, :data::jsonb) ' + + 'WHERE "name" = :pluginName AND "type" = :pluginType' - const toSave = { - [`storage.${key}`]: data + const jsonPath = '{' + key + '}' + + const options = { + replacements: { pluginName, pluginType, key: jsonPath, data: JSON.stringify(data) }, + type: QueryTypes.UPDATE } - return PluginModel.update(toSave, query) + return PluginModel.sequelize.query(query, options) .then(() => undefined) } @@ -246,14 +240,13 @@ export class PluginModel extends Model { if (options.pluginType) query.where['type'] = options.pluginType - return PluginModel - .findAndCountAll(query) - .then(({ rows, count }) => { - return { total: count, data: rows } - }) + return Promise.all([ + PluginModel.count(query), + PluginModel.findAll(query) + ]).then(([ total, data ]) => ({ total, data })) } - static listInstalled (): Bluebird { + static listInstalled (): Promise { const query = { where: { uninstalled: false @@ -280,13 +273,13 @@ export class PluginModel extends Model { } getPublicSettings (registeredSettings: RegisterServerSettingOptions[]) { - const result: { [ name: string ]: string } = {} + 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 + result[r.name] = settings[r.name] ?? r.default ?? null } return result