44
44
../ waku_lightpush/ client as lightpush_client,
45
45
../ waku_lightpush/ common,
46
46
../ waku_lightpush/ protocol,
47
+ ../ waku_lightpush/ self_req_handler,
47
48
../ waku_enr,
48
49
../ waku_peer_exchange,
49
50
../ waku_rln_relay,
@@ -913,7 +914,7 @@ proc mountLightPush*(
913
914
914
915
if publishedCount == 0 :
915
916
# # Agreed change expected to the lightpush protocol to better handle such case. https://github.com/waku-org/pm/issues/93
916
- debug ( " Lightpush request has not been published to any peers" )
917
+ debug " Lightpush request has not been published to any peers"
917
918
918
919
return ok ()
919
920
@@ -942,15 +943,30 @@ proc lightpushPublish*(
942
943
# # Returns whether relaying was successful or not.
943
944
# # `WakuMessage` should contain a `contentTopic` field for light node
944
945
# # functionality.
945
- if node.wakuLightpushClient.isNil ():
946
- return err (" waku lightpush client is nil" )
946
+ if node.wakuLightpushClient.isNil () and node.wakuLightPush.isNil ():
947
+ error " failed to publish message as lightpush not available"
948
+ return err (" Waku lightpush not available" )
949
+
950
+ let internalPublish = proc (
951
+ node: WakuNode ,
952
+ pubsubTopic: PubsubTopic ,
953
+ message: WakuMessage ,
954
+ peer: RemotePeerInfo ,
955
+ ): Future [WakuLightPushResult [void ]] {.async , gcsafe .} =
956
+ if not node.wakuLightpushClient.isNil ():
957
+ debug " publishing message with lightpush" ,
958
+ pubsubTopic = pubsubTopic,
959
+ contentTopic = message.contentTopic,
960
+ peer = peer.peerId
961
+ return await node.wakuLightpushClient.publish (pubsubTopic, message, peer)
962
+
963
+ if not node.wakuLightPush.isNil ():
964
+ debug " publishing message with self hosted lightpush" ,
965
+ pubsubTopic = pubsubTopic, contentTopic = message.contentTopic
966
+ return await node.wakuLightPush.handleSelfLightPushRequest (pubsubTopic, message)
947
967
948
968
if pubsubTopic.isSome ():
949
- debug " publishing message with lightpush" ,
950
- pubsubTopic = pubsubTopic.get (),
951
- contentTopic = message.contentTopic,
952
- peer = peer.peerId
953
- return await node.wakuLightpushClient.publish (pubsubTopic.get (), message, peer)
969
+ return await internalPublish (node, pubsubTopic.get (), message, peer)
954
970
955
971
let topicMapRes = node.wakuSharding.parseSharding (pubsubTopic, message.contentTopic)
956
972
@@ -961,26 +977,27 @@ proc lightpushPublish*(
961
977
topicMapRes.get ()
962
978
963
979
for pubsub, _ in topicMap.pairs: # There's only one pair anyway
964
- debug " publishing message with lightpush" ,
965
- pubsubTopic = pubsub, contentTopic = message.contentTopic, peer = peer.peerId
966
- return await node.wakuLightpushClient.publish ($ pubsub, message, peer)
980
+ return await internalPublish (node, $ pubsub, message, peer)
967
981
968
982
# TODO : Move to application module (e.g., wakunode2.nim)
969
983
proc lightpushPublish * (
970
984
node: WakuNode , pubsubTopic: Option [PubsubTopic ], message: WakuMessage
971
985
): Future [WakuLightPushResult [void ]] {.
972
986
async , gcsafe , deprecated : " Use 'node.lightpushPublish()' instead"
973
987
.} =
974
- if node.wakuLightpushClient.isNil ():
975
- let msg = " waku lightpush client is nil"
976
- error " failed to publish message" , msg = msg
977
- return err (msg)
978
-
979
- let peerOpt = node.peerManager.selectPeer (WakuLightPushCodec )
980
- if peerOpt.isNone ():
981
- let msg = " no suitable remote peers"
982
- error " failed to publish message" , msg = msg
983
- return err (msg)
988
+ if node.wakuLightpushClient.isNil () and node.wakuLightPush.isNil ():
989
+ error " failed to publish message as lightpush not available"
990
+ return err (" waku lightpush not available" )
991
+
992
+ var peerOpt: Option [RemotePeerInfo ] = none (RemotePeerInfo )
993
+ if not node.wakuLightpushClient.isNil ():
994
+ peerOpt = node.peerManager.selectPeer (WakuLightPushCodec )
995
+ if peerOpt.isNone ():
996
+ let msg = " no suitable remote peers"
997
+ error " failed to publish message" , msg = msg
998
+ return err (msg)
999
+ elif not node.wakuLightPush.isNil ():
1000
+ peerOpt = some (RemotePeerInfo .init ($ node.switch.peerInfo.peerId))
984
1001
985
1002
let publishRes =
986
1003
await node.lightpushPublish (pubsubTopic, message, peer = peerOpt.get ())
0 commit comments