From f52150ba83157b782f9a803093e0ac2eb6181341 Mon Sep 17 00:00:00 2001 From: Jazz Turner-Baggs <473256+jazzz@users.noreply.github.com> Date: Fri, 26 Sep 2025 16:14:37 -0700 Subject: [PATCH] add sendmessages to tui --- examples/tui/tui.nim | 126 +++++++++++++++++++++--------------------- src/content_types.nim | 3 + 2 files changed, 67 insertions(+), 62 deletions(-) create mode 100644 src/content_types.nim diff --git a/examples/tui/tui.nim b/examples/tui/tui.nim index cf40445..15adfa7 100644 --- a/examples/tui/tui.nim +++ b/examples/tui/tui.nim @@ -10,13 +10,7 @@ import strutils import sugar import tables -import chat_sdk/[ - client, - conversations, - delivery/waku_client, - links -] - +import chat_sdk import content_types/all import layout @@ -44,6 +38,7 @@ type ConvoInfo = object name: string + convo: Conversation messages: seq[Message] lastMsgTime*: DateTime isTooLong*: bool @@ -71,57 +66,6 @@ proc `==`(a,b: ConvoInfo):bool = else: false -################################################# -# ChatSDK Setup -################################################# - -proc createChatClient(name: string): Future[Client] {.async.} = - var cfg = await getCfg(name) - for key, val in fetchRegistrations(): - if key != name: - cfg.waku.staticPeers.add(val) - - result = newClient(name, cfg.waku, cfg.ident) - - -proc createInviteLink(app: var ChatApp): string = - app.client.createIntroBundle().toLink() - - -proc createConvo(app: ChatApp) {.async.} = - discard await app.client.newPrivateConversation(toBundle(app.inputInviteBuffer.strip()).get()) - - - -proc setupChatSdk(app: ChatApp) = - - let client = app.client - - app.client.onNewMessage(proc(convo: Conversation, msg: ContentFrame) {.async.} = - info "New Message: ", convoId = convo.id(), msg= msg - app.logMsgs.add(LogEntry(level: "info",ts: now(), msg: "NewMsg")) - - var contentStr = case msg.contentType - of text: - decode(msg.bytes, TextFrame).get().text - of unknown: - "" - - app.conversations[convo.id()].messages.add(Message(sender: "???", content: contentStr, timestamp: now())) - ) - - app.client.onNewConversation(proc(convo: Conversation) {.async.} = - app.logMsgs.add(LogEntry(level: "info",ts: now(), msg: fmt"Adding Convo: {convo.id()}")) - info "New Conversation: ", convoId = convo.id() - - app.conversations[convo.id()] = ConvoInfo(name: convo.id(), messages: @[], lastMsgTime: now(), isTooLong: false) - ) - - app.client.onDeliveryAck(proc(convo: Conversation, msgId: string) {.async.} = - info "DeliveryAck", msgId=msgId - app.logMsgs.add(LogEntry(level: "info",ts: now(), msg: fmt"Ack:{msgId}")) - ) - ################################################# # Data Management @@ -151,6 +95,63 @@ proc getSelectedConvo(app: ChatApp): ptr ConvoInfo = return addr app.conversations[app.mostRecentConvos()[0].name] + +################################################# +# ChatSDK Setup +################################################# + +proc createChatClient(name: string): Future[Client] {.async.} = + var cfg = await getCfg(name) + for key, val in fetchRegistrations(): + if key != name: + cfg.waku.staticPeers.add(val) + + result = newClient(cfg.waku, cfg.ident) + + +proc createInviteLink(app: var ChatApp): string = + app.client.createIntroBundle().toLink() + + +proc createConvo(app: ChatApp) {.async.} = + discard await app.client.newPrivateConversation(toBundle(app.inputInviteBuffer.strip()).get()) + +proc sendMessage(app: ChatApp, convoInfo: ptr ConvoInfo, msg: string) {.async.} = + convoInfo[].addMessage("You", app.inputBuffer) + + if convoInfo.convo != nil: + await convoInfo.convo.sendMessage(app.client.ds, initTextFrame(msg).toContentFrame()) + +proc setupChatSdk(app: ChatApp) = + + let client = app.client + + app.client.onNewMessage(proc(convo: Conversation, msg: ContentFrame) {.async.} = + info "New Message: ", convoId = convo.id(), msg= msg + app.logMsgs.add(LogEntry(level: "info",ts: now(), msg: "NewMsg")) + + var contentStr = case msg.contentType + of text: + decode(msg.bytes, TextFrame).get().text + of unknown: + "" + + app.conversations[convo.id()].messages.add(Message(sender: "???", content: contentStr, timestamp: now())) + ) + + app.client.onNewConversation(proc(convo: Conversation) {.async.} = + app.logMsgs.add(LogEntry(level: "info",ts: now(), msg: fmt"Adding Convo: {convo.id()}")) + info "New Conversation: ", convoId = convo.id() + + app.conversations[convo.id()] = ConvoInfo(name: convo.id(), convo: convo, messages: @[], lastMsgTime: now(), isTooLong: false) + ) + + app.client.onDeliveryAck(proc(convo: Conversation, msgId: string) {.async.} = + info "DeliveryAck", msgId=msgId + app.logMsgs.add(LogEntry(level: "info",ts: now(), msg: fmt"Ack:{msgId}")) + ) + + ################################################# # Draw Funcs ################################################# @@ -414,7 +415,7 @@ proc getNextConv(app: ChatApp): string = return convos[min(i+1, convos.len-1)].name -proc handleInput(app: ChatApp, key: Key) = +proc handleInput(app: ChatApp, key: Key) {.async.} = case key of Key.Up: app.selectedConv = app.gePreviousConv() @@ -434,8 +435,9 @@ proc handleInput(app: ChatApp, key: Key) = app.isInviteReady = true else: if app.inputBuffer.len > 0 and app.conversations.len > 0: - var sc = app.getSelectedConvo() - sc[].addMessage("You", app.inputBuffer) + + let sc = app.getSelectedConvo() + await app.sendMessage(sc, app.inputBuffer) app.inputBuffer = "" app.messageScrollOffset = 0 # Auto-scroll to bottom @@ -489,7 +491,7 @@ proc appLoop(app: ChatApp, panes: seq[Pane]) : Future[void] {.async.} = # Handle input let key = getKey() - handleInput(app, key) + await handleInput(app, key) if app.isInviteReady: discard await app.client.newPrivateConversation(toBundle(app.inputInviteBuffer.strip()).get()) diff --git a/src/content_types.nim b/src/content_types.nim new file mode 100644 index 0000000..dc7a027 --- /dev/null +++ b/src/content_types.nim @@ -0,0 +1,3 @@ +import ./content_types/all + +export all \ No newline at end of file