Mailspring/internal_packages/sidebar-fullcontact/lib/fullcontact-store.coffee
Ben Gotow 42ad243824 fix(message-sidebar): New ContactCard injectable role, updated FocusedContactStore
- The FocusedContactStore was triggering too often, and leaving it up to the FullcontactStore to fetch the full Contact model for the focused contact (pulled from thread.)

The FocusedContactStore triggers more responsibly, and registering for the role "MessageListSidebar:ContactCard" now gives you the focused contact as a full database model. The whole ContactCard region also fades in and out.
2015-09-24 18:58:53 -07:00

38 lines
1.1 KiB
CoffeeScript

_ = require 'underscore'
Reflux = require 'reflux'
request = require 'request'
{Contact,
AccountStore
ContactStore,
DatabaseStore,
FocusedContactsStore} = require 'nylas-exports'
FullContactStore = Reflux.createStore
init: ->
dataForContact: (contact) ->
return {} unless contact
if contact.thirdPartyData["FullContact"]
return contact.thirdPartyData["FullContact"]
else
@_attachFullcontactDataToContact(contact)
return {}
_attachFullcontactDataToContact: (contact) ->
email = contact.email.toLowerCase().trim()
return if email.length is 0
url = "https://api.fullcontact.com/v2/person.json?email=#{email}&apiKey=eadcbaf0286562a"
request url, (err, resp, data) =>
return if err
return if resp.statusCode != 200
try
data = JSON.parse(data)
contact.title = data.organizations?[0]?["title"]
contact.company = data.organizations?[0]?["name"]
contact.thirdPartyData ?= {}
contact.thirdPartyData["FullContact"] = data
DatabaseStore.persistModel(contact)
@trigger()
module.exports = FullContactStore