Skip to content

Commit 70bd367

Browse files
committed
refactor: remove useless log
1 parent 567db39 commit 70bd367

File tree

4 files changed

+39
-4
lines changed

4 files changed

+39
-4
lines changed

src/chat/client.nim

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,14 +229,14 @@ proc newPrivateConversation*(client: Client,
229229

230230
#################################################
231231
# Payload Handling
232+
# Receives a incoming payload, decodes it, and processes it.
232233
#################################################
233234

234235
proc parseMessage(client: Client, msg: ChatPayload) {.raises: [ValueError,
235236
SerializationError].} =
236-
## Receives a incoming payload, decodes it, and processes it.
237237
let envelopeRes = decode(msg.bytes, WapEnvelopeV1)
238238
if envelopeRes.isErr:
239-
debug "Failed to decode WapEnvelopeV1", err = envelopeRes.error
239+
debug "Failed to decode WapEnvelopeV1", client = client.getId(), err = envelopeRes.error
240240
return
241241
let envelope = envelopeRes.get()
242242

@@ -268,6 +268,11 @@ proc messageQueueConsumer(client: Client) {.async.} =
268268
while client.isRunning:
269269
let message = await client.inboundQueue.queue.get()
270270

271+
let topicRes = inbox.parseTopic(message.contentTopic).or(private_v1.parseTopic(message.contentTopic))
272+
if topicRes.isErr:
273+
debug "Invalid content topic", client = client.getId(), err = topicRes.error, contentTopic = message.contentTopic
274+
continue
275+
271276
notice "Inbound Message Received", client = client.getId(),
272277
contentTopic = message.contentTopic, len = message.bytes.len()
273278
try:

src/chat/conversations/private_v1.nim

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ type
4242
discriminator: string
4343
doubleratchet: naxolotl.Doubleratchet
4444

45+
const
46+
TopicPrefixPrivateV1 = "/convo/private/"
47+
4548
proc getTopic*(self: PrivateV1): string =
4649
## Returns the topic for the PrivateV1 conversation.
4750
return self.topic
@@ -63,7 +66,18 @@ proc getConvoId*(self: PrivateV1): string =
6366

6467
proc derive_topic(participants: seq[PublicKey], discriminator: string): string =
6568
## Derives a topic from the participants' public keys.
66-
return "/convo/private/" & getConvoIdRaw(participants, discriminator)
69+
return TopicPrefixPrivateV1 & getConvoIdRaw(participants, discriminator)
70+
71+
## Parses the topic to extract the conversation ID.
72+
proc parseTopic*(topic: string): Result[string, ChatError] =
73+
if not topic.startsWith(TopicPrefixPrivateV1):
74+
return err(ChatError(code: errTopic, context: "Invalid topic prefix"))
75+
76+
let id = topic.split('/')[^1]
77+
if id == "":
78+
return err(ChatError(code: errTopic, context: "Empty conversation ID"))
79+
80+
return ok(id)
6781

6882
proc calcMsgId(self: PrivateV1, msgBytes: seq[byte]): string =
6983
let s = fmt"{self.getConvoId()}|{msgBytes}"

src/chat/errors.nim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ type
88
ErrorCode* = enum
99
errTypeError
1010
errWrapped
11+
errTopic
1112

1213
proc `$`*(x: ChatError): string =
1314
fmt"ChatError(code={$x.code}, context: {x.context})"

src/chat/inbox.nim

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import std/[strutils]
2+
13
import
24
chronicles,
35
chronos,
@@ -10,6 +12,7 @@ import
1012
conversation_store,
1113
crypto,
1214
delivery/waku_client,
15+
errors,
1316
proto_types,
1417
types
1518

@@ -21,6 +24,8 @@ type
2124
pubkey: PublicKey
2225
inbox_addr: string
2326

27+
const
28+
TopicPrefixInbox = "/inbox/"
2429

2530
proc `$`*(conv: Inbox): string =
2631
fmt"Inbox: addr->{conv.inbox_addr}"
@@ -56,7 +61,17 @@ proc conversation_id_for*(pubkey: PublicKey): string =
5661

5762
# TODO derive this from instance of Inbox
5863
proc topic_inbox*(client_addr: string): string =
59-
return "/inbox/" & client_addr
64+
return TopicPrefixInbox & client_addr
65+
66+
proc parseTopic*(topic: string): Result[string, ChatError] =
67+
if not topic.startsWith(TopicPrefixInbox):
68+
return err(ChatError(code: errTopic, context: "Invalid inbox topic prefix"))
69+
70+
let id = topic.split('/')[^1]
71+
if id == "":
72+
return err(ChatError(code: errTopic, context: "Empty inbox id"))
73+
74+
return ok(id)
6075

6176
method id*(convo: Inbox): string =
6277
return conversation_id_for(convo.pubkey)

0 commit comments

Comments
 (0)