index.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. /* eslint-disable @typescript-eslint/no-this-alias */
  2. import http from 'http';
  3. import path from 'path';
  4. import { createTerminus } from '@godaddy/terminus';
  5. import attachmentRoutes from '@growi/remark-attachment-refs/dist/server';
  6. import lsxRoutes from '@growi/remark-lsx/dist/server/index.cjs';
  7. import mongoose from 'mongoose';
  8. import next from 'next';
  9. import pkg from '^/package.json';
  10. import { KeycloakUserGroupSyncService } from '~/features/external-user-group/server/service/keycloak-user-group-sync';
  11. import { LdapUserGroupSyncService } from '~/features/external-user-group/server/service/ldap-user-group-sync';
  12. import { startCronIfEnabled as startOpenaiCronIfEnabled } from '~/features/openai/server/services/cron';
  13. import { checkPageBulkExportJobInProgressCronService } from '~/features/page-bulk-export/server/service/check-page-bulk-export-job-in-progress-cron';
  14. import instanciatePageBulkExportJobCleanUpCronService, {
  15. pageBulkExportJobCleanUpCronService,
  16. } from '~/features/page-bulk-export/server/service/page-bulk-export-job-clean-up-cron';
  17. import instanciatePageBulkExportJobCronService from '~/features/page-bulk-export/server/service/page-bulk-export-job-cron';
  18. import QuestionnaireService from '~/features/questionnaire/server/service/questionnaire';
  19. import questionnaireCronService from '~/features/questionnaire/server/service/questionnaire-cron';
  20. import loggerFactory from '~/utils/logger';
  21. import { projectRoot } from '~/utils/project-dir-utils';
  22. import UserEvent from '../events/user';
  23. import { accessTokenParser } from '../middlewares/access-token-parser';
  24. import { aclService as aclServiceSingletonInstance } from '../service/acl';
  25. import AppService from '../service/app';
  26. import AttachmentService from '../service/attachment';
  27. import { configManager as configManagerSingletonInstance } from '../service/config-manager';
  28. import instanciateExportService from '../service/export';
  29. import instanciateExternalAccountService from '../service/external-account';
  30. import { FileUploader, getUploader } from '../service/file-uploader'; // eslint-disable-line no-unused-vars
  31. import { G2GTransferPusherService, G2GTransferReceiverService } from '../service/g2g-transfer';
  32. import GrowiBridgeService from '../service/growi-bridge';
  33. import { initializeImportService } from '../service/import';
  34. import { InstallerService } from '../service/installer';
  35. import { normalizeData } from '../service/normalize-data';
  36. import PageService from '../service/page';
  37. import PageGrantService from '../service/page-grant';
  38. import PageOperationService from '../service/page-operation';
  39. import PassportService from '../service/passport';
  40. import SearchService from '../service/search';
  41. import { SlackIntegrationService } from '../service/slack-integration';
  42. import { SocketIoService } from '../service/socket-io';
  43. import UserGroupService from '../service/user-group';
  44. import { UserNotificationService } from '../service/user-notification';
  45. import { initializeYjsService } from '../service/yjs';
  46. import { getModelSafely, getMongoUri, mongoOptions } from '../util/mongoose-utils';
  47. import { setupModelsDependentOnCrowi } from './setup-models';
  48. const logger = loggerFactory('growi:crowi');
  49. const httpErrorHandler = require('../middlewares/http-error-handler');
  50. const sep = path.sep;
  51. class Crowi {
  52. /**
  53. * For retrieving other packages
  54. * @type {(req: import('express').Request, res: import('express').Response, next: import('express').NextFunction) => Promise<void>}
  55. */
  56. accessTokenParser;
  57. /** @type {AppService} */
  58. appService;
  59. /** @type {import('../service/page').IPageService} */
  60. pageService;
  61. /** @type UserNotificationService */
  62. userNotificationService;
  63. /** @type {FileUploader} */
  64. fileUploadService;
  65. /** @type {SocketIoService} */
  66. socketIoService;
  67. constructor() {
  68. this.version = pkg.version;
  69. this.publicDir = path.join(projectRoot, 'public') + sep;
  70. this.resourceDir = path.join(projectRoot, 'resource') + sep;
  71. this.localeDir = path.join(this.resourceDir, 'locales') + sep;
  72. this.viewsDir = path.resolve(__dirname, '../views') + sep;
  73. this.tmpDir = path.join(projectRoot, 'tmp') + sep;
  74. this.cacheDir = path.join(this.tmpDir, 'cache');
  75. this.express = null;
  76. this.accessTokenParser = accessTokenParser;
  77. this.config = {};
  78. this.configManager = null;
  79. this.s2sMessagingService = null;
  80. this.g2gTransferPusherService = null;
  81. this.g2gTransferReceiverService = null;
  82. this.mailService = null;
  83. this.passportService = null;
  84. this.globalNotificationService = null;
  85. this.aclService = null;
  86. this.appService = null;
  87. this.fileUploadService = null;
  88. this.restQiitaAPIService = null;
  89. this.growiBridgeService = null;
  90. this.pluginService = null;
  91. this.searchService = null;
  92. this.socketIoService = null;
  93. this.syncPageStatusService = null;
  94. this.slackIntegrationService = null;
  95. this.inAppNotificationService = null;
  96. this.activityService = null;
  97. this.commentService = null;
  98. this.questionnaireService = null;
  99. this.openaiThreadDeletionCronService = null;
  100. this.openaiVectorStoreFileDeletionCronService = null;
  101. this.tokens = null;
  102. /** @type {import('./setup-models').ModelsMapDependentOnCrowi} */
  103. this.models = {};
  104. this.env = process.env;
  105. this.node_env = this.env.NODE_ENV || 'development';
  106. this.port = this.env.PORT || 3000;
  107. this.events = {
  108. user: new UserEvent(this),
  109. page: new (require('../events/page'))(this),
  110. activity: new (require('../events/activity'))(this),
  111. bookmark: new (require('../events/bookmark'))(this),
  112. tag: new (require('../events/tag'))(this),
  113. admin: new (require('../events/admin'))(this),
  114. };
  115. }
  116. }
  117. Crowi.prototype.init = async function() {
  118. await this.setupDatabase();
  119. this.models = await setupModelsDependentOnCrowi(this);
  120. await this.setupConfigManager();
  121. await this.setupSessionConfig();
  122. this.setupCron();
  123. // setup messaging services
  124. await this.setupS2sMessagingService();
  125. await this.setupSocketIoService();
  126. // customizeService depends on AppService
  127. // passportService depends on appService
  128. // export and import depends on setUpGrowiBridge
  129. await Promise.all([
  130. this.setUpApp(),
  131. this.setUpGrowiBridge(),
  132. ]);
  133. await Promise.all([
  134. this.setupPassport(),
  135. this.setupSearcher(),
  136. this.setupMailer(),
  137. this.setupSlackIntegrationService(),
  138. this.setupG2GTransferService(),
  139. this.setUpFileUpload(),
  140. this.setUpFileUploaderSwitchService(),
  141. this.setupAttachmentService(),
  142. this.setUpAcl(),
  143. this.setUpRestQiitaAPI(),
  144. this.setupUserGroupService(),
  145. this.setupExport(),
  146. this.setupImport(),
  147. this.setupGrowiPluginService(),
  148. this.setupPageService(),
  149. this.setupInAppNotificationService(),
  150. this.setupActivityService(),
  151. this.setupCommentService(),
  152. this.setupSyncPageStatusService(),
  153. this.setupQuestionnaireService(),
  154. this.setUpCustomize(), // depends on pluginService
  155. ]);
  156. await Promise.all([
  157. // globalNotification depends on slack and mailer
  158. this.setUpGlobalNotification(),
  159. this.setUpUserNotification(),
  160. // depends on passport service
  161. this.setupExternalAccountService(),
  162. this.setupExternalUserGroupSyncService(),
  163. ]);
  164. await normalizeData();
  165. };
  166. /**
  167. * Execute functions that should be run after the express server is ready.
  168. */
  169. Crowi.prototype.asyncAfterExpressServerReady = async function() {
  170. if (this.pageOperationService != null) {
  171. await this.pageOperationService.afterExpressServerReady();
  172. }
  173. };
  174. Crowi.prototype.isPageId = function(pageId) {
  175. if (!pageId) {
  176. return false;
  177. }
  178. if (typeof pageId === 'string' && pageId.match(/^[\da-f]{24}$/)) {
  179. return true;
  180. }
  181. return false;
  182. };
  183. Crowi.prototype.setConfig = function(config) {
  184. this.config = config;
  185. };
  186. Crowi.prototype.getConfig = function() {
  187. return this.config;
  188. };
  189. Crowi.prototype.getEnv = function() {
  190. return this.env;
  191. };
  192. /**
  193. * Wrapper function of mongoose.model()
  194. * @param {string} modelName
  195. * @returns {mongoose.Model}
  196. */
  197. Crowi.prototype.model = function(modelName) {
  198. return getModelSafely(modelName);
  199. };
  200. // getter/setter of event instance
  201. Crowi.prototype.event = function(name, event) {
  202. if (event) {
  203. this.events[name] = event;
  204. }
  205. return this.events[name];
  206. };
  207. Crowi.prototype.setupDatabase = function() {
  208. mongoose.Promise = global.Promise;
  209. // mongoUri = mongodb://user:password@host/dbname
  210. const mongoUri = getMongoUri();
  211. return mongoose.connect(mongoUri, mongoOptions);
  212. };
  213. Crowi.prototype.setupSessionConfig = async function() {
  214. const session = require('express-session');
  215. const sessionMaxAge = this.configManager.getConfig('crowi', 'security:sessionMaxAge') || 2592000000; // default: 30days
  216. const redisUrl = this.env.REDISTOGO_URL || this.env.REDIS_URI || this.env.REDIS_URL || null;
  217. const uid = require('uid-safe').sync;
  218. // generate pre-defined uid for healthcheck
  219. const healthcheckUid = uid(24);
  220. const sessionConfig = {
  221. rolling: true,
  222. secret: this.env.SECRET_TOKEN || 'this is default session secret',
  223. resave: false,
  224. saveUninitialized: true,
  225. cookie: {
  226. maxAge: sessionMaxAge,
  227. },
  228. genid(req) {
  229. // return pre-defined uid when healthcheck
  230. if (req.path === '/_api/v3/healthcheck') {
  231. return healthcheckUid;
  232. }
  233. return uid(24);
  234. },
  235. };
  236. if (this.env.SESSION_NAME) {
  237. sessionConfig.name = this.env.SESSION_NAME;
  238. }
  239. // use Redis for session store
  240. if (redisUrl) {
  241. const redis = require('redis');
  242. const redisClient = redis.createClient({ url: redisUrl });
  243. const RedisStore = require('connect-redis')(session);
  244. sessionConfig.store = new RedisStore({ client: redisClient });
  245. }
  246. // use MongoDB for session store
  247. else {
  248. const MongoStore = require('connect-mongo');
  249. sessionConfig.store = MongoStore.create({ client: mongoose.connection.getClient() });
  250. }
  251. this.sessionConfig = sessionConfig;
  252. };
  253. Crowi.prototype.setupConfigManager = async function() {
  254. this.configManager = configManagerSingletonInstance;
  255. return this.configManager.loadConfigs();
  256. };
  257. Crowi.prototype.setupS2sMessagingService = async function() {
  258. const s2sMessagingService = require('../service/s2s-messaging')(this);
  259. if (s2sMessagingService != null) {
  260. s2sMessagingService.subscribe();
  261. this.configManager.setS2sMessagingService(s2sMessagingService);
  262. // add as a message handler
  263. s2sMessagingService.addMessageHandler(this.configManager);
  264. this.s2sMessagingService = s2sMessagingService;
  265. }
  266. };
  267. Crowi.prototype.setupSocketIoService = async function() {
  268. this.socketIoService = new SocketIoService(this);
  269. };
  270. Crowi.prototype.setupCron = function() {
  271. questionnaireCronService.startCron();
  272. instanciatePageBulkExportJobCronService(this);
  273. checkPageBulkExportJobInProgressCronService.startCron();
  274. instanciatePageBulkExportJobCleanUpCronService(this);
  275. pageBulkExportJobCleanUpCronService.startCron();
  276. startOpenaiCronIfEnabled();
  277. };
  278. Crowi.prototype.setupQuestionnaireService = function() {
  279. this.questionnaireService = new QuestionnaireService(this);
  280. };
  281. Crowi.prototype.getSlack = function() {
  282. return this.slack;
  283. };
  284. Crowi.prototype.getSlackLegacy = function() {
  285. return this.slackLegacy;
  286. };
  287. Crowi.prototype.getGlobalNotificationService = function() {
  288. return this.globalNotificationService;
  289. };
  290. Crowi.prototype.getUserNotificationService = function() {
  291. return this.userNotificationService;
  292. };
  293. Crowi.prototype.getRestQiitaAPIService = function() {
  294. return this.restQiitaAPIService;
  295. };
  296. Crowi.prototype.setupPassport = async function() {
  297. logger.debug('Passport is enabled');
  298. // initialize service
  299. if (this.passportService == null) {
  300. this.passportService = new PassportService(this);
  301. }
  302. this.passportService.setupSerializer();
  303. // setup strategies
  304. try {
  305. this.passportService.setupStrategyById('local');
  306. this.passportService.setupStrategyById('ldap');
  307. this.passportService.setupStrategyById('saml');
  308. this.passportService.setupStrategyById('oidc');
  309. this.passportService.setupStrategyById('google');
  310. this.passportService.setupStrategyById('github');
  311. }
  312. catch (err) {
  313. logger.error(err);
  314. }
  315. // add as a message handler
  316. if (this.s2sMessagingService != null) {
  317. this.s2sMessagingService.addMessageHandler(this.passportService);
  318. }
  319. return Promise.resolve();
  320. };
  321. Crowi.prototype.setupSearcher = async function() {
  322. this.searchService = new SearchService(this);
  323. };
  324. Crowi.prototype.setupMailer = async function() {
  325. const MailService = require('~/server/service/mail');
  326. this.mailService = new MailService(this);
  327. // add as a message handler
  328. if (this.s2sMessagingService != null) {
  329. this.s2sMessagingService.addMessageHandler(this.mailService);
  330. }
  331. };
  332. Crowi.prototype.autoInstall = async function() {
  333. const isInstalled = this.configManager.getConfig('crowi', 'app:installed');
  334. const username = this.configManager.getConfig('crowi', 'autoInstall:adminUsername');
  335. if (isInstalled || username == null) {
  336. return;
  337. }
  338. logger.info('Start automatic installation');
  339. const firstAdminUserToSave = {
  340. username,
  341. name: this.configManager.getConfig('crowi', 'autoInstall:adminName'),
  342. email: this.configManager.getConfig('crowi', 'autoInstall:adminEmail'),
  343. password: this.configManager.getConfig('crowi', 'autoInstall:adminPassword'),
  344. admin: true,
  345. };
  346. const globalLang = this.configManager.getConfig('crowi', 'autoInstall:globalLang');
  347. const allowGuestMode = this.configManager.getConfig('crowi', 'autoInstall:allowGuestMode');
  348. const serverDate = this.configManager.getConfig('crowi', 'autoInstall:serverDate');
  349. const installerService = new InstallerService(this);
  350. try {
  351. await installerService.install(firstAdminUserToSave, globalLang ?? 'en_US', {
  352. allowGuestMode,
  353. serverDate,
  354. });
  355. }
  356. catch (err) {
  357. logger.warn('Automatic installation failed.', err);
  358. }
  359. };
  360. Crowi.prototype.getTokens = function() {
  361. return this.tokens;
  362. };
  363. Crowi.prototype.start = async function() {
  364. const dev = process.env.NODE_ENV !== 'production';
  365. await this.init();
  366. await this.buildServer();
  367. // setup Next.js
  368. this.nextApp = next({ dev });
  369. await this.nextApp.prepare();
  370. // setup CrowiDev
  371. if (dev) {
  372. const CrowiDev = require('./dev');
  373. this.crowiDev = new CrowiDev(this);
  374. this.crowiDev.init();
  375. }
  376. const { express } = this;
  377. const app = (this.node_env === 'development') ? this.crowiDev.setupServer(express) : express;
  378. const httpServer = http.createServer(app);
  379. // setup terminus
  380. this.setupTerminus(httpServer);
  381. // attach to socket.io
  382. this.socketIoService.attachServer(httpServer);
  383. // Initialization YjsService
  384. initializeYjsService(this.socketIoService.io);
  385. await this.autoInstall();
  386. // listen
  387. const serverListening = httpServer.listen(this.port, () => {
  388. logger.info(`[${this.node_env}] Express server is listening on port ${this.port}`);
  389. if (this.node_env === 'development') {
  390. this.crowiDev.setupExpressAfterListening(express);
  391. }
  392. });
  393. // setup Express Routes
  394. this.setupRoutesForPlugins();
  395. this.setupRoutesAtLast();
  396. // setup Global Error Handlers
  397. this.setupGlobalErrorHandlers();
  398. // Execute this asynchronously after the express server is ready so it does not block the ongoing process
  399. this.asyncAfterExpressServerReady();
  400. return serverListening;
  401. };
  402. Crowi.prototype.buildServer = async function() {
  403. const env = this.node_env;
  404. const express = require('express')();
  405. require('./express-init')(this, express);
  406. // use bunyan
  407. if (env === 'production') {
  408. const expressBunyanLogger = require('express-bunyan-logger');
  409. const logger = loggerFactory('express');
  410. express.use(expressBunyanLogger({
  411. logger,
  412. excludes: ['*'],
  413. }));
  414. }
  415. // use morgan
  416. else {
  417. const morgan = require('morgan');
  418. express.use(morgan('dev'));
  419. }
  420. this.express = express;
  421. };
  422. Crowi.prototype.setupTerminus = function(server) {
  423. createTerminus(server, {
  424. signals: ['SIGINT', 'SIGTERM'],
  425. onSignal: async() => {
  426. logger.info('Server is starting cleanup');
  427. await mongoose.disconnect();
  428. return;
  429. },
  430. onShutdown: async() => {
  431. logger.info('Cleanup finished, server is shutting down');
  432. },
  433. });
  434. };
  435. Crowi.prototype.setupRoutesForPlugins = function() {
  436. lsxRoutes(this, this.express);
  437. attachmentRoutes(this, this.express);
  438. };
  439. /**
  440. * setup Express Routes
  441. * !! this must be at last because it includes '/*' route !!
  442. */
  443. Crowi.prototype.setupRoutesAtLast = function() {
  444. require('../routes')(this, this.express);
  445. };
  446. /**
  447. * setup global error handlers
  448. * !! this must be after the Routes setup !!
  449. */
  450. Crowi.prototype.setupGlobalErrorHandlers = function() {
  451. this.express.use(httpErrorHandler);
  452. };
  453. /**
  454. * require API for plugins
  455. *
  456. * @param {string} modulePath relative path from /lib/crowi/index.js
  457. * @return {module}
  458. *
  459. * @memberof Crowi
  460. */
  461. Crowi.prototype.require = function(modulePath) {
  462. return require(modulePath);
  463. };
  464. /**
  465. * setup GlobalNotificationService
  466. */
  467. Crowi.prototype.setUpGlobalNotification = async function() {
  468. const GlobalNotificationService = require('../service/global-notification');
  469. if (this.globalNotificationService == null) {
  470. this.globalNotificationService = new GlobalNotificationService(this);
  471. }
  472. };
  473. /**
  474. * setup UserNotificationService
  475. */
  476. Crowi.prototype.setUpUserNotification = async function() {
  477. if (this.userNotificationService == null) {
  478. this.userNotificationService = new UserNotificationService(this);
  479. }
  480. };
  481. /**
  482. * setup AclService
  483. */
  484. Crowi.prototype.setUpAcl = async function() {
  485. this.aclService = aclServiceSingletonInstance;
  486. };
  487. /**
  488. * setup CustomizeService
  489. */
  490. Crowi.prototype.setUpCustomize = async function() {
  491. const CustomizeService = require('../service/customize');
  492. if (this.customizeService == null) {
  493. this.customizeService = new CustomizeService(this);
  494. this.customizeService.initCustomCss();
  495. this.customizeService.initCustomTitle();
  496. this.customizeService.initGrowiTheme();
  497. // add as a message handler
  498. if (this.s2sMessagingService != null) {
  499. this.s2sMessagingService.addMessageHandler(this.customizeService);
  500. }
  501. }
  502. };
  503. /**
  504. * setup AppService
  505. */
  506. Crowi.prototype.setUpApp = async function() {
  507. if (this.appService == null) {
  508. this.appService = new AppService(this);
  509. // add as a message handler
  510. const isInstalled = this.configManager.getConfig('crowi', 'app:installed');
  511. if (this.s2sMessagingService != null && !isInstalled) {
  512. this.s2sMessagingService.addMessageHandler(this.appService);
  513. }
  514. }
  515. };
  516. /**
  517. * setup FileUploadService
  518. */
  519. Crowi.prototype.setUpFileUpload = async function(isForceUpdate = false) {
  520. if (this.fileUploadService == null || isForceUpdate) {
  521. this.fileUploadService = getUploader(this);
  522. }
  523. };
  524. /**
  525. * setup FileUploaderSwitchService
  526. */
  527. Crowi.prototype.setUpFileUploaderSwitchService = async function() {
  528. const FileUploaderSwitchService = require('../service/file-uploader-switch');
  529. this.fileUploaderSwitchService = new FileUploaderSwitchService(this);
  530. // add as a message handler
  531. if (this.s2sMessagingService != null) {
  532. this.s2sMessagingService.addMessageHandler(this.fileUploaderSwitchService);
  533. }
  534. };
  535. /**
  536. * setup AttachmentService
  537. */
  538. Crowi.prototype.setupAttachmentService = async function() {
  539. if (this.attachmentService == null) {
  540. this.attachmentService = new AttachmentService(this);
  541. }
  542. };
  543. /**
  544. * setup RestQiitaAPIService
  545. */
  546. Crowi.prototype.setUpRestQiitaAPI = async function() {
  547. const RestQiitaAPIService = require('../service/rest-qiita-API');
  548. if (this.restQiitaAPIService == null) {
  549. this.restQiitaAPIService = new RestQiitaAPIService(this);
  550. }
  551. };
  552. Crowi.prototype.setupUserGroupService = async function() {
  553. if (this.userGroupService == null) {
  554. this.userGroupService = new UserGroupService(this);
  555. return this.userGroupService.init();
  556. }
  557. };
  558. Crowi.prototype.setUpGrowiBridge = async function() {
  559. if (this.growiBridgeService == null) {
  560. this.growiBridgeService = new GrowiBridgeService(this);
  561. }
  562. };
  563. Crowi.prototype.setupExport = async function() {
  564. instanciateExportService(this);
  565. };
  566. Crowi.prototype.setupImport = async function() {
  567. initializeImportService(this);
  568. };
  569. Crowi.prototype.setupGrowiPluginService = async function() {
  570. const growiPluginService = await import('~/features/growi-plugin/server/services').then(mod => mod.growiPluginService);
  571. // download plugin repositories, if document exists but there is no repository
  572. // TODO: Cannot download unless connected to the Internet at setup.
  573. await growiPluginService.downloadNotExistPluginRepositories();
  574. };
  575. Crowi.prototype.setupPageService = async function() {
  576. if (this.pageGrantService == null) {
  577. this.pageGrantService = new PageGrantService(this);
  578. }
  579. // initialize after pageGrantService since pageService uses pageGrantService in constructor
  580. if (this.pageService == null) {
  581. this.pageService = new PageService(this);
  582. await this.pageService.createTtlIndex();
  583. }
  584. if (this.pageOperationService == null) {
  585. this.pageOperationService = new PageOperationService(this);
  586. await this.pageOperationService.init();
  587. }
  588. };
  589. Crowi.prototype.setupInAppNotificationService = async function() {
  590. const InAppNotificationService = require('../service/in-app-notification');
  591. if (this.inAppNotificationService == null) {
  592. this.inAppNotificationService = new InAppNotificationService(this);
  593. }
  594. };
  595. Crowi.prototype.setupActivityService = async function() {
  596. const ActivityService = require('../service/activity');
  597. if (this.activityService == null) {
  598. this.activityService = new ActivityService(this);
  599. await this.activityService.createTtlIndex();
  600. }
  601. };
  602. Crowi.prototype.setupCommentService = async function() {
  603. const CommentService = require('../service/comment');
  604. if (this.commentService == null) {
  605. this.commentService = new CommentService(this);
  606. }
  607. };
  608. Crowi.prototype.setupSyncPageStatusService = async function() {
  609. const SyncPageStatusService = require('../service/system-events/sync-page-status');
  610. if (this.syncPageStatusService == null) {
  611. this.syncPageStatusService = new SyncPageStatusService(this, this.s2sMessagingService, this.socketIoService);
  612. // add as a message handler
  613. if (this.s2sMessagingService != null) {
  614. this.s2sMessagingService.addMessageHandler(this.syncPageStatusService);
  615. }
  616. }
  617. };
  618. Crowi.prototype.setupSlackIntegrationService = async function() {
  619. if (this.slackIntegrationService == null) {
  620. this.slackIntegrationService = new SlackIntegrationService(this);
  621. }
  622. // add as a message handler
  623. if (this.s2sMessagingService != null) {
  624. this.s2sMessagingService.addMessageHandler(this.slackIntegrationService);
  625. }
  626. };
  627. Crowi.prototype.setupG2GTransferService = async function() {
  628. if (this.g2gTransferPusherService == null) {
  629. this.g2gTransferPusherService = new G2GTransferPusherService(this);
  630. }
  631. if (this.g2gTransferReceiverService == null) {
  632. this.g2gTransferReceiverService = new G2GTransferReceiverService(this);
  633. }
  634. };
  635. // execute after setupPassport
  636. Crowi.prototype.setupExternalAccountService = function() {
  637. instanciateExternalAccountService(this.passportService);
  638. };
  639. // execute after setupPassport, s2sMessagingService, socketIoService
  640. Crowi.prototype.setupExternalUserGroupSyncService = function() {
  641. this.ldapUserGroupSyncService = new LdapUserGroupSyncService(this.passportService, this.s2sMessagingService, this.socketIoService);
  642. this.keycloakUserGroupSyncService = new KeycloakUserGroupSyncService(this.s2sMessagingService, this.socketIoService);
  643. };
  644. export default Crowi;