From a825544590f7369ad377d5b5e1882b14e4ccd2e8 Mon Sep 17 00:00:00 2001 From: ModulatingForce <116608425+modulatingforce@users.noreply.github.com> Date: Wed, 14 Feb 2024 09:21:50 -0500 Subject: [PATCH] (init) id > demote --- src/core/identity.rs | 88 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 82 insertions(+), 6 deletions(-) diff --git a/src/core/identity.rs b/src/core/identity.rs index 1ae9e60..3401bc1 100644 --- a/src/core/identity.rs +++ b/src/core/identity.rs @@ -411,7 +411,7 @@ pub async fn init(mgr:Arc) botinstance::botlog::debug("Called cmd demote", Some("identity.rs > cmd_demote()".to_string()), Some(&msg)); Log::flush(); - } + // -- If the BotCommand.command was called (e.g., demote) & required roles were validated OUTSIDE of this call // , this is the current function body to execute @@ -440,6 +440,8 @@ pub async fn init(mgr:Arc) */ + } + let tempcomm = BotCommand { module : BotModule(String::from("identity")), command : String::from("getroles"), // command call name @@ -1086,7 +1088,12 @@ impl IdentityManager { } // pub async fn promote(&mut self,trgchatter:String,channel:Option,trg_role:Option) -> ChangeResult { - pub async fn promote(&self,trgchatter:String,channel:Option,trg_role:Option) -> ChangeResult { + pub async fn promote(&self, + trgchatter:String, + channel:Option, + trg_role:Option) -> ChangeResult + { + botinstance::botlog::trace(&format!("IN VARS for promote() : Target Chatter : {} ; Target Channel : {:?} ; Targer Role {:?}", trgchatter,channel,trg_role), @@ -1291,8 +1298,14 @@ impl IdentityManager { return ChangeResult::Success(String::from("Promotion Successful")); }, - Some(_) => (), - None => (), + Some(_) => { + botinstance::botlog::warn(&format!("Runtime reached undeveloped code"), + Some("identity.rs > promote()".to_string()), None); + }, + None => { + botinstance::botlog::warn(&format!("Runtime reached undeveloped code"), + Some("identity.rs > promote()".to_string()), None); + }, } @@ -1362,9 +1375,72 @@ impl IdentityManager { ChangeResult::Success(String::from("TEST > Promotion Successful")) } - pub fn demote(self,trgchatter:String,channel:Option,trgRole:Option) -> ChangeResult { - ChangeResult::Success(String::from("TEST > Promotion Successful")) + pub async fn demote(&self, + authorizer:String, + trgchatter:String, + channel:Option, + trg_role:Option) -> ChangeResult + { + botinstance::botlog::trace(&format!("IN VARS for demote() : Authorizer : {:?} ; Target Chatter : {} ; Target Channel : {:?} ; Targer Role {:?}", + authorizer,trgchatter,channel,trg_role), + Some("identity.rs > demote()".to_string()), None); + Log::flush(); + + /* + Check authorizer roles (if any) for the target channel + Check Targer User roles (if any) for the target channel + Target Channel may be NONE in the case of Non-Channel related roles (FUTURE ENH) + + Use the roles of the above to determine whether the authorizer can demote the target user or not + */ + + if let Some(channel) = channel { + let authusrroles = self.getspecialuserroles( + authorizer.to_lowercase().clone(), + Some(channel.clone())) + .await; + // let authusrroles = authusrroles; + + let trgusrroles = self.getspecialuserroles( + trgchatter.to_lowercase().clone(), + Some(channel.clone())) + .await; + + if ( authusrroles.contains(&UserRole::BotAdmin) || + authusrroles.contains(&UserRole::Broadcaster) || + authusrroles.contains(&UserRole::SupMod(channel.clone())) ) && + trgusrroles.contains(&UserRole::Mod(channel.clone())) + { + let mut srulock = self.special_roles_users.write().await; + let mut usrrolelock = srulock + .get_mut(&trgchatter) + .expect("Error getting roles") + .write().await; + if let Some(indx) = usrrolelock.iter().position(|value| *value == UserRole::Mod(channel.clone())){ + usrrolelock.swap_remove(indx); + return ChangeResult::Success("Demoted successfully".to_string()) + } + } + else if ( authusrroles.contains(&UserRole::BotAdmin) || + authusrroles.contains(&UserRole::Broadcaster) ) && + trgusrroles.contains(&UserRole::SupMod(channel.clone())) + { + let mut srulock = self.special_roles_users.write().await; + let mut usrrolelock = srulock + .get_mut(&trgchatter) + .expect("Error getting roles") + .write().await; + usrrolelock.push(UserRole::SupMod(channel.clone())); + if let Some(indx) = usrrolelock.iter().position(|value| *value == UserRole::SupMod(channel.clone())){ + usrrolelock.swap_remove(indx); + return ChangeResult::Success("Demoted successfully".to_string()) + } + } + } + + + ChangeResult::Failed(String::from("Did not meet criteria to demote succesfully")) } // pub async fn getspecialuserroles(&self,chattername:String,channel:Option) -> Option>>> {