Mailspring/internal_packages/worker-sync/lib/contact-rankings-cache.coffee
Drew Regitsky 0332d7264e fix(contacts): move contact rank fetching to sync workers, refactor
Summary:
Fixes bug where contact ranking was not being fetched, and refactors the refreshing
of contact ranks. Moves periodic refreshing of the database-stored ranks to the sync
workers so it occurs in the background, once per account. Refactors JSON cache code
accordingly.

Test Plan: manual

Reviewers: evan, bengotow

Reviewed By: bengotow

Differential Revision: https://phab.nylas.com/D2137
2015-10-09 12:40:36 -07:00

30 lines
863 B
CoffeeScript

RefreshingJSONCache = require './refreshing-json-cache'
{NylasAPI} = require 'nylas-exports'
# Stores contact rankings
class ContactRankingsCache extends RefreshingJSONCache
constructor: (accountId) ->
@_accountId = accountId
super({
key: "ContactRankingsFor#{accountId}",
version: 1,
refreshInterval: 60 * 60 * 1000 * 24 # one day
})
fetchData: (callback) =>
NylasAPI.makeRequest
accountId: @_accountId
path: "/contacts/rankings"
returnsModel: false
.then (json) =>
# Convert rankings into the format needed for quick lookup
rankings = {}
for [email, rank] in json
rankings[email.toLowerCase()] = rank
callback(rankings)
.catch (err) =>
console.warn("Request for Contact Rankings failed for account #{@_accountId}. #{err}")
module.exports = ContactRankingsCache