index.js 23 KB

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