crypto: move encryption & decryption into abstract "oracle" objects

Summary:
This will allow us to do more interesting key management.

This also decouples 096_migrate_secret_data.py from the oracle code so
that it doesn't break again the next time we change it.  (We shouldn't
be doing re-encryption inside an Alembic migration anyway.)

Test Plan:
- Ran the unit tests.
- Manually tested migrating the database from v0.3.0 (alembic rev
  2b89164aa9cd)

Reviewers: emfree

Reviewed By: emfree

Subscribers: dlitz

Differential Revision: https://review.inboxapp.com/D511
[SCHEMA] Properly reconcile Inbox-created messages in IMAP sync.

Summary:
If we sync a message with an X-INBOX-ID header and already have it in that
namespace's data store, don't create a new message. Just update the existing
thread instead.

Fixes a bug in which a message sent via the Inbox API causes duplicate threads
to appear once you sync the sent message from the backend.

Note: Requires migration, but can be run online. Takes <15 minutes.

Test Plan:
Check that syncing a sent message updates the existing thread,
instead of creating an old one.

Reviewers: spang, kav-ya

Reviewed By: kav-ya

Differential Revision: https://review.inboxapp.com/D528
Limit scope of reconciliation test.

EAS seems to be stripping the X-INBOX-ID header. Needs more investigation;
restrict the test to other providers for now.
Create tags when we create folders in save_folder_names.

Fixes an odd bug in which, when a user somehow has both a [Gmail]/Trash folder
and a Trash label, we try to create the 'trash' tag twice, causing
IntegrityErrors.
Add a basic &view=count parameter

Summary:
I decided to break implementing "view=count" into three subtasks:
- the one you're reading now
- adding a "view=ids" to display a list of ids
- defining a set syntax for parameters "+", "^", "-".

This diff adds support for "view" parameter wherever it makes sense. Also not that there's places where the code returns directly a dict to serialize. This is because the python json library doesn't let us override how basic types (int, long, etc.) get serialized.

Test Plan: Tested that it worked for /threads and /messages

Reviewers: emfree

Reviewed By: emfree

Subscribers: mg, bengotow

Projects: #inbox-api

Maniphest Tasks: T324

Differential Revision: https://review.inboxapp.com/D499
Only raise actions exceptions if it's useful.

Summary:
To reduce error-reporting noise.
For example EAS exceptions require special handling that a blind retry won't fix.
Therefore until we allow custom-retry for the actions code too, we simply log.

Test Plan: None.

Reviewers: emfree

Reviewed By: emfree

Differential Revision: https://review.inboxapp.com/D530
Add indexes to message.subject and thread.subject.

Summary:
Otherwise API queries that filter by ?subject=<...> get slow. This also skews
our response time metrics, since the monitoring does a bunch of these queries.

Test Plan: Nada.

Reviewers: khamidou, spang

Reviewed By: spang

Subscribers: spang

Differential Revision: https://review.inboxapp.com/D531
Fix logging when g_thrid duplicate is encountered.
retry_with_logging takes optional account_id arg.
We probably want the greenlet running the sync fn to also use self.retry_fail_classes.

Summary:
This way, when the EAS sync monitor encounters EAS exceptions a retry won't help with
(for e.g. during provisioning), we don't.

Test Plan: None.

Reviewers: emfree

Reviewed By: emfree

Differential Revision: https://review.inboxapp.com/D534
Download Gmail threads oldest-to-newest for better reconciliation.

Summary:
This fixes the following situation:
1) You send a message via the Inbox API. We create a Message object m1 and thread t1.

2) Somebody replies to that message.

3) We sync that reply, and create a new message m2 and new thread t2.

4) We sync the next oldest message in the thread, which was the message we sent originally. How do we reconcile? Currently, we don't create a new thread, and just update t1 with the g_thrid. But now we've actually split the thread across t1 and t2, and have two objects with the same g_thrid. That's not good.

You could imagine alternatively deleting m1 and t1 at this point, and adding a
new message m3 onto t2. But this breaks the API for users who might want to do
things like "check the original thread t1 for replies to the message that they
sent via the API".

So this seems like the best alternative solution to me. Now the first message
you sync is your original one, so the replies can be bolted on to t1.
This adds a few seconds of latency if you get new mail on a long thread during
initial sync, but that seems like a relatively small cost.

Test Plan: Not really sure how to programatically test this :/

Reviewers: spang

Reviewed By: spang

Subscribers: khamidou

Differential Revision: https://review.inboxapp.com/D535
Create canonical tags before saving folder names.

Otherwise if a user has their own 'archive' folder or similar, we may not map
it correctly to our canonical tags.
Fix broken link in docs.
Add indexes to Thread.{recent|subject}date for great performance.

Since API results for /threads are *ordered by* recentdate, it's kind of
important to have this field be indexed.
Also index message.received_date.
Log exception type when mail sync dies.
Directly filter messages by namespace_id for API queries.

Summary:
Now that messages have a namespace_id, we don't need to do the wonky
joining-to-threads thing. This makes the /messages endpoint two to three times
faster on large mailboxes.

Test Plan: Unit tests.

Reviewers: khamidou

Reviewed By: khamidou

Differential Revision: https://review.inboxapp.com/D539
[sentry] Whitelist stack locals

Summary: Stack locals contain valuable debugging information. Keep some of them which are not PII (so far, only `account_id`).

Test Plan: Triggered an exception, tested that locals got stripped.

Reviewers: dlitz, emfree

Reviewed By: emfree

Projects: #sync-engine

Differential Revision: https://review.inboxapp.com/D546
Validate recipients before sending a draft.

Summary:
We shouldn't let people try to send drafts when the recipients are patently
invalid. So check them before sending.

Test Plan: Unit test added.

Reviewers: khamidou

Reviewed By: khamidou

Differential Revision: https://review.inboxapp.com/D544
Migration to drop easaccount.eas_state column.