diff --git a/src/chat/client.nim b/src/chat/client.nim index 8fcfed3..3d5b374 100644 --- a/src/chat/client.nim +++ b/src/chat/client.nim @@ -229,14 +229,14 @@ proc newPrivateConversation*(client: Client, ################################################# # Payload Handling +# Receives a incoming payload, decodes it, and processes it. ################################################# proc parseMessage(client: Client, msg: ChatPayload) {.raises: [ValueError, SerializationError].} = - ## Receives a incoming payload, decodes it, and processes it. let envelopeRes = decode(msg.bytes, WapEnvelopeV1) if envelopeRes.isErr: - debug "Failed to decode WapEnvelopeV1", err = envelopeRes.error + debug "Failed to decode WapEnvelopeV1", client = client.getId(), err = envelopeRes.error return let envelope = envelopeRes.get() @@ -268,6 +268,11 @@ proc messageQueueConsumer(client: Client) {.async.} = while client.isRunning: let message = await client.inboundQueue.queue.get() + let topicRes = inbox.parseTopic(message.contentTopic).or(private_v1.parseTopic(message.contentTopic)) + if topicRes.isErr: + debug "Invalid content topic", client = client.getId(), err = topicRes.error, contentTopic = message.contentTopic + continue + notice "Inbound Message Received", client = client.getId(), contentTopic = message.contentTopic, len = message.bytes.len() try: diff --git a/src/chat/conversations/private_v1.nim b/src/chat/conversations/private_v1.nim index 5b31f4a..99a0236 100644 --- a/src/chat/conversations/private_v1.nim +++ b/src/chat/conversations/private_v1.nim @@ -42,6 +42,9 @@ type discriminator: string doubleratchet: naxolotl.Doubleratchet +const + TopicPrefixPrivateV1 = "/convo/private/" + proc getTopic*(self: PrivateV1): string = ## Returns the topic for the PrivateV1 conversation. return self.topic @@ -63,7 +66,18 @@ proc getConvoId*(self: PrivateV1): string = proc derive_topic(participants: seq[PublicKey], discriminator: string): string = ## Derives a topic from the participants' public keys. - return "/convo/private/" & getConvoIdRaw(participants, discriminator) + return TopicPrefixPrivateV1 & getConvoIdRaw(participants, discriminator) + +## Parses the topic to extract the conversation ID. +proc parseTopic*(topic: string): Result[string, ChatError] = + if not topic.startsWith(TopicPrefixPrivateV1): + return err(ChatError(code: errTopic, context: "Invalid topic prefix")) + + let id = topic.split('/')[^1] + if id == "": + return err(ChatError(code: errTopic, context: "Empty conversation ID")) + + return ok(id) proc calcMsgId(self: PrivateV1, msgBytes: seq[byte]): string = let s = fmt"{self.getConvoId()}|{msgBytes}" diff --git a/src/chat/errors.nim b/src/chat/errors.nim index f4751b0..c74b5b2 100644 --- a/src/chat/errors.nim +++ b/src/chat/errors.nim @@ -8,6 +8,7 @@ type ErrorCode* = enum errTypeError errWrapped + errTopic proc `$`*(x: ChatError): string = fmt"ChatError(code={$x.code}, context: {x.context})" diff --git a/src/chat/inbox.nim b/src/chat/inbox.nim index 14beed2..6b8f606 100644 --- a/src/chat/inbox.nim +++ b/src/chat/inbox.nim @@ -1,3 +1,5 @@ +import std/[strutils] + import chronicles, chronos, @@ -10,6 +12,7 @@ import conversation_store, crypto, delivery/waku_client, + errors, proto_types, types @@ -21,6 +24,8 @@ type pubkey: PublicKey inbox_addr: string +const + TopicPrefixInbox = "/inbox/" proc `$`*(conv: Inbox): string = fmt"Inbox: addr->{conv.inbox_addr}" @@ -56,7 +61,17 @@ proc conversation_id_for*(pubkey: PublicKey): string = # TODO derive this from instance of Inbox proc topic_inbox*(client_addr: string): string = - return "/inbox/" & client_addr + return TopicPrefixInbox & client_addr + +proc parseTopic*(topic: string): Result[string, ChatError] = + if not topic.startsWith(TopicPrefixInbox): + return err(ChatError(code: errTopic, context: "Invalid inbox topic prefix")) + + let id = topic.split('/')[^1] + if id == "": + return err(ChatError(code: errTopic, context: "Empty inbox id")) + + return ok(id) method id*(convo: Inbox): string = return conversation_id_for(convo.pubkey)