2015-05-20 07:06:59 +08:00
|
|
|
_ = require 'underscore'
|
2015-07-16 23:54:20 +08:00
|
|
|
{Thread,
|
|
|
|
Actions,
|
2015-09-05 03:23:15 +08:00
|
|
|
MailViewFilter,
|
2015-09-03 07:31:50 +08:00
|
|
|
AccountStore,
|
2015-07-16 23:54:20 +08:00
|
|
|
CategoryStore,
|
2015-10-02 10:24:06 +08:00
|
|
|
SoundRegistry,
|
2015-10-30 02:23:51 +08:00
|
|
|
NativeNotifications,
|
2015-08-19 01:18:30 +08:00
|
|
|
DatabaseStore} = require 'nylas-exports'
|
2015-02-21 04:19:34 +08:00
|
|
|
|
|
|
|
module.exports =
|
2015-08-15 06:40:11 +08:00
|
|
|
|
|
|
|
config:
|
|
|
|
enabled:
|
|
|
|
type: 'boolean'
|
|
|
|
default: true
|
|
|
|
|
2015-02-21 04:19:34 +08:00
|
|
|
activate: ->
|
|
|
|
@unlisteners = []
|
|
|
|
@unlisteners.push Actions.didPassivelyReceiveNewModels.listen(@_onNewMailReceived, @)
|
2015-03-11 07:08:07 +08:00
|
|
|
@activationTime = Date.now()
|
2015-07-17 10:28:45 +08:00
|
|
|
@stack = []
|
|
|
|
@stackProcessTimer = null
|
2015-02-21 04:19:34 +08:00
|
|
|
|
|
|
|
deactivate: ->
|
|
|
|
fn() for fn in @unlisteners
|
|
|
|
|
|
|
|
serialize: ->
|
|
|
|
|
2015-07-17 10:28:45 +08:00
|
|
|
_notifyAll: ->
|
2015-10-30 02:23:51 +08:00
|
|
|
NativeNotifications.displayNotification
|
|
|
|
title: "#{@stack.length} Unread Messages",
|
2015-07-17 10:28:45 +08:00
|
|
|
tag: 'unread-update'
|
|
|
|
@stack = []
|
|
|
|
|
|
|
|
_notifyOne: ({message, thread}) ->
|
2015-10-30 02:23:51 +08:00
|
|
|
account = _.find AccountStore.items(), (a) -> a.id is message.accountId
|
2015-07-17 10:28:45 +08:00
|
|
|
from = message.from[0]?.displayName() ? "Unknown"
|
2015-10-30 02:23:51 +08:00
|
|
|
title = from
|
|
|
|
if message.subject and message.subject.length > 0
|
|
|
|
subtitle = message.subject
|
|
|
|
body = message.snippet
|
|
|
|
else
|
|
|
|
subtitle = message.snippet
|
|
|
|
body = null
|
2015-07-17 10:28:45 +08:00
|
|
|
|
2015-10-30 02:23:51 +08:00
|
|
|
notif = NativeNotifications.displayNotification
|
|
|
|
title: title
|
|
|
|
subtitle: subtitle
|
2015-07-16 04:18:10 +08:00
|
|
|
body: body
|
2015-10-30 02:23:51 +08:00
|
|
|
canReply: true
|
2015-07-16 04:18:10 +08:00
|
|
|
tag: 'unread-update'
|
2015-10-30 02:23:51 +08:00
|
|
|
onActivate: ({tag, response, activationType}) =>
|
|
|
|
if activationType is 'replied' and response and _.isString(response)
|
|
|
|
Actions.sendQuickReply({thread, message}, response)
|
|
|
|
else
|
2015-11-12 02:25:11 +08:00
|
|
|
NylasEnv.displayWindow()
|
2015-10-30 02:23:51 +08:00
|
|
|
if AccountStore.current().id isnt thread.accountId
|
|
|
|
Actions.selectAccountId(thread.accountId)
|
2015-09-05 03:23:15 +08:00
|
|
|
|
2015-10-30 02:23:51 +08:00
|
|
|
MailViewFilter filter = MailViewFilter.forCategory(thread.categoryNamed('inbox'))
|
|
|
|
Actions.focusMailView(filter)
|
|
|
|
Actions.setFocus(collection: 'thread', item: thread)
|
2015-07-16 04:18:10 +08:00
|
|
|
|
2015-07-17 10:28:45 +08:00
|
|
|
_notifyMessages: ->
|
2015-07-17 11:34:55 +08:00
|
|
|
if @stack.length >= 5
|
2015-07-17 10:28:45 +08:00
|
|
|
@_notifyAll()
|
|
|
|
else if @stack.length > 0
|
|
|
|
@_notifyOne(@stack.pop())
|
2015-07-16 04:18:10 +08:00
|
|
|
|
2015-07-17 10:28:45 +08:00
|
|
|
@stackProcessTimer = null
|
|
|
|
if @stack.length > 0
|
|
|
|
@stackProcessTimer = setTimeout(( => @_notifyMessages()), 2000)
|
2015-07-16 04:18:10 +08:00
|
|
|
|
2015-10-29 07:55:35 +08:00
|
|
|
# https://phab.nylas.com/D2188
|
|
|
|
_onNewMessagesMissingThreads: (messages) ->
|
|
|
|
setTimeout =>
|
|
|
|
threads = {}
|
|
|
|
for msg in messages
|
|
|
|
threads[msg.threadId] ?= DatabaseStore.find(Thread, msg.threadId)
|
|
|
|
Promise.props(threads).then (threads) =>
|
|
|
|
messages = _.filter messages, (msg) =>
|
|
|
|
threads[msg.threadId]?
|
|
|
|
if messages.length > 0
|
|
|
|
@_onNewMailReceived({message: messages, thread: _.values(threads)})
|
|
|
|
, 10000
|
|
|
|
|
2015-03-28 07:37:13 +08:00
|
|
|
_onNewMailReceived: (incoming) ->
|
|
|
|
new Promise (resolve, reject) =>
|
2015-11-12 02:25:11 +08:00
|
|
|
return resolve() if NylasEnv.config.get('core.notifications.enabled') is false
|
2015-08-15 06:40:11 +08:00
|
|
|
|
2015-03-28 07:37:13 +08:00
|
|
|
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
|
2015-03-28 07:37:13 +08:00
|
|
|
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
|
2015-08-19 01:18:30 +08:00
|
|
|
isFromMe = msg.isFromMe()
|
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
|
|
|
return isUnread and isNew and not isFromMe
|
2015-03-28 07:37:13 +08:00
|
|
|
|
|
|
|
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.
|
2015-07-17 10:28:45 +08:00
|
|
|
threads = {}
|
2015-03-28 07:37:13 +08:00
|
|
|
for msg in newUnread
|
2015-07-17 10:28:45 +08:00
|
|
|
threads[msg.threadId] ?= _.findWhere(incomingThreads, {id: msg.threadId})
|
|
|
|
threads[msg.threadId] ?= DatabaseStore.find(Thread, msg.threadId)
|
2015-03-28 07:37:13 +08:00
|
|
|
|
2015-07-17 10:28:45 +08:00
|
|
|
Promise.props(threads).then (threads) =>
|
2015-03-28 07:37:13 +08:00
|
|
|
# Filter new unread messages to just the ones in the inbox
|
|
|
|
newUnreadInInbox = _.filter newUnread, (msg) ->
|
2015-10-29 07:55:35 +08:00
|
|
|
threads[msg.threadId]?.categoryNamed('inbox')
|
|
|
|
|
|
|
|
# Filter messages that we can't decide whether to display or not
|
|
|
|
# since no associated Thread object has arrived yet.
|
|
|
|
newUnreadMissingThreads = _.filter newUnread, (msg) ->
|
|
|
|
not threads[msg.threadId]?
|
|
|
|
|
|
|
|
if newUnreadMissingThreads.length > 0
|
|
|
|
@_onNewMessagesMissingThreads(newUnreadMissingThreads)
|
2015-03-28 07:37:13 +08:00
|
|
|
|
2015-07-16 04:18:10 +08:00
|
|
|
return resolve() if newUnreadInInbox.length is 0
|
2015-11-12 02:25:11 +08:00
|
|
|
if NylasEnv.config.get("core.notifications.sounds")
|
2015-10-03 08:04:15 +08:00
|
|
|
SoundRegistry.playSound('new-mail')
|
2015-03-28 07:37:13 +08:00
|
|
|
|
2015-07-17 10:28:45 +08:00
|
|
|
for msg in newUnreadInInbox
|
|
|
|
@stack.push({message: msg, thread: threads[msg.threadId]})
|
|
|
|
if not @stackProcessTimer
|
|
|
|
@_notifyMessages()
|
2015-03-28 07:37:13 +08:00
|
|
|
|
|
|
|
resolve()
|