From 87adb3b5d06f7c0db8ecdcd78063a425135aed59 Mon Sep 17 00:00:00 2001 From: Jazz Turner-Baggs <473256+jazzz@users.noreply.github.com> Date: Fri, 22 Aug 2025 22:40:27 -0700 Subject: [PATCH] Add onNewConvo --- src/chat_sdk/client.nim | 12 +++++++++++- src/nim_chat_poc.nim | 21 ++++++++------------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/chat_sdk/client.nim b/src/chat_sdk/client.nim index ee591c0..349bcd4 100644 --- a/src/chat_sdk/client.nim +++ b/src/chat_sdk/client.nim @@ -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 diff --git a/src/nim_chat_poc.nim b/src/nim_chat_poc.nim index 27745e6..adb7d6b 100644 --- a/src/nim_chat_poc.nim +++ b/src/nim_chat_poc.nim @@ -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()