Commit graph

845 commits

Author SHA1 Message Date
Juan Tejada 8b3944d235 [local-sync] Make category syncback tasks save changes after imap success
Summary: This will solve T7579 for actions involving categories (folders/labels)

Test Plan: manual

Reviewers: khamidou, halla, mark, evan, spang

Reviewed By: evan, spang

Differential Revision: https://phab.nylas.com/D3753
2017-01-24 07:30:43 -08:00
Juan Tejada e909c8dfc7 [local-sync] Make thread syncback tasks save changes after imap success
Summary: This will solve T7579 for thread operations

Test Plan: manual

Reviewers: evan, mark, khamidou, halla, spang

Reviewed By: halla, spang

Differential Revision: https://phab.nylas.com/D3755
2017-01-24 07:29:25 -08:00
Christine Spang 359e32282f [local-sync] Sync draft flag from provider to K2 & exclude drafts from Edgehill
Summary:
We've been syncing drafts messages but not the drafts flag in K2, making
them appear in Edgehill as regular old messages.

This commit makes K2 sync the drafts flag, and also correctly label
folders called "Drafts" with the 'drafts' role.

Because 2-way syncing of drafts is very complex and error-prone since
you need to add new drafts and delete the old ones on every update, and
we reaally don't want to do things like create multiplying draft copies
or accidentally lose a draft someone started composing elsewhere, we
simply exclude messages marked as drafts from being serialized to
Edgehill through the delta stream for now. This removes the confusing
behaviour and also sets a better stage for completing drafts sync later.

Eventually we will also want to add functionality to allow users to
select their drafts folder, but for now this code does the right thing
in many more cases.

While investigating this behaviour, I also discovered a bug we've never
seen before where Gmail isn't applying the \Draft flag to draft
messages, no matter which folder we fetch them from. :-/ This is very
unfortunate and there's no way for us to work around it other than to
fetch messages in the Drafts folder and manually apply the flag locally,
since "drafts" is not a label in Gmail, only another IMAP folder. Brandon
Long from the Gmail team says that this is because they've had
problems with clients which sync drafts, so the Gmail web client and
mobile apps do not set the \Draft flag on drafts. (I don't get how this
solves their problem, but okay.) Let's solve the issue on Gmail if it
comes up by user demand—should be relatively straightforward to
implement, but it adds sync work & complexity.

Fixes T7593

Test Plan: manual

Reviewers: halla, juan

Reviewed By: juan

Maniphest Tasks: T7593

Differential Revision: https://phab.nylas.com/D3749
2017-01-23 20:58:32 -08:00
Christine Spang 902ce10881 [local-sync] Don't be so strict about updating threads from messages
This is how we _add_ messages to a thread in the first place, so we cannot
place the constraint that the thread must contain the message already.
2017-01-23 20:54:01 -08:00
Mark Hahnenberg ac6ad38296 [local-sync] Throttle message processing when on battery
Summary:
We don't want to run message processing full tilt when a user isn't plugged in.
This diff adds some detection logic that causes message processing to be
throttled/unthrottled when a user unplugs/plugs in their computer.

Test Plan: Run locally unplugged and plugged in, verify that CPU use goes up/down

Reviewers: evan, juan

Reviewed By: juan

Differential Revision: https://phab.nylas.com/D3759
2017-01-23 18:31:35 -08:00
Halla Moore 9f5631b1fe [local-sync]: Update threads when they have messages removed
Summary:
See title

Depends on D3744

Test Plan: tested locally

Reviewers: spang, evan, juan

Reviewed By: spang, evan, juan

Differential Revision: https://phab.nylas.com/D3745
2017-01-23 17:04:39 -08:00
Christine Spang 0db62579bd [local-sync] Keep only one HTML part in multipart/alternative structures
Summary:
I thought it was gonna be OK that we kept all HTML parts in a multipart/alternative
MIME structure because the world is a sane place and nobody would ever put more
than one HTML part in a multipart/alternative structure.

I was wrong.

We have found extraterrestrial life^W^WI mean emails which contain duplicate,
exactly the same MIME parts within a multipart/alternative MIME structure: two
text/plain parts and two text/html parts. This is likely due to a broken MIME
implementation, or perhaps a bug in someone's email script. So, we should
only keep one text/html MIME part if there are multiple.

Test Plan:
manual for now---added this to my mail parsing regression test list
for implementation once we unify the DBs and have a roughly stable code
structure

Reviewers: halla, juan

Reviewed By: juan

Differential Revision: https://phab.nylas.com/D3750
2017-01-20 17:53:57 -08:00
Halla Moore 4d10abcc2c [local-sync] Be more permissive with content transfer encodings (CTEs)
Summary:
Rather than having a strict model where we don't decode the message if we
don't specifically recognize the CTE, treat any CTEs we don't recognize as
having no encoding. There are several CTE strings that could mean this (e.g.
7bit, 7BITS, 8-bit, binary, NONE, utf8), and we don't want to check for them
all. Additionally, if there is a CTE we don't support, the user will likely
see rendering issues and contact support. This will allow us to obtain more
concrete information about these messages.

Test Plan: manual

Reviewers: spang

Reviewed By: spang

Differential Revision: https://phab.nylas.com/D3748
2017-01-20 13:54:53 -08:00
Juan Tejada 0a12608971 [local-sync] Never show red box on retryable errors
Summary:
Given that we were marking the account as errored if we've encountered
enough RetryableErrors, we would show the red box to the user when in
fact the problem was the user was offline, causing confusion

If the user is offline, we will constantly get RetryableErrors in the
sync loop, and we can't mark the account as errored in that case.

Test Plan: manual

Reviewers: evan

Reviewed By: evan

Differential Revision: https://phab.nylas.com/D3752
2017-01-20 10:16:28 -08:00
Juan Tejada 5025dbe080 🎨 Error message 2017-01-19 23:33:22 -08:00
Christine Spang fb8fc9f9e8 [local-sync] typo fix 2017-01-19 19:27:50 -08:00
Karim Hamidou 9b40df95b4 [mini-feat] Display the IMAP commands we're sending when NYLAS_DEBUG is set.
Makes debugging a bit easier.
2017-01-19 16:59:27 -08:00
Juan Tejada 76ce0d6e2f [local-sync]: Token error handler fix 2017-01-19 11:25:31 -08:00
Juan Tejada 83b57be19f [local-sync] Properly handle errors when refreshing tokens
Summary:
This commit ensures that we handle transient errors correctly when refreshing
tokens

Test Plan: manual

Reviewers: khamidou, evan

Reviewed By: evan

Differential Revision: https://phab.nylas.com/D3740
2017-01-19 11:16:27 -08:00
Juan Tejada f09bc902a0 [local-sync] Add a few improvements to error management
Summary:
In the sync worker:
- Move the backoff logic inside `scheduleNextSync`, where all logic to schedule the next sync loop now lives
- If we've retried a RetryableError a bunch of times, show the error to the user, otherwise the user might think the app is not working for no reason
- Clean up logging

In the message processor:
- Report message processing errors to sentry!

Sync Process Manager:
- Listen to new `Actions.debugSync` to show the Activity Window and open dev tools

Test Plan: manual

Reviewers: khamidou, evan

Reviewed By: khamidou, evan

Differential Revision: https://phab.nylas.com/D3736
2017-01-19 10:52:21 -08:00
Juan Tejada b4ef3f47a7 [local-sync] Batch imap operations in syncback tasks for faster performance
Summary:
This commit makes it so our syncback tasks send as few imap commands as possible by passing a set of UIDs whenever possible. Previously, we would send 1 command per message, with a single UID, which was very wasteful given that we can pass a set of UIDs. This is especially helpful for operating on threads with a large number of messages.

Syncback actions will now group all messages in a thread by the folder they belong to, and issue a single operation on the folder box. When removing all labels from a thread (setting labels to []), we need to issue a command of the form `box.delLabels(uids, labels)`, so we also group messages by set their set of labels to issue as few commands as possible.

This commit only batches imap commands, but we can still batch syncback actions themselves, which can be implemented in a separate patch.

Test Plan: manual

Reviewers: evan, mark, spang

Reviewed By: spang

Subscribers: halla, mg

Differential Revision: https://phab.nylas.com/D3719
2017-01-19 09:53:34 -08:00
Evan Morikawa a124b6c100 [local-sync] protection from reset action being double-fired
Summary: Double-firing protection since the DatabaseStore can now fire this

Test Plan: manual

Reviewers: juan

Reviewed By: juan

Differential Revision: https://phab.nylas.com/D3730
2017-01-18 17:30:30 -08:00
Evan Morikawa 02cf6fbf6e [isomorphic-core] have IMAP auth retry
Summary: IMAP auth retries

Test Plan: manual

Reviewers: khamidou, juan

Reviewed By: khamidou, juan

Differential Revision: https://phab.nylas.com/D3735
2017-01-18 17:29:58 -08:00
Juan Tejada 4e86d26478 [*] Increment default timeout connection
Summary:
We are getting to many imap timeout connection errors because the
authTimeout was just 5 secs

Test Plan: manual

Reviewers: khamidou, evan

Reviewed By: evan

Differential Revision: https://phab.nylas.com/D3733
2017-01-18 14:05:27 -08:00
Juan Tejada 05fd276ec7 [cloud-api] Improve auth error logging 2017-01-18 13:36:50 -08:00
Juan Tejada 7c99e9c55f 🎨 2017-01-18 10:35:25 -08:00
Juan Tejada e1f1b22ee2 [local-sync] Ensure all imap operations are wrapped with timeout
Summary:
Specifically, these imap connections have been known to hang when you close your laptop or go offline/online, even though we are passing a `socketTimeout` to node-imap. When they hang, everything freezes because the promise waiting for the result never resolves.

`_createConnectionPromise` wraps the operations with a timeout we implemented ourselves, and correctly rejects on timeout.

This commit wraps other imap operations that were missing. (I notices because I encountered the hanging on one of these operations)

Test Plan: manual

Reviewers: evan, mark, spang

Reviewed By: spang

Differential Revision: https://phab.nylas.com/D3720
2017-01-18 10:24:26 -08:00
Christine Spang 944b677d3e [local-sync] Set threading headers when uploading message to Sent folder
Summary:
We weren't, which meant that us sending with multi-send or generic IMAP
broke threading. :(

Test Plan: manual

Reviewers: juan, evan

Reviewed By: juan, evan

Differential Revision: https://phab.nylas.com/D3718
2017-01-16 20:17:44 -08:00
Juan Tejada 107fcbf355 [local-sync] Fix attribute sync for non Gmail accounts
Summary:
Before this commit, if folder sync was complete, and the account didn't support CONDSTORE (e.g. Office365, Yahoo), we would only check for attribute updates every 10 minutes.

This commit makes it so we always check for attribute updates if the server doesn't support CONDSTORE

So for example, when marking a thread as read, we would perform the optimistic update in N1, queue the syncback task which would succeed, but the thread in k2s db would never get updated and become stale, with an unreadCount > 0. If we emitted a delta for that thread during the window of time where we ignored attribute updates, it would be set as unread again in N1, even though all of its messages were read.

This still doesn't guarantee that it wont happen (we could still get a delta for the thread before we actually fetch the attribute updates from IMAP), but before this commit it was sure to happen. This should be properly fixed with the sync scheduler refactor

Test Plan: manual

Reviewers: evan, mark, spang

Reviewed By: mark, spang

Differential Revision: https://phab.nylas.com/D3714
2017-01-16 19:09:18 -08:00
Juan Tejada f34269d20f [local-sync] Prioritize syncing archive for non Gmail accounts!
Summary:
Previously, we were not pripritizing archive sync when getting folders to sync, causing it to be synced almost last. I believe this was causing the issues regarding archived items coming back, because we would optimistically archive in N1, but the changes wouldn't be reflected in K2's database until we synced the archive, causing the data to become out of sync. If for whatever reason we got a delta for any of those messages before the archive was synced, they would pop back in the inbox because in k2, they were still in the inbox. This was exacerbated by the fact that all syncback tasks would interrupt the loop, so we would reach the archive until very late, making this scenario way more likely.

This still wont guarantee that it wont happen, because we dont do /any/ optimistic updates in K2, so we could still get deltas before we actually sync the folder, but makes the scenario way less likely. This should be properly fixed with the sync scheduler refactor

Test Plan: manual

Reviewers: spang, evan, mark

Reviewed By: mark

Differential Revision: https://phab.nylas.com/D3716
2017-01-16 17:46:24 -08:00
Juan Tejada 3cbd7e0e88 [local-sync] Correctly interrup! (don't wait for interruption) 2017-01-16 17:33:08 -08:00
Juan Tejada 87c5dd49b2 [local-sync] Surface send network errors
Summary: see title

Test Plan: manual

Reviewers: evan, halla

Reviewed By: halla

Differential Revision: https://phab.nylas.com/D3708
2017-01-16 12:30:56 -08:00
Christine Spang 5cdd82fc62 [local-sync] On Gmail initial sync, prioritize up to 1k inbox UIDs
Summary:
All we do is use the SEARCH X-GM-RAW IMAP extension to find the UIDs
to prioritize at the beginning of initial sync, and download these UIDs
until there are none left. Then we continue downloading All Mail as
usual.

Because of the way we batch via ranges, the most expedient way to
implement this means that all prioritized emails will end up being
downloaded twice (the second time we'll detect that the message exists
and do nothing).

This seems like a worthwhile tradeoff for quick appearance of the
messages in a user's inbox.

Test Plan: manual

Reviewers: evan, juan

Reviewed By: evan, juan

Differential Revision: https://phab.nylas.com/D3706
2017-01-16 10:47:14 -08:00
Evan Morikawa 9986406c9a [local-private] use new action name 2017-01-16 10:30:41 -08:00
Christine Spang bbae3c2155 [local-sync] throw proper error if invalid range passed to FETCH 2017-01-16 06:29:55 -08:00
Juan Tejada f358cc0b14 [local-sync] Fix yahoo sending
Summary: Was missing from in envelope when sending custom message

Test Plan: manual, tested sending from Gmail, Office365, Yahoo, all work

Reviewers: evan, halla

Reviewed By: halla

Differential Revision: https://phab.nylas.com/D3700
2017-01-15 17:29:00 -08:00
Halla Moore 3e193be099 [local-sync, iso-core] Don't treat inline images as attachments
Summary:
Don't show the attachment icon on threads that only have inline
images. We do this by assuming that inline images have a contentID,
and regular attachments do not. Also updates the way we send
attachments in order to adhere to this standard.

Test Plan: tested manually

Reviewers: spang

Reviewed By: spang

Differential Revision: https://phab.nylas.com/D3696
2017-01-15 17:08:16 -08:00
Juan Tejada 79a8aa9319 [local-sync] 🎨 renaming 2017-01-15 17:04:01 -08:00
Juan Tejada c22703bd6e [local-sync] Fix imap box status check
Summary:
When syncing folders, we check if the folder needs syncing by checking if it has any new messages via the STATUS command (STATUS returns uidnext, highestmodseq among others, and is cheaper than SELECT)

However, we can't issue a STATUS on a box that is already selected. Previously, if the box was already selected, we would just return it, but this was incorrect because we wouldn't get the latest box values (e.g. uidnext), causing us to think that there were no updates available, and skip syncing folders that actually needed to be synced.

Now, if the box is already selected when getting the status, we have to re select it to refresh the latest values

Test Plan: manual

Reviewers: evan, khamidou, spang

Reviewed By: spang

Differential Revision: https://phab.nylas.com/D3697
2017-01-15 16:45:16 -08:00
Halla Moore 450f6fde13 [local-sync] Don't run interrupts within the sync-worker serially
Run them at the same time instead, using Promise.all()
2017-01-15 15:53:43 -08:00
Evan Morikawa fa6aec3cee [local-sync] fix lowerbound error where uid set could be zero 2017-01-15 15:35:58 -08:00
Evan Morikawa a7bd1d66b7 [local-sync] decode quoted-printable encoded attachments
Summary:
Needed to stream process quoted-printable attachments
Fixes T7530

Test Plan: manual

Reviewers: juan, spang

Reviewed By: spang

Maniphest Tasks: T7530

Differential Revision: https://phab.nylas.com/D3690
2017-01-15 15:12:18 -08:00
Juan Tejada e2b317d09d [local-sync] Make all syncback tasks interrupt sync so they run fast
Summary:
This commit also lowers the batch size of messages to fetch on folder sync down to 30. This is in order to prevent sync from getting stuck if we queue too many syncback tasks-- given that we only update the range of fetched uids after we've actually fetched and processed messages, if the batch size is too big and we interrupt too often, we might end up never advancing the range and re fetching the same messages over and over.
This also makes the sync loop run faster through all folders in general.

Depends on D3689 to make sure that the batch size actually reflects a message count, i.e. to ensure that we are making /visible/ progress.

Test Plan: manual

Reviewers: spang, khamidou, evan

Reviewed By: evan

Maniphest Tasks: T7477

Differential Revision: https://phab.nylas.com/D3692
2017-01-15 15:07:49 -08:00
Halla Moore 6312c3a06b [iso-core] Use SUPPORTED_PROVIDERS in Account.smtpConfig()
Summary:
Consolidating provider checks to use the same source of truth.
Fixes send issues with some provider types.

Test Plan: tested locally

Reviewers: tomasz

Reviewed By: tomasz

Differential Revision: https://phab.nylas.com/D3694
2017-01-15 14:37:38 -08:00
Christine Spang d91992cdb8 [local-sync] Fetch at least the batch size of messages on each sync iteration
Summary:
Because we optimistically fetch UIDs by expanding a range without looking
at the actual UIDs in the inbox and the actual space of UIDs with messages
attached may be sparse due to message moves, we need to track how many
messages we actually download during a range expansion and continue
expanding the range if we haven't downloaded enough messages.

If we reach a large gap where we download no messages at all during a batch, we
pause and check the actual UID list for the folder for the next UID to
download, as otherwise we may spin indefinitely fetching UIDs that don't exist.
(Example: my "Deleted Items" folder had about 300k worth of empty UIDs between
a very small UID and a very large UID. With the new system, this registers as a
completed sync within a single iteration as soon as sync hits the gap.)

Test Plan: manual

Reviewers: juan, evan

Reviewed By: juan, evan

Differential Revision: https://phab.nylas.com/D3689
2017-01-15 14:28:24 -08:00
Karim Hamidou 078b015202 Fix unit test path. 2017-01-15 14:09:05 -08:00
Karim Hamidou aadc322ef4 [local-sync] Back off exponentially when getting a sync error
Summary:
This patch changes the sync worker to back off exponentially when there is an issue syncing an account. This has two goals:
- first, it's a bit dangerous to retry immediately. We don't want hundreds of thousands of machines trying to refresh tokens unsuccessfully because our service is struggling.
- second, it's nicer on the CPU to wait a bit between retries.

Currently, we sleep for at most 2 minutes, with some random jitter added.

Test Plan: Tested manually, stared at the code a long time.

Reviewers: evan, juan

Reviewed By: evan, juan

Differential Revision: https://phab.nylas.com/D3684
2017-01-15 11:57:47 -08:00
Halla Moore 77ad25af24 [local-sync] Stop sync worker before deleting account database
Summary:
Various errors are thrown when the sync worker tries accessing
a database that we've already deleted, so make sure the sync
worker has been stopped before we remove the database. This diff
involves modifying `Interruptible` so that `interrupt()` returns
a promise that resolves once the interrupt has been completed.

Addresses T7472

Test Plan: manual

Reviewers: evan, juan

Reviewed By: evan, juan

Differential Revision: https://phab.nylas.com/D3679
2017-01-15 10:33:12 -08:00
Christine Spang f06ba78d8a [local-sync] Update comment on _processExistingMessage 2017-01-15 10:06:52 -08:00
Christine Spang 9e426419c5 [local-sync] Correctly add references when processing existing messages 2017-01-15 10:04:29 -08:00
Evan Morikawa 9381b51746 [local-sync] fix fetch unsynced messages logic looking for min 2017-01-14 18:36:51 -08:00
Juan Tejada 50f00e5174 [local-sync] Fix order of execution in fetch-messages-in-folder 2017-01-14 17:43:42 -08:00
Halla Moore c5453ca21b [iso-core] Let auth methods accept all supported providers
Summary: Treat any that aren't gmail or office365 as standard imap

Test Plan: manual

Reviewers: juan, evan

Reviewed By: evan

Differential Revision: https://phab.nylas.com/D3686
2017-01-14 17:35:09 -08:00
Evan Morikawa ae981af646 [local-sync] fix performance of attribute changes
Summary:
On MG's machine this function is EXTREMELY non performant and causes
things like archive to lock up when the console is running here for some
reason. Not entirely sure exactly what's causing it, but there were some
simple DB cleanups that will make it faster for large queries.

There's likely other things involved since the sequelize DB being locked
up shouldn't affect the peformLocal of the edgehill db for things like
archive. Still looking into that

Test Plan: manual

Reviewers: juan

Reviewed By: juan

Differential Revision: https://phab.nylas.com/D3683
2017-01-14 17:27:15 -08:00
Juan Tejada 814581f3d0 [local-sync] 🎨 2017-01-14 17:23:43 -08:00
Juan Tejada fc433dd6c6 [local-sync] Await instead of yielding when fetching the very first batch of messages in folder sync 2017-01-14 16:32:14 -08:00
Juan Tejada f4f78f4449 [local-sync] Minor syntax fix 2017-01-14 16:22:53 -08:00
Juan Tejada 7b2e27b87b [local-sync] Skip unecessary folder syncs
Summary:
Before trying to sync a folder, check if we actually need to do so. This will prevent us from doing unnecessary work that slows down the sync loop (like performing SELECT commands)

We will perform a folder sync if any of the following are true
- The folder hasn't been completely synced
- There are new messages (using imap STATUS command)
- There are attribute changes indicated via highestmodseq (using imap STATUS command)
- If server doesn't support highestmodseq, it has passed enough time since we last ran an attribute scan on the folder.

Addresses T7513

Test Plan: manual

Reviewers: evan, halla, spang

Reviewed By: halla, spang

Differential Revision: https://phab.nylas.com/D3675
2017-01-14 15:56:17 -08:00
Christine Spang ff9c2fd57c [local-sync] grr phab lost this update 2017-01-14 14:57:22 -08:00
Christine Spang 70a4b0fdcf [local-sync] Fetch min UID in each folder for use in sync state
Summary:
Currently, our mail sync strategy of expanding UID ranges from UIDNEXT
backwards until a UID of 1 implicitly assumes that every UID corresponds to an
actual message. This assumption is incorrect, and results in several
significant bugs regarding sync status.

This patch fixes issue 1:

Since UIDs are persistent and, so long as the UIDVALIDITY is valid, ascend
monotonically upward, every time you move a message to a new folder you "lose"
UIDs lower down in the range. In my work Inbox, where I get a lot of mail,
archive all the time, and generally have only a small number of threads in the
mailbox, the smallest UID is over 100k.  This means that, after all my inbox
messages are synced, the sync loop will continue attempting to download
nonexistent old messages in this mailbox for hundreds of sync iterations, and
will not mark the mailbox as fully synced until fetchmin reaches 1, regardless
of the fact that there are no actually messages being pulled down.

This patch needs a small associated patch to N1 to update how sync status is
calculated (coming soon).

The next patch in this series will deal with gaps in the UIDspace that slow
down syncing of a folder.

Test Plan: manual

Reviewers: halla, juan

Reviewed By: juan

Differential Revision: https://phab.nylas.com/D3677
2017-01-14 14:52:35 -08:00
Evan Morikawa 4b7d3b4c2e [isomorphic-core] allow custom smtp configs 2017-01-14 14:47:15 -08:00
Juan Tejada bda6a78ae1 [local-sync] Cleanup, use Provider constants 2017-01-14 14:31:51 -08:00
Christine Spang 941c564443 [local-sync] fix comment 2017-01-14 13:31:24 -08:00
Christine Spang 85a4e31980 [local-sync] canonicalize references 2017-01-14 13:31:16 -08:00
Evan Morikawa cd1548ac26 [local-sync] fix duplicate ensure in sent folder 2017-01-13 19:31:15 -08:00
Juan Tejada a5fec9e7a8 [local-sync] Separate sending tasks from the sync-loop
Summary:
We want to do this in order to prevent send tasks from blocking the sync loop given that they can take a very long time to run. This is especially true when sending emails with large attachments to multiple recipients.
There is no real way to make sending in these cases faster, but we can prevent it from blocking the sync loop at least, especially because sending is mostly I/O bound.

This is a bit messy actually, but should be fixed when we properly implement a sync scheduler

Also added a limit to the total size of attachments you can upload to try to prevent weird EPIPE errors when sending.
See: D3670.

Also moved and renamed stuff a little

Test Plan: manual

Reviewers: halla, evan

Reviewed By: evan

Differential Revision: https://phab.nylas.com/D3669
2017-01-13 19:01:52 -08:00
Evan Morikawa 0f3f5c6ae2 [local-sync] fix reset accounts and data button
Summary: Allows us to reset accounts in local-sync too

Test Plan: manual

Reviewers: mark, juan

Reviewed By: juan

Differential Revision: https://phab.nylas.com/D3672
2017-01-13 18:59:14 -08:00
Evan Morikawa 38184d1847 [local-private] use new Sentry endpoint 2017-01-13 16:17:24 -08:00
Halla Moore f4edc55752 [local-sync] Update oldestProcessedDate logic
Summary:
I happened to be testing between Jan 2017 and Dec 2016, so I
missed this logic flaw. Boo.

Test Plan: tested locally

Reviewers: evan

Reviewed By: evan

Differential Revision: https://phab.nylas.com/D3667
2017-01-13 15:42:23 -08:00
Halla Moore b40ff948ca [local-sync] Make sure we're saving sent messages with 'SEEN' flag
Summary: We did this for gmail, but not for other providers.

Test Plan: tested locally

Reviewers: juan, spang

Reviewed By: spang

Differential Revision: https://phab.nylas.com/D3665
2017-01-13 15:03:29 -08:00
Juan Tejada ffbdfb7fd2 [local-sync] fix build 2017-01-13 15:00:27 -08:00
Halla Moore a828d517d4 [local-private] Update task label
Remove ellipsis, since it is now included as part of a css psuedo element
2017-01-13 13:39:50 -08:00
Juan Tejada 1c37a8b788 [local-sync] Cleanup sync tasks to be more consistent with syncback tasks
Summary: While working on separating send out of the sync loop, I realized sync tasks could use some cleanup to be more consistent with how we implemented syncback tasks. I reorganized and renamed things a little bit. This will also help us move in the direction of the scheduler implementation under which everything is a task.

Test Plan: manual

Reviewers: evan

Reviewed By: evan

Differential Revision: https://phab.nylas.com/D3660
2017-01-13 12:30:43 -08:00
Halla Moore 30037f915d [local-sync] Add oldestProcessedDate to Folder.syncState
Summary:
Only updated within month precision. We can use this to show how
far back a folder has been synced.

Test Plan: tested locally

Reviewers: juan, evan

Reviewed By: evan

Differential Revision: https://phab.nylas.com/D3662
2017-01-13 12:05:45 -08:00
Evan Morikawa e197de86e0 [local-sync] fix attribute scanning in sync worker
Summary:
Fixes https://phab.nylas.com/T7435

The old deepScan (now `scanForAttributeChanges`) and shallowScan (now
`fetchLatestAttributeChanges`) had some fatal flaws.

If you deep scanned it would attempt to load the message attributes of all
messages ever and cause very bad memory leaks.

Also, if you left a mailbox running for a long time, there was a query
that would eventually run `Message.findAll` and, even though it was just
returning the headers, would still run insanely expensive operations

This fixes (and renames) these issues.

Test Plan: manual

Reviewers: spang, halla, juan

Reviewed By: juan

Differential Revision: https://phab.nylas.com/D3657
2017-01-13 12:00:45 -08:00
Christine Spang 7c8146f81b [local-sync] Fix parsing of existing messages derp 2017-01-13 11:15:24 -08:00
Christine Spang a23813a11c [local-sync] For generic IMAP, Thread based on Message-Id, In-Reply-To & References
Summary:
This swaps out our generic IMAP threading mechanism to use the threading
headers on the message instead of the prior way of grouping by subject
and then differentiating based on participants, as that design was
somewhat driven by what we could accomplish easily given legacy data
schema decisions and has serious caveats, such as different threads between
the same people with the same subject being misthreaded together. With K2, we
have free reign to change the data format, so we can do it right.

The algorithm is super simple:
- Define "references" as the union of the Message-Id, In-Reply-To, and
References headers on a message, filtered for valid RFC2822 Message-IDs
- On message sync, if any element of the new message's references
matches any element of an existing message's references, thread them
together

In order to accomplish this, we need to store References in a way that
allows each element to be indexed for fast lookup. That meant either
using the sqlite JSON1 extension + expression-based indices, or creating
a new table. I chose the latter as a time-tested and simple solution,
since we don't need the flexibility of JSON here.

Test Plan: manual - unit tests coming

Reviewers: khamidou, evan, juan

Reviewed By: evan, juan

Differential Revision: https://phab.nylas.com/D3651
2017-01-13 10:39:54 -08:00
Christine Spang 0dcbcee06c [local-sync] Messages generated for send should not be unread 2017-01-13 07:46:18 -08:00
Christine Spang 99309f9c69 [isomorphic-core] Disable nodemailer's automatic X-Mailer header
Examined the headers on a message we sent and found this:
> X-Mailer: nodemailer (2.5.0; +http://nodemailer.com/; SMTP/2.6.0[client:2.8.0])

No need to plaster which sendmail library we're using all over every
email our users send. Turn it off!
2017-01-12 14:16:47 -08:00
Juan Tejada 238b79d06c [local-sync] Don't sync spam until everything else is synced
Summary: Addresses T7426

Test Plan: manual

Reviewers: evan, spang

Reviewed By: spang

Differential Revision: https://phab.nylas.com/D3655
2017-01-12 13:53:17 -08:00
Mark Hahnenberg 59b5961a6c [syncback] Reuse currently open box if possible
Summary:
Previously we would unconditionally issue a SELECT when openBox was
called. Now we check if the currently open box is the one we want first and
return immediately if it is, avoiding the unnecessary SELECT (which can be
quite expensive on large folders like INBOX). We were also calling closeBox
after iterating all the messages in a thread to mark them as read/unread.
This was unnecessary and was causing extra SELECTs to be issued. Now we don't!
This diff is a 5x speedup over the old behavior when marking lots of
threads in the same folder as read all at once.

Test Plan: Run locally, measure perf with log statements

Reviewers: evan, juan

Reviewed By: evan, juan

Differential Revision: https://phab.nylas.com/D3654
2017-01-12 13:16:07 -08:00
Mark Hahnenberg 6a03bbe73f [local-sync] Fix error thrown when Gmail returns no search results
Summary:
We were returning the wrong type in the case that we got no messages
back from the Gmail search API.

Test Plan: Run locally

Reviewers: evan

Reviewed By: evan

Differential Revision: https://phab.nylas.com/D3646
2017-01-12 11:57:07 -08:00
Jackie Luo 21049b2a1f Update Nylas N1 to Nylas Mail
Test Plan: Tested locally.

Reviewers: juan, evan

Reviewed By: juan, evan

Differential Revision: https://phab.nylas.com/D3644
2017-01-12 11:25:39 -08:00
Juan Tejada ab7f994fef [local-sync] Ensure we get new mail ASAP via dedicated imap conn
Summary:
Add a new dedicated imap connection to listen for any updates or new mail on the inbox.
Previously, we wouldn't be able to receive new mail events on the inbox during the sync loop
because other mailboxes would be open while we sync them. This would cause big
delays in receiving new mail, especially if you have a lot of folders

Test Plan: manual

Reviewers: spang, evan

Reviewed By: evan

Differential Revision: https://phab.nylas.com/D3650
2017-01-12 11:16:53 -08:00
Halla Moore 914d69557d [iso-core]: Make sure deltaStreamBuilder passes along delete deltas
Summary:
We don't want to inflate delete Transactions, but we do still want
to pass the delta itself along.

Test Plan: tested locally

Reviewers: evan

Reviewed By: evan

Differential Revision: https://phab.nylas.com/D3647
2017-01-12 10:21:45 -08:00
Christine Spang ed57422412 [local-sync] Enable toggling ENABLE_SEQUELIZE_DEBUG_LOGGING on launch via an env var
It's much more convenient to be able to turn this on/off without
editing the code.
2017-01-12 07:43:34 -08:00
Christine Spang d36ae8db3c [local-sync] Still download messages when they have no body
There are plenty of valid use cases for sending subject-only emails,
and we also want to still download the headers and create message
objects if e.g. the email consists of only an event invitation or
an attachment.
2017-01-11 18:08:20 -08:00
Juan Tejada 3eb450e22c [local-sync] Make sure IMAPConnection times out correctly
Summary:
This commit passes down the `socketTimeout` option to node-imap. However, just
passing this option doesn't seem to work reliably, so this commit manually implements
the socketTimeout option for our IMAPConnection.

How it works is that basically every operation is wrapped with a timeout by
augmenting  the `createConnectionPromise` construct that already existed.

Test Plan:
Locally, tested by sleeping computer and turning off wifi. The
connection will successfully error and be restarted. It will reconnect when the
network is available again

Reviewers: khamidou, halla, evan

Reviewed By: evan

Differential Revision: https://phab.nylas.com/D3642
2017-01-11 16:48:10 -08:00
Christine Spang e1271cf25c [local-sync] Give Nylas Mail messages a different Message-ID from API messages
Summary:
We may find it useful at some point to be able to tell which messages in
a user's mailbox were sent using N1/the REST API vs Nylas Mail.

Test Plan: manual

Reviewers: evan, juan

Reviewed By: juan

Differential Revision: https://phab.nylas.com/D3631
2017-01-11 15:51:12 -08:00
Karim Hamidou 9136b0592b [fix] Add a poor man's two-phase commit algorithm for syncback tasks.
Summary:
Sometimes things go very wrong when trying to syncback an action. For example, the worker window could crash, preventing us from marking the current task as failed. What's worse, another sync iteration could try to run the task which crashed, thinking it a new one. To prevent this, this diff adds a fourth syncback task state, `INPROGRESS`.

New syncback tasks are marked as `INPROGRESS` before being executed. When they complete we mark them as `SUCCEEDED/FAILED`. Stray `INPROGRESS` tasks are automatically marked as `FAILED` at the beginning of every sync iteration, to make sure we don't retry them again.

Test Plan: Tested manually.

Reviewers: evan, juan

Reviewed By: evan, juan

Differential Revision: https://phab.nylas.com/D3635
2017-01-11 12:04:58 -08:00
Evan Morikawa 47f5880d18 [local-sync] fix multiple sync loops starting
A bug allowed multiple sync loop processes to start. This could lead to
double sending and the sync loop appearing as thouogh it couldn't be
interrupted
2017-01-10 18:24:38 -08:00
Juan Tejada 202d56d3f2 Fix error reporter causing crash due to infinite recursion 2017-01-10 15:30:30 -08:00
Christine Spang 9eb1aef139 [local-sync] Properly handle multiple occurrences of From/To/Cc/Bcc headers
Summary:
Well-behaved MUAs don't do this, but it is totally legal and we should
handle it.

The Python sync engine handles this properly also:
7db949fec9/sync-engine/inbox/util/addr.py (L26-L47)

(Found this out while scratching my head over the output format of
mimelib.parseHeaders---turns out it's an array for a reason:

> let to = mimelib.parseHeaders(`To: Christine Spang <spang@nylas.com>
... To: Foo Bar <foo@example.com>`);
undefined
> to
{ to:
   [ 'Christine Spang <spang@nylas.com>',
     'Foo Bar <foo@example.com>' ] }

)

Test Plan: unit test included

Reviewers: halla, jackie, mark

Reviewed By: mark

Differential Revision: https://phab.nylas.com/D3625
2017-01-10 15:07:36 -08:00
Christine Spang 15ac9f795b [local-sync] Remove deprecated comment
With the latest update to N1's CSS, this is no longer true---though a theme
can easily override the font to monospace if so desired.
2017-01-10 13:39:54 -08:00
Juan Tejada 79006f692d [local-sync] Fix check for new mail updates on inbox folder 2017-01-10 12:19:15 -08:00
Juan Tejada b74aad7314 [local-sync] Run send tasks before other syncback tasks
Summary: Update `_getNewSyncbackTasks` to return any send tasks first, and then others

Test Plan: locally

Reviewers: halla, evan

Reviewed By: halla, evan

Differential Revision: https://phab.nylas.com/D3627
2017-01-10 12:11:34 -08:00
Christine Spang 27b2c41fcc [local-sync] Make comment about Date/INTERNALDATE & message hashes better 2017-01-10 12:05:14 -08:00
Juan Tejada 80708dacbc [local-sync] Make syncback task execution interruptible
Summary:
See title. Got rid of that syncback-worker class which was kind of useless and
made things harder. My b.

Test Plan: locally

Reviewers: evan

Reviewed By: evan

Differential Revision: https://phab.nylas.com/D3624
2017-01-10 10:45:53 -08:00
Christine Spang a234570118 [local-sync] Rename extract{Snippet,Contacts} in message-factory
Summary:
We have another function called extractContacts, in its own file,
extract-contacts.js, which is used to create Contact objects. This has
confused me a number of times and also leads to grep collisions.

This patch also makes the snippet unit tests pass again after a recent
API change.

Test Plan: included unit tests, manual

Reviewers: halla, evan, juan

Reviewed By: juan

Differential Revision: https://phab.nylas.com/D3623
2017-01-10 10:41:35 -08:00
Christine Spang fb705fc5dd [local-sync] Remove unused message update hook
Summary:
Message bodies, drafts aside, are immutable, and we set the snippet on
new messages manually in parseFromImap()---meaning this hook, if
invoked, is likely to replace the snippet with a broken version computed
with this old implementation. If we need a hook in the future (e.g. for
updating drafts), it should use the snippet function from
message-factory.

Test Plan: n/a

Reviewers: juan, halla

Reviewed By: juan

Differential Revision: https://phab.nylas.com/D3622
2017-01-10 10:39:43 -08:00
Halla Moore 1e6f7a6e6f [local-sync] Don't delete messages via cascade when a folder is deleted
1. It could be expensive to delete many messages at the exact moment
   when the folder is deleted
2. The folder delete could actually just be a rename, and if we
   deleted all the messages, we would have to re-process them all
3. We already do a clean-up check for orphaned messages at the end
   of the sync loop, where we already know if the folder was
   actually deleted or just renamed
2017-01-10 10:37:08 -08:00
Evan Morikawa b03c0c9bc2 [isomorphic-core] don't try and inflate delete deltas
Summary:
If we inflate delete deltas, the object we're trying to find won't exist
anymore (we just deleted it!). This is likely causing the `While inflating
${sourceName} transactions, we couldn't find models for some ${modelName}
IDs` error.

Fixes T7436

Test Plan: manual

Reviewers: spang, juan, halla

Reviewed By: halla

Maniphest Tasks: T7436

Differential Revision: https://phab.nylas.com/D3621
2017-01-10 10:21:14 -08:00
Christine Spang ac08163c51 [local-sync] s/queryes/queries/ 2017-01-10 08:26:44 -08:00
Christine Spang b618c4ef28 [local-sync] Fix typo in ENABLE_SEQUELIZE_DEBUG_LOGGING 2017-01-10 08:16:16 -08:00
Halla Moore 2ed1a03d34 [local-sync, iso-core]: Cascade deletes on hasMany associations
Summary:
Delete associated children when a parent is deleted to prevent foreign
key constraint errors. Also make sure any child hooks are run.

Test Plan: tested locally

Reviewers: evan

Reviewed By: evan

Differential Revision: https://phab.nylas.com/D3620
2017-01-09 18:13:09 -08:00
Halla Moore aaf0b04ae3 [local-sync] Add a hasAttachment attribute to threads
Summary: N1 uses this to show the little attachment icon in the thread list.

Test Plan: tested locally

Reviewers: evan, juan, spang

Reviewed By: spang

Differential Revision: https://phab.nylas.com/D3617
2017-01-09 17:20:55 -08:00
Christine Spang a959d546d6 [local-sync] Update comment 2017-01-09 15:24:55 -08:00
Juan Tejada 82dbc222b5 [local-sync] Fix send, correctly check for presence of headers 2017-01-09 15:17:19 -08:00
Juan Tejada d48ed92d66 [local-sync] Make the sync loop interruptible
Summary:
This commit introduces interruptible sync operations. Now, the `SyncWorker`, `FetchFolderList` operation and `FetchMessagesInFolder` operation can be interrupted at several points during their execution. This improves the performance of SyncbackTasks, which now run almost immediately.

To achieve this, this commit adds an Interruptible abstraction, which is an object that can run functions and interrupt them at points marked by the function. For more info on how this works, see the docs on the Interruptible class.

This commit also splits up the SyncWorker a little bit to make it smaller, byadding a SyncbackTaskWorker.

Depends on D3613

Test Plan: Manual

Reviewers: spang, mark, jackie, khamidou, evan, halla

Reviewed By: evan, halla

Differential Revision: https://phab.nylas.com/D3612
2017-01-09 14:43:15 -08:00
Evan Morikawa 73db7a4e46 [local-sync] fix changedsince highestmodseq causing N1 crash
Since we weren't giving Node IMAP the proper format for the changedsince
flag, and since node IMAP never warned of improper schemas, we weren't
properly requesting the correct range. This would cause us to request the
metadata attributes of EVERY message in the mailbox and attempt to store
them in a hash. This would eventually lead to a memory leak and take down
the worker window, which caused other subtle issues like sends failing
when the worker window dropped task half way through their perform remotes
and never re-sent the deltas notifying of their success or failure.

This was only triggered when new highestmodseq numbers fired on the remote
server, which would be triggered by the underlying mailbox getting folders
or labels changed on messages
2017-01-09 14:07:31 -08:00
Christine Spang 7073e19fe7 [local-sync] Don't filter contacts w/out emails out of To/From/Cc/Bcc fields
We separately filter out contacts without email addresses before
committing to the contacts table in the database for autocomplete (in
isContactMeaningful()), and if we filter out these already we can end up
excluding legitimate elements of the headers. For example, the Clutter
feature of Office 365 sends emails with a From: header like this:

From: Microsoft Outlook

Fixes: T7413
2017-01-09 13:45:18 -08:00
Halla Moore 1ba4a6eaf2 [local-sync] Add support for inline-images
Summary:
Extract files for inline attachments and store their content id

Fixes T7414

Test Plan: tested locally

Reviewers: evan, spang

Reviewed By: spang

Maniphest Tasks: T7414

Differential Revision: https://phab.nylas.com/D3609
2017-01-09 10:44:42 -08:00
Tomasz Finc 2c0fd79707 Fixing most lint errors in error-logger
Summary: Cleaning up almost all the linting errors in the logger

Test Plan: ... run the build

Reviewers: juan, evan

Reviewed By: evan

Differential Revision: https://phab.nylas.com/D3605
2017-01-09 10:38:48 -08:00
Evan Morikawa afdc5a3ef9 [local-api] linter fixes 2017-01-09 09:57:06 -08:00
Evan Morikawa 4a760ff5ec [local-sync] audit database indices
Summary:
Fixes T7398

We were create unnecessary and duplicate indices for the IDs of all of
our objects and increasing db write overhead.

We were not creating the correct reverse index for our join tables.

The search API'd db is already in scope of the accountId, this is an
unnecessary constraint on the query

Test Plan: manual

Reviewers: spang, juan

Reviewed By: juan

Maniphest Tasks: T7398

Differential Revision: https://phab.nylas.com/D3606
2017-01-09 09:45:33 -08:00
Christine Spang 6d72bb2aaf [local-sync] Optimize header & MIME structure download
Summary:
Headers can be quite big, so we might as well download and store only
the ones that we care about. This patch also makes it so we stop
downloading MIME structures twice per message.

While it's possible that we _may_ want to make more headers accessible
later, we don't currently make the generic pile of headers accessible to
N1 or N1 plugins in any way, so doing that would end up requiring
changes to the sync code regardless. I think it's worth optimizing the
base experience rather than trying to predict what we may want in the
future. Plus, it seems more likely that we'll want to build future
extensibility using thread metadata, rather than message headers.

On inboxapptest1@fastmail.fm, this patch decreases the size of the
generated sqlite file for a fully synced mailbox by 35%.

Test Plan: manual

Reviewers: juan, evan

Reviewed By: evan

Differential Revision: https://phab.nylas.com/D3611
2017-01-08 13:27:10 -08:00
Christine Spang 9d05bb9d1c [local-sync] don't play fast and loose with newlines, or a lack thereof 2017-01-07 14:34:03 -08:00
Christine Spang 8238fe9594 [local-sync] Correctly handle messages with non-alternative multipart text bodies
Summary:
It's possible to have multiple inline HTML parts in a message, or even
a multipart/alternative part that contains text and HTML, followed by a
plaintext signature. Previously, if there was more than one text part in
an email, we would pick the _last_ text/html or text/plain part that we
found, and treat that as the entire message body. This works most of the
time, but fails to display the full message body in some edge cases.
This patch fixes that by resolving multipart/alternative subparts to a
single part in the mimepart fetch stage, and then treating each desired
mime part separately when parsing the message, concatenating them if
there are multiple.

This makes K2's handling of multipart MIME message text better,
bug-wise, than the Python sync engine's, which has been mangling some
rare messages forever. (Example from my email: every email from the MIT
EECS Jobs List has never displayed the mailing list signature in N1.)

Note that this patch also removes our tentative support for PGP
encrypted messages. I'd rather add that back in later when I've dug up
some real example messages to test on, rather than leaving it in in its
current not-really-tested and probably not-really-working state, since
it makes it harder to make sure that the rest of the logic isn't broken.

Test Plan: manual for now - added examples of this to my growing list of regression tests to add to the message parser unit tests once I fix them

Reviewers: juan, evan

Reviewed By: evan

Differential Revision: https://phab.nylas.com/D3600
2017-01-07 14:11:27 -08:00
Christine Spang 78aa3291d6 [local-sync] Bump limit on finding matching threads
I'm kind of worried that weird stuff may happen on short, common
thread subjects. The Python sync engine has _no_ limit, and it seems
to work OK.
2017-01-07 13:13:22 -08:00
Christine Spang 4aa6cb3ca1 [local-sync] Port generic IMAP threading logic from Python Sync Engine
Summary:
We sync messages in the same order as the Python sync engine (newest to
oldest, generally), so we should be able to just use the same threading
algorithm. While we may still want to take into account References /
In-Reply-To at some point, this is a big step up from the current
thread-matching-only.

Test Plan: manual --- could pretty easily port the unit tests from the python codebase if we wanted

Reviewers: khamidou, juan, evan

Reviewed By: evan

Differential Revision: https://phab.nylas.com/D3604
2017-01-07 13:11:33 -08:00
Halla Moore c4588ec011 [local-sync] Re-add code to associate labels with new messages
This code was accidentally removed during a merge conflict. Code was
originally added in commit 333647.
2017-01-07 12:48:37 -08:00
Jackie Luo e7de61b56a [local-sync] Designate role for archive folder 2017-01-06 16:53:24 -08:00
Evan Morikawa 33080805e3 [local-private] Disable mail merge, scheduler, send later, reminders 2017-01-06 15:37:18 -08:00
Evan Morikawa 6f54d9fa6a [local-sync] remove verbose sequelize flag in fork for perf
https://github.com/nylas/sequelize/compare/9cdd4d6bfaa1e7d61a700a6ac3c0e64e45a61...nylas-3.30.0

We spend a HUGE amount of CPU in the sqlite3 verbose logging since we generate stack traces for each and every query we send.

This fork removes the verbose call that causes that.
2017-01-06 15:18:57 -08:00
Juan Tejada 6284905714 Revert "Make K2 recover from connectivity losses."
This reverts commit 0b3e3d2f39.

Interrupting sync by closing connection causes errors downstream when
`syncNow` is called elsewhere. Instead of interrupting by closing the
connection, we will post a patch to interrupt the sync loop properly
2017-01-06 14:30:18 -08:00
Juan Tejada 83ef8c12b3 [local-sync] Restore global queue for message processing to improve perf
Summary:
Sync operations are mostly bound by I/O and the imap connection.
What we believe that is mostly affecting cpu and battery life is that node’s event
loop is being hosed with cpu intensive message processing operations.

To alleviate this, we do a few things:

- Restore a global message processing queue to process messages serially and meter cpu usage (message processing continues to be a fire and forget call from within sync operations)
- Move actual cpu intensive work to the message processing queue, i.e. `MessageFactory.parseFromImap`
- Keep track of message processing queue length, and skip sync operations if queue is too big to prevent massive memory consumption

This commit also renames the package from new-message-processor to
message-processor, given that now it processes both new and existing
messages, and we like to minimize confusion.

Test Plan: manual

Reviewers: spang, khamidou, evan

Reviewed By: evan

Differential Revision: https://phab.nylas.com/D3602
2017-01-06 14:28:33 -08:00
Halla Moore c7f8796409 [local-sync, iso-core] Fix operations on Categories
Summary:
- Ensure delete deltas make it through to N1
- Don't fail if we can't find a category that needs to be deleted

Test Plan: local

Reviewers: juan, evan

Reviewed By: evan

Differential Revision: https://phab.nylas.com/D3593
2017-01-05 16:46:11 -08:00
Evan Morikawa 4030d7cb3b [local-sync] add attachments
Summary:
- Adds `File` objects onto the `Message`s so N1 sees attachments
- Ensures `File` is eagerly loaded for all messages
- Base 64 streams attachments through the local /download endpoint
- ExtractFile only uses disposition type attachment when extracting
  attachments
- Makes sure we save existing messages when processing them

Test Plan: manual :(

Reviewers: juan, spang

Reviewed By: spang

Differential Revision: https://phab.nylas.com/D3595
2017-01-05 16:33:38 -08:00
Christine Spang d6e0b7eb8d [local-sync] Do not consider HTML or plaintext attachments to be body parts
Summary:
We were previously not taking into account the 'Content-Disposition'
MIME header, which differentiates between parts intended for display
('inline') and parts that are instead transferred files ('attachment').
See the RFC for more details:

https://www.ietf.org/rfc/rfc2183.txt

Fixes: T7367

Test Plan: unit test coming soon---have the test data and going to fix all message parsing test cases at once

Reviewers: juan, jackie, evan, halla

Reviewed By: evan, halla

Differential Revision: https://phab.nylas.com/D3585
2017-01-05 16:18:48 -08:00
Jackie Luo 040426e80a [local-sync] Include from field in thread participants 2017-01-05 13:00:57 -08:00
Halla Moore c7bec7150a [local-sync] Make sure labels are properly associated to new messages
Summary:
Labels don't get added via passing in a labels attribute to
create(). We need to call addLabels() instead.

Test Plan: Tested locally

Reviewers: juan, evan

Reviewed By: evan

Differential Revision: https://phab.nylas.com/D3581
2017-01-04 17:49:22 -08:00
Christine Spang 0cbe8e2600 [local-sync] Trim NUL bytes from body strings
These bytes will cause SQLite to blow up with the following error
on insertion:

'SQLITE_ERROR: unrecognized token'

Fixes: T7331
2017-01-04 15:47:16 -08:00
Christine Spang 0b53d8599f Update sequelize point version everywhere 2017-01-04 15:47:16 -08:00
Evan Morikawa 4237cf2bd5 refactor(send): split delivery from sent folder stuffing
Summary:
refactor multi-send

This diff started off by fixing sending with attachments.

The issue is that our `FileUploadStore` listened for
`Actions.sendDraftSuccess` as its signal to remove the files from the
.nylas temp directory. Unfortunately, the old MultiSend tasks, after
delivery of the message, would try and put the base message in the sent
folder. Since we already deleted the file from our local temp dir,
creating the base message for the sent folder would fail.

This exposed a much bigger issue which is that we don't consistently
distinguish between "delivery" of a message and any post-processing we do
(like filling the sent folder). This was leading to a variety of other
subtle issues.

For example, N1 assumes that if the SendMessage task fails, then we pop
the draft back up and ask the user to try again. Unfortunately, since we
were combining "delivery" and "post processing" it was possible for the
message to actually deliver, but fail when stuffing the sent folder, or
fail due to some other random bug. This would cause the user to send the
message twice.

To help us ensure we never "deliver" twice and handle errors more
intuitively, I separated out the two concepts.

Now there are "send" set of tasks and endpoints, and a
"EnsureMessageInSentFolder" set of tasks and endpoint (the latter used to
be ambiguously known as ReconcileMultiSend, whatever that meant)

The logic for send hasn't changed. This is mostly a renaming and moving
files around.

Test Plan: manual :(

Reviewers: jackie, juan, halla

Reviewed By: juan, halla

Differential Revision: https://phab.nylas.com/D3577
2017-01-04 15:41:35 -08:00
Christine Spang f5ef0323c0 [local-sync] Bump sequelize version to latest stable
Nothing major in here, just a couple little bugfixes.
2017-01-04 08:58:22 -08:00
Christine Spang c8e0f4453d [local-sync] Remove no-longer-used getLengthValidator function 2017-01-04 08:21:47 -08:00
Christine Spang dfcf5e0d11 [local-sync] Remove incorrect length validation on From field
Contrary to what you might think, a message can have both an empty From: header
and multiple From: headers / multiple addresses in a From header. In that case,
we must save all of them and let the client decide how to display.

Fixes: T7370
2017-01-04 08:19:35 -08:00
Karim Hamidou 0b3e3d2f39 Make K2 recover from connectivity losses.
Summary: I've found a pretty annoying bug --- N1 would stop syncing all accounts after the Internet connection dropped. It seems that deep inside node-imap or NodeJS itself, connections aren't timing out the right way. To work around this, this diff unilaterally restarts the sync every `nextSyncIn` milliseconds.

Test Plan: Tested manually by cutting internet access and checking that K2 recovered.

Reviewers: evan, juan

Reviewed By: evan

Differential Revision: https://phab.nylas.com/D3573
2017-01-03 16:03:27 -08:00
Christine Spang 0ac22782cf [local-sync] Add node-iconv dependency
The 'encoding' library transparently upgrades to using iconv instead of
iconv-lite, if available. This allows us to support more encodings in
emails, such as ISO-2022-JP.

Fixes: T7358
2017-01-03 11:17:10 -08:00
Juan Tejada c3aa82adfc [local-sync] Convert to async 2017-01-03 09:37:33 -08:00
Evan Morikawa ac20d5b038 [local-sync] show messages sent to self
Summary:
If you sent an email to yourself it would not show up in your inbox. This
is because sent messages would never get a lastMessageReceived timestamp.
Since we order the inbox by lastMessageReceived, setting that to null to
on sent mail would mean it never shows up in the thread list.

Also fixed an assertion bug in SFDC that requires transactions to return a
promise.

Finally added extra debug interfaces that will show more info if the delta
stream detects an inconsistency

Test Plan: manual

Reviewers: juan, halla, jackie

Reviewed By: jackie

Differential Revision: https://phab.nylas.com/D3552
2017-01-03 09:31:34 -08:00
Mark Hahnenberg a0c8d8b692 [local-sync] Return IMAP UIDs for Messages via API
Summary: See title

Test Plan: Run locally

Reviewers: evan, spang, juan

Differential Revision: https://phab.nylas.com/D3569
2016-12-30 13:11:41 -08:00
Jackie Luo a2c9555e2a [local-sync] Fix dates for message hashing
Summary: We were creating duplicate `Message` objects because the formatting for the date was different between `buildForSend` and `parseFromImap`. Now, we create the initial hash using the same format that `buildmail` uses to ensure that we generate the same IDs.

Test Plan: Tested locally.

Reviewers: evan, juan, spang

Reviewed By: spang

Differential Revision: https://phab.nylas.com/D3559
2016-12-29 15:58:21 -08:00
Christine Spang 40c7b09e27 [local-sync] Use mimelib to parse contacts
Summary:
Because of the way we were attempting to parse contacts from
From/To/Cc/Bcc headers by converting them to JSON with a regex, we were
erroneously breaking contacts that contained commas in quoted names into
multiple contacts. This could result in things like parsing multiple
addresses for the From: header, incorrectly!

To resolve the problem, replace our homegrown logic with mimelib's
seemingly excellent parseAddresses(), which handles this and a myriad of
other cases correctly.

Fixes: T7370

Test Plan: unit tests included

Reviewers: mark

Reviewed By: mark

Differential Revision: https://phab.nylas.com/D3565
2016-12-29 15:48:12 -08:00
Christine Spang 952349ee72 [local-sync] Remove TEXT param on message.body model definition
This doesn't do anything with sqlite, and just generates the following
warning in the logs:

>> WARNING: SQLite does not support TEXT with options. Plain `TEXT` will be used instead.
>> Check: https://www.sqlite.org/datatype3.html
2016-12-29 15:47:52 -08:00
Christine Spang 09749c53da [local-sync] Fix lint 2016-12-29 10:59:17 -08:00
Christine Spang e30b299b39 [local-sync] Mark message factory tests for fixing later
Will fix these once I've finished up the current slew of bugfixes I'm
working on---kind of a pain to ensure they're passing in all
intermediate states.
2016-12-29 10:59:17 -08:00
Mark Hahnenberg 5fd23e0f72 [local-sync] Add search for Office365
Summary: See title

Test Plan: Run it locally

Reviewers: juan, evan

Differential Revision: https://phab.nylas.com/D3560
2016-12-27 12:06:54 -08:00
Christine Spang 8135e22213 [local-sync] Catch UniqueConstraintError when associating folders/labels with threads
Summary:
Similar to the fix in D3555, concurrent message processing may cause insert
races for folder and label thread associations. If the row is already present,
we can simply do nothing.

Test Plan: manual for now

Reviewers: jackie

Reviewed By: jackie

Differential Revision: https://phab.nylas.com/D3557
2016-12-23 18:35:09 -08:00
Christine Spang 6dcc12843c [local-sync] Fix message parsing when no Date header present
Summary:
When the Date: header is not present, use the INTERNALDATE from the IMAP server
instead.

Test Plan: manual for now - will add a regression test for this though

Reviewers: juan, jackie

Reviewed By: jackie

Differential Revision: https://phab.nylas.com/D3556
2016-12-23 18:32:29 -08:00
Christine Spang 0644d2663d [local-sync] Fix UniqueConstraintError inserting contacts from messages
Summary:
Because of JavaScript's asynchronous nature, it is possible that we will
be processing several downloaded messages concurrently. This can lead to
calling extractContacts() in an interleaved fashion, which it was not
designed to handle. It looks up which contacts are already in the
database and then performs inserts or updates accordingly, assuming
nothing has changed in the contacts table in between---which is not
true! If several messages have similar contacts, an insert race can
cause one of the inserts to throw an unhandled exception.

We fix this by simply catching the unique constraint error, and falling
back to an update instead. (There's not really a better way to deal
with write races other than to enforce that we process contacts from
messages serially, as transactions are of no help here.)

This commit also removes extractContacts()'s return value, which is not
currently used and I found confusing.

Test Plan: manual

Reviewers: juan, evan, mark, jackie

Reviewed By: jackie

Differential Revision: https://phab.nylas.com/D3555
2016-12-23 18:31:51 -08:00
Christine Spang de1b67287c fix(snippet-parsing): Don't add extraneous spaces after text format tags
Summary:
This was leading us to put funny things like 'Nylas !' in some snippets that used
tags like <i> and <b> for text formatting. This is probs a teeny little bit slower
than the previous version since it invokes a callback on a lot more nodes, but we
can't really fix this issue without knowledge of the preceding tag name.

Test Plan: unit test included!!

Reviewers: evan, jackie

Reviewed By: jackie

Differential Revision: https://phab.nylas.com/D3553
2016-12-23 18:26:35 -08:00
Christine Spang 99899be7c2 [models] Remove unnecessary Reply-To array length validation
Summary:
I have quite a few emails in my mailbox that have both multiple Reply-To
addresses. This is perfectly OK by the spec.

Fixes: T7369

Test Plan:
regression test coming - making a list and planning to update all the tests once I've hammered out the current crop of fixes I've identified

I also tested and made sure that N1 does the right thing in this case...
multiple Reply-To addresses are displayed correctly, and when you hit "Reply" a
new draft is started with both in the To: field. Makes sense given this is
something the Python sync engine supported too.

Reviewers: jackie

Reviewed By: jackie

Differential Revision: https://phab.nylas.com/D3558
2016-12-23 14:41:08 -08:00
Jackie Luo dda0830220 [local-sync] Allow folders and labels to be null in toJSON() 2016-12-22 14:58:18 -08:00
Christine Spang a947cc063a [local-sync] Use mimelib to parse headers
Using node-imap's parseHeader function to parse headers was resulting in
a huge number of message parse failures on Office365 accounts, because
the results contained unicode control character 9 and we'd then feed that
string to JSON.parse when extracting contacts, which would throw an
exception.

Using mimelib's header parsing function eliminates these errors.
2016-12-22 08:51:16 -08:00
Evan Morikawa c299fd9ebe perf(delta): replaces API delta stream with direct in-memory one
Summary:
This replaces the API delta stream with a direct in-memory one

Addresses T7300

Test Plan: manual

Reviewers: jackie, halla, juan

Reviewed By: halla

Differential Revision: https://phab.nylas.com/D3548
2016-12-21 18:42:52 -08:00
Juan Tejada 49c61fde0c [local-sync] Fix authentication notifications
Summary:
Associated N1 Diff: D3545

This commit ensures that authh notifications are showed when the
underlying sync worker fails and are cleared when an account is
successfully reconnected

To achieve this, we manually keep track and update syncStates where
appropriate via `Actions.updateAccount`, given that we have access to
N1's version of the account directly from local-sync.
Initially I was considering account delta stream to the cloud-api and the local-api, but that
just complicated things more than it helped.

This commit also fixes a bug with refreshing the gmail token in which we
we were only attempting a token refresh upon restarting the app

This addresses: T7346, T7305, T7335

Test Plan: Manual

Reviewers: halla, evan

Reviewed By: evan

Differential Revision: https://phab.nylas.com/D3544
2016-12-21 07:22:39 -05:00
Evan Morikawa f0436e17d4 [local-sync] use async await in processNewMessage and fix logging 2016-12-20 17:09:17 -08:00
Evan Morikawa ab5a66d4e7 [local-sync]: better sync reasons 2016-12-20 16:46:58 -08:00
Evan Morikawa 25c20ce92c [local-sync] add logging for syncbackTasks 2016-12-20 16:13:59 -08:00
Evan Morikawa 93db26fdc7 [local-sync, isomophic-core] nicer logging 2016-12-20 16:04:28 -08:00
Christine Spang e924e74c1b [local-sync] Optimize snippet extraction
Summary:
We were seeing JS blocking in snippet extraction of up to 2k ms. This
is because we were walking the entire DOM of a message and extracting
all text, regardless of message size---and using our own homegrown
DOM walker function.

To remedy this, use the standard TreeWalker from the Chrome browser
APIs (which in benchmarks looks 2-4x faster) and also exit out of
the DOM walking process once we've accumulated enough text to create
a snippet. Informal eyeballing of timing metrics for this function suggests
the new implementation is something like 10-100x faster for some messages.

As a bonus, we get to delete some code and end up with a cleaner
implementation!

Test Plan: old unit tests yaay

Reviewers: juan

Reviewed By: juan

Subscribers: evan

Differential Revision: https://phab.nylas.com/D3543
2016-12-20 15:33:27 -08:00
Evan Morikawa 5015d105b8 [isomorphic-core] fix sending on Office 365
Summary:
Fixed sending on Office 365
nodemailer needed a special tls flag beyond the standard SSL.
See: http://stackoverflow.com/questions/29812132/error-sending-email-using-nodemailer-via-office365-smtp-meanjs-scaffold

Test Plan: manual

Reviewers: juan, jackie, halla

Reviewed By: halla

Differential Revision: https://phab.nylas.com/D3541
2016-12-20 13:08:30 -08:00
Jackie Luo 6bbceda709 fix(tracking): Save updated metadata correctly and stop sending account ID with request
Summary:
Save metadata correctly by reassigning an object to value.

Since account IDs are different between N1 and N1 Cloud, use just the message ID, which should be unique.

Test Plan: Tested locally.

Reviewers: evan, juan

Reviewed By: evan, juan

Differential Revision: https://phab.nylas.com/D3524
2016-12-19 15:40:38 -08:00
Juan Tejada 7c701c6369 [local-sync] Fix date parsing and tests
Make sure that we use the header date for our date field because that's
the one we can control and depend on for message id generation
2016-12-19 15:35:50 -08:00
Evan Morikawa bd33c5fdc3 [local-sync] temporarily remove validation in send 2016-12-19 09:38:12 -08:00
Evan Morikawa d55a2af2cd [cloud-api] refactor cloud API routes to use es6 & fix Gmail Auth
Summary:
This is a refactor of the auth APIs to use async/await. Gmail Auth is
pretty confusing and I wanted to make it cleaner to read and easier to
use. This is also part of the general API upgrade to modern ES6

This also fixes the Gmail auth error we saw at showcase

Test Plan: manual

Reviewers: halla, jackie, mark, juan

Reviewed By: juan

Differential Revision: https://phab.nylas.com/D3535
2016-12-19 09:25:07 -08:00
Juan Tejada 5810bb6b79 [local-sync] Fix issue with imap connection mail event
Summary:
See https://github.com/mscdex/node-imap/issues/585 for details.

This issue was causing us constantly run the sync loop without pauses,
i.e. every next sync loop was scheduled immediately.

Currently, when we receive a new `'mail'`event, we trigger a new sync loop. Previously, when this happened while a sync loop was already in progress we would just ignore the event. However, my recent patch keeps track of how many times we tried to start a sync loop while one was already in progress. If the number of times this happens is > 0, it will schedule the next sync loop immediately (as opposed to waiting a constant amount seconds before the next loop).

The problem is that this new logic is making the sync worker always schedule the next sync loop immediately (without pausing for a few seconds). This is due to the following chain of events (assume we are just syncing `all` and `trash` folders):

This commit is a temporary workaround to this problem.

Test Plan: manual

Reviewers: evan

Reviewed By: evan

Differential Revision: https://phab.nylas.com/D3537
2016-12-19 08:08:59 -08:00
Evan Morikawa 39b0e6c3ee [isomorphic-core] add office365 in provider list 2016-12-16 18:51:11 -05:00
Halla Moore b46ffd4a3f [local-sync] Fix a couple bugs that popped up while sending
- Pass in the account when creating an ImapConnection
- Wrap the return value of the SendMessage task in an object
2016-12-16 15:43:39 -08:00
Evan Morikawa 5a1fb3c9ad [local-api] fix validation errors on send 2016-12-16 18:29:52 -05:00
Christine Spang c6a80efc44 [local-sync] Fix parseFromImap specs
Lots has changed for the better! Tests work again also.
2016-12-16 14:42:26 -08:00
Juan Tejada abc5f35255 [local-sync] Ensure send runs fast, clean up multisend tasks
Summary:
Associated N1 Diff: D3530

This commit converts multi-send from a 3 step process into a 2 step
process

The first step creates the base message and sends a message per
recipient, each with its customized message body for tracking.

The second step reconciles all sent messages, specifically removing any
sent messages created by gmail, and saving the correct message to the
sent folder

This commit also ensures that we run the send tasks immediately by
ensuring we restart the sync loop if its already running

Test Plan: Manual

Reviewers: evan, jackie, halla

Reviewed By: jackie, halla

Differential Revision: https://phab.nylas.com/D3529
2016-12-16 14:41:20 -08:00
Evan Morikawa d095551e90 [isomorphic-core] add office365 auth support
Summary:
Adds support for office 365
Depends on D3532

Test Plan: manual

Reviewers: jackie, halla, mark, juan

Reviewed By: juan

Differential Revision: https://phab.nylas.com/D3533
2016-12-16 16:53:05 -05:00
Karim Hamidou 101b99f4a7 [feat] Refresh Gmail access tokens when needed
Summary:
This is a small patch but it's pretty complex, because of the numbers of moving parts. Gmail has two types of tokens, access and refresh tokens. Access tokens have a limited shelf life of one hour. After that they expire and you need to use your refresh token to get a new one.

We've decided to do the access token generation on the server, because we don't feel comfortable giving our users both our Google client id and secret. To do that, I've added an endpoint, `/gmail/auth/refresh` which returns a valid access token as well as an expiration date for the token.

The only place where we handle token expiration is in the sync workers. Before trying opening a new connection we check if our access token is expired. If yes, we get a new one from the API. If there's an issue doing this, we notify N1 using `NylasAPIHelpers.handleAuthenticationFailure`.

There's a second patch for N1 with tiny related fixes.

Test Plan: Tested manually. Will need to test more in the real world.

Reviewers: evan, jackie, juan

Reviewed By: juan

Differential Revision: https://phab.nylas.com/D3522
2016-12-16 11:38:45 -08:00
Juan Tejada 86a2b13730 [local-sync] Fix sync worker
- Correctly await async functions
- Make sync in progress more explicit
2016-12-16 11:38:15 -08:00
Christine Spang e77fbc21e1 [local-sync] Update comment 2016-12-16 11:06:04 -08:00
Halla Moore 5a7aa45629 [local-sync] Fix a couple of message parsing bugs
Summary:
- Don't fail if there's no subject, just set it to `(no subject)`
- Support "BINARY" content-transfer-encoding. (This really means
that there is no encoding, so it's simple to add support for it)

Test Plan: tested locally

Reviewers: jackie

Reviewed By: jackie

Differential Revision: https://phab.nylas.com/D3528
2016-12-16 10:50:35 -08:00
Evan Morikawa 1d254a7aaa [*] Add basic babel toolchain
Summary:
Adds babel to K2
Creates a simple build script so it'll run on prod.

Test Plan: manual

Reviewers: jackie, halla, juan

Reviewed By: juan

Differential Revision: https://phab.nylas.com/D3527
2016-12-16 13:08:21 -05:00
Juan Tejada b17be6b873 [local-sync] Fix sync worker - Restore fns removed by mistake 2016-12-15 20:43:52 -08:00
Juan Tejada 2c6be51970 [local-sync] Fix typo 2016-12-15 18:45:24 -08:00
Juan Tejada 2233992f27 [local-sync] Allow sync-worker to be restarted
Summary:
Add internal state to the sync worker to allow for it to be interrupted
and restarted.

The concept in this commit is that if we've tried to trigger a sync enough times
while its already in progress, bail and start over.

Usually, we manually trigger sync loops when we queue a new SyncbackTasks,
so that the newly queued task gets executed. This is necessary because the only
way to run SyncbackTasks is via the sync loop, for consistency and simplicity
reasons.

For example, we might run into a case where we queue a SendMessage task,
and we want it to be executed ASAP, but if we're in the middle of a
syncing a mailbox with a ton of folders, we wont get to the SendMessage
task after some considerable time.

Specifically this commit makes it so:

- If the number of sync attempts while in progress is > 0, make sure we schedule the next sync immediately
- If we reach a threshold of sync attempts while in progress, interrupt sync and restart

Test Plan: todo :(

Reviewers: mark, spang, evan

Reviewed By: evan

Differential Revision: https://phab.nylas.com/D3520
2016-12-15 15:17:45 -08:00
Juan Tejada 5189407e38 [local-sync] Fix syncback tasks execution
Summary:
We were doing all of this fancy filtering and sorting to determine which
tasks to run, but in the end, we were just running the whole unsorted
list (using the wrong variable)

Also extracted getting the list of tasks into its own function, for
easier unit tests (it should really have some)

And, also wanted to make sure other people looked at this code, since I
believe no one has before.

Test Plan: todo

Reviewers: mark, khamidou, spang, halla

Reviewed By: halla

Differential Revision: https://phab.nylas.com/D3516
2016-12-15 14:41:53 -08:00
Evan Morikawa 2d145800ae [local-sync] fix missing dependency 2016-12-15 17:04:51 -05:00
Evan Morikawa b542edce02 [local-private] require Rx from nylas-exports 2016-12-15 15:32:29 -05:00
Juan Tejada 3910799683 [local-sync] Fix contact parsing from T7327
Summary: See description at T7327

Test Plan: Manual, but this should have unit tests

Reviewers: mark, evan

Reviewed By: evan

Differential Revision: https://phab.nylas.com/D3513
2016-12-15 12:30:59 -08:00
Juan Tejada e17b6d8d17 [local-sync]: Move sendmail-client and errors to isomorphic-core
Summary:
Move sendmail-client and errors to isomorphic-core, given that they will
probably be used by cloud-workers (plugin backends) and cloud-api

Depends on D3510

Test Plan: Manual

Reviewers: halla

Reviewed By: halla

Differential Revision: https://phab.nylas.com/D3512
2016-12-15 12:29:56 -08:00
Juan Tejada c8e71464f9 [local-sync] Update send endpoints to use SyncbackTasks
Summary:
Associated N1 diff: D3511
Convert send endpoints to use syncback tasks for consistency with how we
perform other imap operations, but primarily:
- So that it triggers a sync loop immediately and we pick up changes quickly
- To keep track of various send operations as a single unit (e.g. sending + saving to sent folder or deleting from sent)

This commit also fixes SyncbackRequest error handling and processing in
N1-- previously we were saving error fields to the syncbackRequests with
a format that didn't match N1's API error and which wasn't properly
serializable. (Also rename HTTPError to APIError)

Test Plan: Todo/Manual

Reviewers: jackie, halla, evan

Reviewed By: evan

Differential Revision: https://phab.nylas.com/D3510
2016-12-15 11:55:40 -08:00
Juan Tejada b6d8459d50 [local-sync] Report folder syncState to N1
Summary:
Associated N1 diff: D3515

Stop ignoring deltas for Folder.syncState updates

Test Plan: Manual

Reviewers: mark, evan

Reviewed By: evan

Differential Revision: https://phab.nylas.com/D3514
2016-12-15 11:08:20 -08:00
Christine Spang e5a9e2cc9e [local-sync] Allow logging parsed messages to disk with NYLAS_DEBUG env var
Summary:
I've found this useful for generating test cases and am tired
of adding and removing this code!

Test Plan: inspect output of /tmp/k2-parse-output

Reviewers: juan

Reviewed By: juan

Differential Revision: https://phab.nylas.com/D3518
2016-12-15 10:45:48 -08:00
Christine Spang ac5c7e3d2c [local-sync] Escape HTML entities in plaintext
Summary: This was understandably causing some messages to fail to display correctly.

Test Plan: unit tests are already broken for message parsing -- will fix in follow up diff

Reviewers: juan

Reviewed By: juan

Differential Revision: https://phab.nylas.com/D3517
2016-12-15 10:41:36 -08:00
Evan Morikawa ee831ba0aa [local-private, local-sync] temporarily disable specs
[local-private] Disable more broken specs

Disable specs

Reenable local-sync tests
2016-12-15 12:57:24 -05:00
Juan Tejada b79488ae43 [local-sync, cloud-api, cloud-workers] Fix msg id collision, tracking and sending issues, some refactoring
Summary:
This diff solves a few separate issues from T7313, T7316, T7282, and it refactors
the send code a little bit.

Initially, the problem that led to this diff was generating message ids that
wouldn't collide (which was causing errors in the message-processor). Collisions
in ids were being caused by messages that contained the exact same participants,
subject and date (most likely due bots or scripts sending emails in quick
succession)

To prevent collisions this commit adds the `message-id` header as part of the
database message id, and ensures that we set it correctly before sending, and
that it remains consistent through send, multi-send, and the sync loop.

During the refactor and review, I removed some code that assumed that we were
syncing drafts (which we aren't), and also fixes a few other known and
unknown issues around sending, message creation, and tracking, like assigning
the correct date header (we were previously assigning the draft creation date
from within N1), fixing the tracking regex, among other smaller bugs/typos.

Will address inline TODOs in a separate diff

Test Plan: TODO!!! I will add tests in another diff

Reviewers: evan, halla, jackie, khamidou

Reviewed By: halla, jackie

Differential Revision: https://phab.nylas.com/D3507
2016-12-14 19:35:48 -08:00
Mark Hahnenberg a45bd4515a [local-sync] Fix Contact.toJSON
Summary:
We forgot to refactor `publicId` to just `id`, which was breaking contact
deltas in N1.

Test Plan: Run locally

Reviewers: khamidou, evan, juan

Reviewed By: juan

Differential Revision: https://phab.nylas.com/D3508
2016-12-14 17:20:56 -08:00
Mark Hahnenberg cfc8d3e315 [local-sync] Implement /thread/search endpoint for generic IMAP
Summary: See title

Test Plan: Run locally

Reviewers: juan, evan

Reviewed By: juan, evan

Maniphest Tasks: T7281

Differential Revision: https://phab.nylas.com/D3498
2016-12-14 11:50:22 -08:00
Halla Moore 138c79bf71 [local-sync] Fix contact validation
Summary:
Allow other fields to be passed in with participants, so that Contact
objects can be passed in as JSON. Also fix an error check.

Test Plan: tested locally

Reviewers: jackie

Reviewed By: jackie

Differential Revision: https://phab.nylas.com/D3504
2016-12-14 10:43:20 -08:00
Christine Spang c214ba1e34 [local-sync] Parse DOM to extract snippets
Summary:
This fixes multiple issues, including snippets telling you you
ought to look at the HTML as well as cruft like HTML entities
and CSS in snippets.

Test Plan: unit tests included o.O

Reviewers: juan

Reviewed By: juan

Subscribers: evan

Differential Revision: https://phab.nylas.com/D3500
2016-12-13 16:32:22 -08:00
Jackie Luo 5243629831 [local-sync] Update URLs and regex 2016-12-13 16:26:05 -08:00
Mark Hahnenberg b47cd28d89 [local-sync] Implement /threads/search endpoint for Gmail
Summary: See title

Test Plan: Ran it locally

Reviewers: khamidou, juan, evan

Reviewed By: juan, evan

Differential Revision: https://phab.nylas.com/D3496
2016-12-13 13:44:00 -08:00
Jackie Luo 4a6f1dd012 fix(tracking): Update server URLs and send MESSAGE_ID 2016-12-13 12:53:48 -08:00
Halla Moore 547ff416e7 [local-sync] Fix a couple of multi-send bugs
Summary:
1) Send the custom body, rather than the generic body
2) Extract contacts correctly so that the saved sent message has all the
   participants, rather than just the last one

Test Plan: Tested locally

Reviewers: jackie

Reviewed By: jackie

Differential Revision: https://phab.nylas.com/D3499
2016-12-13 11:45:59 -08:00
Evan Morikawa 655175d94f [cloud-api, local-sync] reordering for lerna 2016-12-12 10:37:43 -05:00
Evan Morikawa 5a5aeb6bb3 [local-private] move old edgehill src/pro into packages/local-private
Summary: This is a test

Test Plan: Testing

Reviewers: juan

Differential Revision: https://phab.nylas.com/D3493

[local-private] add old edgehill src/pro into packages/local-private
2016-12-12 10:09:58 -05:00
Mark Hahnenberg f32d0df7e0 [local-sync] Increment streamAll offset by chunkSize (#5)
Otherwise we'll infinite loop if there are more than 2000 results.
2016-12-09 11:15:04 -08:00
Mark Hahnenberg 60bca4acb2 [local-sync] Add index for Thread.id (#4) 2016-12-09 11:12:47 -08:00
Christine Spang 587f7787a6 fix(local-sync): Fix charset interpretation in message parsing
Summary:
This commit fixes the following bugs in message parsing:
- we were unilaterally decoding MIME bodies as UTF-8; instead, decode according
  to the charset data in the mimepart header
- '7bit' content-transfer-encoding means us-ascii, NOT utf-7
- only interpret valid content-transfer-encodings (previously we were trying
  to treat various charsets as transfer-encodings)
- clearer naming: s/values/parsedMessage/
- unify snippet cleanup between plaintext & stripped HTML (merging
  whitespace etc.)

Test Plan: units tests coming

Reviewers: juan

Differential Revision: https://phab.nylas.com/D3491
2016-12-09 11:01:04 -08:00
Jackie Luo 162dbbd141 fix(error): Treat error as object, not string 2016-12-08 18:32:23 -08:00
Jackie Luo 4a11bfe977 fix(message-factory): Unlink circular dependency 2016-12-08 18:10:17 -08:00
Jackie Luo 6e111c073a fix(message-ids): Use correct hashing for headers 2016-12-08 17:55:39 -08:00
Jackie Luo fae855f0fe feat(message-ids): Hash message IDs and replace in draft before sending 2016-12-08 17:48:34 -08:00
Evan Morikawa 947eb99b8d [local-sync] fix builds. Routes with trailing slash and main extension 2016-12-08 14:17:42 -08:00
Jackie Luo 7a763b604e feat(tracking): Add routes for open and link tracking 2016-12-08 13:38:29 -08:00
Juan Tejada 1ba56c5c05 [local-sync] Fix lint error 2016-12-08 10:28:29 -08:00
Evan Morikawa 2f786a8e91 [local-sync] fix delta sync not getting threads with updated labels 2016-12-07 17:47:49 -08:00
Evan Morikawa d161a30a34 [local-sync] Add global dbs and cleanup orphan messages 2016-12-07 16:39:01 -08:00
Juan Tejada 878735f52e [local-sync] Add todos 2016-12-07 16:16:45 -08:00
Juan Tejada a185a8a5fe [local-sync] Fix file/contact creation
We were getting sql unique constraint violation errors for ids because
we were attempting to create objects with the same id within the same
transaction.

This commit ensures that only attempt to write a contact with the same
id once, and that we check for all exsiting contacts before hand.

For file, we ensure that we don't attempt to write 2 files with the same
id more than once
2016-12-07 14:48:57 -08:00
Halla Moore 56f8d41b8c [local-sync] feat(send): Add support for attachments
Also move some helper function logic onto the Message model
2016-12-07 14:16:36 -08:00
Juan Tejada 8307976df8 [local-sync]: Fix label/folder id creation
We were using the stripped version of label/folder names for the id
hash, e.g. [Gmail]/Drafts would be Drafts.
However, we can't do this because it might collide with other names. e.g. if the
user created a Drafts label, it would end up colliding with [Gmail]/Drafts

Minor lint fix
2016-12-07 13:39:41 -08:00
Evan Morikawa edbf869ff7 [isomorphic-core] add more auth error states 2016-12-07 10:10:49 -08:00
Christine Spang a23c68092e [local-sync] Add specs for message parsing
Summary:
This commit also fixes snippets for HTML-only messages to strip out HTML
tags, and makes us preserve whitespace for plaintext emails by
displaying them in <pre class="nylas-plaintext"> tags, and makes us log
messages that fail to parse at all to a tempdir.

The only issue I found with using <pre> tags for plaintext email was
that some lines may trigger scrolling, so there is an associated commit
(D3484) that changes the CSS for <pre class="nylas-plaintext"> to wrap
lines.

In the future, we can add regression tests to this test suite whenever
we fix parsing bugs.

Test Plan: unit tests included

Reviewers: bengotow

Reviewed By: bengotow

Differential Revision: https://phab.nylas.com/D3483
2016-12-07 07:25:28 -08:00
Ben Gotow c3bd3dc297 [local-sync] Don’t update contacts if name is the same 2016-12-06 17:51:12 -08:00
Evan Morikawa 6ac46d1079 remove console log 2016-12-06 17:20:43 -08:00
Evan Morikawa 7e485228af [cloud-api] fix param names 2016-12-06 16:03:31 -08:00
Evan Morikawa 83ef47d049 [*] package.json updates from lerna 2016-12-06 16:03:31 -08:00
Evan Morikawa 8cbda7505a [*] fix auth 2016-12-06 16:03:31 -08:00
Evan Morikawa 2cbb90bb3b [*] DRY Auth 2016-12-06 16:03:31 -08:00
Evan Morikawa 896f981408 [isomorphic-core] extract AuthHelpers to DRY 2016-12-06 16:03:31 -08:00
Juan Tejada 7468e8123e [local-sync] Update DeleteSentMessage task for ordering 2016-12-06 15:54:45 -08:00
Juan Tejada fda7fe18cc [local-sync] Ensure order correct order when running syncback requests
When running syncback requests, if a task is meant to change the UID of
a message (e.g. move it to a new folder), that task should be run after
other tasks that don't affect the UID. Otherwise, when trying to run the
other tasks, they would reference a UID that is no longer valid.

This commit will make sure that we run any tasks that will change message uids last,
/and/ make sure that we don't run more than 1 task that will affect the uids of
the same messages in a row (i.e. without running a sync loop in between)
2016-12-06 15:45:34 -08:00
Halla Moore e785d73bdc [local-sync, iso-core] feat(send): add finishing touches to gmail multi-send
Delete the sent messages that gmail automatically creates and save our
generic form of the draft as a sent message.
2016-12-06 11:37:47 -08:00
Juan Tejada 1a24840062 [local-sync] Correctly sync folders and labels
This commit will correctly keep track of folder and label ids when
creating them from N1.

Previously, when we sent the request to create a folder or label to our api,
we would immediately get back a serverId because it was created optimistically
in the back end— given that K2 is strictly non-optimistic, we won’t have a serverId
until some undetermined time in the future, and we need to somehow reference
the object that /was/ optimistically created in N1 to update the ui when
we do get the server id.

Since we can deterministically generate ids for folders and labels,
we "guess" what its going to be, and include it in the props of the syncback request
returned to N1. This is the simplest solution to get thing working
correctly right now, but we’ll need to revisit this in the future for
other types of objects (drafts, contacts, events), and revisit how we
will manage optimistic updates in N1 when we merge the 2 codebases
with K2 (given that K2 was designed to be non-optimisitc).
2016-12-05 18:56:36 -08:00
Ben Gotow aed1d59916 [local-sync] fix(folder-list): Support children when parent has no attribs 2016-12-05 16:20:17 -08:00
Ben Gotow 1e3b346c94 [local-sync] feat(specs): Add basic tests for folder sync 2016-12-05 16:07:49 -08:00
Karim Hamidou 04b1763965 [fix] Fix transaction creation for contacts
Summary:
Fixes T7283. We weren't creating deltas for contacts because we were inserting contacts using `UPSERT`, which requires us to add another sequelize hook.  Unfortunately, support for this hook is only available on sequelize 4.x.

I didn't want to upgrade our sequelize version right now and cause of bunch of mysterious failures, so I've simply changed the `UPSERT` to be either an `INSERT`or `UPDATE`. We didn't really need it anyway.

Test Plan: Checked that N1 was receiving contact deltas.

Reviewers: bengotow

Reviewed By: bengotow

Maniphest Tasks: T7283

Differential Revision: https://phab.nylas.com/D3481
2016-12-05 15:37:17 -08:00
Ben Gotow 269d61171a [local-sync] fix(specs): New specs for threading 2016-12-05 15:00:37 -08:00
Halla Moore 63032a2449 [local-sync] Fix(send): change some validation fields for multi-send 2016-12-05 12:55:56 -08:00
Ben Gotow 30c8bedd7a [local-sync] fix(specs): run npm test in local-sync dir 2016-12-05 12:16:53 -08:00
Ben Gotow 017e22c88d [*] Allow zero elements in the replyTo field 2016-12-05 09:57:47 -08:00
Halla Moore 6a51036e48 [local-sync, iso-core, cloud-core] feat(send): add multi-send support
Also renames JSONType() -> buildJSONColumnOptions() and
JSONARRAYType() -> buildJSONARRAYColumnOptions() to prevent passing
those return values in as just the type value instead of the entire
options object.
2016-12-02 15:20:31 -08:00
Evan Morikawa 2ba326dfc6 [local-sync] only enable logging in dev mode of N1 2016-12-02 17:34:05 -05:00
Christine Spang 9752eea9f7 [local-sync] Set thread IDs to the ID of a message in the thread
Since message IDs are now static but there's no good way to generate
static thread IDs while syncing an account from newest message first,
we give threads the ID of any message on that thread and, when setting
metadata, look up the local thread ID by first going through the
message table.
2016-12-02 14:13:47 -08:00
Evan Morikawa d9f3edad0f [isomorphic-core] ignore folder deltas due to just syncState version issues 2016-12-02 16:25:42 -05:00
Evan Morikawa b06566d8bd [local-sync] make syncbackRequest objects N1-ready 2016-12-02 16:14:42 -05:00
Juan Tejada b36f61812d [local-sync]: Don't create or sync \NonExistent or \NoSelect folders 2016-12-02 12:14:09 -08:00
Juan Tejada f1491fa4cc fix observable disposable 2016-12-02 12:09:35 -08:00
Juan Tejada 5ac3fe9ea8 [local-sync] Fix starred role on folders 2016-12-02 12:07:21 -08:00
Ben Gotow 5ff7a92197 [*] fix(deltas): 0 as first cursor, deltas from redis are already JSON 2016-12-01 18:54:26 -08:00
Ben Gotow 7712269402 [*] fix(deltas): Cloud-API not filtering deltas at all, refactor a few things
- Don’t need functions in delta.js which must be called to return promsies. Fun of promsies is that you don’t need to care when they’re built to attach a .then.

- Make boundary between route handler and delta stream builder more explicit, don’t do query parsing in helpers, always reply from handler.

- Remove pushJSON extension to outputStream which never actually received JSON.

- Remove `takeUntil` - disposing of the downstream observable should dispose of all the merged/upstream observables

- Rename inflate => stringify since the returned value is a string not an object.

- Remove support for delta streams with no cursors. Don’t think this was supposed to be a feature.

- Add accountId to Transaction models

- Make database hooks shared in isomorphic core
2016-12-01 18:41:46 -08:00
Karim Hamidou 15cfe2cec0 Strip the '[Gmail]/' prefix from folder names.
Summary:
T7253 has two related parts:
1. Stripping the '[Gmail]/' prefix from any canonical Gmail folders
2. Handling nested IMAP folders.

This diff fixes 1. Changes for 2. are forthcoming.

Test Plan: Tested manually. Checked that the part was stripped from the N1 folder list.

Reviewers: juan

Reviewed By: juan

Differential Revision: https://phab.nylas.com/D3475
2016-12-01 16:49:09 -08:00
Juan Tejada d650be5429 [local-sync] Refactor tasks and /actually/ fix moving between Gmail folders
Moving to between gmail folders (all, spam, trash) or moving to inbox,
involves changing labels /and/ folders, simultaneously. For this I added
a task to perform both operations, and apply labels first before
attempting to move the folder
2016-12-01 16:37:49 -08:00
Ben Gotow 4b4ab726e2 [💄] fix eslint issues before they get overwhelming 2016-12-01 15:37:48 -08:00
Karim Hamidou aa49f85980 Fix bug where we wouldn't create an id for file parts. 2016-12-01 15:30:01 -08:00
Juan Tejada 2d932dd090 [*] Generate persistent id for Accounts based on email+settings
This also ensures that we can use the accountId as part of the ids for
other models, like labels and folders

Before this commit, labels and folders with the same name for different
accounts would have the same id, screwing things up in n1
2016-12-01 14:51:33 -08:00
Ben Gotow 47d8614ed7 [local-sync] Ship first sync metrics to honeycomb 2016-12-01 14:23:52 -08:00