mirror of
https://github.com/logos-messaging/libchat.git
synced 2026-03-27 22:53:07 +00:00
Add Installation name (#58)
* Add Identity name * Update Context to accept a name * Change constructor in bindings * Add name retrieval to bindings * Update constructor string type * Remove uneeded files * rename functions for symmetry
This commit is contained in:
parent
a9ca4ffb7d
commit
eb941387df
@ -49,8 +49,15 @@ pub struct ContextHandle(pub(crate) Context);
|
|||||||
/// # Returns
|
/// # Returns
|
||||||
/// Opaque handle to the store. Must be freed with destroy_context()
|
/// Opaque handle to the store. Must be freed with destroy_context()
|
||||||
#[ffi_export]
|
#[ffi_export]
|
||||||
pub fn create_context() -> repr_c::Box<ContextHandle> {
|
pub fn create_context(name: repr_c::String) -> repr_c::Box<ContextHandle> {
|
||||||
Box::new(ContextHandle(Context::new())).into()
|
// Deference name to to `str` and then borrow to &str
|
||||||
|
Box::new(ContextHandle(Context::new_with_name(&*name))).into()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the friendly name of the contexts installation.
|
||||||
|
#[ffi_export]
|
||||||
|
pub fn installation_name(ctx: &ContextHandle) -> repr_c::String {
|
||||||
|
ctx.0.installation_name().to_string().into()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Destroys a conversation store and frees its memory
|
/// Destroys a conversation store and frees its memory
|
||||||
|
|||||||
@ -21,8 +21,8 @@ pub struct Context {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Context {
|
impl Context {
|
||||||
pub fn new() -> Self {
|
pub fn new_with_name(name: impl Into<String>) -> Self {
|
||||||
let identity = Rc::new(Identity::new());
|
let identity = Rc::new(Identity::new(name));
|
||||||
let inbox = Inbox::new(Rc::clone(&identity)); //
|
let inbox = Inbox::new(Rc::clone(&identity)); //
|
||||||
Self {
|
Self {
|
||||||
_identity: identity,
|
_identity: identity,
|
||||||
@ -31,6 +31,10 @@ impl Context {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn installation_name(&self) -> &str {
|
||||||
|
self._identity.get_name()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn create_private_convo(
|
pub fn create_private_convo(
|
||||||
&mut self,
|
&mut self,
|
||||||
remote_bundle: &Introduction,
|
remote_bundle: &Introduction,
|
||||||
@ -156,8 +160,8 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn ctx_integration() {
|
fn ctx_integration() {
|
||||||
let mut saro = Context::new();
|
let mut saro = Context::new_with_name("saro");
|
||||||
let mut raya = Context::new();
|
let mut raya = Context::new_with_name("raya");
|
||||||
|
|
||||||
// Raya creates intro bundle and sends to Saro
|
// Raya creates intro bundle and sends to Saro
|
||||||
let bundle = raya.create_intro_bundle().unwrap();
|
let bundle = raya.create_intro_bundle().unwrap();
|
||||||
|
|||||||
@ -3,6 +3,7 @@ use std::fmt;
|
|||||||
use crate::crypto::{PrivateKey, PublicKey};
|
use crate::crypto::{PrivateKey, PublicKey};
|
||||||
|
|
||||||
pub struct Identity {
|
pub struct Identity {
|
||||||
|
name: String,
|
||||||
secret: PrivateKey,
|
secret: PrivateKey,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -16,8 +17,9 @@ impl fmt::Debug for Identity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Identity {
|
impl Identity {
|
||||||
pub fn new() -> Self {
|
pub fn new(name: impl Into<String>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
name: name.into(),
|
||||||
secret: PrivateKey::random(),
|
secret: PrivateKey::random(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -29,10 +31,17 @@ impl Identity {
|
|||||||
pub fn secret(&self) -> &PrivateKey {
|
pub fn secret(&self) -> &PrivateKey {
|
||||||
&self.secret
|
&self.secret
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns an associated name for this Identity.
|
||||||
|
// Names are a friendly developer chosen identifier for an Identity which
|
||||||
|
// can provide between logging.
|
||||||
|
pub fn get_name(&self) -> &str {
|
||||||
|
&self.name
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Identity {
|
impl Default for Identity {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self::new()
|
Self::new("default")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -238,10 +238,10 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_invite_privatev1_roundtrip() {
|
fn test_invite_privatev1_roundtrip() {
|
||||||
let saro_ident = Identity::new();
|
let saro_ident = Identity::new("saro");
|
||||||
let saro_inbox = Inbox::new(saro_ident.into());
|
let saro_inbox = Inbox::new(saro_ident.into());
|
||||||
|
|
||||||
let raya_ident = Identity::new();
|
let raya_ident = Identity::new("raya");
|
||||||
let mut raya_inbox = Inbox::new(raya_ident.into());
|
let mut raya_inbox = Inbox::new(raya_ident.into());
|
||||||
|
|
||||||
let bundle = raya_inbox.create_intro_bundle();
|
let bundle = raya_inbox.create_intro_bundle();
|
||||||
|
|||||||
@ -21,8 +21,8 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_message_roundtrip() {
|
fn test_message_roundtrip() {
|
||||||
let mut saro = create_context();
|
let mut saro = create_context("saro".into());
|
||||||
let mut raya = create_context();
|
let mut raya = create_context("raya".into());
|
||||||
|
|
||||||
// Raya Creates Bundle and Sends to Saro
|
// Raya Creates Bundle and Sends to Saro
|
||||||
let intro_result = create_intro_bundle(&mut raya);
|
let intro_result = create_intro_bundle(&mut raya);
|
||||||
|
|||||||
3
nim-bindings/.gitignore
vendored
Normal file
3
nim-bindings/.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
nimble.develop
|
||||||
|
nimble.paths
|
||||||
|
nimbledeps
|
||||||
4
nim-bindings/config.nims
Normal file
4
nim-bindings/config.nims
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
# begin Nimble config (version 2)
|
||||||
|
when withDir(thisDir(), system.fileExists("nimble.paths")):
|
||||||
|
include "nimble.paths"
|
||||||
|
# end Nimble config
|
||||||
@ -15,8 +15,8 @@ proc encode*(s: string): seq[byte] =
|
|||||||
|
|
||||||
proc pingpong() =
|
proc pingpong() =
|
||||||
|
|
||||||
var raya = newConversationsContext()
|
var raya = newConversationsContext("raya")
|
||||||
var saro = newConversationsContext()
|
var saro = newConversationsContext("saro")
|
||||||
|
|
||||||
|
|
||||||
# Perform out of band Introduction
|
# Perform out of band Introduction
|
||||||
|
|||||||
@ -97,7 +97,11 @@ type
|
|||||||
|
|
||||||
## Creates a new libchat Context
|
## Creates a new libchat Context
|
||||||
## Returns: Opaque handle to the context. Must be freed with destroy_context()
|
## Returns: Opaque handle to the context. Must be freed with destroy_context()
|
||||||
proc create_context*(): ContextHandle {.importc, dynlib: CONVERSATIONS_LIB.}
|
proc create_context*(name: ReprCString): ContextHandle {.importc, dynlib: CONVERSATIONS_LIB.}
|
||||||
|
|
||||||
|
## Returns the friendly name of the context's identity
|
||||||
|
## The result must be freed by the caller (repr_c::String ownership transfers)
|
||||||
|
proc installation_name*(ctx: ContextHandle): ReprCString {.importc, dynlib: CONVERSATIONS_LIB.}
|
||||||
|
|
||||||
## Destroys a context and frees its memory
|
## Destroys a context and frees its memory
|
||||||
## - handle must be a valid pointer from create_context()
|
## - handle must be a valid pointer from create_context()
|
||||||
|
|||||||
@ -13,13 +13,20 @@ type
|
|||||||
data*: seq[uint8]
|
data*: seq[uint8]
|
||||||
|
|
||||||
## Create a new conversations context
|
## Create a new conversations context
|
||||||
proc newConversationsContext*(): LibChat =
|
proc newConversationsContext*(name: string): LibChat =
|
||||||
|
|
||||||
result.handle = create_context()
|
result.handle = create_context(name.toReprCString)
|
||||||
result.buffer_size = 256
|
result.buffer_size = 256
|
||||||
if result.handle.isNil:
|
if result.handle.isNil:
|
||||||
raise newException(IOError, "Failed to create context")
|
raise newException(IOError, "Failed to create context")
|
||||||
|
|
||||||
|
## Get the friendly name of this context's installation
|
||||||
|
proc getInstallationName*(ctx: LibChat): string =
|
||||||
|
if ctx.handle == nil:
|
||||||
|
return ""
|
||||||
|
let name = installation_name(ctx.handle)
|
||||||
|
result = $name
|
||||||
|
|
||||||
## Destroy the context and free resources
|
## Destroy the context and free resources
|
||||||
proc destroy*(ctx: var LibChat) =
|
proc destroy*(ctx: var LibChat) =
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user