4 // - Linux/macOS: matrix__bridge__applicationServicePort=9001 node ./scripts/utils/shutdown-matrix-room.js --uri MadLittleMods/delete-test1
5 // - Windows: set matrix__bridge__applicationServicePort=9001&&node ./scripts/utils/shutdown-matrix-room.js --uri MadLittleMods/delete-test1
9 const shutdown = require('shutdown');
10 const persistence = require('gitter-web-persistence');
11 const troupeService = require('gitter-web-rooms/lib/troupe-service');
13 const installBridge = require('gitter-web-matrix-bridge');
14 const matrixBridge = require('gitter-web-matrix-bridge/lib/matrix-bridge');
15 const MatrixUtils = require('gitter-web-matrix-bridge/lib/matrix-utils');
17 require('../../server/event-listeners').install();
19 const matrixUtils = new MatrixUtils(matrixBridge);
21 const opts = require('yargs')
22 .option('gitter-uri', {
25 description: 'URI of the Gitter room associated with the Matrix room ID to shutdown'
27 .option('matrix-room-id', {
30 description: 'Matrix room ID to shutdown'
33 .alias('help', 'h').argv;
35 async function getMatrixRoomIdFromArgs() {
38 const room = await troupeService.findByUri(opts.gitterUri);
40 const bridgedRoomEntry = await persistence.MatrixBridgedRoom.findOne({
45 `Found matrixRoomId=${bridgedRoomEntry.matrixRoomId}, gitterRoomId=${bridgedRoomEntry.troupeId} from gitterUri=${opts.gitterUri}`
48 matrixRoomId = bridgedRoomEntry.matrixRoomId;
49 } else if (opts.matrixRoomId) {
50 matrixRoomId = opts.matrixRoomId;
54 throw new Error('No Matrix room ID provided');
60 async function run() {
62 console.log('Setting up Matrix bridge');
63 await installBridge();
66 const matrixRoomId = await getMatrixRoomIdFromArgs();
68 console.log(`Deleting matrixRoomId=${matrixRoomId}`);
69 await matrixUtils.shutdownMatrixRoom(matrixRoomId);
70 console.log(`Matrix room deleted!`);
72 console.error(`Failed to delete Matrix room: ${err.message}`);
76 // wait 5 seconds to allow for asynchronous `event-listeners` to finish
77 // This isn't clean but works
78 // https://github.com/troupe/gitter-webapp/issues/580#issuecomment-147445395
79 // https://gitlab.com/gitterHQ/webapp/merge_requests/1605#note_222861592
80 console.log(`Waiting 5 seconds to allow for the asynchronous \`event-listeners\` to finish...`);
81 await new Promise(resolve => setTimeout(resolve, 5000));
83 console.error(err, err.stack);
85 shutdown.shutdownGracefully();