2016-03-10 03:33:31 +08:00
|
|
|
import NylasStore from 'nylas-store'
|
|
|
|
import ClearbitDataSource from './clearbit-data-source'
|
|
|
|
import {Utils} from 'nylas-exports'
|
|
|
|
|
|
|
|
// TODO: Back with Metadata
|
|
|
|
const contactCache = {}
|
|
|
|
const CACHE_SIZE = 100
|
|
|
|
const contactCacheKeyIndex = []
|
|
|
|
|
|
|
|
class ParticipantProfileStore extends NylasStore {
|
|
|
|
activate() {
|
|
|
|
this.cacheExpiry = 1000 * 60 * 60 * 24 // 1 day
|
|
|
|
this.dataSource = new ClearbitDataSource()
|
|
|
|
}
|
|
|
|
|
|
|
|
dataForContact(contact) {
|
|
|
|
if (!contact) {
|
|
|
|
return {}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Utils.likelyNonHumanEmail(contact.email)) {
|
|
|
|
return {}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.inCache(contact)) {
|
2016-03-11 07:16:32 +08:00
|
|
|
const data = this.getCache(contact);
|
|
|
|
if (data.cacheDate) {
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
return {}
|
2016-03-10 03:33:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
this.dataSource.find({email: contact.email}).then((data) => {
|
2016-03-11 03:21:24 +08:00
|
|
|
if (data && data.email === contact.email) {
|
2016-03-10 03:33:31 +08:00
|
|
|
this.setCache(contact, data);
|
|
|
|
this.trigger()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return {}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Back by metadata.
|
|
|
|
getCache(contact) {
|
|
|
|
return contactCache[contact.email]
|
|
|
|
}
|
|
|
|
|
|
|
|
inCache(contact) {
|
|
|
|
const cache = contactCache[contact.email]
|
|
|
|
if (!cache) { return false }
|
|
|
|
if (!cache.cacheDate || Date.now() - cache.cacheDate > this.cacheExpiry) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
setCache(contact, value) {
|
|
|
|
contactCache[contact.email] = value
|
|
|
|
contactCacheKeyIndex.push(contact.email)
|
|
|
|
if (contactCacheKeyIndex.length > CACHE_SIZE) {
|
|
|
|
delete contactCache[contactCacheKeyIndex.shift()]
|
|
|
|
}
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
|
|
|
deactivate() {
|
|
|
|
// no op
|
|
|
|
}
|
|
|
|
}
|
2016-05-04 09:08:24 +08:00
|
|
|
const store = new ParticipantProfileStore()
|
|
|
|
export default store
|