v0.2, refactor, improve some messages, fix lints

This commit is contained in:
2021-08-26 22:11:20 +02:00
parent 8afc77dd60
commit 52cf8f8e97
8 changed files with 1146 additions and 941 deletions
+47 -25
View File
@@ -11,9 +11,9 @@ pub(crate) struct Config {
}
impl Config {
pub(crate) fn iter_groups(&self) -> impl Iterator<Item = &GroupConfig> {
self.groups.values()
}
// pub(crate) fn iter_groups(&self) -> impl Iterator<Item = &GroupConfig> {
// self.groups.values()
// }
pub(crate) fn get_group_config(&self, acct: &str) -> Option<&GroupConfig> {
self.groups.get(acct)
@@ -103,8 +103,10 @@ impl GroupConfig {
}
pub(crate) fn set_appdata(&mut self, appdata: AppData) {
if self.appdata != appdata {
self.mark_dirty();
}
self.appdata = appdata;
self.mark_dirty();
}
pub(crate) fn get_admins(&self) -> impl Iterator<Item = &String> {
@@ -120,8 +122,10 @@ impl GroupConfig {
}
pub(crate) fn set_last_notif(&mut self, ts: u64) {
if self.last_notif_ts != ts {
self.mark_dirty();
}
self.last_notif_ts = self.last_notif_ts.max(ts);
self.mark_dirty();
}
pub(crate) fn get_last_notif(&self) -> u64 {
@@ -129,8 +133,10 @@ impl GroupConfig {
}
pub(crate) fn set_last_status(&mut self, ts: u64) {
if self.last_status_ts != ts {
self.mark_dirty();
}
self.last_status_ts = self.last_status_ts.max(ts);
self.mark_dirty();
}
pub(crate) fn get_last_status(&self) -> u64 {
@@ -177,67 +183,81 @@ impl GroupConfig {
}
pub(crate) fn set_admin(&mut self, acct: &str, admin: bool) -> Result<(), GroupError> {
if admin {
let change = if admin {
if self.is_banned(acct) {
return Err(GroupError::UserIsBanned);
}
self.admin_users.insert(acct.to_owned());
self.admin_users.insert(acct.to_owned())
} else {
self.admin_users.remove(acct);
self.admin_users.remove(acct)
};
if change {
self.mark_dirty();
}
self.mark_dirty();
Ok(())
}
pub(crate) fn set_member(&mut self, acct: &str, member: bool) -> Result<(), GroupError> {
if member {
let change = if member {
if self.is_banned(acct) {
return Err(GroupError::UserIsBanned);
}
self.member_users.insert(acct.to_owned());
self.member_users.insert(acct.to_owned())
} else {
self.member_users.remove(acct);
self.member_users.remove(acct)
};
if change {
self.mark_dirty();
}
self.mark_dirty();
Ok(())
}
pub(crate) fn ban_user(&mut self, acct: &str, ban: bool) -> Result<(), GroupError> {
let mut change = false;
if ban {
if self.is_admin(acct) {
return Err(GroupError::UserIsAdmin);
}
self.banned_users.insert(acct.to_owned());
// Banned user is also kicked
change |= self.member_users.remove(acct);
change |= self.banned_users.insert(acct.to_owned());
} else {
self.banned_users.remove(acct);
change |= self.banned_users.remove(acct);
}
if change {
self.mark_dirty();
}
Ok(())
}
pub(crate) fn ban_server(&mut self, server: &str, ban: bool) -> Result<(), GroupError> {
if ban {
let changed = if ban {
for acct in &self.admin_users {
let acct_server = acct_to_server(acct);
if acct_server == server {
return Err(GroupError::AdminsOnServer);
}
}
self.banned_servers.insert(server.to_owned());
self.banned_servers.insert(server.to_owned())
} else {
self.banned_servers.remove(server);
self.banned_servers.remove(server)
};
if changed {
self.mark_dirty();
}
self.mark_dirty();
Ok(())
}
pub(crate) fn add_tag(&mut self, tag: &str) {
self.group_tags.insert(tag.to_string());
self.mark_dirty();
if self.group_tags.insert(tag.to_string()) {
self.mark_dirty();
}
}
pub(crate) fn remove_tag(&mut self, tag: &str) {
self.group_tags.remove(tag);
self.mark_dirty();
if self.group_tags.remove(tag) {
self.mark_dirty();
}
}
pub(crate) fn is_tag_followed(&self, tag: &str) -> bool {
@@ -245,8 +265,10 @@ impl GroupConfig {
}
pub(crate) fn set_member_only(&mut self, member_only: bool) {
if self.member_only != member_only {
self.mark_dirty();
}
self.member_only = member_only;
self.mark_dirty();
}
pub(crate) fn is_member_only(&self) -> bool {
+4 -4
View File
@@ -8,7 +8,7 @@ use tokio::sync::RwLock;
use data::{Config, GroupConfig};
use crate::error::GroupError;
use crate::group_handle::GroupHandle;
use crate::group_handler::GroupHandle;
use std::time::Duration;
pub(crate) mod data;
@@ -162,11 +162,11 @@ 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> {
debug!("Locking mutex");
trace!("Locking mutex");
if let Ok(mut data) = tokio::time::timeout(Duration::from_secs(1), self.data.write()).await {
debug!("Locked");
trace!("Locked");
data.set_group_config(config);
debug!("Writing file");
trace!("Writing file");
self.persist(&data).await?;
} else {
error!("DEADLOCK? Timeout waiting for data RW Lock in settings store");