5 var userService = require('gitter-web-users');
6 var autoLurkerService = require('gitter-web-rooms/lib/auto-lurker-service');
7 var troupeService = require('gitter-web-rooms/lib/troupe-service');
8 var persistence = require('gitter-web-persistence');
9 var collections = require('gitter-web-utils/lib/collections');
10 var Promise = require('bluebird');
11 var shutdown = require('shutdown');
12 var es = require('event-stream');
14 require('../../server/event-listeners').install();
16 var opts = require('yargs')
19 description: 'Room URI'
24 description: 'Minimum time in days since last login'
27 description: 'Minimum number of members in the room'
31 description: 'Just show the users who will be affected'
34 .alias('help', 'h').argv;
36 var minTimeInDays = parseInt(opts.min, 10);
37 var members = parseInt(opts.members, 10);
40 if (opts.room) return handleSingleRoom();
41 if (members) return handleMultipleRooms();
42 return Promise.reject(new Error('invalid usage'));
45 function handleRoom(troupe) {
47 ? autoLurkerService.findLurkCandidates(troupe, { minTimeInDays: minTimeInDays })
48 : autoLurkerService.autoLurkInactiveUsers(troupe, { minTimeInDays: minTimeInDays })
50 .then(function(candidates) {
51 var userIds = candidates.map(function(c) {
54 return [candidates, userService.findByIds(userIds)];
56 .spread(function(candidates, users) {
57 console.log('>>>>>>>>>>> ROOM ', troupe.uri, '> ', candidates.length, 'candidates');
58 var usersHash = collections.indexById(users);
60 candidates.forEach(function(c) {
61 var user = usersHash[c.userId];
65 username: user.username,
66 notificationSettings: c.notificationSettings,
68 lastAccessTime: c.lastAccessTime && c.lastAccessTime.toISOString()
74 function handleSingleRoom() {
75 return troupeService.findByUri(opts.room).then(handleRoom);
78 function handleMultipleRooms() {
79 return new Promise(function(resolve, reject) {
80 persistence.Troupe.find({ userCount: { $gt: members } })
81 .sort({ userCount: -1 })
85 es.through(function(room) {
89 return handleRoom(room)
90 .catch(function(err) {
91 self.emit('error', err);
99 .on('end', function() {
102 .on('error', function(err) {
109 // wait 5 seconds to allow for asynchronous `event-listeners` to finish
110 // https://github.com/troupe/gitter-webapp/issues/580#issuecomment-147445395
111 // https://gitlab.com/gitterHQ/webapp/merge_requests/1605#note_222861592
113 console.log(`Waiting 5 seconds to allow for the asynchronous \`event-listeners\` to finish...`);
114 return new Promise(resolve => setTimeout(resolve, 5000));
117 shutdown.shutdownGracefully();
119 .catch(function(err) {
120 console.error('ERROR IS ', err);
121 console.error('ERROR IS ', err.stack);
122 shutdown.shutdownGracefully(1);