mirror of
https://github.com/logos-messaging/logos-chat.git
synced 2026-02-27 20:33:07 +00:00
fix: rename Client to ChatClient
This commit is contained in:
parent
78d90b7b11
commit
ce0f4e2aae
@ -26,7 +26,7 @@ type ChatCallbacks* = object
|
|||||||
|
|
||||||
proc createChatClient(
|
proc createChatClient(
|
||||||
configJson: cstring, chatCallbacks: ChatCallbacks
|
configJson: cstring, chatCallbacks: ChatCallbacks
|
||||||
): Future[Result[Client, string]] {.async.} =
|
): Future[Result[ChatClient, string]] {.async.} =
|
||||||
try:
|
try:
|
||||||
let config = parseJson($configJson)
|
let config = parseJson($configJson)
|
||||||
|
|
||||||
@ -69,7 +69,7 @@ proc createChatClient(
|
|||||||
except CatchableError as e:
|
except CatchableError as e:
|
||||||
return err("failed to create client: " & e.msg)
|
return err("failed to create client: " & e.msg)
|
||||||
|
|
||||||
registerReqFFI(CreateClientRequest, ctx: ptr FFIContext[Client]):
|
registerReqFFI(CreateClientRequest, ctx: ptr FFIContext[ChatClient]):
|
||||||
proc(
|
proc(
|
||||||
configJson: cstring, chatCallbacks: ChatCallbacks
|
configJson: cstring, chatCallbacks: ChatCallbacks
|
||||||
): Future[Result[string, string]] {.async.} =
|
): Future[Result[string, string]] {.async.} =
|
||||||
@ -79,11 +79,11 @@ registerReqFFI(CreateClientRequest, ctx: ptr FFIContext[Client]):
|
|||||||
return ok("")
|
return ok("")
|
||||||
|
|
||||||
#################################################
|
#################################################
|
||||||
# Client Lifecycle Operations
|
# ChatClient Lifecycle Operations
|
||||||
#################################################
|
#################################################
|
||||||
|
|
||||||
proc chat_start(
|
proc chat_start(
|
||||||
ctx: ptr FFIContext[Client],
|
ctx: ptr FFIContext[ChatClient],
|
||||||
callback: FFICallBack,
|
callback: FFICallBack,
|
||||||
userData: pointer
|
userData: pointer
|
||||||
) {.ffi.} =
|
) {.ffi.} =
|
||||||
@ -95,7 +95,7 @@ proc chat_start(
|
|||||||
return err("failed to start client: " & e.msg)
|
return err("failed to start client: " & e.msg)
|
||||||
|
|
||||||
proc chat_stop(
|
proc chat_stop(
|
||||||
ctx: ptr FFIContext[Client],
|
ctx: ptr FFIContext[ChatClient],
|
||||||
callback: FFICallBack,
|
callback: FFICallBack,
|
||||||
userData: pointer
|
userData: pointer
|
||||||
) {.ffi.} =
|
) {.ffi.} =
|
||||||
@ -107,11 +107,11 @@ proc chat_stop(
|
|||||||
return err("failed to stop client: " & e.msg)
|
return err("failed to stop client: " & e.msg)
|
||||||
|
|
||||||
#################################################
|
#################################################
|
||||||
# Client Info Operations
|
# ChatClient Info Operations
|
||||||
#################################################
|
#################################################
|
||||||
|
|
||||||
proc chat_get_id(
|
proc chat_get_id(
|
||||||
ctx: ptr FFIContext[Client],
|
ctx: ptr FFIContext[ChatClient],
|
||||||
callback: FFICallBack,
|
callback: FFICallBack,
|
||||||
userData: pointer
|
userData: pointer
|
||||||
) {.ffi.} =
|
) {.ffi.} =
|
||||||
@ -120,7 +120,7 @@ proc chat_get_id(
|
|||||||
return ok(clientId)
|
return ok(clientId)
|
||||||
|
|
||||||
proc chat_get_default_inbox_id(
|
proc chat_get_default_inbox_id(
|
||||||
ctx: ptr FFIContext[Client],
|
ctx: ptr FFIContext[ChatClient],
|
||||||
callback: FFICallBack,
|
callback: FFICallBack,
|
||||||
userData: pointer
|
userData: pointer
|
||||||
) {.ffi.} =
|
) {.ffi.} =
|
||||||
@ -133,7 +133,7 @@ proc chat_get_default_inbox_id(
|
|||||||
#################################################
|
#################################################
|
||||||
|
|
||||||
proc chat_list_conversations(
|
proc chat_list_conversations(
|
||||||
ctx: ptr FFIContext[Client],
|
ctx: ptr FFIContext[ChatClient],
|
||||||
callback: FFICallBack,
|
callback: FFICallBack,
|
||||||
userData: pointer
|
userData: pointer
|
||||||
) {.ffi.} =
|
) {.ffi.} =
|
||||||
@ -145,7 +145,7 @@ proc chat_list_conversations(
|
|||||||
return ok($convoList)
|
return ok($convoList)
|
||||||
|
|
||||||
proc chat_get_conversation(
|
proc chat_get_conversation(
|
||||||
ctx: ptr FFIContext[Client],
|
ctx: ptr FFIContext[ChatClient],
|
||||||
callback: FFICallBack,
|
callback: FFICallBack,
|
||||||
userData: pointer,
|
userData: pointer,
|
||||||
convoId: cstring
|
convoId: cstring
|
||||||
|
|||||||
@ -19,7 +19,7 @@ logScope:
|
|||||||
#################################################
|
#################################################
|
||||||
|
|
||||||
proc chat_new_private_conversation(
|
proc chat_new_private_conversation(
|
||||||
ctx: ptr FFIContext[Client],
|
ctx: ptr FFIContext[ChatClient],
|
||||||
callback: FFICallBack,
|
callback: FFICallBack,
|
||||||
userData: pointer,
|
userData: pointer,
|
||||||
introBundleJson: cstring,
|
introBundleJson: cstring,
|
||||||
@ -58,7 +58,7 @@ proc chat_new_private_conversation(
|
|||||||
#################################################
|
#################################################
|
||||||
|
|
||||||
proc chat_send_message(
|
proc chat_send_message(
|
||||||
ctx: ptr FFIContext[Client],
|
ctx: ptr FFIContext[ChatClient],
|
||||||
callback: FFICallBack,
|
callback: FFICallBack,
|
||||||
userData: pointer,
|
userData: pointer,
|
||||||
convoId: cstring,
|
convoId: cstring,
|
||||||
|
|||||||
@ -20,7 +20,7 @@ logScope:
|
|||||||
#################################################
|
#################################################
|
||||||
|
|
||||||
proc chat_get_identity(
|
proc chat_get_identity(
|
||||||
ctx: ptr FFIContext[Client],
|
ctx: ptr FFIContext[ChatClient],
|
||||||
callback: FFICallBack,
|
callback: FFICallBack,
|
||||||
userData: pointer
|
userData: pointer
|
||||||
) {.ffi.} =
|
) {.ffi.} =
|
||||||
@ -39,7 +39,7 @@ proc chat_get_identity(
|
|||||||
#################################################
|
#################################################
|
||||||
|
|
||||||
proc chat_create_intro_bundle(
|
proc chat_create_intro_bundle(
|
||||||
ctx: ptr FFIContext[Client],
|
ctx: ptr FFIContext[ChatClient],
|
||||||
callback: FFICallBack,
|
callback: FFICallBack,
|
||||||
userData: pointer
|
userData: pointer
|
||||||
) {.ffi.} =
|
) {.ffi.} =
|
||||||
|
|||||||
@ -4,7 +4,7 @@ import src/chat/client
|
|||||||
declareLibrary("chat")
|
declareLibrary("chat")
|
||||||
|
|
||||||
proc set_event_callback(
|
proc set_event_callback(
|
||||||
ctx: ptr FFIContext[Client],
|
ctx: ptr FFIContext[ChatClient],
|
||||||
callback: FFICallBack,
|
callback: FFICallBack,
|
||||||
userData: pointer
|
userData: pointer
|
||||||
) {.dynlib, exportc, cdecl.} =
|
) {.dynlib, exportc, cdecl.} =
|
||||||
|
|||||||
@ -34,20 +34,20 @@ proc chat_new(
|
|||||||
): pointer {.dynlib, exportc, cdecl.} =
|
): pointer {.dynlib, exportc, cdecl.} =
|
||||||
initializeLibrary()
|
initializeLibrary()
|
||||||
|
|
||||||
## Creates a new instance of the Chat Client.
|
## Creates a new instance of the ChatClient.
|
||||||
if isNil(callback):
|
if isNil(callback):
|
||||||
echo "error: missing callback in chat_new"
|
echo "error: missing callback in chat_new"
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
## Create the Chat thread that will keep waiting for req from the main thread.
|
## Create the Chat thread that will keep waiting for req from the main thread.
|
||||||
var ctx = ffi.createFFIContext[Client]().valueOr:
|
var ctx = ffi.createFFIContext[ChatClient]().valueOr:
|
||||||
let msg = "Error in createFFIContext: " & $error
|
let msg = "Error in createFFIContext: " & $error
|
||||||
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
|
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
ctx.userData = userData
|
ctx.userData = userData
|
||||||
|
|
||||||
proc onNewMessage(ctx: ptr FFIContext[Client]): MessageCallback =
|
proc onNewMessage(ctx: ptr FFIContext[ChatClient]): MessageCallback =
|
||||||
return proc(conversation: Conversation, msg: ReceivedMessage): Future[void] {.async.} =
|
return proc(conversation: Conversation, msg: ReceivedMessage): Future[void] {.async.} =
|
||||||
callEventCallback(ctx, "onNewMessage"):
|
callEventCallback(ctx, "onNewMessage"):
|
||||||
$newJsonMessageEvent(
|
$newJsonMessageEvent(
|
||||||
@ -57,12 +57,12 @@ proc chat_new(
|
|||||||
msg.timestamp
|
msg.timestamp
|
||||||
)
|
)
|
||||||
|
|
||||||
proc onNewConversation(ctx: ptr FFIContext[Client]): NewConvoCallback =
|
proc onNewConversation(ctx: ptr FFIContext[ChatClient]): NewConvoCallback =
|
||||||
return proc(conversation: Conversation): Future[void] {.async.} =
|
return proc(conversation: Conversation): Future[void] {.async.} =
|
||||||
callEventCallback(ctx, "onNewConversation"):
|
callEventCallback(ctx, "onNewConversation"):
|
||||||
$newJsonConversationEvent(conversation.id(), "private")
|
$newJsonConversationEvent(conversation.id(), "private")
|
||||||
|
|
||||||
proc onDeliveryAck(ctx: ptr FFIContext[Client]): DeliveryAckCallback =
|
proc onDeliveryAck(ctx: ptr FFIContext[ChatClient]): DeliveryAckCallback =
|
||||||
return proc(conversation: Conversation, msgId: MessageId): Future[void] {.async.} =
|
return proc(conversation: Conversation, msgId: MessageId): Future[void] {.async.} =
|
||||||
callEventCallback(ctx, "onDeliveryAck"):
|
callEventCallback(ctx, "onDeliveryAck"):
|
||||||
$newJsonDeliveryAckEvent(conversation.id(), msgId)
|
$newJsonDeliveryAckEvent(conversation.id(), msgId)
|
||||||
@ -83,7 +83,7 @@ proc chat_new(
|
|||||||
return ctx
|
return ctx
|
||||||
|
|
||||||
proc chat_destroy(
|
proc chat_destroy(
|
||||||
ctx: ptr FFIContext[Client], callback: FFICallBack, userData: pointer
|
ctx: ptr FFIContext[ChatClient], callback: FFICallBack, userData: pointer
|
||||||
): cint {.dynlib, exportc, cdecl.} =
|
): cint {.dynlib, exportc, cdecl.} =
|
||||||
initializeLibrary()
|
initializeLibrary()
|
||||||
checkParams(ctx, callback, userData)
|
checkParams(ctx, callback, userData)
|
||||||
|
|||||||
@ -47,7 +47,7 @@ type KeyEntry* = object
|
|||||||
privateKey: PrivateKey
|
privateKey: PrivateKey
|
||||||
timestamp: int64
|
timestamp: int64
|
||||||
|
|
||||||
type Client* = ref object
|
type ChatClient* = ref object
|
||||||
ident: Identity
|
ident: Identity
|
||||||
ds*: WakuClient
|
ds*: WakuClient
|
||||||
keyStore: Table[string, KeyEntry] # Keyed by HexEncoded Public Key
|
keyStore: Table[string, KeyEntry] # Keyed by HexEncoded Public Key
|
||||||
@ -64,9 +64,9 @@ type Client* = ref object
|
|||||||
# Constructors
|
# Constructors
|
||||||
#################################################
|
#################################################
|
||||||
|
|
||||||
proc newClient*(ds: WakuClient, ident: Identity): Client {.raises: [IOError,
|
proc newClient*(ds: WakuClient, ident: Identity): ChatClient {.raises: [IOError,
|
||||||
ValueError, SerializationError].} =
|
ValueError, SerializationError].} =
|
||||||
## Creates new instance of a `Client` with a given `WakuConfig`
|
## Creates new instance of a `ChatClient` with a given `WakuConfig`
|
||||||
try:
|
try:
|
||||||
let rm = newReliabilityManager().valueOr:
|
let rm = newReliabilityManager().valueOr:
|
||||||
raise newException(ValueError, fmt"SDS InitializationError")
|
raise newException(ValueError, fmt"SDS InitializationError")
|
||||||
@ -74,7 +74,7 @@ proc newClient*(ds: WakuClient, ident: Identity): Client {.raises: [IOError,
|
|||||||
let defaultInbox = initInbox(ident)
|
let defaultInbox = initInbox(ident)
|
||||||
|
|
||||||
var q = QueueRef(queue: newAsyncQueue[ChatPayload](10))
|
var q = QueueRef(queue: newAsyncQueue[ChatPayload](10))
|
||||||
var c = Client(ident: ident,
|
var c = ChatClient(ident: ident,
|
||||||
ds: ds,
|
ds: ds,
|
||||||
keyStore: initTable[string, KeyEntry](),
|
keyStore: initTable[string, KeyEntry](),
|
||||||
conversations: initTable[string, Conversation](),
|
conversations: initTable[string, Conversation](),
|
||||||
@ -96,17 +96,17 @@ proc newClient*(ds: WakuClient, ident: Identity): Client {.raises: [IOError,
|
|||||||
# Parameter Access
|
# Parameter Access
|
||||||
#################################################
|
#################################################
|
||||||
|
|
||||||
proc getId*(client: Client): string =
|
proc getId*(client: ChatClient): string =
|
||||||
result = client.ident.getName()
|
result = client.ident.getName()
|
||||||
|
|
||||||
proc identity*(client: Client): Identity =
|
proc identity*(client: ChatClient): Identity =
|
||||||
result = client.ident
|
result = client.ident
|
||||||
|
|
||||||
proc defaultInboxConversationId*(self: Client): string =
|
proc defaultInboxConversationId*(self: ChatClient): string =
|
||||||
## Returns the default inbox address for the client.
|
## Returns the default inbox address for the client.
|
||||||
result = conversationIdFor(self.ident.getPubkey())
|
result = conversationIdFor(self.ident.getPubkey())
|
||||||
|
|
||||||
proc getConversationFromHint(self: Client,
|
proc getConversationFromHint(self: ChatClient,
|
||||||
conversationHint: string): Result[Option[Conversation], string] =
|
conversationHint: string): Result[Option[Conversation], string] =
|
||||||
|
|
||||||
# TODO: Implementing Hinting
|
# TODO: Implementing Hinting
|
||||||
@ -116,31 +116,31 @@ proc getConversationFromHint(self: Client,
|
|||||||
ok(some(self.conversations[conversationHint]))
|
ok(some(self.conversations[conversationHint]))
|
||||||
|
|
||||||
|
|
||||||
proc listConversations*(client: Client): seq[Conversation] =
|
proc listConversations*(client: ChatClient): seq[Conversation] =
|
||||||
result = toSeq(client.conversations.values())
|
result = toSeq(client.conversations.values())
|
||||||
|
|
||||||
#################################################
|
#################################################
|
||||||
# Callback Handling
|
# Callback Handling
|
||||||
#################################################
|
#################################################
|
||||||
|
|
||||||
proc onNewMessage*(client: Client, callback: MessageCallback) =
|
proc onNewMessage*(client: ChatClient, callback: MessageCallback) =
|
||||||
client.newMessageCallbacks.add(callback)
|
client.newMessageCallbacks.add(callback)
|
||||||
|
|
||||||
proc notifyNewMessage*(client: Client, convo: Conversation, msg: ReceivedMessage) =
|
proc notifyNewMessage*(client: ChatClient, convo: Conversation, msg: ReceivedMessage) =
|
||||||
for cb in client.newMessageCallbacks:
|
for cb in client.newMessageCallbacks:
|
||||||
discard cb(convo, msg)
|
discard cb(convo, msg)
|
||||||
|
|
||||||
proc onNewConversation*(client: Client, callback: NewConvoCallback) =
|
proc onNewConversation*(client: ChatClient, callback: NewConvoCallback) =
|
||||||
client.newConvoCallbacks.add(callback)
|
client.newConvoCallbacks.add(callback)
|
||||||
|
|
||||||
proc notifyNewConversation(client: Client, convo: Conversation) =
|
proc notifyNewConversation(client: ChatClient, convo: Conversation) =
|
||||||
for cb in client.newConvoCallbacks:
|
for cb in client.newConvoCallbacks:
|
||||||
discard cb(convo)
|
discard cb(convo)
|
||||||
|
|
||||||
proc onDeliveryAck*(client: Client, callback: DeliveryAckCallback) =
|
proc onDeliveryAck*(client: ChatClient, callback: DeliveryAckCallback) =
|
||||||
client.deliveryAckCallbacks.add(callback)
|
client.deliveryAckCallbacks.add(callback)
|
||||||
|
|
||||||
proc notifyDeliveryAck(client: Client, convo: Conversation,
|
proc notifyDeliveryAck(client: ChatClient, convo: Conversation,
|
||||||
messageId: MessageId) =
|
messageId: MessageId) =
|
||||||
for cb in client.deliveryAckCallbacks:
|
for cb in client.deliveryAckCallbacks:
|
||||||
discard cb(convo, messageId)
|
discard cb(convo, messageId)
|
||||||
@ -149,7 +149,7 @@ proc notifyDeliveryAck(client: Client, convo: Conversation,
|
|||||||
# Functional
|
# Functional
|
||||||
#################################################
|
#################################################
|
||||||
|
|
||||||
proc createIntroBundle*(self: var Client): IntroBundle =
|
proc createIntroBundle*(self: var ChatClient): 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.
|
||||||
|
|
||||||
@ -175,16 +175,16 @@ proc createIntroBundle*(self: var Client): IntroBundle =
|
|||||||
# Conversation Initiation
|
# Conversation Initiation
|
||||||
#################################################
|
#################################################
|
||||||
|
|
||||||
proc addConversation*(client: Client, convo: Conversation) =
|
proc addConversation*(client: ChatClient, convo: Conversation) =
|
||||||
notice "Creating conversation", client = client.getId(), convoId = convo.id()
|
notice "Creating conversation", client = client.getId(), convoId = convo.id()
|
||||||
client.conversations[convo.id()] = convo
|
client.conversations[convo.id()] = convo
|
||||||
client.notifyNewConversation(convo)
|
client.notifyNewConversation(convo)
|
||||||
|
|
||||||
proc getConversation*(client: Client, convoId: string): Conversation =
|
proc getConversation*(client: ChatClient, convoId: string): Conversation =
|
||||||
notice "Get conversation", client = client.getId(), convoId = convoId
|
notice "Get conversation", client = client.getId(), convoId = convoId
|
||||||
result = client.conversations[convoId]
|
result = client.conversations[convoId]
|
||||||
|
|
||||||
proc newPrivateConversation*(client: Client,
|
proc newPrivateConversation*(client: ChatClient,
|
||||||
introBundle: IntroBundle, content: Content): Future[Option[ChatError]] {.async.} =
|
introBundle: IntroBundle, content: Content): Future[Option[ChatError]] {.async.} =
|
||||||
## Creates a private conversation with the given `IntroBundle`.
|
## Creates a private conversation with the given `IntroBundle`.
|
||||||
## `IntroBundles` are provided out-of-band.
|
## `IntroBundles` are provided out-of-band.
|
||||||
@ -202,7 +202,7 @@ proc newPrivateConversation*(client: Client,
|
|||||||
# Receives a incoming payload, decodes it, and processes it.
|
# Receives a incoming payload, decodes it, and processes it.
|
||||||
#################################################
|
#################################################
|
||||||
|
|
||||||
proc parseMessage(client: Client, msg: ChatPayload) {.raises: [ValueError,
|
proc parseMessage(client: ChatClient, msg: ChatPayload) {.raises: [ValueError,
|
||||||
SerializationError].} =
|
SerializationError].} =
|
||||||
let envelopeRes = decode(msg.bytes, WapEnvelopeV1)
|
let envelopeRes = decode(msg.bytes, WapEnvelopeV1)
|
||||||
if envelopeRes.isErr:
|
if envelopeRes.isErr:
|
||||||
@ -231,7 +231,7 @@ proc parseMessage(client: Client, msg: ChatPayload) {.raises: [ValueError,
|
|||||||
# Async Tasks
|
# Async Tasks
|
||||||
#################################################
|
#################################################
|
||||||
|
|
||||||
proc messageQueueConsumer(client: Client) {.async.} =
|
proc messageQueueConsumer(client: ChatClient) {.async.} =
|
||||||
## Main message processing loop
|
## Main message processing loop
|
||||||
info "Message listener started"
|
info "Message listener started"
|
||||||
|
|
||||||
@ -257,8 +257,8 @@ proc messageQueueConsumer(client: Client) {.async.} =
|
|||||||
# Control Functions
|
# Control Functions
|
||||||
#################################################
|
#################################################
|
||||||
|
|
||||||
proc start*(client: Client) {.async.} =
|
proc start*(client: ChatClient) {.async.} =
|
||||||
## Start `Client` and listens for incoming messages.
|
## Start `ChatClient` and listens for incoming messages.
|
||||||
client.ds.addDispatchQueue(client.inboundQueue)
|
client.ds.addDispatchQueue(client.inboundQueue)
|
||||||
asyncSpawn client.ds.start()
|
asyncSpawn client.ds.start()
|
||||||
|
|
||||||
@ -268,7 +268,7 @@ proc start*(client: Client) {.async.} =
|
|||||||
|
|
||||||
notice "Client start complete", client = client.getId()
|
notice "Client start complete", client = client.getId()
|
||||||
|
|
||||||
proc stop*(client: Client) {.async.} =
|
proc stop*(client: ChatClient) {.async.} =
|
||||||
## Stop the client.
|
## Stop the client.
|
||||||
await client.ds.stop()
|
await client.ds.stop()
|
||||||
client.isRunning = false
|
client.isRunning = false
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user