This commit is contained in:
2021-08-26 19:52:52 +02:00
parent 3b7700a4b1
commit 8afc77dd60
7 changed files with 567 additions and 473 deletions
+1 -1
View File
@@ -7,7 +7,7 @@ use crate::error::GroupError;
/// This is the inner data struct holding the config
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub(crate) struct Config {
groups: HashMap<String, GroupConfig>,
pub(crate) groups: HashMap<String, GroupConfig>,
}
impl Config {
+16 -5
View File
@@ -9,6 +9,7 @@ use data::{Config, GroupConfig};
use crate::error::GroupError;
use crate::group_handle::GroupHandle;
use std::time::Duration;
pub(crate) mod data;
@@ -82,6 +83,7 @@ impl ConfigStore {
pub async fn reauth_group(self: &Arc<Self>, acct: &str) -> Result<GroupHandle, GroupError> {
let groups = self.data.read().await;
let mut config = groups.get_group_config(acct).ok_or(GroupError::GroupNotExist)?.clone();
drop(groups);
println!("--- Re-authenticating bot user @{} ---", acct);
let registration = Registration::new(config.get_appdata().base.to_string())
@@ -92,6 +94,8 @@ impl ConfigStore {
.await?;
let client = elefren::helpers::cli::authenticate(registration).await?;
println!("Auth complete");
let appdata = client.data.clone();
config.set_appdata(appdata);
@@ -106,8 +110,8 @@ impl ConfigStore {
/// Spawn existing group using saved creds
pub async fn spawn_groups(self: Arc<Self>) -> Vec<GroupHandle> {
let groups = self.data.read().await;
let groups_iter = groups.iter_groups().cloned();
let groups = self.data.read().await.clone();
let groups_iter = groups.groups.into_values();
// Connect in parallel
futures::stream::iter(groups_iter)
@@ -158,9 +162,15 @@ impl ConfigStore {
//noinspection RsSelfConvention
/// Set group config to the store. The store then saved.
pub(crate) async fn set_group_config(&self, config: GroupConfig) -> Result<(), GroupError> {
let mut data = self.data.write().await;
data.set_group_config(config);
self.persist(&data).await?;
debug!("Locking mutex");
if let Ok(mut data) = tokio::time::timeout(Duration::from_secs(1), self.data.write()).await {
debug!("Locked");
data.set_group_config(config);
debug!("Writing file");
self.persist(&data).await?;
} else {
error!("DEADLOCK? Timeout waiting for data RW Lock in settings store");
}
Ok(())
}
@@ -187,6 +197,7 @@ fn make_scopes() -> Scopes {
| Scopes::read(scopes::Read::Follows)
| Scopes::write(scopes::Write::Statuses)
| Scopes::write(scopes::Write::Media)
| Scopes::write(scopes::Write::Follows)
}
// trait TapOk<T> {