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