Mailspring/internal_packages/worker-sync/lib/contact-rankings-cache.es6

49 lines
1.1 KiB
JavaScript

import {
NylasAPI,
NylasAPIRequest,
} from 'nylas-exports'
import RefreshingJSONCache from './refreshing-json-cache'
// Stores contact rankings
class ContactRankingsCache extends RefreshingJSONCache {
constructor(accountId) {
super({
key: `ContactRankingsFor${accountId}`,
version: 1,
refreshInterval: 60 * 60 * 1000 * 24, // one day
})
this._accountId = accountId
}
fetchData = (callback) => {
if (NylasEnv.inSpecMode()) return
const request = new NylasAPIRequest({
api: NylasAPI,
options: {
accountId: this._accountId,
path: "/contacts/rankings",
returnsModel: false,
},
})
request.run()
.then((json) => {
if (!json || !(json instanceof Array)) return
// Convert rankings into the format needed for quick lookup
const rankings = {}
for (const [email, rank] of json) {
rankings[email.toLowerCase()] = rank
}
callback(rankings)
})
.catch((err) => {
console.warn(`Request for Contact Rankings failed for
account ${this._accountId}. ${err}`)
})
}
}
export default ContactRankingsCache