diff --git a/examples/pingpong.nim b/examples/pingpong.nim index bfdb67c..5789e04 100644 --- a/examples/pingpong.nim +++ b/examples/pingpong.nim @@ -12,9 +12,16 @@ import ../src/chat/crypto proc getContent(content: ContentFrame): string = # Skip type checks and assume its a TextFrame let m = decode(content.bytes, TextFrame).valueOr: - raise newException(ValueError, fmt"Badly formed Content") + raise newException(ValueError, fmt"Badly formed ContentType") return fmt"{m}" +proc toBytes(content: ContentFrame): seq[byte] = + encode(content) + +proc fromBytes(bytes: seq[byte]): ContentFrame = + decode(bytes, ContentFrame).valueOr: + raise newException(ValueError, fmt"Badly formed Content") + proc main() {.async.} = # Create Configurations @@ -37,9 +44,10 @@ proc main() {.async.} = var ri = 0 # Wire Callbacks saro.onNewMessage(proc(convo: Conversation, msg: ReceivedMessage) {.async.} = - echo " Saro <------ :: " & getContent(msg.content) + let contentFrame = msg.content.fromBytes() + echo " Saro <------ :: " & getContent(contentFrame) await sleepAsync(5000.milliseconds) - discard await convo.sendMessage(initTextFrame("Ping").toContentFrame()) + discard await convo.sendMessage(initTextFrame("Ping").toContentFrame().toBytes()) ) @@ -51,19 +59,20 @@ proc main() {.async.} = raya.onNewMessage(proc(convo: Conversation,msg: ReceivedMessage) {.async.} = - echo fmt" ------> Raya :: from:{msg.sender} " & getContent(msg.content) + let contentFrame = msg.content.fromBytes() + echo fmt" ------> Raya :: from:{msg.sender} " & getContent(contentFrame) await sleepAsync(500.milliseconds) - discard await convo.sendMessage(initTextFrame("Pong" & $ri).toContentFrame()) + discard await convo.sendMessage(initTextFrame("Pong" & $ri).toContentFrame().toBytes()) await sleepAsync(800.milliseconds) - discard await convo.sendMessage(initTextFrame("Pong" & $ri).toContentFrame()) + discard await convo.sendMessage(initTextFrame("Pong" & $ri).toContentFrame().toBytes()) await sleepAsync(500.milliseconds) - discard await convo.sendMessage(initTextFrame("Pong" & $ri).toContentFrame()) + discard await convo.sendMessage(initTextFrame("Pong" & $ri).toContentFrame().toBytes()) inc ri ) raya.onNewConversation(proc(convo: Conversation) {.async.} = echo " ------> Raya :: New Conversation: " & convo.id() - discard await convo.sendMessage(initTextFrame("Hello").toContentFrame()) + discard await convo.sendMessage(initTextFrame("Hello").toContentFrame().toBytes()) ) raya.onDeliveryAck(proc(convo: Conversation, msgId: string) {.async.} = echo " raya -- Read Receipt for " & msgId diff --git a/protos/private_v1.proto b/protos/private_v1.proto index 9d12c4f..7dd69ff 100644 --- a/protos/private_v1.proto +++ b/protos/private_v1.proto @@ -2,10 +2,6 @@ syntax = "proto3"; package wap.convos.private_v1; -import "common_frames.proto"; - - - message Placeholder { uint32 counter = 1; @@ -16,7 +12,7 @@ message PrivateV1Frame { bytes sender = 2; int64 timestamp = 3; // Sender reported timestamp oneof frame_type { - common_frames.ContentFrame content = 10; + bytes content = 10; Placeholder placeholder = 11; // .... } diff --git a/src/chat.nim b/src/chat.nim index 46f2833..398e3fe 100644 --- a/src/chat.nim +++ b/src/chat.nim @@ -5,13 +5,12 @@ import chat/[ delivery/waku_client, identity, links, - proto_types, types ] export client, conversations, identity, links, waku_client #export specific frames need by applications -export ContentFrame, MessageId +export MessageId export toHex diff --git a/src/chat/conversations/convo_type.nim b/src/chat/conversations/convo_type.nim index d497fd9..b810eed 100644 --- a/src/chat/conversations/convo_type.nim +++ b/src/chat/conversations/convo_type.nim @@ -24,6 +24,6 @@ method id*(self: Conversation): string {.raises: [Defect, ValueError].} = # TODO: make this a compile time check panic("ProgramError: Missing concrete implementation") -method sendMessage*(convo: Conversation, content_frame: ContentFrame) : Future[MessageId] {.async, base, gcsafe.} = +method sendMessage*(convo: Conversation, content_frame: Content) : Future[MessageId] {.async, base, gcsafe.} = # TODO: make this a compile time check panic("ProgramError: Missing concrete implementation") diff --git a/src/chat/conversations/message.nim b/src/chat/conversations/message.nim index 0ecc143..6f82edf 100644 --- a/src/chat/conversations/message.nim +++ b/src/chat/conversations/message.nim @@ -1,5 +1,4 @@ import ../crypto -import ../proto_types # How to surface different verifability of properties across conversation types @@ -7,6 +6,6 @@ import ../proto_types type ReceivedMessage* = ref object of RootObj sender*: PublicKey timestamp*: int64 - content*: ContentFrame + content*: seq[byte] diff --git a/src/chat/conversations/private_v1.nim b/src/chat/conversations/private_v1.nim index 7554bc9..f8ee8ea 100644 --- a/src/chat/conversations/private_v1.nim +++ b/src/chat/conversations/private_v1.nim @@ -28,7 +28,7 @@ import ../../naxolotl as nax type ReceivedPrivateV1Message* = ref object of ReceivedMessage -proc initReceivedMessage(sender: PublicKey, timestamp: int64, content: ContentFrame) : ReceivedPrivateV1Message = +proc initReceivedMessage(sender: PublicKey, timestamp: int64, content: Content) : ReceivedPrivateV1Message = ReceivedPrivateV1Message(sender:sender, timestamp:timestamp, content:content) @@ -209,7 +209,7 @@ proc handleFrame*[T: ConversationStore](convo: PrivateV1, client: T, return case frame.getKind(): - of typeContentFrame: + of typeContent: # TODO: Using client.getId() results in an error in this context client.notifyNewMessage(convo, initReceivedMessage(convo.participant, frame.timestamp, frame.content)) @@ -217,7 +217,7 @@ proc handleFrame*[T: ConversationStore](convo: PrivateV1, client: T, notice "Got Placeholder", text = frame.placeholder.counter -method sendMessage*(convo: PrivateV1, content_frame: ContentFrame) : Future[MessageId] {.async.} = +method sendMessage*(convo: PrivateV1, content_frame: Content) : Future[MessageId] {.async.} = try: let frame = PrivateV1Frame(sender: @(convo.owner.getPubkey().bytes()), diff --git a/src/chat/inbox.nim b/src/chat/inbox.nim index 14beed2..cbaa288 100644 --- a/src/chat/inbox.nim +++ b/src/chat/inbox.nim @@ -9,7 +9,6 @@ import conversations, conversation_store, crypto, - delivery/waku_client, proto_types, types @@ -107,6 +106,6 @@ proc handleFrame*[T: ConversationStore](convo: Inbox, client: T, bytes: seq[ notice "Receive Note", client = client.getId(), text = frame.note.text -method sendMessage*(convo: Inbox, content_frame: ContentFrame) : Future[MessageId] {.async.} = +method sendMessage*(convo: Inbox, content_frame: Content) : Future[MessageId] {.async.} = warn "Cannot send message to Inbox" result = "program_error" diff --git a/src/chat/proto_types.nim b/src/chat/proto_types.nim index 43c82cb..2e3a25c 100644 --- a/src/chat/proto_types.nim +++ b/src/chat/proto_types.nim @@ -12,13 +12,11 @@ import_proto3 "../../protos/inbox.proto" # import_proto3 "../protos/invite.proto" // Import3 follows protobuf includes so this will result in a redefinition error import_proto3 "../../protos/encryption.proto" import_proto3 "../../protos/envelope.proto" -# import_proto3 "../protos/common_frames.proto" import_proto3 "../../protos/private_v1.proto" type EncryptableTypes = InboxV1Frame | EncryptedPayload -export ContentFrame export EncryptedPayload export InboxV1Frame export PrivateV1Frame @@ -94,12 +92,12 @@ proc getKind*(obj: InboxV1Frame): InboxV1FrameType = type PrivateV1FrameType* = enum - type_ContentFrame, type_Placeholder + type_Content, type_Placeholder proc getKind*(obj: PrivateV1Frame): PrivateV1FrameType = - if obj.content != ContentFrame(): - return type_ContentFrame + if obj.content != @[]: + return type_Content if obj.placeholder != Placeholder(): return type_Placeholder diff --git a/src/chat/types.nim b/src/chat/types.nim index d8acc92..d5989fd 100644 --- a/src/chat/types.nim +++ b/src/chat/types.nim @@ -1 +1,2 @@ type MessageId* = string +type Content* = seq[byte] diff --git a/src/content_types/all.nim b/src/content_types/all.nim index 4fd7d25..f4b6425 100644 --- a/src/content_types/all.nim +++ b/src/content_types/all.nim @@ -12,7 +12,7 @@ import ../chat/proto_types export protobuf_serialization import_proto3 "protos/text_frame.proto" -# import_proto3 "../../protos/common_frames.proto" +import_proto3 "protos/common_frames.proto" export ContentFrame, TextFrame diff --git a/protos/common_frames.proto b/src/content_types/protos/common_frames.proto similarity index 100% rename from protos/common_frames.proto rename to src/content_types/protos/common_frames.proto