2015-03-21 10:15:32 +08:00
|
|
|
_ = require 'underscore-plus'
|
|
|
|
Reflux = require 'reflux'
|
|
|
|
request = require 'request'
|
2015-04-29 10:23:28 +08:00
|
|
|
{FocusedContactsStore,
|
|
|
|
NamespaceStore,
|
2015-05-15 08:08:30 +08:00
|
|
|
PriorityUICoordinator} = require 'nylas-exports'
|
2015-03-21 10:15:32 +08:00
|
|
|
|
|
|
|
module.exports =
|
2015-04-29 10:23:28 +08:00
|
|
|
# The InternalAdminStore manages the data that backs the admin sidebar and emits
|
|
|
|
# a "trigger" event that the view listens to.
|
|
|
|
#
|
|
|
|
# If the Admin sidebar allowed you to take actions, like modifying someone's
|
2015-05-19 10:21:54 +08:00
|
|
|
# Nylas account, the InternalAdminStore would also listen for those user actions
|
2015-04-29 10:23:28 +08:00
|
|
|
# and perform business logic.
|
|
|
|
#
|
|
|
|
InternalAdminStore = Reflux.createStore
|
2015-03-21 10:15:32 +08:00
|
|
|
|
|
|
|
init: ->
|
|
|
|
@_accountCache = null
|
|
|
|
@_applicationCache = null
|
2015-03-22 02:59:47 +08:00
|
|
|
@_enabled = false
|
|
|
|
@_error = null
|
|
|
|
|
2015-04-29 10:23:28 +08:00
|
|
|
# Stores often listen to other stores to vend correct data to their views.
|
|
|
|
# Since we serve information about a contact we listen for changes to the
|
2015-05-15 08:08:30 +08:00
|
|
|
# focused contact. Since we only want to be enabled for @nylas.com emails,
|
2015-04-29 10:23:28 +08:00
|
|
|
# we listen for changes to available Namespaces.
|
2015-03-21 10:15:32 +08:00
|
|
|
@listenTo FocusedContactsStore, @_onFocusedContacts
|
2015-03-22 02:59:47 +08:00
|
|
|
@listenTo NamespaceStore, @_onNamespaceChanged
|
2015-03-21 10:15:32 +08:00
|
|
|
|
2015-03-22 02:59:47 +08:00
|
|
|
@_onNamespaceChanged()
|
2015-03-21 10:15:32 +08:00
|
|
|
|
2015-04-29 10:23:28 +08:00
|
|
|
|
2015-03-21 10:15:32 +08:00
|
|
|
dataForFocusedContact: ->
|
|
|
|
return {loading: true} if @_accountCache is null or @_applicationCache is null
|
|
|
|
contact = FocusedContactsStore.focusedContact()
|
2015-03-21 10:18:05 +08:00
|
|
|
return {} unless contact
|
2015-04-29 10:23:28 +08:00
|
|
|
|
2015-03-21 10:15:32 +08:00
|
|
|
account = _.find @_accountCache, (account) -> account.email is contact.email
|
|
|
|
apps = undefined
|
2015-04-29 10:23:28 +08:00
|
|
|
apps = @_applicationCache.accounts["#{account.id}"] if account
|
|
|
|
|
|
|
|
# Coffeescript shorthand for {account: account, apps: apps}
|
2015-03-21 10:15:32 +08:00
|
|
|
{account, apps}
|
|
|
|
|
2015-03-22 02:59:47 +08:00
|
|
|
enabled: ->
|
|
|
|
@_enabled
|
|
|
|
|
|
|
|
error: ->
|
|
|
|
@_error
|
|
|
|
|
2015-03-21 10:15:32 +08:00
|
|
|
_onFocusedContacts: ->
|
2015-04-29 10:23:28 +08:00
|
|
|
# When the user focuses on a contact, we don't need to do any work because we
|
|
|
|
# cache everything. Just trigger so that our view updates and calls
|
|
|
|
# `dataForFocusedContact`.
|
2015-03-21 10:15:32 +08:00
|
|
|
@trigger(@)
|
|
|
|
|
2015-03-22 02:59:47 +08:00
|
|
|
_onNamespaceChanged: ->
|
|
|
|
clearInterval(@_fetchInterval) if @_fetchInterval
|
|
|
|
@_fetchInterval = null
|
|
|
|
|
2015-05-15 08:08:30 +08:00
|
|
|
# We only want to enable this package for users with nylas.com email addresses.
|
2015-03-22 02:59:47 +08:00
|
|
|
n = NamespaceStore.current()
|
2015-04-29 10:23:28 +08:00
|
|
|
if n and n.emailAddress.indexOf('@nylas.com') > 0
|
2015-03-22 02:59:47 +08:00
|
|
|
@_fetchInterval = setInterval(( => @_fetchAPIData()), 5 * 60 * 1000)
|
|
|
|
@_fetchAPIData()
|
|
|
|
@_enabled = true
|
|
|
|
else
|
|
|
|
@_accountCache = null
|
|
|
|
@_applicationCache = null
|
|
|
|
@_enabled = false
|
|
|
|
@trigger(@)
|
|
|
|
|
2015-03-21 10:15:32 +08:00
|
|
|
_fetchAPIData: ->
|
2015-04-29 10:23:28 +08:00
|
|
|
# Make a HTTP request to the Admin service using the `request` library. Using
|
|
|
|
# the priority UI coordinator ensures that the expensive JSON.parse operation
|
|
|
|
# doesn't happen while an animation is running.
|
2015-05-15 08:08:30 +08:00
|
|
|
request 'https://admin.nylas.com/api/status/accounts', (err, resp, data) =>
|
2015-03-31 09:08:38 +08:00
|
|
|
PriorityUICoordinator.settle.then =>
|
|
|
|
if err
|
2015-03-26 09:22:52 +08:00
|
|
|
@_error = err
|
2015-03-31 09:08:38 +08:00
|
|
|
else
|
|
|
|
@_error = null
|
|
|
|
try
|
|
|
|
@_accountCache = JSON.parse(data)
|
|
|
|
catch err
|
|
|
|
@_error = err
|
|
|
|
@_accountCache = null
|
|
|
|
@trigger(@)
|
2015-03-21 10:15:32 +08:00
|
|
|
|
2015-05-15 08:08:30 +08:00
|
|
|
request 'https://admin.nylas.com/api/status/accounts/applications', (err, resp, data) =>
|
2015-03-31 09:08:38 +08:00
|
|
|
PriorityUICoordinator.settle.then =>
|
|
|
|
if err
|
2015-03-26 09:22:52 +08:00
|
|
|
@_error = err
|
2015-03-31 09:08:38 +08:00
|
|
|
else
|
|
|
|
@_error = null
|
|
|
|
try
|
|
|
|
@_applicationCache = JSON.parse(data)
|
|
|
|
catch err
|
|
|
|
@_error = err
|
|
|
|
@_applicationCache = null
|
2015-04-29 10:23:28 +08:00
|
|
|
@trigger(@)
|