Commit graph

89 commits

Author SHA1 Message Date
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
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
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 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
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 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
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
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 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 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
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
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
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
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
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
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 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
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 0b53d8599f Update sequelize point version everywhere 2017-01-04 15:47:16 -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
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
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 93db26fdc7 [local-sync, isomophic-core] nicer logging 2016-12-20 16:04:28 -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
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
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
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 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 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
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 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
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
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
Evan Morikawa edbf869ff7 [isomorphic-core] add more auth error states 2016-12-07 10:10:49 -08:00