idmgr helper fns

This commit is contained in:
ModulatingForce 2024-02-18 19:23:50 -05:00
parent e4b20624f8
commit c137194c79

View file

@ -1104,6 +1104,43 @@ impl IdentityManager {
}
}
async fn add_role(&self, trgchatter:String,trg_role:UserRole)
{
let mut srulock = self.special_roles_users.write().await;
let mut usrrolelock = srulock
.get_mut(&trgchatter)
.expect("Error retrieving roles")
.write().await;
usrrolelock.push(trg_role);
}
async fn remove_role(&self, trgchatter:String,trg_role:UserRole)
{
let mut srulock = self.special_roles_users.write().await;
let mut usrrolelock = srulock
.get_mut(&trgchatter)
.expect("Error retrieving roles")
.write().await;
if let Some(indx) = usrrolelock.iter().position(|value| *value == trg_role){
usrrolelock.swap_remove(indx);
//return ChangeResult::Success("Demoted successfully".to_string())
}
}
async fn affirm_chatter_in_db(&self, trgchatter:String)
{
let mut srulock = self.special_roles_users.write().await;
srulock.entry(trgchatter.clone()).or_insert(Arc::new(RwLock::new(vec![])));
botinstance::botlog::trace(&format!("Ensuring User in Roles {:?}",srulock.entry(trgchatter.clone())),
Some("IdentityManager > affirm_chatter_in_db()".to_string()), None);
Log::flush();
}
// [ ] Maybe I should create a can_user_run version that simply takes PrvMsg, but then calls can_user_run directly
// pub fn can_user_run_PRVMSG(self,msg:&PrivmsgMessage,cmdreqroles:Vec<UserRole>) -> Result<Permissible,Box<dyn Error>>
@ -1298,15 +1335,16 @@ impl IdentityManager {
botinstance::botlog::trace("Read lock on : Special_Roles_User",
Some("identity.rs > can_user_run()".to_string()), None);
{
// {
// If target user doesn't exist in special_roles_users , add with blank vector roles
let mut srulock = self.special_roles_users.write().await;
srulock.entry(usr.clone()).or_insert(Arc::new(RwLock::new(vec![])));
botinstance::botlog::trace(&format!("Ensuring Chatter in Roles {:?}",srulock.entry(usr.clone())),
Some("identity.rs > promote()".to_string()), None);
Log::flush();
}
// // If target user doesn't exist in special_roles_users , add with blank vector roles
// let mut srulock = self.special_roles_users.write().await;
// srulock.entry(usr.clone()).or_insert(Arc::new(RwLock::new(vec![])));
// botinstance::botlog::trace(&format!("Ensuring Chatter in Roles {:?}",srulock.entry(usr.clone())),
// Some("identity.rs > promote()".to_string()), None);
// Log::flush();
// }
self.affirm_chatter_in_db(usr.clone()).await;
let mut roleslock = roleslock.write().await;
match (*roleslock).get(&usr.to_lowercase()) {
@ -1482,15 +1520,18 @@ impl IdentityManager {
// (*authusrroles_mut).push(UserRole::Mod(channel.clone()));
authusrroles.push(UserRole::Mod(channel.clone()));
let mut srulock = self.special_roles_users.write().await;
srulock
.get_mut(&trgchatter)
.expect("Error getting roles")
// !! [ ] Unsure what happens if promoting a chatter that doesn't exist at
.write().await
.push(UserRole::Mod(channel.clone()));
// let mut srulock = self.special_roles_users.write().await;
// srulock
// .get_mut(&trgchatter)
// .expect("Error getting roles")
// // !! [ ] Unsure what happens if promoting a chatter that doesn't exist at
// .write().await
// .push(UserRole::Mod(channel.clone()));
self.add_role(trgchatter.clone(), UserRole::Mod(channel.clone())).await;
}
_ => (),
}
} // mut block
@ -1531,28 +1572,31 @@ impl IdentityManager {
return ChangeResult::NoChange("Already has the role".to_string());
} else {
{
let mut srulock = self.special_roles_users.write().await;
// {
// let mut srulock = self.special_roles_users.write().await;
srulock.entry(trgchatter.clone()).or_insert(Arc::new(RwLock::new(vec![])));
botinstance::botlog::trace(&format!("Ensuring Target Chatter in Roles > {:?}",srulock.entry(trgchatter.clone())),
Some("identity.rs > promote()".to_string()), None);
Log::flush();
}
// srulock.entry(trgchatter.clone()).or_insert(Arc::new(RwLock::new(vec![])));
// botinstance::botlog::trace(&format!("Ensuring Target Chatter in Roles > {:?}",srulock.entry(trgchatter.clone())),
// Some("identity.rs > promote()".to_string()), None);
// Log::flush();
// }
self.affirm_chatter_in_db(trgchatter.clone()).await;
{
// {
let mut srulock = self.special_roles_users.write().await;
// let mut srulock = self.special_roles_users.write().await;
srulock
.get_mut(&trgchatter)
.expect("Error getting roles for the user")
.write().await
.push(UserRole::BotAdmin); // <-- Adds the specific role
botinstance::botlog::trace(&format!("Inserting Role > {:?}",srulock.entry(trgchatter.clone())),
Some("identity.rs > promote()".to_string()), None);
Log::flush();
}
// srulock
// .get_mut(&trgchatter)
// .expect("Error getting roles for the user")
// .write().await
// .push(UserRole::BotAdmin); // <-- Adds the specific role
// botinstance::botlog::trace(&format!("Inserting Role > {:?}",srulock.entry(trgchatter.clone())),
// Some("identity.rs > promote()".to_string()), None);
// Log::flush();
// }
self.add_role(trgchatter.clone(), UserRole::BotAdmin).await;
return ChangeResult::Success("Promotion Successful".to_string());
@ -1598,27 +1642,30 @@ impl IdentityManager {
|| authusrroles.contains(&UserRole::BotAdmin)
{
{
// {
// If target user doesn't exist in special_roles_users , add with blank vector roles
let mut srulock = self.special_roles_users.write().await;
srulock.entry(trgchatter.clone()).or_insert(Arc::new(RwLock::new(vec![])));
botinstance::botlog::trace(&format!("Ensuring Chatter in Roles {:?}",srulock.entry(trgchatter.clone())),
Some("identity.rs > promote()".to_string()), None);
Log::flush();
}
{
// promote target after
let mut srulock = self.special_roles_users.write().await;
srulock
.get_mut(&trgchatter)
.expect("Error getting roles")
.write().await
.push(UserRole::Mod(trg_chnl.clone())); // Role to Add
botinstance::botlog::trace(&format!("Adding Roles to Chatter {:?}",srulock.entry(trgchatter.clone())),
Some("identity.rs > promote()".to_string()), None);
Log::flush();
}
// // If target user doesn't exist in special_roles_users , add with blank vector roles
// let mut srulock = self.special_roles_users.write().await;
// srulock.entry(trgchatter.clone()).or_insert(Arc::new(RwLock::new(vec![])));
// botinstance::botlog::trace(&format!("Ensuring Chatter in Roles {:?}",srulock.entry(trgchatter.clone())),
// Some("identity.rs > promote()".to_string()), None);
// Log::flush();
// }
self.affirm_chatter_in_db(trgchatter.clone()).await;
// {
// // promote target after
// let mut srulock = self.special_roles_users.write().await;
// srulock
// .get_mut(&trgchatter)
// .expect("Error getting roles")
// .write().await
// .push(UserRole::Mod(trg_chnl.clone())); // Role to Add
// botinstance::botlog::trace(&format!("Adding Roles to Chatter {:?}",srulock.entry(trgchatter.clone())),
// Some("identity.rs > promote()".to_string()), None);
// Log::flush();
// }
self.add_role(trgchatter.clone(), UserRole::Mod(trg_chnl.clone())).await;
return ChangeResult::Success(String::from("Promotion Successful"));
@ -1645,39 +1692,47 @@ impl IdentityManager {
|| authusrroles.contains(&UserRole::BotAdmin)
{
{ // Inserts user if doesn't exist
let mut srulock = self.special_roles_users.write().await;
srulock.entry(trgchatter.clone()).or_insert(Arc::new(RwLock::new(vec![])));
botinstance::botlog::trace(&format!("Ensuring User in Roles {:?}",srulock.entry(trgchatter.clone())),
Some("identity.rs > promote()".to_string()), None);
Log::flush();
}
{ // Adds the requested role for the user
let mut srulock = self.special_roles_users.write().await;
srulock
.get_mut(&trgchatter)
.expect("Error getting roles")
// !! [ ] Unsure what happens if promoting a chatter that doesn't exist at
.write().await
.push(UserRole::SupMod(trg_chnl.clone()));
botinstance::botlog::trace(&format!("Adding Required Role > {:?}",srulock.entry(trgchatter.clone())),
Some("identity.rs > promote()".to_string()), None);
Log::flush();
}
{ // Removes the lower role (mod) from the user
let mut srulock = self.special_roles_users.write().await;
let mut uroleslock = srulock
.get_mut(&trgchatter)
.expect("Error getting roles")
.write().await;
if let Some(indx) = uroleslock.iter().position(|value| *value == UserRole::Mod(trg_chnl.clone())){
uroleslock.swap_remove(indx);
}
// { // Inserts user if doesn't exist
// let mut srulock = self.special_roles_users.write().await;
// srulock.entry(trgchatter.clone()).or_insert(Arc::new(RwLock::new(vec![])));
// botinstance::botlog::trace(&format!("Ensuring User in Roles {:?}",srulock.entry(trgchatter.clone())),
// Some("identity.rs > promote()".to_string()), None);
// Log::flush();
// }
self.affirm_chatter_in_db(trgchatter.clone()).await;
botinstance::botlog::trace(&format!("Removing lower role > {:?}",uroleslock),
Some("identity.rs > promote()".to_string()), None);
Log::flush();
}
// { // Adds the requested role for the user
// let mut srulock = self.special_roles_users.write().await;
// srulock
// .get_mut(&trgchatter)
// .expect("Error getting roles")
// // !! [ ] Unsure what happens if promoting a chatter that doesn't exist at
// .write().await
// .push(UserRole::SupMod(trg_chnl.clone()));
// botinstance::botlog::trace(&format!("Adding Required Role > {:?}",srulock.entry(trgchatter.clone())),
// Some("identity.rs > promote()".to_string()), None);
// Log::flush();
// }
self.add_role(trgchatter.clone(), UserRole::SupMod(trg_chnl.clone())).await;
// { // Removes the lower role (mod) from the user
// let mut srulock = self.special_roles_users.write().await;
// let mut uroleslock = srulock
// .get_mut(&trgchatter)
// .expect("Error getting roles")
// .write().await;
// if let Some(indx) = uroleslock.iter().position(|value| *value == UserRole::Mod(trg_chnl.clone())){
// uroleslock.swap_remove(indx);
// }
// botinstance::botlog::trace(&format!("Removing lower role > {:?}",uroleslock),
// Some("identity.rs > promote()".to_string()), None);
// Log::flush();
// }
self.remove_role(trgchatter, UserRole::Mod(trg_chnl.clone())).await;
return ChangeResult::Success(String::from("Promotion Successful"));
@ -2097,13 +2152,16 @@ impl IdentityManager {
// (*authusrroles_mut).push(UserRole::Mod(channel.clone()));
authusrroles.push(UserRole::Mod(channel.clone()));
let mut srulock = self.special_roles_users.write().await;
srulock
.get_mut(&trgchatter)
.expect("Error getting roles")
// !! [ ] Unsure what happens if promoting a chatter that doesn't exist at
.write().await
.push(UserRole::Mod(channel.clone()));
// [ ] below pushes mod to authorizer
// let mut srulock = self.special_roles_users.write().await;
// srulock
// .get_mut(&trgchatter)
// .expect("Error getting roles")
// // !! [ ] Unsure what happens if promoting a chatter that doesn't exist at
// .write().await
// .push(UserRole::Mod(channel.clone()));
self.add_role(authorizer.clone(), UserRole::Mod(channel.clone())).await;
}
_ => (),
@ -2129,34 +2187,45 @@ impl IdentityManager {
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())
}
// // [ ] Below removes Mod from trgchatter
// 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())
// }
self.remove_role(trgchatter.clone(), UserRole::Mod(channel.clone())).await;
return ChangeResult::Success("Demoted successfully".to_string())
}
// [x] 4b. Authorizers who are BotAdmin, Broadcaster can demote a SupMod
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::Mod(channel.clone())); // pushes Mod , and removes SupMod
if let Some(indx) = usrrolelock.iter().position(|value| *value == UserRole::SupMod(channel.clone())){
usrrolelock.swap_remove(indx);
return ChangeResult::Success("Demoted successfully".to_string())
}
{
// [ ] For Trgchatter, below pushes Mod UserRole and removes SupMod
// 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::Mod(channel.clone())); // pushes Mod , and removes SupMod
// if let Some(indx) = usrrolelock.iter().position(|value| *value == UserRole::SupMod(channel.clone())){
// usrrolelock.swap_remove(indx);
// return ChangeResult::Success("Demoted successfully".to_string())
// }
self.add_role(trgchatter.clone(), UserRole::Mod(channel.clone())).await;
self.remove_role(trgchatter.clone(), UserRole::SupMod(channel.clone())).await;
return ChangeResult::Success("Demoted successfully".to_string());
}
// [x] 4c. When Target chatter isnt a Mod or SupMod to demote
else if !trgusrroles.contains(&UserRole::Mod(channel.clone())) &&
!trgusrroles.contains(&UserRole::SupMod(channel.clone())) {