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', {
22 description: 'Admin username that will show up for the ban',
25 .option('target-username', {
27 description: 'username to ban e.g trevorah',
32 description: 'room URI to ban user from',
39 description: 'unban user from hell'
41 .option('remove-messages', {
44 description: 'remove all of the messages from the user'
47 .alias('help', 'h').argv;
49 async function banUser() {
50 const adminUser = await userService.findByUsername(opts.adminUsername);
53 const troupe = await troupeService.findByUri(opts.roomUri);
56 const policy = await policyFactory.createPolicyForRoomId(adminUser, troupe.id);
57 const roomWithPolicyService = new RoomWithPolicyService(troupe, adminUser, policy);
61 if (checkForMatrixUsername(opts.targetUsername)) {
64 externalId: opts.targetUsername
67 targetUser = await userService.findByUsername(opts.targetUsername);
72 console.log('Unbanning user...');
74 return roomWithPolicyService.unbanVirtualUserFromRoom(virtualUser);
77 return roomWithPolicyService.unbanUserFromRoom(targetUser.id);
79 console.log('Banning user...');
81 return roomWithPolicyService.banVirtualUserFromRoom(virtualUser, {
82 removeMessages: opts.removeMessages
86 return roomWithPolicyService.banUserFromRoom(targetUser.username, {
87 removeMessages: opts.removeMessages
96 `Successfully ${opts.unban ? 'unbanned' : 'banned'} ${opts.targetUsername} from ${
98 } (via admin ${opts.adminUsername})`
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));
108 console.log('Error', err, err.stack);
110 shutdown.shutdownGracefully();