Fix T569.

Summary:
resync_uids() was correctly asking for an additinal paramter... self.
However, being a closure, it takes self directly from the environment,
thus we don't need to declare it.

Test Plan:
Just sync a bunch of accounts, we should not encounter the TypeError reported
in https://review.inboxapp.com/T569 anymore. We should see a
NotImplementedError, which should be gracefully by the retry wrapper.

Reviewers: dlitz, khamidou, emfree

Reviewed By: emfree

Subscribers: spang

Differential Revision: https://review.inboxapp.com/D677
Search v1.0: Thread, message search for a namespace.

Summary:
1. Script to create an Elasticsearch index for each namespace and index its Threads, Messages
s.t. they can be queried on both thread/message attributes.

2. DSL query constructor to construct Elasticsearch DSL queries for
* no restrictions/ field-restricted/ all fields
* exact phrase/ any match
* equally weighted/ boosted
searches.

3. API functions.
4. Docs.

Test Plan:
Tests for:
Index creation.
Thread, message search.
Cross-attribute search.

Reviewers: emfree

Reviewed By: emfree

Differential Revision: https://review.inboxapp.com/D672
Fixes nameerror on prod

Summary: https://app.getsentry.com/inbox/prod/group/38940736/ -- I mistakenly removed an import while refactoring this part of the code.

Test Plan: That's an import.

Reviewers: emfree

Reviewed By: emfree

Differential Revision: https://review.inboxapp.com/D680
Basic syncback for google calendars

Summary:
This is a basic implementation of syncback. I did two things:
- adapted the syncback code to handle additional actions. I copied the structure of the IMAP syncback code: there's a inbox.events.actions which mirrors inbox.actions, with a backend/ subfolder.
- wrote a syncback module for gmail. So far, it only supports saving to the primary calendar.

Test Plan: Tested manually.

Reviewers: kav-ya, emfree

Reviewed By: emfree

Subscribers: spang

Projects: #sync-engine

Differential Revision: https://review.inboxapp.com/D679
Add support for using tox to run tests

Summary:
tox aims to automate and standardize testing in Python.  See docs:
http://tox.readthedocs.org/

If you have tox installed (`pip install tox`), you can now run the tests
simply by running `tox` in the same directory as tox.ini.

Test Plan: Installed tox; Ran tox.

Reviewers: emfree, kav-ya

Reviewed By: kav-ya

Subscribers: khamidou, siro

Differential Revision: https://review.inboxapp.com/D657
bin/providers-as-json: Pretty-print JSON & use deterministic sort ordering
Revert to forked flanker with upstream fixes.

They still can't unicode; breaks everything.
requirements: Update 'requests' from v2.3.0 to v2.4.3

Summary: This lets us use use the `json` parameter in a request.

Test Plan: Ran `pip install -r requirements.txt`

Reviewers: spang, emfree

Reviewed By: emfree

Differential Revision: https://review.inboxapp.com/D686
Calendar bug fixes

Summary:
This diff fixes a bunch of bugs with the Gmail calendars.

The most egregious one is a weird issue with the mapping we used to assign participants to an event. I changed it to use one of SQLAlchemy's default mapping.

There's also a couple fixes in the way we handle changes in events -- previously we'd barf if an event field was updated on the server. I modified the merging strategy to "barf if setting an field to None". I think this should be okay most of the time and eventually consistent.

Finally, I changed the gmail update code to export more of our db fields.

Also, I think I'm going to release code like this, in daily batches. This should make thing easier to review.

Test Plan: Tested manually.

Reviewers: kav-ya, emfree

Reviewed By: kav-ya, emfree

Subscribers: spang

Projects: #sync-engine

Differential Revision: https://review.inboxapp.com/D693
Define THROTTLE_WAIT in mailsync/base.py
Fix outdated docstring.
Fix indentation typo in Gmail throttling.
Eagerly signal sync status.

Summary:
The greenlet executing the sync engine wasn't talking to the sync status
consumer greenlet till the first state transition.
This commit fixes this misbehavior.

Test Plan:
Verifying the previously present bug is kind of tricky, I just noticed it
because I was working on replicating the sync status on Redis.

Reviewers: khamidou, emfree

Reviewed By: emfree

Differential Revision: https://review.inboxapp.com/D698
Log search errors in ns_api.py.
Fix ES thread mapping.
Optimize namespace indexing script.

Summary:
1. Parallelize - 2 greenlets per namespace, one for messages + one for threads)
2. Use ES.bulk_index() to index - uses default chunk_size of 500.
3. Use safer_yield_per to load messages, threads.

Test Plan:
None (run locally but doesn't count!)
Will run on staging.

Reviewers: emfree

Reviewed By: emfree

Differential Revision: https://review.inboxapp.com/D707
Remove unnecessary imports from util/rdb.py.

Summary: Fixes T592.

Test Plan: Run unit test suite.

Reviewers: khamidou

Maniphest Tasks: T592

Differential Revision: https://review.inboxapp.com/D697
Ensure syncs are restarted if necessary after reauth.

Summary:
This is a bit of a stopgap fix for the problem of syncs not being automatically
restarted if they were previously stopped because of invalid credentials, but
the user actually reauths.

Test Plan:
Set test account state to invalid; check that sync gets restarted
after reauth.

Reviewers: kav-ya

Differential Revision: https://review.inboxapp.com/D701
Reject sending to an empty list of recipients.

Previously we'd just reject your post body if you didn't include the recipients
field at all.

Fixes T589.
Script to delete ES namespace indexes.
Needed to change mappings (for e.g. to support nested queries).