index.js 23 KB

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