@ -87,7 +87,7 @@ MessageReceiver.prototype.extend({
localLokiServer . start ( localServerPort ) . then ( port => {
localLokiServer . start ( localServerPort ) . then ( port => {
window . log . info ( ` Local Server started at localhost: ${ port } ` ) ;
window . log . info ( ` Local Server started at localhost: ${ port } ` ) ;
libloki . api . broadcastOnlineStatus ( ) ;
libloki . api . broadcastOnlineStatus ( ) ;
localLokiServer . on ( 'message' , this . h ttpPollingResource. h andleMessage) ;
localLokiServer . on ( 'message' , this . h andleP2p Message. bind ( this ) ) ;
} ) ;
} ) ;
// TODO: Rework this socket stuff to work with online messaging
// TODO: Rework this socket stuff to work with online messaging
@ -119,6 +119,9 @@ MessageReceiver.prototype.extend({
// all cached envelopes are processed.
// all cached envelopes are processed.
this . incoming = [ this . pending ] ;
this . incoming = [ this . pending ] ;
} ,
} ,
handleP2pMessage ( message ) {
this . httpPollingResource . handleMessage ( message , true ) ;
} ,
shutdown ( ) {
shutdown ( ) {
if ( this . socket ) {
if ( this . socket ) {
this . socket . onclose = null ;
this . socket . onclose = null ;
@ -135,7 +138,7 @@ MessageReceiver.prototype.extend({
if ( localLokiServer ) {
if ( localLokiServer ) {
localLokiServer . removeListener (
localLokiServer . removeListener (
'message' ,
'message' ,
this . h ttpPollingResource. h andleMessage
this . h andleP2p Message. bind ( this )
) ;
) ;
}
}
} ,
} ,
@ -194,7 +197,7 @@ MessageReceiver.prototype.extend({
// return this.dispatchAndWait(event);
// return this.dispatchAndWait(event);
// });
// });
} ,
} ,
handleRequest ( request ) {
handleRequest ( request , isP2p = false ) {
this . incoming = this . incoming || [ ] ;
this . incoming = this . incoming || [ ] ;
const lastPromise = _ . last ( this . incoming ) ;
const lastPromise = _ . last ( this . incoming ) ;
@ -214,6 +217,9 @@ MessageReceiver.prototype.extend({
const promise = Promise . resolve ( request . body . toArrayBuffer ( ) ) // textsecure.crypto
const promise = Promise . resolve ( request . body . toArrayBuffer ( ) ) // textsecure.crypto
. then ( plaintext => {
. then ( plaintext => {
const envelope = textsecure . protobuf . Envelope . decode ( plaintext ) ;
const envelope = textsecure . protobuf . Envelope . decode ( plaintext ) ;
if ( isP2p ) {
lokiP2pAPI . setContactOnline ( envelope . source ) ;
}
// After this point, decoding errors are not the server's
// After this point, decoding errors are not the server's
// fault, and we should handle them gracefully and tell the
// fault, and we should handle them gracefully and tell the
// user they received an invalid message
// user they received an invalid message
@ -223,6 +229,7 @@ MessageReceiver.prototype.extend({
}
}
envelope . id = envelope . serverGuid || window . getGuid ( ) ;
envelope . id = envelope . serverGuid || window . getGuid ( ) ;
envelope . isP2p = isP2p ;
envelope . serverTimestamp = envelope . serverTimestamp
envelope . serverTimestamp = envelope . serverTimestamp
? envelope . serverTimestamp . toNumber ( )
? envelope . serverTimestamp . toNumber ( )
: null ;
: null ;
@ -901,7 +908,12 @@ MessageReceiver.prototype.extend({
} ,
} ,
async handleLokiAddressMessage ( envelope , lokiAddressMessage ) {
async handleLokiAddressMessage ( envelope , lokiAddressMessage ) {
const { p2pAddress , p2pPort } = lokiAddressMessage ;
const { p2pAddress , p2pPort } = lokiAddressMessage ;
lokiP2pAPI . addContactP2pDetails ( envelope . source , p2pAddress , p2pPort ) ;
lokiP2pAPI . addContactP2pDetails (
envelope . source ,
p2pAddress ,
p2pPort ,
envelope . isP2p
) ;
return this . removeFromCache ( envelope ) ;
return this . removeFromCache ( envelope ) ;
} ,
} ,
handleDataMessage ( envelope , msg ) {
handleDataMessage ( envelope , msg ) {