2015-10-10 03:40:36 +08:00
|
|
|
|
|
|
|
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) =>
|
2015-11-12 02:25:11 +08:00
|
|
|
return if NylasEnv.inSpecMode()
|
2015-10-10 04:42:24 +08:00
|
|
|
|
2015-10-10 03:40:36 +08:00
|
|
|
NylasAPI.makeRequest
|
|
|
|
accountId: @_accountId
|
|
|
|
path: "/contacts/rankings"
|
|
|
|
returnsModel: false
|
|
|
|
.then (json) =>
|
2015-10-10 04:42:24 +08:00
|
|
|
return unless json and json instanceof Array
|
|
|
|
|
|
|
|
# Convert rankings into the format needed for quick lookup
|
2015-10-10 03:40:36 +08:00
|
|
|
rankings = {}
|
|
|
|
for [email, rank] in json
|
|
|
|
rankings[email.toLowerCase()] = rank
|
|
|
|
callback(rankings)
|
|
|
|
|
2015-10-10 04:42:24 +08:00
|
|
|
.catch (err) =>
|
|
|
|
console.warn("Request for Contact Rankings failed for
|
|
|
|
account #{@_accountId}. #{err}")
|
2015-10-10 03:40:36 +08:00
|
|
|
|
2015-10-10 04:42:24 +08:00
|
|
|
module.exports = ContactRankingsCache
|