Start connecting C++ process to onboarding flow

This commit is contained in:
Ben Gotow 2017-07-03 11:44:28 -07:00
parent 40eca53dbc
commit 57c5c5b28c
5 changed files with 105 additions and 38 deletions

BIN
packages/client-app/MailSync Executable file

Binary file not shown.

View file

@ -5,7 +5,7 @@ import {RetinaImg} from 'nylas-component-kit';
import {NylasAPI, Actions} from 'nylas-exports'; import {NylasAPI, Actions} from 'nylas-exports';
import OnboardingActions from '../onboarding-actions'; import OnboardingActions from '../onboarding-actions';
import {runAuthRequest} from '../onboarding-helpers'; import {runAuthValidation} from '../onboarding-helpers';
import FormErrorMessage from '../form-error-message'; import FormErrorMessage from '../form-error-message';
import AccountTypes from '../account-types' import AccountTypes from '../account-types'
@ -99,7 +99,7 @@ const CreatePageForForm = (FormComponent) => {
this.setState({submitting: true}); this.setState({submitting: true});
runAuthRequest(accountInfo) runAuthValidation(accountInfo)
.then((json) => { .then((json) => {
OnboardingActions.moveToPage('account-onboarding-success') OnboardingActions.moveToPage('account-onboarding-success')
OnboardingActions.accountJSONReceived(json, json.localToken, json.cloudToken) OnboardingActions.accountJSONReceived(json, json.localToken, json.cloudToken)

View file

@ -7,8 +7,10 @@ import {
NylasAPI, NylasAPI,
NylasAPIRequest, NylasAPIRequest,
RegExpUtils, RegExpUtils,
MailsyncProcess,
} from 'nylas-exports'; } from 'nylas-exports';
const IMAP_FIELDS = new Set([ const IMAP_FIELDS = new Set([
"imap_host", "imap_host",
"imap_port", "imap_port",
@ -86,7 +88,7 @@ export function buildGmailAuthURL(sessionKey) {
return `${N1CloudAPI.APIRoot}/auth/gmail?state=${sessionKey}`; return `${N1CloudAPI.APIRoot}/auth/gmail?state=${sessionKey}`;
} }
export function runAuthRequest(accountInfo) { export function runAuthValidation(accountInfo) {
const {username, type, email, name} = accountInfo; const {username, type, email, name} = accountInfo;
const data = { const data = {
@ -121,43 +123,37 @@ export function runAuthRequest(accountInfo) {
} }
} }
const noauth = {
user: '',
pass: '',
sendImmediately: true,
};
// Send the form data directly to Nylas to get code // Send the form data directly to Nylas to get code
// If this succeeds, send the received code to N1 server to register the account // If this succeeds, send the received code to N1 server to register the account
// Otherwise process the error message from the server and highlight UI as needed // Otherwise process the error message from the server and highlight UI as needed
const n1CloudIMAPAuthRequest = new NylasAPIRequest({ data.settings.id = 'test';
api: N1CloudAPI, const proc = new MailsyncProcess('test', data.settings);
options: { return proc.test().then((accountJSON) => {
path: '/auth', return accountJSON;
method: 'POST', });
timeout: 1000 * 180, // Same timeout as server timeout (most requests are faster than 90s, but server validation can be slow in some cases)
body: data, // TODO BG Re-enable cloud services
auth: noauth, // return n1CloudIMAPAuthRequest.run().then((remoteJSON) => {
}, // const localSyncIMAPAuthRequest = new NylasAPIRequest({
}) // api: NylasAPI,
return n1CloudIMAPAuthRequest.run().then((remoteJSON) => { // options: {
const localSyncIMAPAuthRequest = new NylasAPIRequest({ // path: `/auth`,
api: NylasAPI, // method: 'POST',
options: { // timeout: 1000 * 180, // Same timeout as server timeout (most requests are faster than 90s, but server validation can be slow in some cases)
path: `/auth`, // body: data,
method: 'POST', // auth: {
timeout: 1000 * 180, // Same timeout as server timeout (most requests are faster than 90s, but server validation can be slow in some cases) // user: '',
body: data, // pass: '',
auth: noauth, // sendImmediately: true,
}, // },
}) // },
return localSyncIMAPAuthRequest.run().then((localJSON) => { // })
const accountWithTokens = Object.assign({}, localJSON); // return localSyncIMAPAuthRequest.run().then((localJSON) => {
accountWithTokens.localToken = localJSON.account_token; // const accountWithTokens = Object.assign({}, localJSON);
accountWithTokens.cloudToken = remoteJSON.account_token; // accountWithTokens.localToken = localJSON.account_token;
return accountWithTokens // accountWithTokens.cloudToken = '';//remoteJSON.account_token;
}) // return accountWithTokens
}) // })
} }
export function isValidHost(value) { export function isValidHost(value) {

View file

@ -61,7 +61,7 @@ lazyLoad(`EdgehillAPI`, 'flux/edgehill-api');
lazyLoad(`LegacyEdgehillAPI`, 'flux/legacy-edgehill-api'); lazyLoad(`LegacyEdgehillAPI`, 'flux/legacy-edgehill-api');
lazyLoad(`NylasAPIHelpers`, 'flux/nylas-api-helpers'); lazyLoad(`NylasAPIHelpers`, 'flux/nylas-api-helpers');
lazyLoad(`NylasAPIRequest`, 'flux/nylas-api-request'); lazyLoad(`NylasAPIRequest`, 'flux/nylas-api-request');
lazyLoad(`MailsyncProcess`, 'mailsync-process');
// The Database // The Database
lazyLoad(`Matcher`, 'flux/attributes/matcher'); lazyLoad(`Matcher`, 'flux/attributes/matcher');
lazyLoad(`DatabaseStore`, 'flux/stores/database-store'); lazyLoad(`DatabaseStore`, 'flux/stores/database-store');

View file

@ -0,0 +1,71 @@
import { spawn } from 'child_process';
import path from 'path';
const LocalizedErrorStrings = {
ErrorConnection: "Connection Error",
ErrorInvalidAccount: "This account is invalid, or does not have an inbox or all folder.",
ErrorTLSNotAvailable: "TLS Not Available",
ErrorParse: "Parsing Error",
ErrorCertificate: "Certificate Error",
ErrorAuthentication: "Authentication Error - Check your username and password.",
ErrorGmailIMAPNotEnabled: "Gmail IMAP is not enabled. Visit Gmail settings to turn it on.",
ErrorGmailExceededBandwidthLimit: "Gmail bandwidth exceeded. Please try again later.",
ErrorGmailTooManySimultaneousConnections: "There are too many active connections to your Gmail account. Please try again later.",
ErrorMobileMeMoved: "MobileMe has moved.",
ErrorYahooUnavailable: "Yahoo is unavailable.",
ErrorNonExistantFolder: "Sorry, this folder does not exist.",
ErrorStartTLSNotAvailable: "StartTLS is not available.",
ErrorGmailApplicationSpecificPasswordRequired: "A Gmail application-specific password is required.",
ErrorOutlookLoginViaWebBrowser: "The Outlook server said you must sign in via a web browser.",
ErrorNeedsConnectToWebmail: "The server said you must sign in via your webmail.",
ErrorNoValidServerFound: "No valid server found.",
ErrorAuthenticationRequired: "Authentication required.",
};
export default class MailsyncProcess {
constructor(mode, account) {
this.mode = mode;
this.account = account;
}
_spawnProcess() {
const binaryPath = path.join(NylasEnv.getLoadSettings().resourcePath, 'MailSync');
const sync = spawn(binaryPath, [`--mode`, this.mode]);
if (this.mode !== 'migrate') {
sync.stdout.once('data', () => {
console.log(this.account);
sync.stdin.write(`${JSON.stringify(this.account)}\n`);
});
}
return sync;
}
test() {
return new Promise((resolve, reject) => {
const sync = this._spawnProcess();
let buffer = Buffer.from([]);
sync.stdout.on('data', (data) => {
buffer += data;
});
sync.stderr.on('data', (data) => {
buffer += data;
});
sync.on('error', (err) => {
reject(err, buffer);
});
sync.on('close', (code) => {
try {
const lastLine = buffer.toString('UTF-8').split('\n').pop();
const response = JSON.parse(lastLine);
if (code === 0) {
resolve(response);
} else {
reject(new Error(LocalizedErrorStrings[response.error]))
}
} catch (err) {
reject(err);
}
});
});
}
}