app.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. const logger = require('@alias/logger')('growi:service:AppService'); // eslint-disable-line no-unused-vars
  2. const { pathUtils } = require('growi-commons');
  3. const S2sMessage = require('../models/vo/s2s-message');
  4. const S2sMessageHandlable = require('./s2s-messaging/handlable');
  5. /**
  6. * the service class of AppService
  7. */
  8. class AppService extends S2sMessageHandlable {
  9. constructor(crowi) {
  10. super();
  11. this.crowi = crowi;
  12. this.configManager = crowi.configManager;
  13. this.s2sMessagingService = crowi.s2sMessagingService;
  14. }
  15. /**
  16. * @inheritdoc
  17. */
  18. shouldHandleS2sMessage(s2sMessage) {
  19. const { eventName } = s2sMessage;
  20. if (eventName !== 'systemInstalled') {
  21. return false;
  22. }
  23. const isInstalled = this.crowi.configManager.getConfig('crowi', 'app:installed');
  24. return !isInstalled;
  25. }
  26. /**
  27. * @inheritdoc
  28. */
  29. async handleS2sMessage(s2sMessage) {
  30. logger.info('Invoke post installation process by pubsub notification');
  31. const { crowi, configManager, s2sMessagingService } = this;
  32. // load config and setup
  33. await configManager.loadConfigs();
  34. const isInstalled = this.crowi.configManager.getConfig('crowi', 'app:installed');
  35. if (isInstalled) {
  36. crowi.setupAfterInstall();
  37. // remove message handler
  38. s2sMessagingService.removeMessageHandler(this);
  39. }
  40. }
  41. async publishPostInstallationMessage() {
  42. const { s2sMessagingService } = this;
  43. if (s2sMessagingService != null) {
  44. const s2sMessage = new S2sMessage('systemInstalled');
  45. try {
  46. await s2sMessagingService.publish(s2sMessage);
  47. }
  48. catch (e) {
  49. logger.error('Failed to publish post installation message with S2sMessagingService: ', e.message);
  50. }
  51. // remove message handler
  52. s2sMessagingService.removeMessageHandler(this);
  53. }
  54. }
  55. getAppTitle() {
  56. return this.configManager.getConfig('crowi', 'app:title') || 'GROWI';
  57. }
  58. /**
  59. * get the site url
  60. *
  61. * If the config for the site url is not set, this returns a message "[The site URL is not set. Please set it!]".
  62. *
  63. * With version 3.2.3 and below, there is no config for the site URL, so the system always uses auto-generated site URL.
  64. * With version 3.2.4 to 3.3.4, the system uses the auto-generated site URL only if the config is not set.
  65. * With version 3.3.5 and above, the system use only a value from the config.
  66. */
  67. /* eslint-disable no-else-return */
  68. getSiteUrl() {
  69. const siteUrl = this.configManager.getConfig('crowi', 'app:siteUrl');
  70. if (siteUrl != null) {
  71. return pathUtils.removeTrailingSlash(siteUrl);
  72. }
  73. else {
  74. return '[The site URL is not set. Please set it!]';
  75. }
  76. }
  77. /* eslint-enable no-else-return */
  78. getTzoffset() {
  79. return -(this.configManager.getConfig('crowi', 'app:timezone') || 9) * 60;
  80. }
  81. getAppConfidential() {
  82. return this.configManager.getConfig('crowi', 'app:confidential');
  83. }
  84. /**
  85. * Execute only once for installing application
  86. */
  87. async initDB(globalLang) {
  88. const initialConfig = this.configManager.configModel.getConfigsObjectForInstalling();
  89. initialConfig['app:globalLang'] = globalLang;
  90. await this.configManager.updateConfigsInTheSameNamespace('crowi', initialConfig, true);
  91. }
  92. async isDBInitialized() {
  93. const appInstalled = await this.configManager.getConfigFromDB('crowi', 'app:installed');
  94. return appInstalled;
  95. }
  96. }
  97. module.exports = AppService;