fix(notifications): make notifications clickable

This commit is contained in:
Ben Gotow 2015-07-16 19:28:45 -07:00
parent 3983269004
commit b52077c1c3

View file

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