index.js 22 KB

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