From e5c6036bda7a9acdc7e70336123a17cd5249a676 Mon Sep 17 00:00:00 2001 From: Ben Gotow Date: Mon, 1 Feb 2016 19:12:00 -0800 Subject: [PATCH] fix(contact): Converge on displayName(), don't show account label in ThreadList. Fixes #1157 --- .../message-list/lib/message-item.cjsx | 2 +- .../lib/message-participants.cjsx | 2 +- .../lib/thread-list-participants.cjsx | 4 +- spec/models/contact-spec.coffee | 25 +++--------- src/flux/models/contact.coffee | 40 ++++++++++--------- 5 files changed, 32 insertions(+), 41 deletions(-) diff --git a/internal_packages/message-list/lib/message-item.cjsx b/internal_packages/message-list/lib/message-item.cjsx index 15753616b..a534f9db8 100644 --- a/internal_packages/message-list/lib/message-item.cjsx +++ b/internal_packages/message-list/lib/message-item.cjsx @@ -61,7 +61,7 @@ class MessageItem extends React.Component
- {@props.message.from?[0]?.displayFirstName()} + {@props.message.from?[0]?.displayName(compact: true)}
{@props.message.snippet} diff --git a/internal_packages/message-list/lib/message-participants.cjsx b/internal_packages/message-list/lib/message-participants.cjsx index 9881eb07b..c6253f06b 100644 --- a/internal_packages/message-list/lib/message-participants.cjsx +++ b/internal_packages/message-list/lib/message-participants.cjsx @@ -68,7 +68,7 @@ class MessageParticipants extends React.Component
_shortNames: (contacts=[]) => - _.map(contacts, (c) -> c.displayFirstName()).join(", ") + _.map(contacts, (c) -> c.displayName(includeAccountLabel: true, compact: true)).join(", ") _fullContact: (contacts=[]) => if contacts.length is 0 diff --git a/internal_packages/thread-list/lib/thread-list-participants.cjsx b/internal_packages/thread-list/lib/thread-list-participants.cjsx index b32ee4f14..ac6713e8a 100644 --- a/internal_packages/thread-list/lib/thread-list-participants.cjsx +++ b/internal_packages/thread-list/lib/thread-list-participants.cjsx @@ -44,9 +44,9 @@ class ThreadListParticipants extends React.Component else if contact.name.length > 0 if items.length > 1 - short = contact.displayFirstName() + short = contact.displayName(includeAccountLabel: false, compact: true) else - short = contact.displayName() + short = contact.displayName(includeAccountLabel: false) else short = contact.email if idx < items.length-1 and not items[idx+1].spacer diff --git a/spec/models/contact-spec.coffee b/spec/models/contact-spec.coffee index 5c01f5b3f..6db1486a4 100644 --- a/spec/models/contact-spec.coffee +++ b/spec/models/contact-spec.coffee @@ -68,26 +68,23 @@ describe "Contact", -> expect(c8.firstName()).toBe "Olivia" expect(c8.lastName()).toBe "Pope" - it "properly parses evan (Evan Morikawa)", -> + it "should not by fancy about the contents of parenthesis (Evan Morikawa)", -> c8 = new Contact {name: "evan (Evan Morikawa)"} expect(c8.firstName()).toBe "Evan" - expect(c8.lastName()).toBe "Morikawa" + expect(c8.lastName()).toBe "(Evan Morikawa)" it "falls back to the first component of the email if name isn't present", -> c1 = new Contact {name: " Evan Morikawa ", email: "evan@nylas.com"} expect(c1.displayName()).toBe "Evan Morikawa" - expect(c1.displayFirstName()).toBe "Evan" - expect(c1.displayLastName()).toBe "Morikawa" + expect(c1.displayName(compact: true)).toBe "Evan" c2 = new Contact {name: "", email: "evan@nylas.com"} expect(c2.displayName()).toBe "Evan" - expect(c2.displayFirstName()).toBe "Evan" - expect(c2.displayLastName()).toBe "" + expect(c2.displayName(compact: true)).toBe "Evan" c3 = new Contact {name: "", email: ""} expect(c3.displayName()).toBe "" - expect(c3.displayFirstName()).toBe "" - expect(c3.displayLastName()).toBe "" + expect(c3.displayName(compact: true)).toBe "" it "properly parses names with @", -> @@ -107,15 +104,6 @@ describe "Contact", -> expect(c3.firstName()).toBe "Nyl@s" expect(c3.lastName()).toBe "2000" - c4 = new Contact {name: " Ev@n Morikawa ", email: "evan@nylas.com"} - expect(c4.displayName()).toBe "Ev@n Morikawa" - expect(c4.displayFirstName()).toBe "Ev@n" - expect(c4.displayLastName()).toBe "Morikawa" - - c5 = new Contact {name: "ev@n (Evan Morik@wa)"} - expect(c5.firstName()).toBe "Evan" - expect(c5.lastName()).toBe "Morik@wa" - c6 = new Contact {name: "ev@nylas.com", email: "ev@nylas.com"} expect(c6.firstName()).toBe "Ev@nylas.com" expect(c6.lastName()).toBe "" @@ -131,8 +119,7 @@ describe "Contact", -> it "should properly return `You` as the display name for the current user", -> c1 = new Contact {name: " Test Monkey", email: @account.emailAddress} expect(c1.displayName()).toBe "You" - expect(c1.displayFirstName()).toBe "You" - expect(c1.displayLastName()).toBe "" + expect(c1.displayName(compact: true)).toBe "You" describe "isMe", -> it "returns true if the contact name matches the account email address", -> diff --git a/src/flux/models/contact.coffee b/src/flux/models/contact.coffee index 47b9a5ce0..8082db919 100644 --- a/src/flux/models/contact.coffee +++ b/src/flux/models/contact.coffee @@ -108,28 +108,36 @@ class Contact extends Model return null - isMePhrase: -> + isMePhrase: ({includeAccountLabel} = {}) -> account = @isMeAccount() return null unless account - FocusedPerspectiveStore ?= require '../stores/focused-perspective-store' - if account and FocusedPerspectiveStore.current().accountIds.length > 1 - return "You (#{account.label})" + if includeAccountLabel + 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 + # - "You" if the contact is the current user or an alias for the current user. + # - `name` if the contact has a populated name # - `email` in all other cases. - displayName: -> - @isMePhrase() ? @_nameParts().join(' ') + # + # You can pass several options to customize the name: + # - includeAccountLabel: If the contact represents the current user, include + # the account label afer "You" + # - compact: If the contact has a name, make the name as short as possible + # (generally returns just the first name.) + # + displayName: ({includeAccountLabel, compact} = {}) -> + compact ?= false + includeAccountLabel ?= !compact - displayFirstName: -> - @isMePhrase() ? @firstName() - - displayLastName: -> - return "" if @isMe() - @lastName() + if compact + @isMePhrase({includeAccountLabel}) ? @firstName() + else + @isMePhrase({includeAccountLabel}) ? @_nameParts().join(' ') firstName: -> articles = ['a', 'the'] @@ -152,10 +160,6 @@ class Contact extends Model # Unless there that would result in an empty string. name = name.split('@')[0] if name.indexOf('@') > 0 - # Take care of phrases like "evan (Evan Morikawa)" that should be displayed - # as the contents of the parenthesis - name = name.split(/[()]/)[1] if name.split(/[()]/).length > 1 - # Take care of phrases like "Mike Kaylor via LinkedIn" that should be displayed # as the contents before the separator word. Do not break "Olivia" name = name.split(/(\svia\s)/i)[0]