Apply distinct() to filtered API results.

Summary:
If we don't do this, the problem is that when we join threads to messages, the
limit parameter is applied to the resulting thread-message pairs, not to
threads alone. This leads to wrong behavior when multiple messages on a thread
match, say, the recipient address you're filtering on.

Fixes T336 and T337.

Test Plan: Regression test added.

Reviewers: kav-ya

Reviewed By: kav-ya

Maniphest Tasks: T336

Differential Revision: https://review.inboxapp.com/D376