4 const userService = require('gitter-web-users');
5 const troupeService = require('gitter-web-rooms/lib/troupe-service');
6 const chatService = require('gitter-web-chats');
7 const pushNotificationGateway = require('../../server/gateways/push-notification-gateway');
8 const serializer = require('../../server/serializers/notification-serializer');
9 const oneToOneRoomService = require('gitter-web-rooms/lib/one-to-one-room-service');
10 const shutdown = require('shutdown');
11 const Promise = require('bluebird');
13 const opts = require('yargs')
15 description: 'username to look up e.g trevorah',
20 description: 'room uri for chat'
22 .option('other-user', {
23 description: 'Other user'
26 .alias('help', 'h').argv;
28 async function findRoom(user, opts) {
30 return troupeService.findByUri(opts.roomUri);
34 const otherUser = await userService.findByUsername(opts.otherUser);
35 return oneToOneRoomService.findOneToOneRoom(user._id, otherUser._id);
38 throw new Error('Require either other user or roomUri');
41 async function exec() {
43 const user = await userService.findByUsername(opts.username).bind({});
45 const room = await findRoom(user, opts);
47 const chats = await chatService.findChatMessagesForTroupe(room._id, {
50 var troupeStrategy = new serializer.TroupeIdStrategy({ recipientUserId: user._id });
51 var chatStrategy = new serializer.ChatIdStrategy();
53 const serializedRoom = await serializer.serializeObject(room._id, troupeStrategy);
54 const serializedChats = await serializer.serialize(
55 chats.map(function(x) {
61 return pushNotificationGateway.sendUserNotification('new_chat', user.id, {
62 chats: serializedChats,
67 return Promise.try(function() {
68 throw new Error('username or appleToken required');
74 .catch(function(err) {
75 console.error(err, err.stack);
78 shutdown.shutdownGracefully();