3 var Promise
= require('bluebird');
4 var appEvents
= require('gitter-web-appevents');
5 var resolveUserAvatarUrl
= require('gitter-web-shared/avatars/resolve-user-avatar-url');
6 var troupeDao
= require('../daos/troupe-dao').lean
;
7 var userDao
= require('../daos/user-dao').lean
;
8 var chatService
= require('gitter-web-chats');
9 var _
= require('lodash');
10 var debug
= require('debug')('gitter:app:online-notification-generator');
12 function generateChatMessageNotification(troupeId
, chatId
) {
14 chatService
.findByIdLean(chatId
, { fromUserId
: 1, virtualUser
: 1, text
: 1 }),
15 troupeDao
.findByIdRequired(troupeId
, { uri
: 1, oneToOne
: true })
17 .spread(function(chat
, troupe
) {
18 if (!chat
) throw new Error('Chat not found');
23 userDao
.findById(chat
.fromUserId
, {
31 .spread(function(chat
, troupe
, fromUser
) {
32 var oneToOne
= troupe
.oneToOne
;
33 if (!fromUser
) throw new Error('User not found');
35 let displayName
= fromUser
.displayName
;
36 if (chat
.virtualUser
) {
37 displayName
= chat
.virtualUser
.displayName
;
40 let avatarUrl
= resolveUserAvatarUrl(fromUser
, 128);
41 if (chat
.virtualUser
) {
42 avatarUrl
= chat
.virtualUser
.avatarUrl
;
49 link
: '/' + fromUser
.username
,
55 title
: displayName
+ ' 💬 ' + troupe
.uri
,
56 link
: '/' + troupe
.uri
,
63 // Takes an array of notification items, which looks like
64 exports
.sendOnlineNotifications
= Promise
.method(function(troupeId
, chatId
, userIds
) {
65 if (!userIds
.length
) return;
66 debug('sendOnlineNotifications to %s users', userIds
.length
);
68 return generateChatMessageNotification(troupeId
, chatId
).then(function(notification
) {
69 _
.forEach(userIds
, function(userId
) {
73 title
: notification
.title
,
74 text
: notification
.text
,
75 link
: notification
.link
,
76 icon
: notification
.icon
,
77 sound
: notification
.sound
,
81 debug('Online notifications: %j', n
);
82 appEvents
.userNotification(n
);