mirror of
https://github.com/logos-messaging/logos-messaging-nim.git
synced 2026-02-17 04:13:30 +00:00
api changes to integrate with logos-chat-ui app
This commit is contained in:
parent
45da95f45e
commit
03bcaf10c7
@ -9,7 +9,11 @@ import
|
||||
metrics,
|
||||
ffi
|
||||
import
|
||||
waku/factory/waku, waku/node/waku_node, waku/node/health_monitor, library/declare_lib
|
||||
waku/factory/waku,
|
||||
waku/node/waku_node,
|
||||
waku/node/health_monitor,
|
||||
library/declare_lib,
|
||||
waku/waku_core/codecs
|
||||
|
||||
proc getMultiaddresses(node: WakuNode): seq[string] =
|
||||
return node.info().listenAddresses
|
||||
@ -48,3 +52,19 @@ proc waku_is_online(
|
||||
ctx: ptr FFIContext[Waku], callback: FFICallBack, userData: pointer
|
||||
) {.ffi.} =
|
||||
return ok($ctx.myLib[].healthMonitor.onlineMonitor.amIOnline())
|
||||
|
||||
proc waku_get_mixnode_pool_size(
|
||||
ctx: ptr FFIContext[Waku], callback: FFICallBack, userData: pointer
|
||||
) {.ffi.} =
|
||||
## Returns the number of mix nodes in the pool
|
||||
if ctx.myLib[].node.wakuMix.isNil():
|
||||
return ok("0")
|
||||
return ok($ctx.myLib[].node.getMixNodePoolSize())
|
||||
|
||||
proc waku_get_lightpush_peers_count(
|
||||
ctx: ptr FFIContext[Waku], callback: FFICallBack, userData: pointer
|
||||
) {.ffi.} =
|
||||
## Returns the count of all peers in peerstore supporting lightpush protocol
|
||||
let peers =
|
||||
ctx.myLib[].node.peerManager.switch.peerStore.getPeersByProtocol(WakuLightPushCodec)
|
||||
return ok($peers.len)
|
||||
|
||||
@ -47,10 +47,13 @@ proc waku_filter_subscribe(
|
||||
error "fail filter subscribe", error = errorMsg
|
||||
return err(errorMsg)
|
||||
|
||||
let pubsubTopicOpt =
|
||||
if ($pubsubTopic).len > 0:
|
||||
some(PubsubTopic($pubsubTopic))
|
||||
else:
|
||||
none(PubsubTopic)
|
||||
let subFut = ctx.myLib[].node.filterSubscribe(
|
||||
some(PubsubTopic($pubsubTopic)),
|
||||
($contentTopics).split(",").mapIt(ContentTopic(it)),
|
||||
peer,
|
||||
pubsubTopicOpt, ($contentTopics).split(",").mapIt(ContentTopic(it)), peer
|
||||
)
|
||||
if not await subFut.withTimeout(FilterOpTimeout):
|
||||
let errorMsg = "filter subscription timed out"
|
||||
|
||||
@ -39,13 +39,15 @@ proc waku_lightpush_publish(
|
||||
let errorMsg = "failed to lightpublish message, no suitable remote peers"
|
||||
error "PUBLISH failed", error = errorMsg
|
||||
return err(errorMsg)
|
||||
let topic =
|
||||
if ($pubsubTopic).len == 0:
|
||||
none(PubsubTopic)
|
||||
else:
|
||||
some(PubsubTopic($pubsubTopic))
|
||||
|
||||
let msgHashHex = (
|
||||
await ctx.myLib[].node.wakuLegacyLightpushClient.publish(
|
||||
$pubsubTopic, msg, peer = peerOpt.get()
|
||||
)
|
||||
).valueOr:
|
||||
error "PUBLISH failed", error = error
|
||||
return err($error)
|
||||
let messageHash = (await ctx.myLib[].node.lightpushPublish(topic, msg, peerOpt)).valueOr:
|
||||
let errorMsg = error.desc.get($error.code.int)
|
||||
error "PUBLISH failed", error = errorMsg
|
||||
return err(errorMsg)
|
||||
|
||||
return ok(msgHashHex)
|
||||
return ok($messageHash)
|
||||
|
||||
@ -85,8 +85,8 @@ proc waku_relay_subscribe(
|
||||
callback: FFICallBack,
|
||||
userData: pointer,
|
||||
pubSubTopic: cstring,
|
||||
contentTopic: cstring,
|
||||
) {.ffi.} =
|
||||
echo "Subscribing to topic: " & $pubSubTopic & " ..."
|
||||
proc onReceivedMessage(ctx: ptr FFIContext[Waku]): WakuRelayHandler =
|
||||
return proc(pubsubTopic: PubsubTopic, msg: WakuMessage) {.async.} =
|
||||
callEventCallback(ctx, "onReceivedMessage"):
|
||||
@ -94,21 +94,38 @@ proc waku_relay_subscribe(
|
||||
|
||||
var cb = onReceivedMessage(ctx)
|
||||
|
||||
ctx.myLib[].node.subscribe(
|
||||
(kind: SubscriptionKind.PubsubSub, topic: $pubsubTopic),
|
||||
handler = WakuRelayHandler(cb),
|
||||
).isOkOr:
|
||||
# If contentTopic is provided and non-empty, use ContentSub, otherwise use PubsubSub
|
||||
let subscription =
|
||||
if contentTopic != nil and len($contentTopic) > 0:
|
||||
echo "Subscribing to content topic: " & $contentTopic & " ..."
|
||||
(kind: SubscriptionKind.ContentSub, topic: $contentTopic)
|
||||
else:
|
||||
echo "Subscribing to pubsub topic: " & $pubSubTopic & " ..."
|
||||
(kind: SubscriptionKind.PubsubSub, topic: $pubsubTopic)
|
||||
|
||||
ctx.myLib[].node.subscribe(subscription, handler = WakuRelayHandler(cb)).isOkOr:
|
||||
error "SUBSCRIBE failed", error = error
|
||||
return err($error)
|
||||
return ok("")
|
||||
|
||||
# NOTE: When unsubscribing via contentTopic, this will unsubscribe from the entire
|
||||
# underlying pubsub topic/shard that the content topic maps to. This affects ALL
|
||||
# content topics on the same shard, not just the specified content topic.
|
||||
proc waku_relay_unsubscribe(
|
||||
ctx: ptr FFIContext[Waku],
|
||||
callback: FFICallBack,
|
||||
userData: pointer,
|
||||
pubSubTopic: cstring,
|
||||
contentTopic: cstring,
|
||||
) {.ffi.} =
|
||||
ctx.myLib[].node.unsubscribe((kind: SubscriptionKind.PubsubSub, topic: $pubsubTopic)).isOkOr:
|
||||
# If contentTopic is provided and non-empty, use ContentUnsub, otherwise use PubsubUnsub
|
||||
let subscription =
|
||||
if contentTopic != nil and len($contentTopic) > 0:
|
||||
(kind: SubscriptionKind.ContentUnsub, topic: $contentTopic)
|
||||
else:
|
||||
(kind: SubscriptionKind.PubsubUnsub, topic: $pubsubTopic)
|
||||
|
||||
ctx.myLib[].node.unsubscribe(subscription).isOkOr:
|
||||
error "UNSUBSCRIBE failed", error = error
|
||||
return err($error)
|
||||
|
||||
|
||||
@ -8,8 +8,13 @@ import
|
||||
waku/waku_store/common,
|
||||
waku/waku_store/client,
|
||||
waku/common/paging,
|
||||
waku/common/base64,
|
||||
library/declare_lib
|
||||
|
||||
# Custom JSON serialization for seq[byte] to avoid ambiguity
|
||||
proc `%`*(data: seq[byte]): JsonNode =
|
||||
%base64.encode(data)
|
||||
|
||||
func fromJsonNode(jsonContent: JsonNode): Result[StoreQueryRequest, string] =
|
||||
var contentTopics: seq[string]
|
||||
if jsonContent.contains("contentTopics"):
|
||||
@ -90,5 +95,6 @@ proc waku_store_query(
|
||||
).valueOr:
|
||||
return err("StoreRequest failed store query: " & $error)
|
||||
|
||||
let res = $(%*(queryResponse.toHex()))
|
||||
let hexResponse = queryResponse.toHex()
|
||||
let res = $(%*hexResponse)
|
||||
return ok(res) ## returning the response in json format
|
||||
|
||||
@ -85,7 +85,8 @@ extern "C"
|
||||
int waku_relay_subscribe(void *ctx,
|
||||
FFICallBack callback,
|
||||
void *userData,
|
||||
const char *pubSubTopic);
|
||||
const char *pubSubTopic,
|
||||
const char *contentTopic);
|
||||
|
||||
int waku_relay_add_protected_shard(void *ctx,
|
||||
FFICallBack callback,
|
||||
@ -97,7 +98,8 @@ extern "C"
|
||||
int waku_relay_unsubscribe(void *ctx,
|
||||
FFICallBack callback,
|
||||
void *userData,
|
||||
const char *pubSubTopic);
|
||||
const char *pubSubTopic,
|
||||
const char *contentTopic);
|
||||
|
||||
int waku_filter_subscribe(void *ctx,
|
||||
FFICallBack callback,
|
||||
@ -247,6 +249,14 @@ extern "C"
|
||||
FFICallBack callback,
|
||||
void *userData);
|
||||
|
||||
int waku_get_mixnode_pool_size(void *ctx,
|
||||
FFICallBack callback,
|
||||
void *userData);
|
||||
|
||||
int waku_get_lightpush_peers_count(void *ctx,
|
||||
FFICallBack callback,
|
||||
void *userData);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@ -247,11 +247,14 @@ proc lightpushPublish*(
|
||||
return lighpushErrorResult(
|
||||
LightPushErrorCode.SERVICE_NOT_AVAILABLE, "Waku lightpush not available"
|
||||
)
|
||||
if mixify and node.wakuMix.isNil():
|
||||
error "failed to publish message using mix as mix protocol is not mounted"
|
||||
var lmixify = mixify
|
||||
if not node.wakuMix.isNil():
|
||||
lmixify = true
|
||||
|
||||
#[ error "failed to publish message using mix as mix protocol is not mounted"
|
||||
return lighpushErrorResult(
|
||||
LightPushErrorCode.SERVICE_NOT_AVAILABLE, "Waku lightpush with mix not available"
|
||||
)
|
||||
) ]#
|
||||
let toPeer: RemotePeerInfo = peerOpt.valueOr:
|
||||
if not node.wakuLightPush.isNil():
|
||||
RemotePeerInfo.init(node.peerId())
|
||||
@ -281,4 +284,4 @@ proc lightpushPublish*(
|
||||
error "lightpush publish error", error = msg
|
||||
return lighpushErrorResult(LightPushErrorCode.INTERNAL_SERVER_ERROR, msg)
|
||||
|
||||
return await lightpushPublishHandler(node, pubsubForPublish, message, toPeer, mixify)
|
||||
return await lightpushPublishHandler(node, pubsubForPublish, message, toPeer, lmixify)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user