Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ else # "variables.mk" was included. Business as usual until the end of this file
.PHONY: all update clean

# default target, because it's the first one that doesn't start with '.'
all: | waku_example nim_chat_poc tui
all: | waku_example tui bot_echo pingpong

test_file := $(word 2,$(MAKECMDGOALS))
define test_name
Expand Down Expand Up @@ -82,11 +82,6 @@ waku_example: | build-waku-librln build-waku-nat nim_chat_poc.nims
\
$(ENV_SCRIPT) nim waku_example $(NIM_PARAMS) nim_chat_poc.nims

nim_chat_poc: | build-waku-librln build-waku-nat nim_chat_poc.nims
echo -e $(BUILD_MSG) "build/$@" && \
\
$(ENV_SCRIPT) nim nim_chat_poc $(NIM_PARAMS) nim_chat_poc.nims

# Ensure there is a nimble task with a name that matches the target
tui bot_echo pingpong: | build-waku-librln build-waku-nat nim_chat_poc.nims
echo -e $(BUILD_MSG) "build/$@" && \
Expand Down
10 changes: 5 additions & 5 deletions examples/bot_echo.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ import ./tui/persistence # Temporary Workaround for PeerDiscovery
proc main() {.async.} =

let cfg = await getCfg("EchoBot")
# let dsConfig = DefaultConfig()
# let ident = createIdentity("EchoBot")
let dsConfig = DefaultConfig()
let ident = createIdentity("EchoBot")
var chatClient = newClient(dsConfig, ident)

chatClient.onNewMessage(proc(convo: Conversation, msg: ContentFrame) {.async.} =
chatClient.onNewMessage(proc(convo: Conversation, msg: ReceivedMessage) {.async.} =
info "New Message: ", convoId = convo.id(), msg= msg
await convo.sendMessage(chatClient.ds, msg)
discard await convo.sendMessage(chatClient.ds, msg.content)
)

chatClient.onNewConversation(proc(convo: Conversation) {.async.} =
info "New Conversation Initiated: ", convoId = convo.id()
await convo.sendMessage(chatClient.ds, initTextFrame("Hello!").toContentFrame())
discard await convo.sendMessage(chatClient.ds, initTextFrame("Hello!").toContentFrame())
)

await chatClient.start()
Expand Down
111 changes: 0 additions & 111 deletions examples/nim_chat_poc.nim

This file was deleted.

8 changes: 4 additions & 4 deletions examples/pingpong.nim
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ proc main() {.async.} =

var ri = 0
# Wire Callbacks
saro.onNewMessage(proc(convo: Conversation, msg: ContentFrame) {.async.} =
echo " Saro <------ :: " & getContent(msg)
saro.onNewMessage(proc(convo: Conversation, msg: ReceivedMessage) {.async.} =
echo " Saro <------ :: " & getContent(msg.content)
await sleepAsync(5000)
discard await convo.sendMessage(saro.ds, initTextFrame("Ping").toContentFrame())

Expand All @@ -50,8 +50,8 @@ proc main() {.async.} =



raya.onNewMessage(proc(convo: Conversation, msg: ContentFrame) {.async.} =
echo " ------> Raya :: " & getContent(msg)
raya.onNewMessage(proc(convo: Conversation,msg: ReceivedMessage) {.async.} =
echo fmt" ------> Raya :: from:{msg.sender} " & getContent(msg.content)
await sleepAsync(500)
discard await convo.sendMessage(raya.ds, initTextFrame("Pong" & $ri).toContentFrame())
await sleepAsync(800)
Expand Down
8 changes: 4 additions & 4 deletions examples/tui/tui.nim
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,17 @@ proc setupChatSdk(app: ChatApp) =

let client = app.client

app.client.onNewMessage(proc(convo: Conversation, msg: ContentFrame) {.async.} =
app.client.onNewMessage(proc(convo: Conversation, msg: ReceivedMessage) {.async.} =
info "New Message: ", convoId = convo.id(), msg= msg
app.logMsgs.add(LogEntry(level: "info",ts: now(), msg: "NewMsg"))

var contentStr = case msg.contentType
var contentStr = case msg.content.contentType
of text:
decode(msg.bytes, TextFrame).get().text
decode(msg.content.bytes, TextFrame).get().text
of unknown:
"<Unhandled Message Type>"

app.conversations[convo.id()].messages.add(Message(sender: "???", content: contentStr, timestamp: now()))
app.conversations[convo.id()].messages.add(Message(sender: msg.sender.toHex(), content: contentStr, timestamp: now()))
)

app.client.onNewConversation(proc(convo: Conversation) {.async.} =
Expand Down
4 changes: 0 additions & 4 deletions nim_chat_poc.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ task waku_example, "Build Waku based simple example":
let name = "waku_example"
buildBinary name, "examples/", " -d:chronicles_log_level='TRACE' "

task nim_chat_poc, "Build Waku based simple example":
let name = "nim_chat_poc"
buildBinary name, "examples/", " -d:chronicles_log_level='INFO' "

task tui, "Build Waku based simple example":
let name = "tui"
buildBinary name, "examples/", " -d:chronicles_enabled=on -d:chronicles_log_level='INFO' -d:chronicles_sinks=textlines[file]"
Expand Down
6 changes: 4 additions & 2 deletions src/chat_sdk.nim
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import chat_sdk/[
client,
crypto,
conversations,
delivery/waku_client,
identity,
links,
proto_types,
types

]

export client, conversations, waku_client, identity, links
export client, conversations, identity, links, waku_client

#export specific frames need by applications
export ContentFrame, MessageId

export toHex
11 changes: 5 additions & 6 deletions src/chat_sdk/client.nim
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ logScope:
#################################################

type
MessageCallback*[T] = proc(conversation: Conversation, msg: T): Future[void] {.async.}
MessageCallback* = proc(conversation: Conversation, msg: ReceivedMessage): Future[void] {.async.}
NewConvoCallback* = proc(conversation: Conversation): Future[void] {.async.}
DeliveryAckCallback* = proc(conversation: Conversation,
msgId: MessageId): Future[void] {.async.}
Expand All @@ -56,7 +56,7 @@ type Client* = ref object
inboundQueue: QueueRef
isRunning: bool

newMessageCallbacks: seq[MessageCallback[ContentFrame]]
newMessageCallbacks: seq[MessageCallback]
newConvoCallbacks: seq[NewConvoCallback]
deliveryAckCallbacks: seq[DeliveryAckCallback]

Expand Down Expand Up @@ -122,13 +122,12 @@ proc listConversations*(client: Client): seq[Conversation] =
# Callback Handling
#################################################

proc onNewMessage*(client: Client, callback: MessageCallback[ContentFrame]) =
proc onNewMessage*(client: Client, callback: MessageCallback) =
client.newMessageCallbacks.add(callback)

proc notifyNewMessage(client: Client, convo: Conversation,
content: ContentFrame) =
proc notifyNewMessage*(client: Client, convo: Conversation, msg: ReceivedMessage) =
for cb in client.newMessageCallbacks:
discard cb(convo, content)
discard cb(convo, msg)

proc onNewConversation*(client: Client, callback: NewConvoCallback) =
client.newConvoCallbacks.add(callback)
Expand Down
5 changes: 2 additions & 3 deletions src/chat_sdk/conversation_store.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import std/[options, times]

import ./conversations/convo_type
import ./conversations/[convo_type, message]
import crypto
import identity
import proto_types
Expand All @@ -15,7 +15,6 @@ type
proc identity(self: Self): Identity
proc getId(self: Self): string

proc notifyNewMessage(self: Self, convo: Conversation,
content: ContentFrame)
proc notifyNewMessage(self: Self, convo: Conversation, msg: ReceivedMessage)
proc notifyDeliveryAck(self: Self, convo: Conversation,
msgId: MessageId)
4 changes: 2 additions & 2 deletions src/chat_sdk/conversations.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import
./conversations/[convo_type, private_v1]
./conversations/[convo_type, private_v1, message]


export private_v1, convo_type
export private_v1, convo_type, message
12 changes: 12 additions & 0 deletions src/chat_sdk/conversations/message.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import ../crypto
import ../proto_types

# How to surface different verifability of properties across conversation types


type ReceivedMessage* = ref object of RootObj
sender*: PublicKey
timestamp*: int64
content*: ContentFrame


22 changes: 16 additions & 6 deletions src/chat_sdk/conversations/private_v1.nim
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,34 @@ import ../[
utils
]
import convo_type
import message

import ../../naxolotl as nax


type
ReceivedPrivateV1Message* = ref object of ReceivedMessage

proc initReceivedMessage(sender: PublicKey, timestamp: int64, content: ContentFrame) : ReceivedPrivateV1Message =
ReceivedPrivateV1Message(sender:sender, timestamp:timestamp, content:content)


type
PrivateV1* = ref object of Conversation
# Placeholder for PrivateV1 conversation type
sdsClient: ReliabilityManager
owner: Identity
topic: string
participants: seq[PublicKey]
participant: PublicKey
discriminator: string
doubleratchet: naxolotl.Doubleratchet

proc getTopic*(self: PrivateV1): string =
## Returns the topic for the PrivateV1 conversation.
return self.topic

proc allParticipants(self: PrivateV1): seq[PublicKey] =
return @[self.owner.getPubkey(), self.participant]

proc getConvoIdRaw(participants: seq[PublicKey],
discriminator: string): string =
Expand All @@ -49,7 +59,7 @@ proc getConvoIdRaw(participants: seq[PublicKey],
return utils.hash_func(raw)

proc getConvoId*(self: PrivateV1): string =
return getConvoIdRaw(self.participants, self.discriminator)
return getConvoIdRaw(@[self.owner.getPubkey(), self.participant], self.discriminator)

proc derive_topic(participants: seq[PublicKey], discriminator: string): string =
## Derives a topic from the participants' public keys.
Expand Down Expand Up @@ -131,7 +141,7 @@ proc initPrivateV1*(owner: Identity, participant: PublicKey, seedKey: array[32,
sdsClient: rm,
owner: owner,
topic: derive_topic(participants, discriminator),
participants: participants,
participant: participant,
discriminator: discriminator,
doubleratchet: initDoubleratchet(seedKey, owner.privateKey.bytes, participant.bytes, isSender)
)
Expand Down Expand Up @@ -169,7 +179,7 @@ proc sendFrame(self: PrivateV1, ds: WakuClient,


method id*(self: PrivateV1): string =
return getConvoIdRaw(self.participants, self.discriminator)
return getConvoIdRaw(self.allParticipants(), self.discriminator)

proc handleFrame*[T: ConversationStore](convo: PrivateV1, client: T,
bytes: seq[byte]) =
Expand Down Expand Up @@ -200,8 +210,8 @@ proc handleFrame*[T: ConversationStore](convo: PrivateV1, client: T,
case frame.getKind():
of typeContentFrame:
# TODO: Using client.getId() results in an error in this context
client.notifyNewMessage(convo, frame.content)

client.notifyNewMessage(convo, initReceivedMessage(convo.participant, frame.timestamp, frame.content))
of typePlaceholder:
notice "Got Placeholder", text = frame.placeholder.counter

Expand Down
Loading