Mailspring/internal_packages/unread-notifications/lib/main.coffee

84 lines
2.6 KiB
CoffeeScript
Raw Normal View History

_ = require 'underscore'
{Thread,
Actions,
CategoryStore,
DatabaseStore,
NamespaceStore} = require 'nylas-exports'
module.exports =
activate: ->
@unlisteners = []
@unlisteners.push Actions.didPassivelyReceiveNewModels.listen(@_onNewMailReceived, @)
@activationTime = Date.now()
deactivate: ->
fn() for fn in @unlisteners
serialize: ->
_notifyMessage: (msg) ->
body = msg.subject
if not body or body.length is 0
body = msg.snippet
from = msg.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])
_notifyMessages: (msgs) ->
return if msgs.length is 0
@_notifyMessage(msgs.pop())
setTimeout (=> @_notifyMessages msgs), 2000
_onNewMailReceived: (incoming) ->
new Promise (resolve, reject) =>
incomingMessages = incoming['message'] ? []
incomingThreads = incoming['thread'] ? []
feat(*): draft icon, misc fixes, and WorkspaceStore / custom toolbar in secondary windows Summary: Features: - ThreadListParticipants ignores drafts when computing participants, renders "Draft" label, pending design - Put the WorkspaceStore in every window—means they all get toolbars and custom gumdrop icons on Mac OS X Bug Fixes: - Never display notifications for email the user just sent - Fix obscure issue with DatabaseView trying to update metadata on items it froze. This resolves issue with names remaining bold after marking as read, drafts not appearing in message list immediately. - When you pop out a draft, save it first and *wait* for the commit() promise to succeed. - If you scroll very fast, you node.contentWindow can be null in eventedIframe Other: Make it OK to re-register the same component Make it possible to unregister a hot window Break the Sheet Toolbar out into it's own file to make things manageable Replace `package.windowPropsReceived` with a store-style model where anyone can listen for changes to `windowProps` When I put the WorkspaceStore in every window, I ran into a problem because the package was no longer rendering an instance of the Composer, it was declaring a root sheet with a composer in it. This meant that it was actually a React component that needed to listen to window props, not the package itself. `atom` is already an event emitter, so I added a `onWindowPropsReceived` hook so that components can listen to window props as if they were listening to a store. I think this might be more flexible than only broadcasting the props change event to packages. Test Plan: Run tests Reviewers: evan Reviewed By: evan Differential Revision: https://phab.nylas.com/D1592
2015-06-04 07:02:19 +08:00
# Filter for new messages that are not sent by the current user
myEmail = NamespaceStore.current().emailAddress
newUnread = _.filter incomingMessages, (msg) =>
feat(*): draft icon, misc fixes, and WorkspaceStore / custom toolbar in secondary windows Summary: Features: - ThreadListParticipants ignores drafts when computing participants, renders "Draft" label, pending design - Put the WorkspaceStore in every window—means they all get toolbars and custom gumdrop icons on Mac OS X Bug Fixes: - Never display notifications for email the user just sent - Fix obscure issue with DatabaseView trying to update metadata on items it froze. This resolves issue with names remaining bold after marking as read, drafts not appearing in message list immediately. - When you pop out a draft, save it first and *wait* for the commit() promise to succeed. - If you scroll very fast, you node.contentWindow can be null in eventedIframe Other: Make it OK to re-register the same component Make it possible to unregister a hot window Break the Sheet Toolbar out into it's own file to make things manageable Replace `package.windowPropsReceived` with a store-style model where anyone can listen for changes to `windowProps` When I put the WorkspaceStore in every window, I ran into a problem because the package was no longer rendering an instance of the Composer, it was declaring a root sheet with a composer in it. This meant that it was actually a React component that needed to listen to window props, not the package itself. `atom` is already an event emitter, so I added a `onWindowPropsReceived` hook so that components can listen to window props as if they were listening to a store. I think this might be more flexible than only broadcasting the props change event to packages. Test Plan: Run tests Reviewers: evan Reviewed By: evan Differential Revision: https://phab.nylas.com/D1592
2015-06-04 07:02:19 +08:00
isUnread = msg.unread is true
isNew = msg.date?.valueOf() >= @activationTime
isFromMe = msg.from[0]?.email is myEmail
return isUnread and isNew and not isFromMe
return resolve() if newUnread.length is 0
# For each message, find it's corresponding thread. First, look to see
# if it's already in the `incoming` payload (sent via delta sync
# at the same time as the message.) If it's not, try loading it from
# the local cache.
#
# Note we may receive multiple unread msgs for the same thread.
# Using a map and ?= to avoid repeating work.
@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) =>
# Filter new unread messages to just the ones in the inbox
newUnreadInInbox = _.filter newUnread, (msg) ->
threads[msg.threadId]?.hasCategoryName('inbox')
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'
})
resolve()