Skip to content

Commit

Permalink
initialize user account in bridge from libqaul
Browse files Browse the repository at this point in the history
  • Loading branch information
MathJud committed Aug 28, 2023
1 parent a8451cd commit ca233a4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 122 deletions.
11 changes: 6 additions & 5 deletions rust/clients/bridge/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ mod user_accounts;
mod users;

use rpc::Rpc;
use user_accounts::UserAccounts;

/// Events of the async loop
enum EventType {
Rpc(bool),
Expand All @@ -57,12 +57,15 @@ async fn main() {
std::thread::sleep(Duration::from_millis(10));
}

// Set user account
// if no account, creating new accounts
if libqaul::node::user_accounts::UserAccounts::len() == 0 {
// TODO: the name of the user account should be configurable
libqaul::node::user_accounts::UserAccounts::create("Qaul Matrix Bridge Bot".to_owned());
println!("Starting the Bridge...");
std::thread::sleep(Duration::from_secs(2));
}
let default_user = libqaul::node::user_accounts::UserAccounts::get_default_user().unwrap();
// initialize user account
user_accounts::UserAccounts::init(default_user);
println!("Matrix Bot has been initialized as a Qaul User");

// Get the command-line arguments as a collection of strings.
Expand All @@ -74,8 +77,6 @@ async fn main() {
for (index, arg) in args.iter().enumerate().skip(1) {
arguments.insert(index, arg.to_owned());
}
// initialize user accounts
UserAccounts::init();

thread::spawn(|| {
// connect the matrix bot with the qaul-cli
Expand Down
4 changes: 2 additions & 2 deletions rust/clients/bridge/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl Rpc {
// TODO: authorisation
}
Some(proto::Modules::Useraccounts) => {
super::user_accounts::UserAccounts::rpc(message.data);
//super::user_accounts::UserAccounts::rpc(message.data);
}
Some(proto::Modules::Users) => {
super::users::Users::rpc(message.data, message.request_id);
Expand All @@ -104,7 +104,7 @@ impl Rpc {
super::chatfile::ChatFile::rpc(message.data);
}
Some(proto::Modules::Group) => {
super::group::Group::rpc(message.data,message.request_id);
super::group::Group::rpc(message.data, message.request_id);
}
Some(proto::Modules::Rtc) => {
super::rtc::Rtc::rpc(message.data);
Expand Down
120 changes: 5 additions & 115 deletions rust/clients/bridge/src/user_accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

//! # User Accounts Module Functions
use super::rpc::Rpc;
use prost::Message;
use libqaul::node::user_accounts::UserAccount;
use state::Storage;
use std::sync::RwLock;

Expand All @@ -17,34 +16,19 @@ mod proto {
static USERACCOUNTS: Storage<RwLock<UserAccounts>> = Storage::new();
pub static BOT_USER_ACCOUNT_ID: Storage<String> = Storage::new();

/// default user initialization
pub enum MyUserAccountInitialiation {
/// there was no request sent yet
Uninitialized,
/// no user account created yet
NoDefaultAccount,
/// user account is initialized
Initialized,
}

/// user accounts module function handling
pub struct UserAccounts {
initialiation: MyUserAccountInitialiation,
my_user_account: Option<proto::MyUserAccount>,
my_user_account: Option<UserAccount>,
}

impl UserAccounts {
/// Initialize User Accounts
pub fn init() {
pub fn init(user_account: UserAccount) {
// create user accounts state
let user_accounts = UserAccounts {
initialiation: MyUserAccountInitialiation::Uninitialized,
my_user_account: None,
my_user_account: Some(user_account),
};
USERACCOUNTS.set(RwLock::new(user_accounts));

// request default user
Self::request_default_account();
}

/// return user id
Expand All @@ -53,103 +37,9 @@ impl UserAccounts {
let user_accounts = USERACCOUNTS.get().read().unwrap();

if let Some(my_user_account) = &user_accounts.my_user_account {
return Some(my_user_account.id.clone());
return Some(my_user_account.id.to_bytes());
}

None
}

/// Request default user account
fn request_default_account() {
// create info request message
let proto_message = proto::UserAccounts {
message: Some(proto::user_accounts::Message::GetDefaultUserAccount(true)),
};

// encode message
let mut buf = Vec::with_capacity(proto_message.encoded_len());
proto_message
.encode(&mut buf)
.expect("Vec<u8> provides capacity as needed");

// send message
Rpc::send_message(
buf,
super::rpc::proto::Modules::Useraccounts.into(),
"".to_string(),
);
}

/// Process received RPC message
///
/// Decodes received protobuf encoded binary RPC message
/// of the user accounts module.
pub fn rpc(data: Vec<u8>) {
match proto::UserAccounts::decode(&data[..]) {
Ok(user_accounts) => {
match user_accounts.message {
Some(proto::user_accounts::Message::DefaultUserAccount(
proto_defaultuseraccount,
)) => {
// get state
let mut user_accounts = USERACCOUNTS.get().write().unwrap();

// check if default user is set
if proto_defaultuseraccount.user_account_exists {
if let Some(my_user_account) = proto_defaultuseraccount.my_user_account
{
// print user account
println!("Your user account is:");
println!(
"{}, ID[{}]",
my_user_account.name, my_user_account.id_base58
);
println!(" public key: {}", my_user_account.key_base58);
BOT_USER_ACCOUNT_ID.set(my_user_account.clone().id_base58);
// save it to state
user_accounts.my_user_account = Some(my_user_account);
user_accounts.initialiation =
MyUserAccountInitialiation::Initialized;
} else {
log::error!("unexpected message configuration");
}
} else {
// print message to create a new user account
println!("No user account created yet");
println!("Please create a user account:");
println!("");
println!(" account create {{Your User Name}}");
println!("");

// save it to state
user_accounts.initialiation =
MyUserAccountInitialiation::NoDefaultAccount;
}
}
Some(proto::user_accounts::Message::MyUserAccount(proto_myuseraccount)) => {
// get state
let mut user_accounts = USERACCOUNTS.get().write().unwrap();

// print received user
println!("New user account created:");
println!(
"{}, ID[{}]",
proto_myuseraccount.name, proto_myuseraccount.id_base58
);
println!(" public key: {}", proto_myuseraccount.key_base58);

// save it to state
user_accounts.my_user_account = Some(proto_myuseraccount);
user_accounts.initialiation = MyUserAccountInitialiation::Initialized;
}
_ => {
log::error!("unprocessable RPC user accounts message");
}
}
}
Err(error) => {
log::error!("{:?}", error);
}
}
}
}

0 comments on commit ca233a4

Please sign in to comment.