Mailspring/internal_packages/unread-notifications/lib/main.coffee
Ben Gotow c952ea3b12 feat(notifications): Initial pass at new mail notifications
Summary:
Eventually, notification stuff should be moved out of InboxAPI into a separate package, and we should have some documented way of watching for "new" things. (Right now it'd be a bit hard to do on the database store...)

Additional fixes:
- AddRemoveTagsTask now optimistically updates the version. Before it would get the new version from the API response. This was bad because it could still cause local changes to be overwritten if you were changing tags really fast.

- AddRemoveTagsTask now takes a threadId, not a thread for consistency with all the rest of our Tasks

Test Plan: Run tests

Reviewers: evan

Reviewed By: evan

Differential Revision: https://review.inboxapp.com/D1214
2015-02-20 12:19:34 -08:00

33 lines
947 B
CoffeeScript

_ = require 'underscore-plus'
{Actions} = require 'inbox-exports'
module.exports =
activate: ->
@unlisteners = []
@unlisteners.push Actions.didPassivelyReceiveNewModels.listen(@_onNewMailReceived, @)
deactivate: ->
fn() for fn in @unlisteners
serialize: ->
_onNewMailReceived: (models) ->
# Display a notification if we've received new messages
newUnreadMessages = _.filter (models['message'] ? []), (msg) ->
msg.unread is true
if newUnreadMessages.length is 1
msg = newUnreadMessages.pop()
notif = new Notification(msg.from[0].displayName(), {
body: msg.subject
tag: 'unread-update'
})
notif.onclick = -> Actions.selectThreadId(msg.threadId)
if newUnreadMessages.length > 1
new Notification("#{newUnreadMessages.length} Unread Messages", {
tag: 'unread-update'
})
if newUnreadMessages.length > 0
atom.playSound('new_mail.ogg')