Add `/.well-known/matrix/client` for Matrix clients
[gitter.git] / scripts / utils / ban-user-from-room.js
blobff73f4bf60742da88229201f511f995e49495cbd
1 #!/usr/bin/env node
2 /*jslint node: true */
3 'use strict';
5 // Usage:
6 // node scripts/utils/ban-user-from-room.js --admin-username MadLittleMods --target-username EricGitterTester --room-uri grgrgress/community
7 // node scripts/utils/ban-user-from-room.js --admin-username MadLittleMods --target-username EricGitterTester --room-uri grgrgress/community --unban
9 const assert = require('assert');
10 const shutdown = require('shutdown');
11 const userService = require('gitter-web-users');
12 const policyFactory = require('gitter-web-permissions/lib/policy-factory');
13 const troupeService = require('gitter-web-rooms/lib/troupe-service');
14 const RoomWithPolicyService = require('gitter-web-rooms/lib/room-with-policy-service');
15 const { checkForMatrixUsername } = require('gitter-web-users/lib/virtual-user-service');
17 require('../../server/event-listeners').install();
19 var opts = require('yargs')
20   .option('admin-username', {
21     required: true,
22     description: 'Admin username that will show up for the ban',
23     string: true
24   })
25   .option('target-username', {
26     required: true,
27     description: 'username to ban e.g trevorah',
28     string: true
29   })
30   .option('room-uri', {
31     required: true,
32     description: 'room URI to ban user from',
33     string: true
34   })
35   .option('unban', {
36     alias: 'u',
37     type: 'boolean',
38     default: false,
39     description: 'unban user from hell'
40   })
41   .option('remove-messages', {
42     type: 'boolean',
43     default: false,
44     description: 'remove all of the messages from the user'
45   })
46   .help('help')
47   .alias('help', 'h').argv;
49 async function banUser() {
50   const adminUser = await userService.findByUsername(opts.adminUsername);
51   assert(adminUser);
53   const troupe = await troupeService.findByUri(opts.roomUri);
54   assert(troupe);
56   const policy = await policyFactory.createPolicyForRoomId(adminUser, troupe.id);
57   const roomWithPolicyService = new RoomWithPolicyService(troupe, adminUser, policy);
59   let targetUser;
60   let virtualUser;
61   if (checkForMatrixUsername(opts.targetUsername)) {
62     virtualUser = {
63       type: 'matrix',
64       externalId: opts.targetUsername
65     };
66   } else {
67     targetUser = await userService.findByUsername(opts.targetUsername);
68     assert(targetUser);
69   }
71   if (opts.unban) {
72     console.log('Unbanning user...');
73     if (virtualUser) {
74       return roomWithPolicyService.unbanVirtualUserFromRoom(virtualUser);
75     }
77     return roomWithPolicyService.unbanUserFromRoom(targetUser.id);
78   } else {
79     console.log('Banning user...');
80     if (virtualUser) {
81       return roomWithPolicyService.banVirtualUserFromRoom(virtualUser, {
82         removeMessages: opts.removeMessages
83       });
84     }
86     return roomWithPolicyService.banUserFromRoom(targetUser.username, {
87       removeMessages: opts.removeMessages
88     });
89   }
92 (async () => {
93   try {
94     await banUser();
95     console.log(
96       `Successfully ${opts.unban ? 'unbanned' : 'banned'} ${opts.targetUsername} from ${
97         opts.roomUri
98       } (via admin ${opts.adminUsername})`
99     );
101     // wait 5 seconds to allow for asynchronous `event-listeners` to finish
102     // This isn't clean but works
103     // https://github.com/troupe/gitter-webapp/issues/580#issuecomment-147445395
104     // https://gitlab.com/gitterHQ/webapp/merge_requests/1605#note_222861592
105     console.log(`Waiting 5 seconds to allow for the asynchronous \`event-listeners\` to finish...`);
106     await new Promise(resolve => setTimeout(resolve, 5000));
107   } catch (err) {
108     console.log('Error', err, err.stack);
109   } finally {
110     shutdown.shutdownGracefully();
111   }
112 })();