[Fix] Stability

This commit is contained in:
Alex Emmet 2026-08-28 13:22:59 +02:00
commit 8160f8d0cb
No known key found for this signature in database
44 changed files with 796 additions and 1296 deletions

View file

@ -40,7 +40,7 @@ impl Community {
let mut buf = [0u8; 56];
let mut rng = OsRng;
rng.fill_bytes(&mut buf);
let private_key = Secret::from_bytes(&buf).unwrap();
let private_key = Secret::from(buf);
let public_key = PublicKey::from(&private_key);
Community {
name: String::new(),
@ -58,7 +58,7 @@ impl Community {
let mut buf = [0u8; 56];
let mut rng = OsRng;
rng.fill_bytes(&mut buf);
let private_key = Secret::from_bytes(&buf).unwrap();
let private_key = Secret::from(buf);
let public_key = PublicKey::from(&private_key);
let c = Community {
name,
@ -125,7 +125,7 @@ impl Community {
self.members.clone()
}
pub fn get_private_key(&self) -> Secret {
Secret::from_bytes(self.private_key.as_bytes()).unwrap()
Secret::from(*self.private_key.as_bytes())
}
pub fn get_public_key(&self) -> &PublicKey {
&self.public_key
@ -222,12 +222,14 @@ impl Community {
for interactable in target_interactables.iter() {
if interactable.get_name() == name {
if interactable.get_codec() == "category" {
let category: &Category =
interactable.as_any().downcast_ref::<Category>().unwrap();
let Some(category) = interactable.as_any().downcast_ref::<Category>() else {
return CommunicationValue::new(CommunicationType::ErrorInternal);
};
// cannot move a value of type dyn Interactable the size of dyn Interactable cannot be statically determined (rustc E0161)
return category
.get_child(path.to_string(), name.to_string())
.unwrap()
.ok_or(CommunicationValue::new(CommunicationType::ErrorInternal))
.unwrap_or_else(|error| return error)
.run_function(cv.clone())
.await;
} else {
@ -267,7 +269,7 @@ impl Community {
let mut data = JsonValue::new_object();
let mut permissions = JsonValue::new_array();
for perm in self.permissions.get(user).unwrap() {
for perm in self.permissions.get(user).into_iter().flatten() {
if let Ok(_) = permissions.push(perm.to_string()) {}
}
@ -287,10 +289,10 @@ impl Community {
}
pub async fn load(name: &String) -> Option<Arc<Community>> {
let file_contents = file_util::load_file(&format!("communities/{}/", name), "config.json");
let json_content = json::parse(&file_contents).unwrap();
let json_content = json::parse(&file_contents).ok()?;
let user_data = file_util::load_file(&format!("communities/{}/", name), "users.json");
let user_json: JsonValue = json::parse(&user_data).unwrap();
let user_json: JsonValue = json::parse(&user_data).ok()?;
let mut users = Vec::new();
let mut permissions: HashMap<i64, Vec<Permission>> = HashMap::new();
@ -318,25 +320,17 @@ pub async fn load(name: &String) -> Option<Arc<Community>> {
};
let community = Community {
name: json_content["name"].as_str().unwrap().to_string(),
name: json_content["name"].as_str()?.to_string(),
owner_id: Arc::new(RwLock::new(json_content["owner_id"].as_i64().unwrap_or(0))),
members: users,
roles,
permissions,
private_key: Secret::from_bytes(
&STANDARD
.decode(json_content["private_key"].as_str().unwrap())
.unwrap(),
)
.unwrap(),
public_key: PublicKey::from(
&Secret::from_bytes(
&STANDARD
.decode(json_content["private_key"].as_str().unwrap())
.unwrap(),
)
.unwrap(),
),
&STANDARD.decode(json_content["private_key"].as_str()?).ok()?,
)?,
public_key: PublicKey::from(&Secret::from_bytes(
&STANDARD.decode(json_content["private_key"].as_str()?).ok()?,
)?),
interactables: Arc::new(RwLock::new(Vec::new())),
connections: Arc::new(RwLock::new(HashMap::new())),
};
@ -346,7 +340,7 @@ pub async fn load(name: &String) -> Option<Arc<Community>> {
file_util::get_children(&format!("communities/{}/interactables/", name));
for file in interactable_files {
if file.contains(".json") {
let name = file.split('.').next().unwrap().to_string();
let Some(name) = file.split('.').next().map(str::to_string) else { continue };
let interactable: Box<dyn Interactable> =
registry::load(comarc.clone(), String::new(), name).await;
comarc.add_interactable(Arc::new(interactable)).await;

View file

@ -52,7 +52,9 @@ impl CommunityConnection {
pub async fn send_message(&self, message: &CommunicationValue) {
let mut sender = self.sender.write().await; // Access the SplitSink
let message_text = Message::Text(Utf8Bytes::from(message.to_json().to_string()));
sender.send(message_text).await.unwrap(); // Send the message via the SplitSink
if let Err(error) = sender.send(message_text).await {
log::error!("failed to send community message: {error}");
}
}
pub async fn get_community(&self) -> Option<Arc<Community>> {
self.community.read().await.clone()
@ -94,14 +96,15 @@ impl CommunityConnection {
}
}
async fn handle_function(&self, cv: CommunicationValue) {
let name = cv.get_data(DataType::Name).unwrap().as_str().unwrap();
let path = cv.get_data(DataType::Path).unwrap().as_str().unwrap();
let function = cv.get_data(DataType::Function).unwrap().as_str().unwrap();
let Some(name) = cv.get_data(DataType::Name).as_str() else { return };
let Some(path) = cv.get_data(DataType::Path).as_str() else { return };
let Some(function) = cv.get_data(DataType::Function).as_str() else { return };
let result = self
.get_community()
.await
.unwrap()
.ok_or(())
.unwrap_or_else(|_| return)
.run_function(self.get_user_id().await, name, path, function, &cv)
.await;
@ -364,13 +367,9 @@ impl CommunityConnection {
pub async fn handle_close(self: Arc<Self>) {
if self.is_identified().await {
if self.get_user_id().await != 0 {
self.community
.read()
.await
.as_ref()
.unwrap()
.remove_connection(self.clone())
.await;
if let Some(community) = self.community.read().await.as_ref() {
community.remove_connection(self.clone()).await;
}
}
}
}

View file

@ -30,14 +30,13 @@ impl Category {
.find(|child| child.get_name() == &name)
.cloned()
} else {
let sub_module = path.split("/").next().unwrap();
let sub_module = path.split('/').next()?;
let next = self
.children
.iter()
.find(|child| child.get_name() == sub_module)
.unwrap();
.find(|child| child.get_name() == sub_module)?;
if next.get_codec() == "category" {
let next_cat = next.as_any().downcast_ref::<Category>().unwrap();
let next_cat = next.as_any().downcast_ref::<Category>()?;
next_cat.get_child(path, name)
} else {
Some(next.clone())