2024-01-29 06:18:27 -05:00
2024-02-04 14:28:37 -05:00
use std ::borrow ::Borrow ;
2024-01-29 06:18:27 -05:00
use std ::collections ::HashMap ;
2024-01-29 12:41:26 -05:00
use std ::error ::Error ;
2024-01-29 11:09:33 -05:00
use crate ::core ::botmodules ::{ ModulesManager , Listener , BotModule , BotActionTrait , BotCommand } ;
use crate ::core ::botmodules ::bot_actions ::actions_util ;
2024-02-04 14:28:37 -05:00
use crate ::core ::botinstance ::{ self , BotInstance } ;
use futures ::lock ::Mutex ;
2024-01-29 22:57:07 -05:00
use twitch_irc ::message ::{ Badge , PrivmsgMessage } ;
2024-01-29 11:09:33 -05:00
2024-01-29 12:41:26 -05:00
use crate ::core ::botmodules ::ChType ;
2024-02-04 14:28:37 -05:00
use crate ::core ::botinstance ::ArcBox ;
use std ::rc ::Rc ;
use std ::cell ::RefCell ;
use std ::sync ::{ Arc , RwLock } ;
2024-02-12 01:25:12 -05:00
use super ::botmodules ::bot_actions ::actions_util ::BotAR ;
2024-01-29 06:18:27 -05:00
fn adminvector ( ) -> Vec < String > {
vec! [ String ::from ( " ModulatingForce " ) ]
}
2024-02-04 14:28:37 -05:00
// pub fn init(mgr:&mut ModulesManager)
2024-02-12 01:25:12 -05:00
pub fn init ( mgr :Arc < ModulesManager > )
2024-01-29 11:09:33 -05:00
{
2024-02-12 01:25:12 -05:00
let a = actions_util ::asyncbox ( cmd_promote ) ;
2024-01-29 11:09:33 -05:00
BotCommand {
module : BotModule ( String ::from ( " identity " ) ) ,
command : String ::from ( " promote " ) , // command call name
alias : vec ! [ ] , // String of alternative names
exec_body : actions_util ::asyncbox ( cmd_promote ) ,
help : String ::from ( " promote " ) ,
2024-01-30 09:13:59 -05:00
required_roles : vec ! [
UserRole ::Mod ( ChType ::Channel ( String ::new ( ) ) ) ,
UserRole ::SupMod ( ChType ::Channel ( String ::new ( ) ) ) ,
UserRole ::Broadcaster ,
UserRole ::BotAdmin ,
] ,
2024-02-12 01:25:12 -05:00
} . add_to_modmgr ( Arc ::clone ( & mgr ) ) ;
2024-01-29 11:09:33 -05:00
2024-02-12 01:25:12 -05:00
// async fn cmd_promote(mut bot:Arc<Mutex<BotInstance>>,msg:PrivmsgMessage)
// async fn cmd_promote(mut bot:&BotInstance,msg:PrivmsgMessage) -> &BotInstance
async fn cmd_promote ( bot :BotAR , msg :PrivmsgMessage ) -> ( )
2024-02-04 14:28:37 -05:00
{
2024-01-29 11:09:33 -05:00
//println!("(#{}) {}: {}", msg.channel_login, msg.sender.name, msg.message_text);
2024-01-29 22:57:07 -05:00
println! ( " Called cmd promote " ) ;
2024-01-29 12:41:26 -05:00
2024-01-31 21:30:08 -05:00
// -- If the BotCommand.command was called (e.g., promote) & required roles were validated OUTSIDE of this call
// , this is the current function body to execute
2024-01-30 15:07:40 -05:00
/*
- ` promote ` / ` demote `
- [ ] ` SupMod ` & ` Broadcaster ` & ` BotAdmin ` can run
- [ ] ` UserRole ` s that can run , can
- [ ] run ` promote ` on a regular ` Chatter ` to make them a ` Mod `
- [ ] run ` demote ` on a ` Mod ` to make them a ` Chatter `
- [ ] Only ` BotAdmin ` can :
- [ ] target themselves to ` promote ` / ` demote ` , in the case that they want to make themselves either a ` Mod ` or ` SupMod ` for the channel temporarily
- [ ] ` promote admin < Chatter > ` to assign them ` BotAdmin ` role
- ` [ ] Broadcaster ` & ` BotAdmin ` can ` demote ` a ` SupMod ` to make them a ` Mod `
* /
2024-01-31 21:30:08 -05:00
/*
Usage :
promote < user > < channel >
demote < user > < channel >
promote admin < user >
* /
2024-02-04 14:28:37 -05:00
//let bot = Rcbot;
2024-02-01 08:40:09 -05:00
println! ( " {} " , msg . message_text ) ;
let mut argv = msg . message_text . split ( " " ) ;
argv . next ( ) ; // Skip the command name
let arg1 = argv . next ( ) ;
let arg2 = argv . next ( ) ;
let mut sender_badge :Option < ChatBadge > = None ;
for b in & msg . badges {
if b . name = = " moderator " {
sender_badge = Some ( ChatBadge ::Mod ) ;
} else if b . name = = " broadcaster " {
sender_badge = Some ( ChatBadge ::Broadcaster ) ;
}
}
match arg1 {
2024-02-04 14:28:37 -05:00
Some ( a1 ) if a1 = = String ::from ( " admin " ) = > {
2024-02-01 08:40:09 -05:00
// - BotAdmins can promote admin to give BotAdmin UserRole
2024-02-04 14:28:37 -05:00
//let mut bot = Rc::clone(&bot);
// let a = bot.botmgrs.identity.getspecialuserroles(msg.sender.name.to_lowercase(), Some(ChType::Channel(msg.channel_login.to_lowercase())));
// let a = Rc::try_unwrap(bot).ok().unwrap().into_inner().botmgrs.identity.getspecialuserroles(msg.sender.name.to_lowercase(), Some(ChType::Channel(msg.channel_login.to_lowercase())));
// let a = Rc::try_unwrap(bot.clone()).ok().unwrap().into_inner().botmgrs.identity.getspecialuserroles(msg.sender.name.to_lowercase(), Some(ChType::Channel(msg.channel_login.to_lowercase())));
// let a = Rc::try_unwrap(bot).ok().unwrap()
// // .borrow_mut()
// .into_inner()
// .botmgrs
// .identity
// .getspecialuserroles(msg.sender.name.to_lowercase(),
// Some(ChType::Channel(msg.channel_login.to_lowercase())));
// let p = Rc::try_unwrap(bot.clone())
// let p = Arc::try_unwrap(bot.clone())
// .ok()
// .unwrap()
// //.into_inner()
// //.to_owned()
// // .get_mut()
// .into_inner();
// // .ok()
// // .unwrap();
// // let p = p.ok().unwrap();
// let p = bot.lock().await.get_identity();
// let p = p.lock().await;
// let ta = p.getspecialuserroles(String::from("Hello"), Some(ChType::Channel(msg.channel_login.to_lowercase()))).await;
// // let ta = *ta.await;
// let ta = *ta.lock().await;
// if let Some(a) = ta {
2024-02-12 01:25:12 -05:00
// let p = bot.lock().await.get_identity();
// let mut p = p.lock().await;
// let ta = p.getspecialuserroles(String::from("Hello"), Some(ChType::Channel(msg.channel_login.to_lowercase()))).await;
let botlock = bot . read ( ) . await ;
let ta = botlock . get_identity ( ) . read ( ) . await . getspecialuserroles ( String ::from ( " Hello " ) , Some ( ChType ::Channel ( msg . channel_login . to_lowercase ( ) ) ) ) . await ;
2024-02-04 14:28:37 -05:00
if let Some ( a ) = ta {
2024-02-01 08:40:09 -05:00
if a . contains ( & UserRole ::BotAdmin ) {
println! ( " BotAdmin allowed to promote admin " ) ;
2024-02-04 14:28:37 -05:00
// let bota = Rc::clone(&bot);
// let bota = Rc::get_mut(&bota);
// match bota.botmgrs.identity.promote(msg.sender.name.to_lowercase(), None, Some(UserRole::BotAdmin)) {
//let mut bot = Rc::clone(&bot);
// let bot = Rc::<&botinstance::BotInstance>::get_mut(&mut bot);
// let mut bot = Rc::make_mut(&mut bot);
// let mut bot = Rc::make_mut(&bot);
// match Rc::<&botinstance::BotInstance>::get_mut(bot) {
// Some(bot) => {
// match bot.botmgrs.identity.promote(msg.sender.name.to_lowercase(), None, Some(UserRole::BotAdmin)) {
// // Success(_) => {
// // ;
// // },
// ChangeResult::Success(a) => println!("Succesfully promoted : {a} ;"),
// ChangeResult::Failed(a) => println!("Failed to promote : {a} ; "),
// ChangeResult::NoChange(a) => println!("No Changes Made : {a} ; "),
// }
// },
// None => (),
// }
//let bot = Rc::<botinstance::BotInstance>::make_mut(bot);
// match Rc::<IdentityManager>::make_mut(&mut Rc::new(bot.botmgrs.identity)).promote(msg.sender.name.to_lowercase(), None, Some(UserRole::BotAdmin)) {
// match Rc::try_unwrap(bot.clone()).ok().unwrap().into_inner().botmgrs.identity.promote(msg.sender.name.to_lowercase(), None, Some(UserRole::BotAdmin)) {
// match Arc::try_unwrap(bot.clone()).ok().unwrap().into_inner().ok().unwrap().botmgrs.identity.promote(msg.sender.name.to_lowercase(), None, Some(UserRole::BotAdmin)) {
// match Arc::try_unwrap(bot.clone()).ok().unwrap().into_inner().botmgrs.identity.promote(msg.sender.name.to_lowercase(), None, Some(UserRole::BotAdmin)) {
// match bot.read().ok().unwrap().get_identity().promote(msg.sender.name.to_lowercase(), None, Some(UserRole::BotAdmin)) {
// let a = bot.get_mut();
// match a.get_identity().promote(msg.sender.name.to_lowercase(), None, Some(UserRole::BotAdmin)) {
// let a = (*bot).clone();
{
// let a = bot.lock().await.get_botmgrs().clone();
// let a = bot.lock().await;
// let mut mutex = Arc::clone(&bot);
// match mutex.get_mut().get_identity().promote(msg.sender.name.to_lowercase(), None, Some(UserRole::BotAdmin)) {
// slet ee = ArcBox(bot);
// println!("tester: {:?}",(*bot).lock().await.get_prefix());
// let mutex = Arc::clone(&bot);
// let mut mutex2 = Arc::new(*mutex).into_inner().get_identity();
// let mut mutex2 = Arc::clone(&mutex).into_inner().get_identity();
// let a = (*bot).lock().await.get_identity();
// let mut a = a.lock().await;
2024-02-12 01:25:12 -05:00
// let p = bot.lock().await.get_identity();
// let mut p = p.lock().await;
// let ta = p.promote(msg.sender.name.to_lowercase(), None, Some(UserRole::BotAdmin)).await;
let botlock = Arc ::clone ( & bot . read ( ) . await . get_identity ( ) ) ;
let idlock = botlock . write ( ) . await ;
// let mut idlock = *idlock;
let ta = idlock . promote ( msg . sender . name . to_lowercase ( ) , None , Some ( UserRole ::BotAdmin ) ) . await ;
2024-02-04 14:28:37 -05:00
match ta {
// Success(_) => {
// ;
// },
ChangeResult ::Success ( a ) = > println! ( " Succesfully promoted : {a} ; " ) ,
ChangeResult ::Failed ( a ) = > println! ( " Failed to promote : {a} ; " ) ,
ChangeResult ::NoChange ( a ) = > println! ( " No Changes Made : {a} ; " ) ,
}
2024-02-01 08:40:09 -05:00
}
}
}
} ,
Some ( _ ) = > {
// -
} ,
_ = > ( ) ,
}
// match String::from(arg1) {
// a if a == String::from("admin") => (),
// _ => (),
// }
// match argv[1] {
// String::from("admin") => (),
// }
let arg2 = argv . next ( ) ;
let targetchnl = arg2 ;
2024-02-12 01:25:12 -05:00
// bot
2024-01-30 15:07:40 -05:00
2024-01-29 11:09:33 -05:00
}
BotCommand {
module : BotModule ( String ::from ( " identity " ) ) ,
command : String ::from ( " demote " ) , // command call name
alias : vec ! [ ] , // String of alternative names
exec_body : actions_util ::asyncbox ( cmd_demote ) ,
help : String ::from ( " demote " ) ,
2024-01-30 09:13:59 -05:00
required_roles : vec ! [
UserRole ::Mod ( ChType ::Channel ( String ::new ( ) ) ) ,
UserRole ::SupMod ( ChType ::Channel ( String ::new ( ) ) ) ,
UserRole ::Broadcaster ,
UserRole ::BotAdmin ,
] ,
2024-02-12 01:25:12 -05:00
} . add_to_modmgr ( Arc ::clone ( & mgr ) ) ;
2024-01-29 11:09:33 -05:00
2024-02-12 01:25:12 -05:00
// async fn cmd_demote(mut _chat:Arc<Mutex<BotInstance>>,_msg:PrivmsgMessage) {
async fn cmd_demote ( mut _chat :BotAR , _msg :PrivmsgMessage ) {
2024-01-29 22:57:07 -05:00
println! ( " Called cmd demote " ) ;
2024-01-29 11:09:33 -05:00
}
2024-01-31 21:30:08 -05:00
BotCommand {
module : BotModule ( String ::from ( " identity " ) ) ,
command : String ::from ( " getroles " ) , // command call name
alias : vec ! [ ] , // String of alternative names
exec_body : actions_util ::asyncbox ( getroles ) ,
help : String ::from ( " getroles " ) ,
required_roles : vec ! [
UserRole ::Mod ( ChType ::Channel ( String ::new ( ) ) ) ,
UserRole ::SupMod ( ChType ::Channel ( String ::new ( ) ) ) ,
UserRole ::Broadcaster ,
UserRole ::BotAdmin ,
] ,
2024-02-12 01:25:12 -05:00
} . add_to_modmgr ( Arc ::clone ( & mgr ) ) ;
2024-01-31 21:30:08 -05:00
2024-02-12 01:25:12 -05:00
// async fn getroles(bot:Arc<Mutex<BotInstance>>,msg:PrivmsgMessage) {
async fn getroles ( bot :BotAR , msg :PrivmsgMessage ) {
2024-01-31 21:30:08 -05:00
println! ( " Called cmd getroles " ) ;
/*
Usage
getroles < user > < Channel >
- If channel is provided , provide roles for that channel specifically
* /
// IN other code areas , I see this
// ServerMessage::Privmsg(msg) => {
// println!("(#{}) {}: {}", msg.channel_login, msg.sender.name, msg.message_text);
2024-02-01 08:40:09 -05:00
// println!("{}",msg.message_text);
2024-01-31 21:30:08 -05:00
let mut argv = msg . message_text . split ( " " ) ;
// for v in argv {
// println!("args : {v}");
// }
2024-02-01 08:40:09 -05:00
argv . next ( ) ; // Skip the command name
2024-01-31 21:30:08 -05:00
let arg1 = argv . next ( ) ;
// if arg == None {
// return ; // Do nothing if no arguments
// }
let targetuser = match arg1 {
None = > return , // exit if no arguments
Some ( arg ) = > arg ,
} ;
// match String::from(arg1) {
// a if a == String::from("admin") => (),
// _ => (),
// }
// match argv[1] {
// String::from("admin") => (),
// }
let arg2 = argv . next ( ) ;
let targetchnl = arg2 ;
2024-02-04 14:28:37 -05:00
// // let a = bot.read().ok().unwrap().get_identity();
// let a = bot.lock().await;
// // let a = a.lock().await;
// // let a = a.get_identity().await;
// let a = a.botmgrs;
// // let a = *(*a).lock().await;
// let a = *a.lock().await;
// let a = a.identity;
// let a = *a.lock().await;
// let a = bot.clone();
// let a = a.into_inner();
// let a = a.botmgrs;
// let a = a.into_inner();
// let a = a.identity;
// let a = a.into_inner();
2024-02-12 01:25:12 -05:00
// let a = bot.clone();
2024-02-04 14:28:37 -05:00
// let a = a.lock().await;
2024-02-12 01:25:12 -05:00
// let a = a.get_identity();
// let a = a.lock().await;
// let a = bot.get_identity();
let botlock = bot . read ( ) . await ;
let idlock = botlock . get_identity ( ) ;
let idlock = idlock . read ( ) . await ;
2024-02-04 14:28:37 -05:00
let sproles = match targetchnl {
2024-01-31 21:30:08 -05:00
None = > {
2024-02-04 14:28:37 -05:00
// let bot = Rc::clone(&bot);
//let bot = Arc::clone(&bot);
// let a = bot.botmgrs.identity.getspecialuserroles(String::from(targetuser),None);
// let a = Arc::try_unwrap(bot).ok().unwrap().into_inner().ok().unwrap();
// let a = Arc::try_unwrap(bot.clone()).ok().unwrap().into_inner();
// let a = a.botmgrs.identity.getspecialuserroles(String::from(targetuser),None);
// let a = a.ok().getspecialuserroles(String::from(targetuser),None);
// let a = bot.read().ok().unwrap().rIdentity().getspecialuserroles(String::from(targetuser),None);
// println!("Retrieved User Roles >> {:?}",a);
2024-02-12 01:25:12 -05:00
// let a = idlock.read().await;
idlock . getspecialuserroles ( String ::from ( targetuser ) , None ) . await
2024-01-31 21:30:08 -05:00
} ,
Some ( targetchnl ) = > {
2024-02-04 14:28:37 -05:00
// let bot = Rc::clone(&bot);
// let bot = Arc::clone(&bot);
// let a = bot.botmgrs.identity.getspecialuserroles(String::from(targetuser), Some(ChType::Channel(String::from(targetchnl))));
// Arc::try_unwrap(boxed_bot.clone()).ok().unwrap().into_inner()
// let a = Arc::try_unwrap(bot).ok().unwrap().into_inner().ok().unwrap();
// let a = Arc::try_unwrap(bot.clone()).ok().unwrap().into_inner();
// let a = a.botmgrs.identity.getspecialuserroles(String::from(targetuser), Some(ChType::Channel(String::from(targetchnl))));
// let a = bot.read().ok().unwrap().rIdentity().getspecialuserroles(String::from(targetuser),None);
// println!("Retrieved User Roles >> {:?}",a);
// bot.read().ok().unwrap().rIdentity().getspecialuserroles(String::from(targetuser),None)
2024-02-12 01:25:12 -05:00
// let a = a.read().await;
idlock . getspecialuserroles ( String ::from ( targetuser ) , None ) . await
2024-02-04 14:28:37 -05:00
2024-01-31 21:30:08 -05:00
} ,
2024-02-04 14:28:37 -05:00
} ;
2024-01-31 21:30:08 -05:00
2024-02-04 14:28:37 -05:00
println! ( " Retrieved User Roles >> {:?} " , sproles ) ;
2024-01-31 21:30:08 -05:00
// let a = bot.identity.getuserroles(String::from("ModulatingForce"), Some(ChType::Channel(String::from("ModulatingForcebot"))));
// println!("{:?}",a);
}
2024-01-29 11:09:33 -05:00
}
2024-01-29 22:57:07 -05:00
// #[derive(Debug, PartialEq, Eq, Hash, Clone)]
// pub enum ChType {
// Channel(String),
// }
#[ derive(Debug, PartialEq, Eq , Clone) ]
2024-01-29 11:09:33 -05:00
pub enum UserRole {
2024-01-29 06:18:27 -05:00
Chatter ,
2024-01-29 13:08:35 -05:00
Mod ( ChType ) , // String specifies Channel
SupMod ( ChType ) , // String specifies Channel
2024-01-29 06:18:27 -05:00
Broadcaster ,
BotAdmin ,
2024-01-29 12:41:26 -05:00
2024-01-29 06:18:27 -05:00
}
2024-01-29 22:57:07 -05:00
pub enum Permissible {
2024-01-29 12:41:26 -05:00
Allow ,
Block
}
2024-01-31 18:36:23 -05:00
#[ derive(Clone) ]
2024-01-29 06:18:27 -05:00
pub struct IdentityManager {
2024-02-04 14:28:37 -05:00
// special_roles_users : HashMap<String,Vec<UserRole>>, // # <-- (!) This must be String instead of ChType because we're checking a User not a Channel
special_roles_users : Arc < Mutex < HashMap < String , Vec < UserRole > > > > , // # <-- (!) This must be String instead of ChType because we're checking a User not a Channel
2024-01-31 09:31:14 -05:00
// parent_mgr : Box<crate::core::botinstance::BotManagers>,
2024-01-31 21:30:08 -05:00
//parent_mgr : Option<Box<crate::core::botinstance::BotManagers>>,
2024-01-29 06:18:27 -05:00
}
2024-01-29 22:57:07 -05:00
pub enum ChatBadge {
2024-01-29 12:41:26 -05:00
Broadcaster ,
Mod ,
}
2024-01-29 06:18:27 -05:00
2024-01-31 21:30:08 -05:00
enum ChangeResult {
Success ( String ) ,
Failed ( String ) ,
NoChange ( String ) ,
}
2024-01-29 06:18:27 -05:00
impl IdentityManager {
pub fn init ( ) -> IdentityManager {
let mut a = HashMap ::new ( ) ;
for admn in adminvector ( ) {
2024-01-29 11:09:33 -05:00
a . insert ( admn . to_lowercase ( ) , vec! [ UserRole ::BotAdmin ] ) ;
2024-01-29 06:18:27 -05:00
} ;
IdentityManager {
2024-02-04 14:28:37 -05:00
special_roles_users : Arc ::new ( Mutex ::new ( a ) ) ,
2024-01-31 21:30:08 -05:00
//parent_mgr : None,
2024-01-29 06:18:27 -05:00
}
}
2024-01-29 12:41:26 -05:00
2024-01-29 22:57:07 -05:00
// [ ] 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>>
2024-02-04 14:28:37 -05:00
// pub fn can_user_run_PRVMSG(&self,msg:&PrivmsgMessage,cmdreqroles:Vec<UserRole>) -> Permissible
2024-02-12 01:25:12 -05:00
// pub async fn can_user_run_PRVMSG(self,msg:&PrivmsgMessage,cmdreqroles:Vec<UserRole>) -> Permissible
pub fn can_user_run_PRVMSG ( & self , msg :& PrivmsgMessage , cmdreqroles :Vec < UserRole > ) -> Permissible
2024-01-29 22:57:07 -05:00
{
// println!("(#{}) {}: {}", msg.channel_login, msg.sender.name, msg.message_text);
// [ ] Check what Badges in PrivmsgMessage
let mut sender_badge :Option < ChatBadge > = None ;
for b in & msg . badges {
if b . name = = " moderator " {
sender_badge = Some ( ChatBadge ::Mod ) ;
} else if b . name = = " broadcaster " {
sender_badge = Some ( ChatBadge ::Broadcaster ) ;
}
}
// if &msg.badges.contains(Badge{}) {
// }
if let Some ( sender_badge ) = sender_badge {
2024-01-31 18:36:23 -05:00
// return &self.can_user_run(msg.sender.name.to_owned(),
// ChType::Channel(msg.channel_login.to_owned()),
// sender_badge,
// cmdreqroles
2024-02-04 14:28:37 -05:00
// return self.can_user_run(msg.sender.name.to_owned(),
// let a = Arc::new(Mutex::new(self));
// let mut a = a.lock().await;
// let a = **a;
// let a = a.can_user_run(msg.sender.name.to_owned(),
// ChType::Channel(msg.channel_login.to_owned()),
// sender_badge,
// cmdreqroles
// ) ;
// let a = *self;
// let a = Arc::new(Mutex::new(a));
// let a = a.lock().await.can_user_run(msg.sender.name.to_owned(),
// ChType::Channel(msg.channel_login.to_owned()),
// sender_badge,
// cmdreqroles
// ) ;
// return a;
self . can_user_run ( msg . sender . name . to_owned ( ) ,
ChType ::Channel ( msg . channel_login . to_owned ( ) ) ,
sender_badge ,
cmdreqroles
) ;
2024-01-29 22:57:07 -05:00
}
// [ ] Call can_user_run()
2024-02-12 01:25:12 -05:00
// (self,Permissible::Block)
2024-01-31 18:36:23 -05:00
Permissible ::Block
2024-01-29 22:57:07 -05:00
}
2024-02-12 01:25:12 -05:00
2024-02-04 14:28:37 -05:00
pub async fn can_user_run ( & self ,
2024-01-29 12:41:26 -05:00
usr :String ,
channelname :ChType ,
2024-01-29 22:57:07 -05:00
chat_badge :ChatBadge ,
2024-01-29 12:41:26 -05:00
cmdreqroles :Vec < UserRole >
2024-01-29 22:57:07 -05:00
// ) -> Result<Permissible,Box<dyn Error>> {
) -> Permissible {
2024-01-29 12:41:26 -05:00
/*
canUserRun -
Input :
usr :String ,
channelname :ChType ,
2024-01-29 22:57:07 -05:00
chat_badge :ChatBadge ,
2024-01-29 12:41:26 -05:00
cmdreqroles :Vec < UserRole >
Output : Result < Permissible , Box < dyn Error > >
Some Possible outcomes : Ok ( Permissible ::Allow ) , Ok ( Permissible ::Block )
Description
For a Given Chatter ( with any special ChatBadge ) who ran the Command at a Given Channel , check if that user can
run the command based on the given cmdreqroles required by the command
Inputs and business logic determine if the user can run the command based on the command ' s required roles
* /
2024-01-29 13:08:35 -05:00
// Requirements
/*
2024-01-29 22:57:07 -05:00
[ x ] If cmdreqroles is empty vector , automatically assume Ok ( Permissible ::Allow )
[ x ] If chatBadge ::Broadcaster .. .
[ x ] and cmdreqroles includes UserRole ::Broadcaster , Ok ( Permissible ::Allow )
[ x ] and cmdreqroles includes UserRole ::Mod ( " " ) OR UserRole ::SupMod ( " " ) , Ok ( Permissible ::Allow )
[ x ] If chatBadge ::Mod .. .
[ x ] Check if they have either UserRole ::Mod ( channelname ::ChType ) or UserRole ::SupMod ( channelname ::ChType )
[ x ] If not , assign them UserRole ::Mod ( channelname ::ChType )
[ x ] If cmdreqroles includes UserRole ::Mod ( " " ) , checks if chatter has UserRole ::Mod ( channelname ::ChType ) or UserRole ::SupMod ( channelname ::ChType ) to determine if Ok ( Permissible ::Allow )
[ x ] If cmdreqroles includes UserRole ::SupMod ( " " ) , checks if chatter has UserRole ::SupMod ( channelname ::ChType ) to determine if Ok ( Permissible ::Allow )
[ x ] If cmdreqroles includes UserRole ::BotAdmin and chatter has UserRole ::BotAdmin , Ok ( Permissible ::Allow )
[ x ] Otherwise , Ok ( Permissible ::Block )
2024-01-29 13:08:35 -05:00
* /
2024-01-29 12:41:26 -05:00
2024-01-29 22:57:07 -05:00
// [x] If cmdreqroles is empty vector , automatically assume Ok(Permissible::Allow)
2024-01-29 12:41:26 -05:00
2024-01-29 22:57:07 -05:00
if cmdreqroles . len ( ) = = 0 {
// return Ok(Permissible::Allow)
return Permissible ::Allow
}
match chat_badge {
// [x] If chatBadge::Broadcaster ...
// [x] and cmdreqroles includes UserRole::Broadcaster , Ok(Permissible::Allow)
// [x] and cmdreqroles includes UserRole::Mod("") OR UserRole::SupMod("") , Ok(Permissible::Allow)
ChatBadge ::Broadcaster = > {
if cmdreqroles . contains ( & UserRole ::Broadcaster ) | |
cmdreqroles . contains ( & UserRole ::Mod ( ChType ::Channel ( String ::new ( ) ) ) ) | |
cmdreqroles . contains ( & UserRole ::SupMod ( ChType ::Channel ( String ::new ( ) ) ) ) {
// return Ok(Permissible::Allow)
return Permissible ::Allow
}
} ,
// [x] If chatBadge::Mod ...
// [x] Check if they have either UserRole::Mod(channelname::ChType) or UserRole::SupMod(channelname::ChType)
// [x] If not, assign them UserRole::Mod(channelname::ChType)
ChatBadge ::Mod = > {
2024-01-30 09:13:59 -05:00
// println!("Mod Chatbadge detected");
2024-01-29 22:57:07 -05:00
2024-01-30 09:13:59 -05:00
// println!("debug special roles : {:?}",self.special_roles_users);
// println!("debug usr : {}",&usr.to_lowercase());
2024-01-29 22:57:07 -05:00
// let Some((k,v)) = self.special_roles_users.get_key_value(usr);
2024-01-31 09:31:14 -05:00
// match self.special_roles_users.get_mut(&usr.to_lowercase()) {
2024-02-04 14:28:37 -05:00
// match self.special_roles_users.get(&usr.to_lowercase()) {
match self . special_roles_users . lock ( ) . await . get ( & usr . to_lowercase ( ) ) {
2024-01-29 22:57:07 -05:00
Some ( usrroles ) = > {
2024-01-30 09:13:59 -05:00
// println!("contains mod : {}", usrroles.contains(&UserRole::Mod(channelname.clone())));
// println!("contains supmod : {}", usrroles.contains(&UserRole::SupMod(channelname.clone())));
2024-01-29 22:57:07 -05:00
if usrroles . contains ( & UserRole ::Mod ( channelname . clone ( ) ) ) | |
usrroles . contains ( & UserRole ::SupMod ( channelname . clone ( ) ) ) {
// Do nothing - this is expected
} else {
// in this case, they have a ChatBadge::Mod but should have this for the channel
2024-01-31 09:31:14 -05:00
// let mut a = usrroles;
// usrroles.push(UserRole::Mod(channelname.clone()));
// a.push(UserRole::Mod(channelname.clone()));
self . special_roles_users
2024-02-04 14:28:37 -05:00
. lock ( ) . await
2024-01-31 09:31:14 -05:00
. get_mut ( & usr . to_lowercase ( ) )
. expect ( " ERROR " )
. push ( UserRole ::Mod ( channelname . clone ( ) ) ) ;
2024-01-30 09:13:59 -05:00
// println!("debug special roles : {:?}",self.special_roles_users);
2024-01-29 22:57:07 -05:00
}
} ,
_ = > ( )
}
} ,
// _ => (),
}
// [x] If cmdreqroles includes UserRole::Mod("") , checks if chatter has UserRole::Mod(channelname::ChType) or UserRole::SupMod(channelname::ChType) to determine if Ok(Permissible::Allow)
println! ( " cmd required roles : {:?} " , cmdreqroles ) ;
if cmdreqroles . contains ( & UserRole ::Mod ( ChType ::Channel ( String ::new ( ) ) ) ) {
// match self.special_roles_users.get(&channelname) {
// Some(usrroles) => {},
// None => (),
// }
println! ( " Mod Role required " ) ;
2024-02-04 14:28:37 -05:00
if let Some ( a ) = self . special_roles_users . lock ( ) . await . get ( & usr . to_lowercase ( ) ) {
2024-01-29 22:57:07 -05:00
if a . contains ( & UserRole ::Mod ( channelname . clone ( ) ) ) | | a . contains ( & UserRole ::SupMod ( channelname . clone ( ) ) ) {
// return Ok(Permissible::Allow);
return Permissible ::Allow
}
}
}
// [x] If cmdreqroles includes UserRole::SupMod("") , checks if chatter has UserRole::SupMod(channelname::ChType) to determine if Ok(Permissible::Allow)
if cmdreqroles . contains ( & UserRole ::SupMod ( ChType ::Channel ( String ::new ( ) ) ) ) {
2024-02-04 14:28:37 -05:00
if let Some ( a ) = self . special_roles_users . lock ( ) . await . get ( & usr . to_lowercase ( ) ) {
2024-01-29 22:57:07 -05:00
if a . contains ( & UserRole ::SupMod ( channelname . clone ( ) ) ) {
// return Ok(Permissible::Allow);
return Permissible ::Allow
}
}
}
// [x] If cmdreqroles includes UserRole::BotAdmin and chatter has UserRole::BotAdmin , Ok(Permissible::Allow)
println! ( " Eval cmdreqroles with botadmin : {} " , cmdreqroles . contains ( & UserRole ::BotAdmin ) ) ;
if cmdreqroles . contains ( & UserRole ::BotAdmin ) {
2024-02-04 14:28:37 -05:00
println! ( " special roles get : {:?} " , self . special_roles_users . lock ( ) . await . get ( & usr . to_lowercase ( ) ) ) ;
if let Some ( a ) = self . special_roles_users . lock ( ) . await . get ( & usr . to_lowercase ( ) ) {
2024-01-29 22:57:07 -05:00
println! ( " special roles contains BotAdmin: {} " , a . contains ( & UserRole ::BotAdmin ) ) ;
if a . contains ( & UserRole ::BotAdmin ) {
// return Ok(Permissible::Allow);
return Permissible ::Allow
}
}
}
Permissible ::Block
2024-01-29 12:41:26 -05:00
}
2024-01-31 21:30:08 -05:00
2024-02-12 01:25:12 -05:00
// pub async fn promote(&mut self,trgchatter:String,channel:Option<ChType>,trg_role:Option<UserRole>) -> ChangeResult {
pub async fn promote ( & self , trgchatter :String , channel :Option < ChType > , trg_role :Option < UserRole > ) -> ChangeResult {
2024-02-01 08:40:09 -05:00
// Note : If channel is none, getspecialuserroles() returns all roles for the user
// let chatterroles = self.getspecialuserroles(trgchatter, channel);
2024-02-04 14:28:37 -05:00
// let chatterroles = self.getspecialuserroles(trgchatter.clone(), channel.clone());
// let chatterroles = *self.getspecialuserroles(trgchatter.clone(), channel.clone()).await;
// let chatterroles = chatterroles.lock().await;
// // let chatterroles = *chatterroles;
// let chatterroles = chatterroles.unwrap();
2024-02-01 08:40:09 -05:00
2024-02-04 14:28:37 -05:00
let chatterroles = self . getspecialuserroles ( trgchatter . clone ( ) , channel . clone ( ) ) . await ;
// let chatterroles = chatterroles.lock().await;
// let chatterroles = *chatterroles;
let chatterroles = chatterroles . unwrap ( ) ;
// let emptyvec = vec![];
// let chatterroles = match chatterroles {
// Some(a) => a,
// _ => &(emptyvec),
// };
2024-02-01 08:40:09 -05:00
match trg_role {
Some ( UserRole ::Mod ( a ) ) = > {
if let Some ( trg_chnl ) = channel {
if chatterroles . contains ( & UserRole ::Mod ( trg_chnl . clone ( ) ) ) {
return ChangeResult ::NoChange ( String ::from ( " Target User already has Target Role " ) ) ;
}
// # otherwise, trg_role for the given chnl is not assigned to the trgchatter
// chatterroles.push(UserRole::Mod(trg_chnl.clone()));
self . special_roles_users
2024-02-04 14:28:37 -05:00
. lock ( ) . await
2024-02-01 08:40:09 -05:00
. get_mut ( & trgchatter )
. expect ( " Error getting roles " )
. push ( UserRole ::Mod ( trg_chnl ) ) ;
return ChangeResult ::Success ( String ::from ( " Promotion Successful " ) ) ;
}
} ,
Some ( UserRole ::SupMod ( a ) ) = > ( ) ,
Some ( UserRole ::BotAdmin ) = > ( ) ,
Some ( _ ) = > ( ) ,
None = > ( ) ,
}
// match chatterroles {
// Some(chatterroles) => {
// // [x] chatter already has the target role
// if chatterroles.contains(&trg_role) {
// return ChangeResult::NoChange(String::from("Target User already has Target Role"));
// }
// // By this point, chatteroles does not contain target role
// // match trgRole {
// // Some(trgRole) => {
// // match trgRole {
// // UserRole::Mod(a) => {
// // },
// // UserRole::SupMod(a) => (),
// // UserRole::BotAdmin => (),
// // _ => (), // <-- do nothing with al other options
// // }
// // },
// // None => {
// // /*
// // - If trgRole is None , then promote by implicit rules . For example,
// // - For UserRoles without Mod or SupMod & Caller is SupMod | Broadcaster | BotAdmin > To Mod
// // - For Mod & Caller is SupMod | Broadcaster | BotAdmin > To SupMod
// // - For UserRoles without BotAdmin & Caller is BotAdmin > To BotAdmin
// // */
// // },
// // }
// // let trgRole = match trgRole {
// // Some(UserRole::Mod(a)) => a,
// // Some(UserRole::SupMod(a)) => a,
// // Some(UserRole::BotAdmin) => UserRole::BotAdmin,
// // None => {
// // /*
// // - If trgRole is None , then promote by implicit rules . For example,
// // - For UserRoles without Mod or SupMod & Caller is SupMod | Broadcaster | BotAdmin > To Mod
// // - For Mod & Caller is SupMod | Broadcaster | BotAdmin > To SupMod
// // - For UserRoles without BotAdmin & Caller is BotAdmin > To BotAdmin
// // */
// // },
// // };
// // if let Some(trgRole) = trgRole {
// // // [x] chatter already has the target role
// // if chatterroles.contains(&trgRole) {
// // return ChangeResult::NoChange(String::from("Target User already has Target Role"));
// // }
// // // [ ] trgRole should be assigned based on the input channel
// // let roletoassign = UserRole::
// // }
// },
// _ => (),
// }
2024-01-31 21:30:08 -05:00
2024-02-01 08:40:09 -05:00
ChangeResult ::Success ( String ::from ( " TEST > Promotion Successful " ) )
2024-01-31 21:30:08 -05:00
}
2024-02-01 08:40:09 -05:00
pub fn demote ( self , trgchatter :String , channel :Option < ChType > , trgRole :Option < UserRole > ) -> ChangeResult {
2024-01-31 21:30:08 -05:00
2024-02-01 08:40:09 -05:00
ChangeResult ::Success ( String ::from ( " TEST > Promotion Successful " ) )
2024-01-31 21:30:08 -05:00
}
2024-02-04 14:28:37 -05:00
pub async fn getspecialuserroles ( & self , chattername :String , channel :Option < ChType > ) -> Option < Vec < UserRole > > {
2024-01-31 21:30:08 -05:00
// let a = chattername.to_lowercase();
// self.special_roles_users.get(&a)
// for k in self.special_roles_users.keys() {
// println!("Special Roles Keys {k}");
// for v in
// }
// for (k,v) in &self.special_roles_users {
// println!("User {k}");
// println!("> Roles : {:?}",v);
// }
let a = chattername . to_lowercase ( ) ;
// println!("{a}");
2024-02-04 14:28:37 -05:00
// let b = self.special_roles_users.lock().await.get(&a);
// match b
// {
// Some(b) => Some(*b),
// None => None,
// }
let b = self . special_roles_users . lock ( ) . await . remove ( & a ) ;
let outp = b ;
// let b = Arc::new(Mutex::new(outp));
outp
// let b = Arc::new(Mutex::new(b));
// b
2024-01-31 21:30:08 -05:00
// Some(vec![UserRole::Mod(ChType::Channel(String::from("modulatingforcebot")))])
}
2024-01-29 06:18:27 -05:00
}