Communities
This commit is contained in:
parent
6ae6e85b5a
commit
1c08956387
26 changed files with 1988 additions and 338 deletions
45
src/communities/community_manager.rs
Normal file
45
src/communities/community_manager.rs
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
use crate::communities::community::{self, Community};
|
||||
use crate::gui::log_panel;
|
||||
use crate::util::file_util;
|
||||
use once_cell::sync::Lazy;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::Mutex;
|
||||
use uuid::Uuid;
|
||||
|
||||
pub static COMMUNITY_REGISTRY: Lazy<Arc<Mutex<HashMap<String, Arc<Community>>>>> =
|
||||
Lazy::new(|| Arc::new(Mutex::new(HashMap::new())));
|
||||
|
||||
pub async fn add_community(community: Arc<Community>) {
|
||||
COMMUNITY_REGISTRY
|
||||
.lock()
|
||||
.await
|
||||
.insert(community.get_name().to_string(), community);
|
||||
}
|
||||
pub async fn get_community(name: &str) -> Option<Arc<Community>> {
|
||||
if let Some(c) = COMMUNITY_REGISTRY.lock().await.get(name) {
|
||||
Some(c.clone())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn load_communities() {
|
||||
let community_names = file_util::get_children("communities");
|
||||
for name in community_names {
|
||||
if let Some(community) = community::load(&name).await {
|
||||
add_community(community).await;
|
||||
} else {
|
||||
log_panel::log_message(format!("failed to load the {} community", &name));
|
||||
}
|
||||
}
|
||||
}
|
||||
pub async fn save_communities() {
|
||||
for community in COMMUNITY_REGISTRY.lock().await.values() {
|
||||
community.save().await;
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get_communities() -> Vec<Arc<Community>> {
|
||||
COMMUNITY_REGISTRY.lock().await.values().cloned().collect()
|
||||
}
|
||||
Loading…
Reference in a new issue