mirror of
https://github.com/Foundry376/Mailspring.git
synced 2025-03-04 03:56:33 +08:00
fix(sidebar): keep trying on 202s
Also fixed a bug where the email could come back with different capitalization causing a cache miss
This commit is contained in:
parent
2d8e65ead4
commit
ae4ca59526
1 changed files with 34 additions and 23 deletions
|
@ -1,11 +1,15 @@
|
|||
# This file is in coffeescript just to use the existential operator!
|
||||
{AccountStore, EdgehillAPI} = require 'nylas-exports'
|
||||
|
||||
MAX_RETRY = 10
|
||||
|
||||
module.exports = class ClearbitDataSource
|
||||
clearbitAPI: ->
|
||||
return "https://person.clearbit.com/v2/combined"
|
||||
|
||||
find: ({email}) ->
|
||||
find: ({email, tryCount}) ->
|
||||
if (tryCount ? 0) >= MAX_RETRY
|
||||
return Promise.resolve(null)
|
||||
tok = AccountStore.tokenForAccountId(AccountStore.accounts()[0].id)
|
||||
new Promise (resolve, reject) =>
|
||||
EdgehillAPI.request
|
||||
|
@ -14,34 +18,41 @@ module.exports = class ClearbitDataSource
|
|||
pass: ""
|
||||
path: "/proxy/clearbit/#{@clearbitAPI()}/find?email=#{email}",
|
||||
success: (body, response) =>
|
||||
resolve(@parseResponse(body, response, email))
|
||||
@parseResponse(body, response, email, tryCount).then(resolve).catch(reject)
|
||||
error: reject
|
||||
|
||||
# The clearbit -> Nylas adapater
|
||||
parseResponse: (body={}, response, requestedEmail) ->
|
||||
# This means it's in the process of fetching. Return null so we don't
|
||||
# cache and try again.
|
||||
if response.statusCode isnt 200
|
||||
return null
|
||||
parseResponse: (body={}, response, requestedEmail, tryCount=0) =>
|
||||
new Promise (resolve, reject) =>
|
||||
# This means it's in the process of fetching. Return null so we don't
|
||||
# cache and try again.
|
||||
if response.statusCode is 202
|
||||
setTimeout =>
|
||||
@find({email: requestedEmail, tryCount: tryCount+1}).then(resolve).catch(reject)
|
||||
, 1000
|
||||
return
|
||||
else if response.statusCode isnt 200
|
||||
resolve(null)
|
||||
return
|
||||
|
||||
person = body.person
|
||||
person = body.person
|
||||
|
||||
# This means there was no data about the person available. Return a
|
||||
# valid, but empty object for us to cache. This can happen when we
|
||||
# have company data, but no personal data.
|
||||
if not person
|
||||
person = {email: requestedEmail}
|
||||
# This means there was no data about the person available. Return a
|
||||
# valid, but empty object for us to cache. This can happen when we
|
||||
# have company data, but no personal data.
|
||||
if not person
|
||||
person = {email: requestedEmail}
|
||||
|
||||
return {
|
||||
cacheDate: Date.now()
|
||||
email: person.email # Used as checksum
|
||||
bio: person.bio ? person.twitter?.bio ? person.aboutme?.bio,
|
||||
location: person.location ? person.geo?.city
|
||||
currentTitle: person.employment?.title,
|
||||
currentEmployer: person.employment?.name,
|
||||
profilePhotoUrl: person.avatar,
|
||||
socialProfiles: @_socialProfiles(person)
|
||||
}
|
||||
resolve({
|
||||
cacheDate: Date.now()
|
||||
email: requestedEmail # Used as checksum
|
||||
bio: person.bio ? person.twitter?.bio ? person.aboutme?.bio,
|
||||
location: person.location ? person.geo?.city
|
||||
currentTitle: person.employment?.title,
|
||||
currentEmployer: person.employment?.name,
|
||||
profilePhotoUrl: person.avatar,
|
||||
socialProfiles: @_socialProfiles(person)
|
||||
})
|
||||
|
||||
_socialProfiles: (person={}) ->
|
||||
profiles = {}
|
||||
|
|
Loading…
Reference in a new issue