From 395b7b9226d537db264c5dd403e9ec40221db01b Mon Sep 17 00:00:00 2001 From: Ben Gotow Date: Fri, 29 Jan 2016 09:29:25 -0800 Subject: [PATCH] feat(me): Display "Me (Account Label)" in unified inbox --- spec/spec-helper.coffee | 4 ++++ spec/utils-spec.coffee | 9 +++++---- src/flux/models/contact.coffee | 25 ++++++++++++++++++------- src/flux/models/message.coffee | 7 +++++-- src/flux/models/thread.coffee | 4 ++++ 5 files changed, 36 insertions(+), 13 deletions(-) diff --git a/spec/spec-helper.coffee b/spec/spec-helper.coffee index 796ec6c62..377e41230 100644 --- a/spec/spec-helper.coffee +++ b/spec/spec-helper.coffee @@ -26,6 +26,8 @@ pathwatcher = require 'pathwatcher' TaskQueue, AccountStore, DatabaseStore, + MailboxPerspective, + FocusedPerspectiveStore, ComponentRegistry} = require "nylas-exports" NylasEnv.themes.loadBaseStylesheets() @@ -170,6 +172,8 @@ beforeEach -> }) ] + FocusedPerspectiveStore._current = MailboxPerspective.forNothing() + # reset config before each spec; don't load or save from/to `config.json` spyOn(Config::, 'load') spyOn(Config::, 'save') diff --git a/spec/utils-spec.coffee b/spec/utils-spec.coffee index 54eac445f..977881b32 100644 --- a/spec/utils-spec.coffee +++ b/spec/utils-spec.coffee @@ -20,14 +20,15 @@ describe "registeredObjectReviver / registeredObjectReplacer", -> beforeEach -> @testThread = new Thread id: 'local-1' + accountId: '1' participants: [ - new Contact(id: 'local-a', name: 'Juan', email:'juan@nylas.com'), - new Contact(id: 'local-b', name: 'Ben', email:'ben@nylas.com') + new Contact(id: 'local-a', name: 'Juan', email:'juan@nylas.com', accountId: '1'), + new Contact(id: 'local-b', name: 'Ben', email:'ben@nylas.com', accountId: '1') ] subject: 'Test 1234' it "should serialize and de-serialize models correctly", -> - expectedString = '[{"client_id":"local-1","subject":"Test 1234","participants":[{"client_id":"local-a","name":"Juan","email":"juan@nylas.com","thirdPartyData":{},"id":"local-a"},{"client_id":"local-b","name":"Ben","email":"ben@nylas.com","thirdPartyData":{},"id":"local-b"}],"id":"local-1","__constructorName":"Thread"}]' + expectedString = '[{"client_id":"local-1","account_id":"1","subject":"Test 1234","participants":[{"client_id":"local-a","account_id":"1","name":"Juan","email":"juan@nylas.com","thirdPartyData":{},"id":"local-a"},{"client_id":"local-b","account_id":"1","name":"Ben","email":"ben@nylas.com","thirdPartyData":{},"id":"local-b"}],"id":"local-1","__constructorName":"Thread"}]' jsonString = JSON.stringify([@testThread], Utils.registeredObjectReplacer) expect(jsonString).toEqual(expectedString) @@ -37,7 +38,7 @@ describe "registeredObjectReviver / registeredObjectReplacer", -> it "should re-inflate Models in places they're not explicitly declared types", -> b = new JSONBlob({id: "local-ThreadsToProcess", json: [@testThread]}) jsonString = JSON.stringify(b, Utils.registeredObjectReplacer) - expectedString = '{"client_id":"local-ThreadsToProcess","server_id":"local-ThreadsToProcess","json":[{"client_id":"local-1","subject":"Test 1234","participants":[{"client_id":"local-a","name":"Juan","email":"juan@nylas.com","thirdPartyData":{},"id":"local-a"},{"client_id":"local-b","name":"Ben","email":"ben@nylas.com","thirdPartyData":{},"id":"local-b"}],"id":"local-1","__constructorName":"Thread"}],"id":"local-ThreadsToProcess","__constructorName":"JSONBlob"}' + expectedString = '{"client_id":"local-ThreadsToProcess","server_id":"local-ThreadsToProcess","json":[{"client_id":"local-1","account_id":"1","subject":"Test 1234","participants":[{"client_id":"local-a","account_id":"1","name":"Juan","email":"juan@nylas.com","thirdPartyData":{},"id":"local-a"},{"client_id":"local-b","account_id":"1","name":"Ben","email":"ben@nylas.com","thirdPartyData":{},"id":"local-b"}],"id":"local-1","__constructorName":"Thread"}],"id":"local-ThreadsToProcess","__constructorName":"JSONBlob"}' expect(jsonString).toEqual(expectedString) revived = JSON.parse(jsonString, Utils.registeredObjectReviver) diff --git a/src/flux/models/contact.coffee b/src/flux/models/contact.coffee index f3dfbacaf..47b9a5ce0 100644 --- a/src/flux/models/contact.coffee +++ b/src/flux/models/contact.coffee @@ -3,6 +3,7 @@ Utils = require './utils' Attributes = require '../attributes' RegExpUtils = require '../../regexp-utils' AccountStore = require '../stores/account-store' +FocusedPerspectiveStore = null # Circular Dependency _ = require 'underscore' name_prefixes = {} @@ -94,27 +95,37 @@ class Contact extends Model # You should use this method instead of comparing the user's email address to # the account email, since it is case-insensitive and future-proof. isMe: -> + @isMeAccount() isnt null + + isMeAccount: -> for account in AccountStore.accounts() if Utils.emailIsEquivalent(@email, account.emailAddress) - return true + return account for alias in account.aliases if Utils.emailIsEquivalent(@email, Contact.fromString(alias).email) - return true + return account - return false + return null + + isMePhrase: -> + account = @isMeAccount() + return null unless account + + FocusedPerspectiveStore ?= require '../stores/focused-perspective-store' + if account and FocusedPerspectiveStore.current().accountIds.length > 1 + return "You (#{account.label})" + return "You" # Returns a {String} display name. # - "You" if the contact is the current user # - `name` if the contact has a populated name value # - `email` in all other cases. displayName: -> - return "You" if @isMe() - @_nameParts().join(' ') + @isMePhrase() ? @_nameParts().join(' ') displayFirstName: -> - return "You" if @isMe() - @firstName() + @isMePhrase() ? @firstName() displayLastName: -> return "" if @isMe() diff --git a/src/flux/models/message.coffee b/src/flux/models/message.coffee index 8d4819d8c..202ae3cbb 100644 --- a/src/flux/models/message.coffee +++ b/src/flux/models/message.coffee @@ -192,8 +192,11 @@ class Message extends Model if json.object? @draft = (json.object is 'draft') - for file in (@files ? []) - file.accountId = @accountId + for attr in ['to', 'from', 'cc', 'bcc', 'files'] + values = @[attr] + continue unless values and values instanceof Array + item.accountId = @accountId for item in values + return @ canReplyAll: -> diff --git a/src/flux/models/thread.coffee b/src/flux/models/thread.coffee index 96a265fd1..d04b0cceb 100644 --- a/src/flux/models/thread.coffee +++ b/src/flux/models/thread.coffee @@ -109,6 +109,10 @@ class Thread extends Model if value @categories = @constructor.attributes.categories.fromJSON(value) + if @participants + for contact in @participants + contact.accountId = @accountId + @ # Public: Returns true if the thread has a {Category} with the given