mirror of
https://github.com/logos-messaging/logos-chat.git
synced 2026-05-16 17:39:28 +00:00
Reorg client
This commit is contained in:
parent
a138227a85
commit
a2098e275c
@ -22,6 +22,11 @@ import #local
|
|||||||
logScope:
|
logScope:
|
||||||
topics = "chat client"
|
topics = "chat client"
|
||||||
|
|
||||||
|
#################################################
|
||||||
|
# Definitions
|
||||||
|
#################################################
|
||||||
|
|
||||||
|
|
||||||
type KeyEntry* = object
|
type KeyEntry* = object
|
||||||
keytype: string
|
keytype: string
|
||||||
privateKey: PrivateKey
|
privateKey: PrivateKey
|
||||||
@ -49,6 +54,9 @@ type Client* = ref object
|
|||||||
inboundQueue: QueueRef
|
inboundQueue: QueueRef
|
||||||
isRunning: bool
|
isRunning: bool
|
||||||
|
|
||||||
|
#################################################
|
||||||
|
# Constructors
|
||||||
|
#################################################
|
||||||
|
|
||||||
proc newClient*(name: string, cfg: WakuConfig): Client =
|
proc newClient*(name: string, cfg: WakuConfig): Client =
|
||||||
let waku = initWakuClient(cfg)
|
let waku = initWakuClient(cfg)
|
||||||
@ -70,6 +78,10 @@ proc newClient*(name: string, cfg: WakuConfig): Client =
|
|||||||
default_inbox = default_inbox
|
default_inbox = default_inbox
|
||||||
result = c
|
result = c
|
||||||
|
|
||||||
|
#################################################
|
||||||
|
# Parameter Access
|
||||||
|
#################################################
|
||||||
|
|
||||||
proc getId(client: Client): string =
|
proc getId(client: Client): string =
|
||||||
result = client.getId()
|
result = client.getId()
|
||||||
|
|
||||||
@ -77,6 +89,20 @@ proc default_inbox_conversation_id*(self: Client): string =
|
|||||||
## Returns the default inbox address for the client.
|
## Returns the default inbox address for the client.
|
||||||
result = conversation_id_for(self.ident.getPubkey())
|
result = conversation_id_for(self.ident.getPubkey())
|
||||||
|
|
||||||
|
proc getConversationFromHint(self: Client,
|
||||||
|
conversation_hint: string): Result[Option[ConvoWrapper], string] =
|
||||||
|
|
||||||
|
# TODO: Implementing Hinting
|
||||||
|
if not self.conversations.hasKey(conversation_hint):
|
||||||
|
ok(none(ConvoWrapper))
|
||||||
|
else:
|
||||||
|
ok(some(self.conversations[conversation_hint]))
|
||||||
|
|
||||||
|
|
||||||
|
#################################################
|
||||||
|
# Functional
|
||||||
|
#################################################
|
||||||
|
|
||||||
proc createIntroBundle*(self: var Client): IntroBundle =
|
proc createIntroBundle*(self: var Client): IntroBundle =
|
||||||
## Generates an IntroBundle for the client, which includes
|
## Generates an IntroBundle for the client, which includes
|
||||||
## the required information to send a message.
|
## the required information to send a message.
|
||||||
@ -97,6 +123,11 @@ proc createIntroBundle*(self: var Client): IntroBundle =
|
|||||||
notice "IntroBundleCreated", client = self.getId(),
|
notice "IntroBundleCreated", client = self.getId(),
|
||||||
pubBytes = result.ident
|
pubBytes = result.ident
|
||||||
|
|
||||||
|
|
||||||
|
#################################################
|
||||||
|
# Conversation Initiation
|
||||||
|
#################################################
|
||||||
|
|
||||||
proc createPrivateConversation(client: Client, participant: PublicKey,
|
proc createPrivateConversation(client: Client, participant: PublicKey,
|
||||||
discriminator: string = "default"): Option[ChatError] =
|
discriminator: string = "default"): Option[ChatError] =
|
||||||
# Creates a private conversation with the given participant and discriminator.
|
# Creates a private conversation with the given participant and discriminator.
|
||||||
@ -159,16 +190,9 @@ proc acceptPrivateInvite(client: Client,
|
|||||||
result = none(ChatError)
|
result = none(ChatError)
|
||||||
|
|
||||||
|
|
||||||
|
#################################################
|
||||||
proc getConversationFromHint(self: Client,
|
# Payload Handling
|
||||||
conversation_hint: string): Result[Option[ConvoWrapper], string] =
|
#################################################
|
||||||
|
|
||||||
# TODO: Implementing Hinting
|
|
||||||
if not self.conversations.hasKey(conversation_hint):
|
|
||||||
ok(none(ConvoWrapper))
|
|
||||||
else:
|
|
||||||
ok(some(self.conversations[conversation_hint]))
|
|
||||||
|
|
||||||
|
|
||||||
proc handleInboxFrame(client: Client, frame: InboxV1Frame) =
|
proc handleInboxFrame(client: Client, frame: InboxV1Frame) =
|
||||||
case getKind(frame):
|
case getKind(frame):
|
||||||
@ -230,6 +254,19 @@ proc parseMessage(client: Client, msg: ChatPayload) =
|
|||||||
of PrivateV1Type:
|
of PrivateV1Type:
|
||||||
client.handlePrivateFrame(wrapped_convo.private_v1, env.payload)
|
client.handlePrivateFrame(wrapped_convo.private_v1, env.payload)
|
||||||
|
|
||||||
|
proc addMessage*(client: Client, convo: PrivateV1,
|
||||||
|
text: string = "") {.async.} =
|
||||||
|
|
||||||
|
let message = PrivateV1Frame(content: ContentFrame(domain: 0, tag: 1,
|
||||||
|
bytes: text.toBytes()))
|
||||||
|
|
||||||
|
await convo.sendMessage(client.ds, message)
|
||||||
|
|
||||||
|
|
||||||
|
#################################################
|
||||||
|
# Async Tasks
|
||||||
|
#################################################
|
||||||
|
|
||||||
proc messageQueueConsumer(client: Client) {.async.} =
|
proc messageQueueConsumer(client: Client) {.async.} =
|
||||||
## Main message processing loop
|
## Main message processing loop
|
||||||
info "Message listener started"
|
info "Message listener started"
|
||||||
@ -249,13 +286,6 @@ proc messageQueueConsumer(client: Client) {.async.} =
|
|||||||
pubsub = message.pubsubTopic, contentTopic = message.contentTopic
|
pubsub = message.pubsubTopic, contentTopic = message.contentTopic
|
||||||
# Continue running even if there's an error
|
# Continue running even if there's an error
|
||||||
|
|
||||||
proc addMessage*(client: Client, convo: PrivateV1,
|
|
||||||
text: string = "") {.async.} =
|
|
||||||
|
|
||||||
let message = PrivateV1Frame(content: ContentFrame(domain: 0, tag: 1,
|
|
||||||
bytes: text.toBytes()))
|
|
||||||
|
|
||||||
await convo.sendMessage(client.ds, message)
|
|
||||||
|
|
||||||
proc simulateMessages(client: Client){.async.} =
|
proc simulateMessages(client: Client){.async.} =
|
||||||
|
|
||||||
@ -271,6 +301,9 @@ proc simulateMessages(client: Client){.async.} =
|
|||||||
if convoWrapper.convo_type == PrivateV1Type:
|
if convoWrapper.convo_type == PrivateV1Type:
|
||||||
await client.addMessage(convoWrapper.privateV1, fmt"message: {a}")
|
await client.addMessage(convoWrapper.privateV1, fmt"message: {a}")
|
||||||
|
|
||||||
|
#################################################
|
||||||
|
# Control Functions
|
||||||
|
#################################################
|
||||||
|
|
||||||
proc start*(client: Client) {.async.} =
|
proc start*(client: Client) {.async.} =
|
||||||
# Start the message listener in the backgrounds
|
# Start the message listener in the backgrounds
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user