fix(notifications): only show notification for new messages

This commit is contained in:
Evan Morikawa 2015-03-10 16:08:07 -07:00
parent f875650c27
commit 72f1035324
2 changed files with 19 additions and 2 deletions

View file

@ -5,6 +5,7 @@ module.exports =
activate: ->
@unlisteners = []
@unlisteners.push Actions.didPassivelyReceiveNewModels.listen(@_onNewMailReceived, @)
@activationTime = Date.now()
deactivate: ->
fn() for fn in @unlisteners
@ -13,8 +14,8 @@ module.exports =
_onNewMailReceived: (models) ->
# Display a notification if we've received new messages
newUnreadMessages = _.filter (models['message'] ? []), (msg) ->
msg.unread is true
newUnreadMessages = _.filter (models['message'] ? []), (msg) =>
msg.unread is true and msg.date?.valueOf() >= @activationTime
if newUnreadMessages.length is 1
msg = newUnreadMessages.pop()

View file

@ -5,19 +5,31 @@ Main = require '../lib/main'
describe "UnreadNotifications", ->
beforeEach ->
Main.activate()
spyOn(window, 'Notification').andCallFake ->
@msg1 = new Message
unread: true
date: new Date()
from: [new Contact(name: 'Ben', email: 'ben@example.com')]
subject: "Hello World"
@msg2 = new Message
unread: true
date: new Date()
from: [new Contact(name: 'Mark', email: 'mark@example.com')]
subject: "Hello World 2"
@msgRead = new Message
unread: false
date: new Date()
from: [new Contact(name: 'Mark', email: 'mark@example.com')]
subject: "Hello World Read Already"
@msgOld = new Message
unread: true
date: new Date(2000,1,1)
from: [new Contact(name: 'Mark', email: 'mark@example.com')]
subject: "Hello World Old"
afterEach ->
Main.deactivate()
it "should create a Notification if there is one unread message", ->
Main._onNewMailReceived({message: [@msgRead, @msg1]})
@ -39,3 +51,7 @@ describe "UnreadNotifications", ->
Main._onNewMailReceived({message: [@msgRead]})
expect(window.Notification).not.toHaveBeenCalled()
it "should not create a Notification if the new messages are actually old ones", ->
Main._onNewMailReceived({message: [@msgOld]})
expect(window.Notification).not.toHaveBeenCalled()