Commit graph

518 commits

Author SHA1 Message Date
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