@@ -174,18 +174,24 @@ router.post('/messages', async (req, res, next) => {
174174 } else {
175175 throw new RequestError ( 'Missing recipient' , 400 ) ;
176176 }
177- let recipientPeer = config . peers . find ( peer => peer . id === recipientID ) ;
178- if ( recipientPeer === undefined ) {
179- throw new RequestError ( `Unknown recipient ${ recipientID } ` , 400 ) ;
180- }
181- if ( recipientDestination !== undefined && ! recipientPeer . destinations ?. includes ( recipientDestination ) ) {
182- throw new RequestError ( `Unknown recipient destination expected=${ recipientPeer . destinations ?. join ( '|' ) ?? 'none' } recieved=${ recipientDestination } ` , 400 ) ;
177+ let recipientEndpoint : string ;
178+ if ( recipientID === peerID ) {
179+ recipientEndpoint = config . p2p . endpoint ?? `https://${ config . p2p . hostname } :${ config . p2p . port } ` ;
180+ } else {
181+ let recipientPeer = config . peers . find ( peer => peer . id === recipientID ) ;
182+ if ( recipientPeer === undefined ) {
183+ throw new RequestError ( `Unknown recipient ${ recipientID } ` , 400 ) ;
184+ }
185+ recipientEndpoint = recipientPeer . endpoint ;
186+ if ( recipientDestination !== undefined && ! recipientPeer . destinations ?. includes ( recipientDestination ) ) {
187+ throw new RequestError ( `Unknown recipient destination expected=${ recipientPeer . destinations ?. join ( '|' ) ?? 'none' } recieved=${ recipientDestination } ` , 400 ) ;
188+ }
183189 }
184190 let requestId = uuidV4 ( ) ;
185191 if ( typeof req . body . requestId === 'string' ) {
186192 requestId = req . body . requestId ;
187193 }
188- messagesHandler . sendMessage ( req . body . message , recipientID , recipientPeer . endpoint , requestId , senderDestination , recipientDestination ) ;
194+ messagesHandler . sendMessage ( req . body . message , recipientID , recipientEndpoint , requestId , senderDestination , recipientDestination ) ;
189195 res . send ( { requestId } ) ;
190196 } catch ( err ) {
191197 next ( err ) ;
0 commit comments