growi-plugin.ts 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { GrowiThemeMetadata, GrowiThemeSchemeType } from '@growi/core';
  2. import {
  3. Schema, Model, Document, Types,
  4. } from 'mongoose';
  5. import {
  6. GrowiPlugin, GrowiPluginMeta, GrowiPluginOrigin, GrowiPluginResourceType, GrowiThemePluginMeta,
  7. } from '~/interfaces/plugin';
  8. import { getOrCreateModel } from '../util/mongoose-utils';
  9. export interface GrowiPluginDocument extends GrowiPlugin, Document {
  10. }
  11. export interface GrowiPluginModel extends Model<GrowiPluginDocument> {
  12. findEnabledPlugins(): Promise<GrowiPlugin[]>
  13. findEnabledPluginsIncludingAnyTypes(includingTypes: GrowiPluginResourceType[]): Promise<GrowiPlugin[]>
  14. activatePlugin(id: Types.ObjectId): Promise<string>
  15. deactivatePlugin(id: Types.ObjectId): Promise<string>
  16. }
  17. const growiThemeMetadataSchema = new Schema<GrowiThemeMetadata>({
  18. name: { type: String, required: true },
  19. manifestKey: { type: String, required: true },
  20. schemeType: {
  21. type: String,
  22. enum: GrowiThemeSchemeType,
  23. require: true,
  24. },
  25. bg: { type: String, required: true },
  26. topbar: { type: String, required: true },
  27. sidebar: { type: String, required: true },
  28. accent: { type: String, required: true },
  29. });
  30. const growiPluginMetaSchema = new Schema<GrowiPluginMeta|GrowiThemePluginMeta>({
  31. name: { type: String, required: true },
  32. types: {
  33. type: [String],
  34. enum: GrowiPluginResourceType,
  35. require: true,
  36. },
  37. desc: { type: String },
  38. author: { type: String },
  39. themes: [growiThemeMetadataSchema],
  40. });
  41. const growiPluginOriginSchema = new Schema<GrowiPluginOrigin>({
  42. url: { type: String },
  43. ghBranch: { type: String },
  44. ghTag: { type: String },
  45. });
  46. const growiPluginSchema = new Schema<GrowiPluginDocument, GrowiPluginModel>({
  47. isEnabled: { type: Boolean },
  48. installedPath: { type: String },
  49. origin: growiPluginOriginSchema,
  50. meta: growiPluginMetaSchema,
  51. });
  52. growiPluginSchema.statics.findEnabledPlugins = async function(): Promise<GrowiPlugin[]> {
  53. return this.find({ isEnabled: true });
  54. };
  55. growiPluginSchema.statics.findEnabledPluginsIncludingAnyTypes = async function(types: GrowiPluginResourceType[]): Promise<GrowiPlugin[]> {
  56. return this.find({
  57. isEnabled: true,
  58. 'meta.types': { $in: types },
  59. });
  60. };
  61. growiPluginSchema.statics.activatePlugin = async function(id: Types.ObjectId): Promise<string> {
  62. const growiPlugin = await this.findOneAndUpdate({ _id: id }, { isEnabled: true });
  63. if (growiPlugin == null) {
  64. const message = 'No plugin found for this ID.';
  65. throw new Error(message);
  66. }
  67. const pluginName = growiPlugin.meta.name;
  68. return pluginName;
  69. };
  70. growiPluginSchema.statics.deactivatePlugin = async function(id: Types.ObjectId): Promise<string> {
  71. const growiPlugin = await this.findOneAndUpdate({ _id: id }, { isEnabled: false });
  72. if (growiPlugin == null) {
  73. const message = 'No plugin found for this ID.';
  74. throw new Error(message);
  75. }
  76. const pluginName = growiPlugin.meta.name;
  77. return pluginName;
  78. };
  79. export default getOrCreateModel<GrowiPluginDocument, GrowiPluginModel>('GrowiPlugin', growiPluginSchema);