growi-plugin.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { GrowiPluginType } from '@growi/core';
  2. import {
  3. Schema, type Model, type Document, type Types,
  4. } from 'mongoose';
  5. import { getOrCreateModel } from '~/server/util/mongoose-utils';
  6. import type {
  7. IGrowiPlugin, IGrowiPluginMeta, IGrowiPluginMetaByType, IGrowiPluginOrigin, IGrowiTemplatePluginMeta, IGrowiThemePluginMeta,
  8. } from '../../interfaces';
  9. export interface IGrowiPluginDocument<M extends IGrowiPluginMeta = IGrowiPluginMeta> extends IGrowiPlugin<M>, Document {
  10. metaJson: IGrowiPluginMeta & IGrowiThemePluginMeta & IGrowiTemplatePluginMeta,
  11. }
  12. export interface IGrowiPluginModel extends Model<IGrowiPluginDocument> {
  13. findEnabledPlugins(): Promise<IGrowiPluginDocument[]>
  14. findEnabledPluginsByType<T extends GrowiPluginType>(type: T): Promise<IGrowiPluginDocument<IGrowiPluginMetaByType<T>>[]>
  15. activatePlugin(id: Types.ObjectId): Promise<string>
  16. deactivatePlugin(id: Types.ObjectId): Promise<string>
  17. }
  18. const growiPluginMetaSchema = new Schema<IGrowiPluginMeta & IGrowiThemePluginMeta & IGrowiTemplatePluginMeta>({
  19. name: { type: String, required: true },
  20. types: {
  21. type: [String],
  22. enum: GrowiPluginType,
  23. require: true,
  24. },
  25. desc: { type: String },
  26. author: { type: String },
  27. themes: [Map],
  28. templateSummaries: [Map],
  29. });
  30. const growiPluginOriginSchema = new Schema<IGrowiPluginOrigin>({
  31. url: { type: String },
  32. ghBranch: { type: String },
  33. ghTag: { type: String },
  34. });
  35. const growiPluginSchema = new Schema<IGrowiPluginDocument, IGrowiPluginModel>({
  36. isEnabled: { type: Boolean },
  37. installedPath: { type: String },
  38. organizationName: { type: String },
  39. origin: growiPluginOriginSchema,
  40. meta: growiPluginMetaSchema,
  41. });
  42. growiPluginSchema.statics.findEnabledPlugins = async function(): Promise<IGrowiPlugin[]> {
  43. return this.find({ isEnabled: true }).lean();
  44. };
  45. growiPluginSchema.statics.findEnabledPluginsByType = async function<T extends GrowiPluginType>(
  46. type: T,
  47. ): Promise<IGrowiPlugin<IGrowiPluginMetaByType<T>>[]> {
  48. return this.find({
  49. isEnabled: true,
  50. 'meta.types': { $in: type },
  51. }).lean();
  52. };
  53. growiPluginSchema.statics.activatePlugin = async function(id: Types.ObjectId): Promise<string> {
  54. const growiPlugin = await this.findOneAndUpdate({ _id: id }, { isEnabled: true });
  55. if (growiPlugin == null) {
  56. const message = 'No plugin found for this ID.';
  57. throw new Error(message);
  58. }
  59. const pluginName = growiPlugin.meta.name;
  60. return pluginName;
  61. };
  62. growiPluginSchema.statics.deactivatePlugin = async function(id: Types.ObjectId): Promise<string> {
  63. const growiPlugin = await this.findOneAndUpdate({ _id: id }, { isEnabled: false });
  64. if (growiPlugin == null) {
  65. const message = 'No plugin found for this ID.';
  66. throw new Error(message);
  67. }
  68. const pluginName = growiPlugin.meta.name;
  69. return pluginName;
  70. };
  71. export const GrowiPlugin = getOrCreateModel<IGrowiPluginDocument, IGrowiPluginModel>('GrowiPlugin', growiPluginSchema);