Add ability to pass in Matrix room ID
[gitter.git] / scripts / utils / shutdown-matrix-room.js
blob6cbd834d14d31897ad2dbe894c5deea0202be26d
1 #!/usr/bin/env node
2 //
3 // Usage:
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
6 //
7 'use strict';
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', {
23     alias: 'u',
24     required: true,
25     description: 'URI of the Gitter room associated with the Matrix room ID to shutdown'
26   })
27   .option('matrix-room-id', {
28     alias: 'u',
29     required: true,
30     description: 'Matrix room ID to shutdown'
31   })
32   .help('help')
33   .alias('help', 'h').argv;
35 async function getMatrixRoomIdFromArgs() {
36   let matrixRoomId;
37   if (opts.gitterUri) {
38     const room = await troupeService.findByUri(opts.gitterUri);
40     const bridgedRoomEntry = await persistence.MatrixBridgedRoom.findOne({
41       troupeId: room._id
42     }).exec();
44     console.log(
45       `Found matrixRoomId=${bridgedRoomEntry.matrixRoomId}, gitterRoomId=${bridgedRoomEntry.troupeId} from gitterUri=${opts.gitterUri}`
46     );
48     matrixRoomId = bridgedRoomEntry.matrixRoomId;
49   } else if (opts.matrixRoomId) {
50     matrixRoomId = opts.matrixRoomId;
51   }
53   if (!matrixRoomId) {
54     throw new Error('No Matrix room ID provided');
55   }
57   return matrixRoomId;
60 async function run() {
61   try {
62     console.log('Setting up Matrix bridge');
63     await installBridge();
65     try {
66       const matrixRoomId = await getMatrixRoomIdFromArgs();
68       console.log(`Deleting matrixRoomId=${matrixRoomId}`);
69       await matrixUtils.shutdownMatrixRoom(matrixRoomId);
70       console.log(`Matrix room deleted!`);
71     } catch (err) {
72       console.error(`Failed to delete Matrix room: ${err.message}`);
73       throw err;
74     }
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));
82   } catch (err) {
83     console.error(err, err.stack);
84   }
85   shutdown.shutdownGracefully();
88 run();