v0.2, refactor, improve some messages, fix lints
This commit is contained in:
+47
-25
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user