Просмотр исходного кода

ensure that CustomizeService extends ConfigPubsubMessageHandlable

Yuki Takei 5 лет назад
Родитель
Сommit
1f55cd27f5
3 измененных файлов с 62 добавлено и 4 удалено
  1. 4 1
      src/server/crowi/index.js
  2. 6 2
      src/server/routes/apiv3/customize-setting.js
  3. 52 1
      src/server/service/customize.js

+ 4 - 1
src/server/crowi/index.js

@@ -496,9 +496,12 @@ Crowi.prototype.setUpAcl = async function() {
 Crowi.prototype.setUpCustomize = async function() {
 Crowi.prototype.setUpCustomize = async function() {
   const CustomizeService = require('../service/customize');
   const CustomizeService = require('../service/customize');
   if (this.customizeService == null) {
   if (this.customizeService == null) {
-    this.customizeService = new CustomizeService(this.configManager, this.appService, this.xssService);
+    this.customizeService = new CustomizeService(this.configManager, this.appService, this.xssService, this.configPubsub);
     this.customizeService.initCustomCss();
     this.customizeService.initCustomCss();
     this.customizeService.initCustomTitle();
     this.customizeService.initCustomTitle();
+
+    // add as a message handler
+    this.configPubsub.addMessageHandler(this.customizeService);
   }
   }
 };
 };
 
 

+ 6 - 2
src/server/routes/apiv3/customize-setting.js

@@ -375,7 +375,9 @@ module.exports = (crowi) => {
     };
     };
 
 
     try {
     try {
-      await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
+      await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams, true);
+      crowi.customizeService.publishUpdatedMessage();
+
       const customizedParams = {
       const customizedParams = {
         customizeTitle: await crowi.configManager.getConfig('crowi', 'customize:title'),
         customizeTitle: await crowi.configManager.getConfig('crowi', 'customize:title'),
       };
       };
@@ -458,7 +460,9 @@ module.exports = (crowi) => {
       'customize:css': req.body.customizeCss,
       'customize:css': req.body.customizeCss,
     };
     };
     try {
     try {
-      await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
+      await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams, true);
+      crowi.customizeService.publishUpdatedMessage();
+
       const customizedParams = {
       const customizedParams = {
         customizeCss: await crowi.configManager.getConfig('crowi', 'customize:css'),
         customizeCss: await crowi.configManager.getConfig('crowi', 'customize:css'),
       };
       };

+ 52 - 1
src/server/service/customize.js

@@ -3,15 +3,62 @@ const logger = require('@alias/logger')('growi:service:CustomizeService');
 
 
 const DevidedPagePath = require('@commons/models/devided-page-path');
 const DevidedPagePath = require('@commons/models/devided-page-path');
 
 
+const ConfigPubsubMessage = require('../models/vo/config-pubsub-message');
+const ConfigPubsubMessageHandlable = require('./config-pubsub/handlable');
+
+
 /**
 /**
  * the service class of CustomizeService
  * the service class of CustomizeService
  */
  */
-class CustomizeService {
+class CustomizeService extends ConfigPubsubMessageHandlable {
 
 
   constructor(configManager, appService, xssService) {
   constructor(configManager, appService, xssService) {
+    super();
+
     this.configManager = configManager;
     this.configManager = configManager;
     this.appService = appService;
     this.appService = appService;
     this.xssService = xssService;
     this.xssService = xssService;
+
+    this.lastLoadedAt = null;
+  }
+
+  /**
+   * @inheritdoc
+   */
+  shouldHandleConfigPubsubMessage(configPubsubMessage) {
+    const { eventName, updatedAt } = configPubsubMessage;
+    if (eventName !== 'customizeServiceUpdated' || updatedAt == null) {
+      return false;
+    }
+
+    return this.lastLoadedAt == null || this.lastLoadedAt < new Date(configPubsubMessage.updatedAt);
+  }
+
+  /**
+   * @inheritdoc
+   */
+  async handleConfigPubsubMessage(configPubsubMessage) {
+    const { configManager } = this.appService;
+
+    logger.info('Reset customized value by pubsub notification');
+    await configManager.loadConfigs();
+    this.initCustomCss();
+    this.initCustomTitle();
+  }
+
+  async publishUpdatedMessage() {
+    const { configPubsub } = this.appService;
+
+    if (configPubsub != null) {
+      const configPubsubMessage = new ConfigPubsubMessage('customizeServiceUpdated', { updatedAt: new Date() });
+
+      try {
+        await configPubsub.publish(configPubsubMessage);
+      }
+      catch (e) {
+        logger.error('Failed to publish update message with configPubsub: ', e.message);
+      }
+    }
   }
   }
 
 
   /**
   /**
@@ -24,6 +71,8 @@ class CustomizeService {
 
 
     // uglify and store
     // uglify and store
     this.customCss = uglifycss.processString(rawCss);
     this.customCss = uglifycss.processString(rawCss);
+
+    this.lastLoadedAt = new Date();
   }
   }
 
 
   getCustomCss() {
   getCustomCss() {
@@ -42,6 +91,8 @@ class CustomizeService {
     }
     }
 
 
     this.customTitleTemplate = configValue;
     this.customTitleTemplate = configValue;
+
+    this.lastLoadedAt = new Date();
   }
   }
 
 
   generateCustomTitle(pageOrPath) {
   generateCustomTitle(pageOrPath) {