2021-01-06 09:35:05 +00:00
|
|
|
## Contains types and utilities for pagination.
|
2021-07-22 08:43:41 +00:00
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
2022-02-17 10:00:45 +00:00
|
|
|
import
|
2022-07-25 11:01:37 +00:00
|
|
|
stew/byteutils,
|
2022-09-12 13:51:06 +00:00
|
|
|
nimcrypto/sha2
|
2022-07-25 11:01:37 +00:00
|
|
|
import
|
|
|
|
../protocol/waku_message,
|
|
|
|
./time
|
2022-02-17 10:00:45 +00:00
|
|
|
|
2022-09-22 09:17:38 +00:00
|
|
|
const
|
|
|
|
MaxPageSize*: uint64 = 100
|
|
|
|
|
|
|
|
DefaultPageSize*: uint64 = 20 # A recommended default number of waku messages per page
|
|
|
|
|
|
|
|
|
2022-09-27 19:10:11 +00:00
|
|
|
type MessageDigest* = MDigest[256]
|
|
|
|
|
|
|
|
type PagingIndex* = object
|
2022-07-25 11:01:37 +00:00
|
|
|
## This type contains the description of an Index used in the pagination of WakuMessages
|
|
|
|
pubsubTopic*: string
|
2022-09-12 13:51:06 +00:00
|
|
|
senderTime*: Timestamp # the time at which the message is generated
|
|
|
|
receiverTime*: Timestamp
|
2022-09-26 09:50:15 +00:00
|
|
|
digest*: MessageDigest # calculated over payload and content topic
|
2022-09-12 13:51:06 +00:00
|
|
|
|
2022-09-26 09:50:15 +00:00
|
|
|
proc computeDigest*(msg: WakuMessage): MessageDigest =
|
2022-09-12 13:51:06 +00:00
|
|
|
var ctx: sha256
|
|
|
|
ctx.init()
|
|
|
|
defer: ctx.clear()
|
|
|
|
|
|
|
|
ctx.update(msg.contentTopic.toBytes())
|
|
|
|
ctx.update(msg.payload)
|
2022-07-25 11:01:37 +00:00
|
|
|
|
2022-09-12 13:51:06 +00:00
|
|
|
# Computes the hash
|
|
|
|
return ctx.finish()
|
2022-07-25 11:01:37 +00:00
|
|
|
|
2022-09-27 19:10:11 +00:00
|
|
|
proc compute*(T: type PagingIndex, msg: WakuMessage, receivedTime: Timestamp, pubsubTopic: string): T =
|
2022-07-25 11:01:37 +00:00
|
|
|
## Takes a WakuMessage with received timestamp and returns its Index.
|
|
|
|
let
|
2022-09-12 13:51:06 +00:00
|
|
|
digest = computeDigest(msg)
|
2022-07-25 11:01:37 +00:00
|
|
|
senderTime = msg.timestamp
|
|
|
|
|
2022-09-27 19:10:11 +00:00
|
|
|
PagingIndex(
|
2022-09-12 13:51:06 +00:00
|
|
|
pubsubTopic: pubsubTopic,
|
2022-07-25 11:01:37 +00:00
|
|
|
senderTime: senderTime,
|
2022-09-12 13:51:06 +00:00
|
|
|
receiverTime: receivedTime,
|
|
|
|
digest: digest
|
2022-07-25 11:01:37 +00:00
|
|
|
)
|
2022-02-17 10:00:45 +00:00
|
|
|
|
2022-09-27 19:10:11 +00:00
|
|
|
proc `==`*(x, y: PagingIndex): bool =
|
2022-02-28 16:29:01 +00:00
|
|
|
## receiverTime plays no role in index equality
|
|
|
|
(x.senderTime == y.senderTime) and
|
|
|
|
(x.digest == y.digest) and
|
|
|
|
(x.pubsubTopic == y.pubsubTopic)
|
2022-02-17 10:00:45 +00:00
|
|
|
|
2022-07-25 11:01:37 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
PagingDirection* {.pure.} = enum
|
|
|
|
## PagingDirection determines the direction of pagination
|
|
|
|
BACKWARD = uint32(0)
|
|
|
|
FORWARD = uint32(1)
|
|
|
|
|
|
|
|
PagingInfo* = object
|
|
|
|
## This type holds the information needed for the pagination
|
|
|
|
pageSize*: uint64
|
2022-09-27 19:10:11 +00:00
|
|
|
cursor*: PagingIndex
|
2022-07-25 11:01:37 +00:00
|
|
|
direction*: PagingDirection
|