Commit graph

29 commits

Author SHA1 Message Date
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
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 09749c53da [local-sync] Fix lint 2016-12-29 10:59:17 -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 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 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
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
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
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
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
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
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
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
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
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
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
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 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
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 30c8bedd7a [local-sync] fix(specs): run npm test in local-sync dir 2016-12-05 12:16:53 -08:00
Karim Hamidou 47e0683cac First step to getting persistent id in K2
Summary:
This diff adds persistent unique ids for messages and contacts. For messages, we just take a hash of the headers. For contacts, we hash the contact's email address.

This diff bundles a couple of tiny fixes too, like always trying to restart an account's sync, even after an exception.

Note that since there's no reliable way to have persistent ids for threads, we'll have to change our code to use message ids instead. Alas, that's a story for another (massive) diff.

Test Plan: Tested manually.

Reviewers: bengotow

Reviewed By: bengotow

Differential Revision: https://phab.nylas.com/D3468
2016-12-01 11:15:13 -08:00
Juan Tejada a9be3bb3ed [local-sync]: Refactor message ingestion to write new messages only once
This will only generate 1 delta per new message instead of 2
Adds a MessageFactory to create messages from imapmessage objects, and
unifies and cleans up that logic
Also modified the message-processor to be more suited for local sync,
made emphasis that it's only for processing new messages, and got rid of
the while "pipeline" concept

- TODO message-processor specs are broken
2016-11-29 11:24:27 -08:00