diff --git a/internal_packages/unread-notifications/lib/main.coffee b/internal_packages/unread-notifications/lib/main.coffee index 9de6b38b3..9fc58c22b 100644 --- a/internal_packages/unread-notifications/lib/main.coffee +++ b/internal_packages/unread-notifications/lib/main.coffee @@ -10,32 +10,44 @@ module.exports = @unlisteners = [] @unlisteners.push Actions.didPassivelyReceiveNewModels.listen(@_onNewMailReceived, @) @activationTime = Date.now() + @stack = [] + @stackProcessTimer = null deactivate: -> fn() for fn in @unlisteners serialize: -> - _notifyMessage: (msg) -> - body = msg.subject + _notifyAll: -> + new Notification("#{@stack.length} Unread Messages", { + tag: 'unread-update' + }) + @stack = [] + + _notifyOne: ({message, thread}) -> + body = message.subject if not body or body.length is 0 - body = msg.snippet - from = msg.from[0]?.displayName() ? "Unknown" + body = message.snippet + from = message.from[0]?.displayName() ? "Unknown" + notif = new Notification(from, { body: body tag: 'unread-update' }) - notif.onclick = => atom.displayWindow() Actions.focusCategory(CategoryStore.getStandardCategory("inbox")) - Actions.setFocus(collection: 'thread', item: @threads[msg.threadId]) + Actions.setFocus(collection: 'thread', item: thread) - _notifyMessages: (msgs) -> + _notifyMessages: -> + if @stack.length > 5 + @_notifyAll() + else if @stack.length > 0 + @_notifyOne(@stack.pop()) - return if msgs.length is 0 - @_notifyMessage(msgs.pop()) - setTimeout (=> @_notifyMessages msgs), 2000 + @stackProcessTimer = null + if @stack.length > 0 + @stackProcessTimer = setTimeout(( => @_notifyMessages()), 2000) _onNewMailReceived: (incoming) -> new Promise (resolve, reject) => @@ -59,13 +71,12 @@ module.exports = # # Note we may receive multiple unread msgs for the same thread. # Using a map and ?= to avoid repeating work. - @threads = {} + threads = {} for msg in newUnread - @threads[msg.threadId] ?= _.findWhere(incomingThreads, {id: msg.threadId}) - @threads[msg.threadId] ?= DatabaseStore.find(Thread, msg.threadId) - - Promise.props(@threads).then (threads) => + threads[msg.threadId] ?= _.findWhere(incomingThreads, {id: msg.threadId}) + threads[msg.threadId] ?= DatabaseStore.find(Thread, msg.threadId) + Promise.props(threads).then (threads) => # Filter new unread messages to just the ones in the inbox newUnreadInInbox = _.filter newUnread, (msg) -> threads[msg.threadId]?.hasCategoryName('inbox') @@ -73,11 +84,9 @@ module.exports = return resolve() if newUnreadInInbox.length is 0 atom.playSound('new_mail.ogg') - if newUnreadInInbox.length < 5 - @_notifyMessages(newUnreadInInbox) - else - new Notification("#{newUnreadInInbox.length} Unread Messages", { - tag: 'unread-update' - }) + for msg in newUnreadInInbox + @stack.push({message: msg, thread: threads[msg.threadId]}) + if not @stackProcessTimer + @_notifyMessages() resolve()