2024-11-08 14:36:16 +07:00
|
|
|
import std/[json, sugar, strutils, options]
|
2025-12-19 17:00:43 +01:00
|
|
|
import chronos, chronicles, results, stew/byteutils, ffi
|
2024-08-29 14:29:02 +02:00
|
|
|
import
|
2025-12-19 17:00:43 +01:00
|
|
|
waku/factory/waku,
|
|
|
|
|
library/utils,
|
|
|
|
|
waku/waku_core/peers,
|
|
|
|
|
waku/waku_core/message/digest,
|
|
|
|
|
waku/waku_store/common,
|
|
|
|
|
waku/waku_store/client,
|
|
|
|
|
waku/common/paging,
|
|
|
|
|
library/declare_lib
|
|
|
|
|
|
|
|
|
|
func fromJsonNode(jsonContent: JsonNode): Result[StoreQueryRequest, string] =
|
2025-02-12 18:35:50 +02:00
|
|
|
var contentTopics: seq[string]
|
2025-04-11 12:07:35 +03:00
|
|
|
if jsonContent.contains("contentTopics"):
|
2025-02-12 18:35:50 +02:00
|
|
|
contentTopics = collect(newSeq):
|
2025-04-11 12:07:35 +03:00
|
|
|
for cTopic in jsonContent["contentTopics"].getElems():
|
2025-02-12 18:35:50 +02:00
|
|
|
cTopic.getStr()
|
2024-08-29 14:29:02 +02:00
|
|
|
|
2025-01-30 10:15:31 +02:00
|
|
|
var msgHashes: seq[WakuMessageHash]
|
2025-04-11 12:07:35 +03:00
|
|
|
if jsonContent.contains("messageHashes"):
|
|
|
|
|
for hashJsonObj in jsonContent["messageHashes"].getElems():
|
2025-01-30 10:15:31 +02:00
|
|
|
let hash = hashJsonObj.getStr().hexToHash().valueOr:
|
|
|
|
|
return err("Failed converting message hash hex string to bytes: " & error)
|
|
|
|
|
msgHashes.add(hash)
|
2024-08-29 14:29:02 +02:00
|
|
|
|
|
|
|
|
let pubsubTopic =
|
2025-04-11 12:07:35 +03:00
|
|
|
if jsonContent.contains("pubsubTopic"):
|
|
|
|
|
some(jsonContent["pubsubTopic"].getStr())
|
2024-08-29 14:29:02 +02:00
|
|
|
else:
|
|
|
|
|
none(string)
|
|
|
|
|
|
|
|
|
|
let paginationCursor =
|
2025-04-11 12:07:35 +03:00
|
|
|
if jsonContent.contains("paginationCursor"):
|
|
|
|
|
let hash = jsonContent["paginationCursor"].getStr().hexToHash().valueOr:
|
|
|
|
|
return err("Failed converting paginationCursor hex string to bytes: " & error)
|
2024-08-29 14:29:02 +02:00
|
|
|
some(hash)
|
|
|
|
|
else:
|
|
|
|
|
none(WakuMessageHash)
|
|
|
|
|
|
2025-04-11 12:07:35 +03:00
|
|
|
let paginationForwardBool = jsonContent["paginationForward"].getBool()
|
2024-08-29 14:29:02 +02:00
|
|
|
let paginationForward =
|
|
|
|
|
if paginationForwardBool: PagingDirection.FORWARD else: PagingDirection.BACKWARD
|
|
|
|
|
|
|
|
|
|
let paginationLimit =
|
2025-04-11 12:07:35 +03:00
|
|
|
if jsonContent.contains("paginationLimit"):
|
|
|
|
|
some(uint64(jsonContent["paginationLimit"].getInt()))
|
2024-08-29 14:29:02 +02:00
|
|
|
else:
|
|
|
|
|
none(uint64)
|
|
|
|
|
|
2025-04-11 12:07:35 +03:00
|
|
|
let startTime = ?jsonContent.getProtoInt64("timeStart")
|
|
|
|
|
let endTime = ?jsonContent.getProtoInt64("timeEnd")
|
2025-01-03 12:26:46 +01:00
|
|
|
|
2024-12-10 13:52:21 -04:00
|
|
|
return ok(
|
|
|
|
|
StoreQueryRequest(
|
2025-04-11 12:07:35 +03:00
|
|
|
requestId: jsonContent["requestId"].getStr(),
|
|
|
|
|
includeData: jsonContent["includeData"].getBool(),
|
2024-12-10 13:52:21 -04:00
|
|
|
pubsubTopic: pubsubTopic,
|
|
|
|
|
contentTopics: contentTopics,
|
2025-01-03 12:26:46 +01:00
|
|
|
startTime: startTime,
|
|
|
|
|
endTime: endTime,
|
2024-12-10 13:52:21 -04:00
|
|
|
messageHashes: msgHashes,
|
|
|
|
|
paginationCursor: paginationCursor,
|
|
|
|
|
paginationForward: paginationForward,
|
|
|
|
|
paginationLimit: paginationLimit,
|
|
|
|
|
)
|
2024-08-29 14:29:02 +02:00
|
|
|
)
|
2023-12-11 08:49:13 +01:00
|
|
|
|
2025-12-19 17:00:43 +01:00
|
|
|
proc waku_store_query(
|
|
|
|
|
ctx: ptr FFIContext[Waku],
|
|
|
|
|
callback: FFICallBack,
|
|
|
|
|
userData: pointer,
|
2024-08-29 14:29:02 +02:00
|
|
|
jsonQuery: cstring,
|
2024-03-16 00:08:47 +01:00
|
|
|
peerAddr: cstring,
|
|
|
|
|
timeoutMs: cint,
|
2025-12-19 17:00:43 +01:00
|
|
|
) {.ffi.} =
|
2024-08-29 14:29:02 +02:00
|
|
|
let jsonContentRes = catch:
|
2025-12-19 17:00:43 +01:00
|
|
|
parseJson($jsonQuery)
|
2024-08-29 14:29:02 +02:00
|
|
|
|
|
|
|
|
if jsonContentRes.isErr():
|
2025-01-03 12:26:46 +01:00
|
|
|
return err("StoreRequest failed parsing store request: " & jsonContentRes.error.msg)
|
2024-08-29 14:29:02 +02:00
|
|
|
|
2025-12-19 17:00:43 +01:00
|
|
|
let storeQueryRequest = ?fromJsonNode(jsonContentRes.get())
|
2024-08-29 14:29:02 +02:00
|
|
|
|
2025-12-19 17:00:43 +01:00
|
|
|
let peer = peers.parsePeerInfo(($peerAddr).split(",")).valueOr:
|
2025-01-03 12:26:46 +01:00
|
|
|
return err("StoreRequest failed to parse peer addr: " & $error)
|
2024-08-29 14:29:02 +02:00
|
|
|
|
2025-12-19 17:00:43 +01:00
|
|
|
let queryResponse = (
|
|
|
|
|
await ctx.myLib[].node.wakuStoreClient.query(storeQueryRequest, peer)
|
|
|
|
|
).valueOr:
|
2025-01-03 12:26:46 +01:00
|
|
|
return err("StoreRequest failed store query: " & $error)
|
2024-08-29 14:29:02 +02:00
|
|
|
|
2025-01-30 10:15:31 +02:00
|
|
|
let res = $(%*(queryResponse.toHex()))
|
|
|
|
|
return ok(res) ## returning the response in json format
|