Gitter migration: Setup redirects (rollout pt. 3)
[gitter.git] / modules / matrix-bridge / index.js
blob2576185e1cb770d70d3f2bf9b5f0bcc4a8036a73
1 'use strict';
3 const env = require('gitter-web-env');
4 const logger = env.logger;
5 const config = env.config;
6 const errorReporter = env.errorReporter;
7 const obfuscateToken = require('gitter-web-github').obfuscateToken;
9 const matrixBridge = require('./lib/matrix-bridge');
10 const GitterBridge = require('./lib/gitter-bridge');
11 const MatrixUtils = require('./lib/matrix-utils');
13 const bridgePortFromConfig = config.get('matrix:bridge:applicationServicePort');
14 const hsToken = config.get('matrix:bridge:hsToken');
15 const asToken = config.get('matrix:bridge:asToken');
17 // Ensures the bridge bot user is registered and updates its profile info.
18 async function ensureCorrectMatrixBridgeUserProfile() {
19 try {
20 const matrixUtils = new MatrixUtils(matrixBridge);
21 await matrixUtils.ensureCorrectMatrixBridgeUserProfile();
22 } catch (err) {
23 logger.error(`Failed to update the bridge user profile`, {
24 exception: err
25 });
26 errorReporter(err, { operation: 'matrixBridge.install' }, { module: 'matrix-bridge-install' });
30 const gitterBridge = new GitterBridge(matrixBridge);
32 async function install(bridgePort = bridgePortFromConfig) {
33 if (!bridgePort || !hsToken || !asToken) {
34 logger.error(
35 `No (bridgePort=${bridgePort}, hsToken=${obfuscateToken(hsToken)}, asToken=${obfuscateToken(
36 asToken
37 )}) specified for Matrix bridge so we won't start it up`
39 return;
42 await matrixBridge.run(bridgePort);
43 logger.info(`Matrix bridge listening on port ${bridgePort}`);
45 await gitterBridge.start();
47 // Fire and forget this (no need to hold up the process by awaiting it)
48 ensureCorrectMatrixBridgeUserProfile();
50 return async function stop() {
51 await matrixBridge.close();
52 await gitterBridge.stop();
56 module.exports = install;