2017-10-06 01:54:52 +08:00
|
|
|
import { MailspringAPIRequest, Utils } from 'mailspring-exports';
|
|
|
|
const { makeRequest } = MailspringAPIRequest;
|
|
|
|
|
|
|
|
const CACHE_SIZE = 200;
|
2017-10-12 13:31:37 +08:00
|
|
|
const CACHE_INDEX_KEY = 'pp-cache-v3-keys';
|
|
|
|
const CACHE_KEY_PREFIX = 'pp-cache-v3-';
|
2017-10-06 01:54:52 +08:00
|
|
|
|
|
|
|
class ParticipantProfileDataSource {
|
|
|
|
constructor() {
|
|
|
|
try {
|
|
|
|
this._cacheIndex = JSON.parse(window.localStorage.getItem(CACHE_INDEX_KEY) || `[]`);
|
|
|
|
} catch (err) {
|
|
|
|
this._cacheIndex = [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async find(email) {
|
|
|
|
if (!email || Utils.likelyNonHumanEmail(email)) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
const data = this.getCache(email);
|
|
|
|
if (data) {
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
let body = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
body = await makeRequest({
|
|
|
|
server: 'identity',
|
|
|
|
method: 'GET',
|
|
|
|
path: `/api/info-for-email-v2/${email}`,
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
// we don't care about errors returned by this clearbit proxy
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2017-10-12 13:31:37 +08:00
|
|
|
if (!body.person) {
|
|
|
|
body.person = { email };
|
2017-10-06 01:54:52 +08:00
|
|
|
}
|
2017-10-12 13:31:37 +08:00
|
|
|
if (!body.company) {
|
|
|
|
body.company = {};
|
2017-10-06 01:54:52 +08:00
|
|
|
}
|
2017-10-12 13:31:37 +08:00
|
|
|
|
|
|
|
this.setCache(email, body);
|
|
|
|
return body;
|
2017-10-06 01:54:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// LocalStorage Retrieval / Saving
|
|
|
|
|
|
|
|
hasCache(email) {
|
|
|
|
return localStorage.getItem(`${CACHE_KEY_PREFIX}${email}`) !== null;
|
|
|
|
}
|
|
|
|
|
|
|
|
getCache(email) {
|
|
|
|
const raw = localStorage.getItem(`${CACHE_KEY_PREFIX}${email}`);
|
|
|
|
if (!raw) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
return JSON.parse(raw);
|
|
|
|
} catch (err) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setCache(email, value) {
|
|
|
|
localStorage.setItem(`${CACHE_KEY_PREFIX}${email}`, JSON.stringify(value));
|
|
|
|
const updatedIndex = this._cacheIndex.filter(e => e !== email);
|
|
|
|
updatedIndex.push(email);
|
|
|
|
|
|
|
|
if (updatedIndex.length > CACHE_SIZE) {
|
|
|
|
const oldestKey = updatedIndex.shift();
|
|
|
|
localStorage.removeItem(`${CACHE_KEY_PREFIX}${oldestKey}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
localStorage.setItem(CACHE_INDEX_KEY, JSON.stringify(updatedIndex));
|
|
|
|
this._cacheIndex = updatedIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default new ParticipantProfileDataSource();
|