growi-plugin.ts 3.1 KB

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