index.js 23 KB

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