Mailspring/internal_packages/sidebar-inbox-internal/lib/internal-admin-store.coffee
Ben Gotow 81d2edcaf9 feat(accounts): Kill namespaces, long live accounts
Summary:
This diff replaces the Namespace object with the Account object, and changes all references to namespace_id => account_id, etc. The endpoints are now `/threads` instead of `/n/<id>/threads`.

This diff also adds preliminary support for multiple accounts. When you log in, we now log you in to all the attached accounts on edgehill server. From the preferences panel, you can auth with / unlink additional accounts. Shockingly, this all seems to pretty much work.

When replying to a thread, you cannot switch from addresses. However, when creating a new message in a popout composer, you can change the from address and the SaveDraftTask will delete/re-root the draft on the new account.

Search bar doesn't need to do full refresh on clear if it never committed

Allow drafts to be switched to a different account when not in reply to an existing thread

Fix edge case where ChangeMailTask throws exception if no models are modified during performLocal

Show many dots for many accounts in long polling status bar

add/remove accounts from prefs

Spec fixes!

Test Plan: Run tests, none broken!

Reviewers: evan, dillon

Reviewed By: evan, dillon

Differential Revision: https://phab.nylas.com/D1928
2015-08-21 15:29:58 -07:00

88 lines
2.7 KiB
CoffeeScript

_ = require 'underscore'
Reflux = require 'reflux'
request = require 'request'
{FocusedContactsStore,
AccountStore,
PriorityUICoordinator} = require 'nylas-exports'
module.exports =
# 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
# Nylas account, the InternalAdminStore would also listen for those user actions
# and perform business logic.
#
InternalAdminStore = Reflux.createStore
init: ->
@_accountCache = null
@_enabled = false
@_error = null
# 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
# focused contact. Since we only want to be enabled for @nylas.com emails,
# we listen for changes to available Accounts.
@listenTo FocusedContactsStore, @_onFocusedContacts
@listenTo AccountStore, @_onAccountChanged
@_onAccountChanged()
dataForFocusedContact: ->
return {loading: true} if @_accountCache is null
contact = FocusedContactsStore.focusedContact()
return {} unless contact
account = _.find @_accountCache, (account) -> account.email is contact.email
apps = undefined
apps = account.applications if account
# Coffeescript shorthand for {account: account, apps: apps}
{account, apps}
enabled: ->
@_enabled
error: ->
@_error
_onFocusedContacts: ->
# 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`.
@trigger(@)
_onAccountChanged: ->
clearInterval(@_fetchInterval) if @_fetchInterval
@_fetchInterval = null
# We only want to enable this package for users with nylas.com email addresses.
n = AccountStore.current()
if n and n.emailAddress.indexOf('@nylas.com') > 0
@_fetchInterval = setInterval(( => @_fetchAPIData()), 5 * 60 * 1000)
@_fetchAPIData()
@_enabled = true
else
@_accountCache = null
@_enabled = false
@trigger(@)
_fetchAPIData: ->
# 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.
request 'https://admin.nylas.com/api/status/accounts', (err, resp, data) =>
PriorityUICoordinator.settle.then =>
if err
@_error = err
else
@_error = null
try
@_accountCache = JSON.parse(data)
catch err
@_error = err
@_accountCache = null
@trigger(@)