index.js 23 KB

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