Fixes T1743: Add replyTo field handling, tests to DraftStore

Summary: Fixed T1743. Adds the replyTo field to Message objects and handling to the DraftStore. Also adds specs to make sure we don't break it.

Test Plan: Run 3 new tests

Reviewers: evan

Reviewed By: evan

Maniphest Tasks: T1743

Differential Revision: https://phab.nylas.com/D1565
This commit is contained in:
Ben Gotow 2015-05-28 18:13:46 -07:00
parent 4d6235b006
commit 736275cee1
3 changed files with 83 additions and 22 deletions

View file

@ -15,6 +15,7 @@ fakeThread = null
fakeMessage1 = null
fakeMessage2 = null
msgFromMe = null
msgWithReplyTo = null
fakeMessages = null
describe "DraftStore", ->
@ -55,10 +56,23 @@ describe "DraftStore", ->
subject: 'Re: Fake Subject'
date: new Date(1415814587)
msgWithReplyTo = new Message
id: 'fake-message-reply-to'
to: [new Contact(email: '1@1.com'), new Contact(email: '2@2.com')]
cc: [new Contact(email: '3@3.com'), new Contact(email: '4@4.com')]
bcc: [new Contact(email: '5@5.com'), new Contact(email: '6@6.com')]
replyTo: [new Contact(email: 'reply-to@5.com'), new Contact(email: 'reply-to@6.com')]
from: [new Contact(email: 'from@5.com')]
threadId: 'fake-thread-id'
body: 'Fake Message 2'
subject: 'Re: Fake Subject'
date: new Date(1415814587)
fakeMessages =
'fake-message-1': fakeMessage1
'fake-message-3': msgFromMe
'fake-message-2': fakeMessage2
'fake-message-reply-to': msgWithReplyTo
spyOn(DatabaseStore, 'find').andCallFake (klass, id) ->
query = new ModelQuery(klass, {id})
@ -92,28 +106,31 @@ describe "DraftStore", ->
it "should set the replyToMessageId to the previous message's ids", ->
expect(@model.replyToMessageId).toEqual(fakeMessage1.id)
describe "when the reply-to address is you", ->
it "on reply sends to all of the last messages's to recipients only", ->
runs ->
DraftStore._onComposeReply({threadId: fakeThread.id, messageId: msgFromMe.id})
waitsFor ->
DatabaseStore.persistModel.callCount > 0
runs ->
@model = DatabaseStore.persistModel.mostRecentCall.args[0]
expect(@model.to).toEqual(msgFromMe.to)
expect(@model.cc.length).toBe 0
expect(@model.bcc.length).toBe 0
describe "onComposeReply", ->
describe "when the message provided as context has one or more 'ReplyTo' recipients", ->
it "addresses the draft to all of the message's 'ReplyTo' recipients", ->
runs ->
DraftStore._onComposeReply({threadId: fakeThread.id, messageId: msgWithReplyTo.id})
waitsFor ->
DatabaseStore.persistModel.callCount > 0
runs ->
@model = DatabaseStore.persistModel.mostRecentCall.args[0]
expect(@model.to).toEqual(msgWithReplyTo.replyTo)
expect(@model.cc.length).toBe 0
expect(@model.bcc.length).toBe 0
it "on reply-all sends to all of the last messages's recipients", ->
runs ->
DraftStore._onComposeReplyAll({threadId: fakeThread.id, messageId: msgFromMe.id})
waitsFor ->
DatabaseStore.persistModel.callCount > 0
runs ->
@model = DatabaseStore.persistModel.mostRecentCall.args[0]
expect(@model.to).toEqual(msgFromMe.to)
expect(@model.cc).toEqual(msgFromMe.cc)
expect(@model.bcc.length).toBe 0
describe "onComposeReply", ->
describe "when the message provided as context was sent by the current user", ->
it "addresses the draft to all of the last messages's 'To' recipients", ->
runs ->
DraftStore._onComposeReply({threadId: fakeThread.id, messageId: msgFromMe.id})
waitsFor ->
DatabaseStore.persistModel.callCount > 0
runs ->
@model = DatabaseStore.persistModel.mostRecentCall.args[0]
expect(@model.to).toEqual(msgFromMe.to)
expect(@model.cc.length).toBe 0
expect(@model.bcc.length).toBe 0
describe "onComposeReplyAll", ->
beforeEach ->
@ -146,6 +163,37 @@ describe "DraftStore", ->
it "should set the replyToMessageId to the previous message's ids", ->
expect(@model.replyToMessageId).toEqual(fakeMessage1.id)
describe "onComposeReplyAll", ->
describe "when the message provided as context has one or more 'ReplyTo' recipients", ->
beforeEach ->
runs ->
DraftStore._onComposeReply({threadId: fakeThread.id, messageId: msgWithReplyTo.id})
waitsFor ->
DatabaseStore.persistModel.callCount > 0
runs ->
@model = DatabaseStore.persistModel.mostRecentCall.args[0]
it "addresses the draft to all of the message's 'ReplyTo' recipients", ->
expect(@model.to).toEqual(msgWithReplyTo.replyTo)
it "should not include the message's 'From' recipient in any field", ->
all = [].concat(@model.to, @model.cc, @model.bcc)
match = _.find all, (c) -> c.email is msgWithReplyTo.from[0].email
expect(match).toEqual(undefined)
describe "onComposeReplyAll", ->
describe "when the message provided as context was sent by the current user", ->
it "addresses the draft to all of the last messages's recipients", ->
runs ->
DraftStore._onComposeReplyAll({threadId: fakeThread.id, messageId: msgFromMe.id})
waitsFor ->
DatabaseStore.persistModel.callCount > 0
runs ->
@model = DatabaseStore.persistModel.mostRecentCall.args[0]
expect(@model.to).toEqual(msgFromMe.to)
expect(@model.cc).toEqual(msgFromMe.cc)
expect(@model.bcc.length).toBe 0
describe "onComposeForward", ->
beforeEach ->
runs ->

View file

@ -25,6 +25,8 @@ All messages are part of a thread, even if that thread has only one message.
`from`: {AttributeCollection} A collection of {Contact} objects.
`replyTo`: {AttributeCollection} A collection of {Contact} objects.
`date`: {AttributeDateTime} When the message was delivered. Queryable.
`subject`: {AttributeString} The subject of the thread. Queryable.
@ -77,6 +79,11 @@ class Message extends Model
modelKey: 'from'
itemClass: Contact
'replyTo': Attributes.Collection
modelKey: 'replyTo'
jsonKey: 'reply_to'
itemClass: Contact
'date': Attributes.DateTime
queryable: true
modelKey: 'date'
@ -131,6 +138,7 @@ class Message extends Model
@to ||= []
@cc ||= []
@bcc ||= []
@replyTo ||= []
@
toJSON: ->

View file

@ -170,6 +170,8 @@ class DraftStore
@_newMessageWithContext context, (thread, message) =>
if @_isMe(message.from[0])
to = message.to
else if message.replyTo.length
to = message.replyTo
else
to = message.from
@ -186,7 +188,10 @@ class DraftStore
else
excluded = message.from.map (c) -> c.email
excluded.push(NamespaceStore.current().me().email)
to = message.from
if message.replyTo.length
to = message.replyTo
else
to = message.from
cc = [].concat(message.cc, message.to).filter (p) ->
!_.contains(excluded, p.email)