mirror of
https://github.com/logos-messaging/nim-chat-poc.git
synced 2026-02-17 20:43:09 +00:00
Add onNewConvo
This commit is contained in:
parent
eacb98b50c
commit
87adb3b5d0
@ -35,6 +35,7 @@ logScope:
|
||||
|
||||
type
|
||||
MessageCallback[T] = proc(conversation: Conversation, msg: T): Future[void] {.async.}
|
||||
NewConvoCallback = proc(conversation: Conversation): Future[void] {.async.}
|
||||
|
||||
|
||||
type KeyEntry* = object
|
||||
@ -51,6 +52,7 @@ type Client* = ref object
|
||||
isRunning: bool
|
||||
|
||||
newMessageCallbacks: seq[MessageCallback[string]]
|
||||
newConvoCallbacks: seq[NewConvoCallback]
|
||||
|
||||
#################################################
|
||||
# Constructors
|
||||
@ -70,7 +72,8 @@ proc newClient*(name: string, cfg: WakuConfig): Client {.raises: [IOError,
|
||||
conversations: initTable[string, Conversation](),
|
||||
inboundQueue: q,
|
||||
isRunning: false,
|
||||
newMessageCallbacks: @[])
|
||||
newMessageCallbacks: @[],
|
||||
newConvoCallbacks: @[])
|
||||
|
||||
let defaultInbox = initInbox(c.ident.getPubkey())
|
||||
c.conversations[defaultInbox.id()] = defaultInbox
|
||||
@ -119,6 +122,12 @@ proc notifyNewMessage(client: Client, convo: Conversation, msg: string) =
|
||||
for cb in client.newMessageCallbacks:
|
||||
discard cb(convo, msg)
|
||||
|
||||
proc onNewConversation*(client: Client, callback: NewConvoCallback) =
|
||||
client.newConvoCallbacks.add(callback)
|
||||
|
||||
proc notifyNewConversation(client: Client, convo: Conversation) =
|
||||
for cb in client.newConvoCallbacks:
|
||||
discard cb(convo)
|
||||
|
||||
#################################################
|
||||
# Functional
|
||||
@ -153,6 +162,7 @@ proc createIntroBundle*(self: var Client): IntroBundle =
|
||||
proc addConversation*(client: Client, convo: Conversation) =
|
||||
notice "Creating conversation", client = client.getId(), topic = convo.id()
|
||||
client.conversations[convo.id()] = convo
|
||||
client.notifyNewConversation(convo)
|
||||
|
||||
proc getConversation*(client: Client, convoId: string): Conversation =
|
||||
notice "Get conversation", client = client.getId(), convoId = convoId
|
||||
|
||||
@ -39,27 +39,22 @@ proc main() {.async.} =
|
||||
raya.onNewMessage(proc(convo: Conversation, msg: string) {.async.} =
|
||||
echo " ------> Raya :: " & msg
|
||||
await sleepAsync(10000)
|
||||
await convo.sendMessage(raya.ds, "Pong"))
|
||||
await convo.sendMessage(raya.ds, "Pong")
|
||||
)
|
||||
|
||||
raya.onNewConversation(proc(convo: Conversation) {.async.} =
|
||||
echo " ------> Raya :: New Conversation: " & convo.id()
|
||||
await convo.sendMessage(raya.ds, "Hello")
|
||||
)
|
||||
await raya.start()
|
||||
|
||||
|
||||
await sleepAsync(5000)
|
||||
|
||||
# Perform OOB Introduction: Raya -> Saro
|
||||
let raya_bundle = raya.createIntroBundle()
|
||||
discard await saro.newPrivateConversation(raya_bundle)
|
||||
|
||||
await sleepAsync(5000)
|
||||
|
||||
|
||||
try:
|
||||
for convo in raya.listConversations():
|
||||
notice " Convo", convo = convo.id()
|
||||
await convo.sendMessage(raya.ds, "Hello")
|
||||
# Let messages process
|
||||
|
||||
except Exception as e:
|
||||
panic("UnCaught Exception"&e.msg)
|
||||
|
||||
await sleepAsync(400000)
|
||||
|
||||
saro.stop()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user