Minor fixes for chat2 and store (#407)

This commit is contained in:
Hanno Cornelius 2021-03-08 09:45:10 +02:00 committed by GitHub
parent 307a49bb78
commit 398b701f4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 4 deletions

View File

@ -48,6 +48,7 @@ type Chat = ref object
connected: bool # if the node is connected to another peer
started: bool # if the node has started
nick: string # nickname for this chat session
prompt: bool # chat prompt is showing
type
PrivateKey* = crypto.PrivateKey
@ -70,6 +71,12 @@ proc connectToNodes(c: Chat, nodes: seq[string]) {.async.} =
await c.node.connectToNodes(nodes)
c.connected = true
proc showChatPrompt(c: Chat) =
if not c.prompt:
stdout.write(">> ")
stdout.flushFile()
c.prompt = true
proc selectRandomNode(): string =
randomize()
let
@ -125,8 +132,7 @@ proc writeAndPrint(c: Chat) {.async.} =
# echo "type /[help|?] for help"
# Chat prompt
stdout.write(">> ")
stdout.flushFile()
showChatPrompt(c)
let line = await c.transp.readLine()
if line.startsWith("/help") or line.startsWith("/?") or not c.started:
@ -212,7 +218,7 @@ proc processInput(rfd: AsyncFD, rng: ref BrHmacDrbgContext) {.async.} =
let nick = await readNick(transp)
echo "Welcome, " & nick & "!"
var chat = Chat(node: node, transp: transp, subscribed: true, connected: false, started: true, nick: nick)
var chat = Chat(node: node, transp: transp, subscribed: true, connected: false, started: true, nick: nick, prompt: false)
if conf.staticnodes.len > 0:
await connectToNodes(chat, conf.staticnodes)
@ -288,6 +294,8 @@ proc processInput(rfd: AsyncFD, rng: ref BrHmacDrbgContext) {.async.} =
if decodedPayload.isOK():
let payload = string.fromBytes(decodedPayload.get().payload)
echo &"{payload}"
chat.prompt = false
showChatPrompt(chat)
info "Hit subscribe handler", topic, payload,
contentTopic = msg.contentTopic
else:
@ -297,6 +305,8 @@ proc processInput(rfd: AsyncFD, rng: ref BrHmacDrbgContext) {.async.} =
# No payload encoding/encryption from Waku
let payload = string.fromBytes(msg.payload)
echo &"{payload}"
chat.prompt = false
showChatPrompt(chat)
info "Hit subscribe handler", topic, payload,
contentTopic = msg.contentTopic
else:

View File

@ -93,7 +93,7 @@ method getAll*(db: WakuMessageStore, onData: message_store.DataProc): MessageSto
onData(uint64(timestamp), WakuMessage(contentTopic: ContentTopic(int(topic)), payload: @(toOpenArray(p, 0, l-1))))
let res = db.database.query("SELECT timestamp, contentTopic, payload FROM messages", msg)
let res = db.database.query("SELECT timestamp, contentTopic, payload FROM messages ORDER BY timestamp ASC", msg)
if res.isErr:
return err("failed")