@@ -6,7 +6,6 @@ import arraybuffers = require('../../arraybuffers/arraybuffers');
66import crypto = require( 'crypto' ) ;
77import linefeeder = require( '../../net/linefeeder' ) ;
88import logging = require( '../../logging/logging' ) ;
9- import promises = require( '../../promises/promises' ) ;
109import queue = require( '../../handler/queue' ) ;
1110
1211// https://github.com/borisyankov/DefinitelyTyped/blob/master/ssh2/ssh2-tests.ts
@@ -209,24 +208,15 @@ export class CloudSocialProvider {
209208 } ) ;
210209 }
211210
212- let numAttempts = 0 ;
213- const connect = ( ) => {
214- log . debug ( 'connection attempt %1...' , ( ++ numAttempts ) ) ;
215- const connection = new Connection ( invite , ( message : Object ) => {
216- this . dispatchEvent_ ( 'onMessage' , {
217- from : makeClientState ( invite . host ) ,
218- // SIGNAL_FROM_SERVER_PEER,
219- message : JSON . stringify ( makeVersionedPeerMessage ( 3002 , message ) )
220- } ) ;
221- } ) ;
222- return connection . connect ( ) . then ( ( ) => {
223- return connection ;
211+ const connection = new Connection ( invite , ( message : Object ) => {
212+ this . dispatchEvent_ ( 'onMessage' , {
213+ from : makeClientState ( invite . host ) ,
214+ // SIGNAL_FROM_SERVER_PEER,
215+ message : JSON . stringify ( makeVersionedPeerMessage ( 3002 , message ) )
224216 } ) ;
225- } ;
226-
227- this . clients_ [ invite . host ] = promises . retryWithExponentialBackoff ( connect ,
228- MAX_CONNECTION_INTERVAL_MS , INITIAL_CONNECTION_INTERVAL_MS ) . then (
229- ( connection :Connection ) => {
217+ } ) ;
218+
219+ this . clients_ [ invite . host ] = connection . connect ( ) . then ( ( ) => {
230220 log . info ( 'connected to zork on %1' , invite . host ) ;
231221
232222 // Fetch the banner, if available, then emit an instance message.
@@ -289,7 +279,9 @@ export class CloudSocialProvider {
289279 log . debug ( 'saved contacts' ) ;
290280 } ) . catch ( ( e ) => {
291281 log . error ( 'could not save contacts: %1' , e ) ;
292- Promise . reject ( e ) ;
282+ Promise . reject ( {
283+ message : e . message
284+ } ) ;
293285 } ) ;
294286 }
295287
@@ -322,7 +314,9 @@ export class CloudSocialProvider {
322314 // the instance (safe because all we've done is run ping).
323315 log . info ( 'new proxying session %1' , payload . proxyingId ) ;
324316 if ( ! ( destinationClientId in this . savedContacts_ ) ) {
325- return Promise . reject ( new Error ( 'unknown client ' + destinationClientId ) ) ;
317+ return Promise . reject ( {
318+ message : 'unknown client ' + destinationClientId
319+ } ) ;
326320 }
327321 return this . reconnect_ ( this . savedContacts_ [ destinationClientId ] . invite ) . then (
328322 ( connection : Connection ) => {
@@ -335,30 +329,39 @@ export class CloudSocialProvider {
335329 connection . sendMessage ( JSON . stringify ( payload ) ) ;
336330 } ) ;
337331 } else {
338- return Promise . reject ( new Error ( 'unknown client ' + destinationClientId ) ) ;
332+ return Promise . reject ( {
333+ message : 'unknown client ' + destinationClientId
334+ } ) ;
339335 }
340336 }
341337 } else {
342- return Promise . reject ( new Error ( 'message has no or wrong type field' ) ) ;
338+ return Promise . reject ( {
339+ message : 'message has no or wrong type field'
340+ } ) ;
343341 }
344342 } catch ( e ) {
345- return Promise . reject ( new Error ( 'could not de-serialise message: ' + e . message ) ) ;
343+ return Promise . reject ( {
344+ message : 'could not de-serialise message: ' + e . message
345+ } ) ;
346346 }
347347 }
348348
349349 public clearCachedCredentials = ( ) : Promise < void > => {
350- return Promise . reject (
351- new Error ( 'clearCachedCredentials unimplemented' ) ) ;
350+ return Promise . reject ( {
351+ message : 'clearCachedCredentials unimplemented'
352+ } ) ;
352353 }
353354
354355 public getUsers = ( ) : Promise < freedom . Social . Users > => {
355- return Promise . reject (
356- new Error ( 'getUsers unimplemented' ) ) ;
356+ return Promise . reject ( {
357+ message : 'getUsers unimplemented'
358+ } ) ;
357359 }
358360
359361 public getClients = ( ) : Promise < freedom . Social . Clients > => {
360- return Promise . reject (
361- new Error ( 'getClients unimplemented' ) ) ;
362+ return Promise . reject ( {
363+ message : 'getClients unimplemented'
364+ } ) ;
362365 }
363366
364367 public logout = ( ) : Promise < void > => {
@@ -391,24 +394,35 @@ export class CloudSocialProvider {
391394 } ) ;
392395 }
393396
394- // Parses an invite code, received from uProxy in JSON format.
395- // This is the networkData field of the invite codes distributed
396- // to uProxy users.
397+ // Parses the networkData field, serialised to JSON, of invites.
398+ // The contact is immediately saved and added to the contacts list.
397399 public acceptUserInvitation = ( inviteJson : string ) : Promise < void > => {
398400 log . debug ( 'acceptUserInvitation' ) ;
399401 try {
400- var invite = < Invite > JSON . parse ( inviteJson ) ;
401- return this . reconnect_ ( invite ) . then ( ( connection : Connection ) => {
402- // Return nothing for type checking purposes.
402+ const invite = < Invite > JSON . parse ( inviteJson ) ;
403+
404+ this . notifyOfUser_ ( invite ) ;
405+ this . savedContacts_ [ invite . host ] = {
406+ invite : invite
407+ } ;
408+ this . saveContacts_ ( ) ;
409+
410+ // Connect in the background in order to fetch metadata such as
411+ // the banner (description).
412+ this . reconnect_ ( invite ) . catch ( ( e : Error ) => {
413+ log . warn ( 'failed to log into cloud server during invite accept: %1' , e . message ) ;
403414 } ) ;
415+
416+ return Promise . resolve < void > ( ) ;
404417 } catch ( e ) {
405418 return Promise . reject ( new Error ( 'could not parse invite code: ' + e . message ) ) ;
406419 }
407420 }
408421
409422 public blockUser = ( userId : string ) : Promise < void > => {
410- return Promise . reject (
411- new Error ( 'blockUser unimplemented' ) ) ;
423+ return Promise . reject ( {
424+ message : 'blockUser unimplemented'
425+ } ) ;
412426 }
413427
414428 // Removes a cloud contact from storage
@@ -599,7 +613,9 @@ class Connection {
599613 private exec_ = ( command : string ) : Promise < string > => {
600614 log . debug ( '%1: execute command: %2' , this . name_ , command ) ;
601615 if ( this . state_ !== ConnectionState . ESTABLISHED ) {
602- return Promise . reject ( new Error ( 'can only execute commands in ESTABLISHED state' ) ) ;
616+ return Promise . reject ( {
617+ message : 'can only execute commands in ESTABLISHED state'
618+ } ) ;
603619 }
604620 return new Promise < string > ( ( F , R ) => {
605621 this . connection_ . exec ( command , ( e : Error , stream : ssh2 . Channel ) => {
0 commit comments