pub async fn filter_trainings(
    ctx: &Context,
    trainings: Vec<db::Training>,
    user: &User,
) -> Result<Vec<db::Training>> {
    let roles = {
        let guild = ctx
            .data
            .read()
            .await
            .get::<ConfigValuesData>()
            .unwrap()
            .main_guild_id;
        guild.member(ctx, user.id).await?.roles
    };

    let tiers = db::Tier::all(ctx).await?;

    let mut tier_map: HashMap<i32, Vec<db::TierMapping>> = HashMap::new();

    for t in tiers {
        let t = Arc::new(t);
        let discord_roles = t.get_discord_roles(ctx).await?;
        tier_map.insert(t.id, discord_roles);
    }

    Ok(trainings
        .into_iter()
        .filter(|tier| match tier.tier_id {
            None => true,
            Some(id) => match tier_map.get(&id) {
                None => false,
                Some(tm_vec) => tm_vec
                    .iter()
                    .any(|tm| roles.iter().any(|r| *r == (tm.discord_role_id as u64))),
            },
        })
        .collect())
}
/// Looks at the user permissions and filters out trainings the user has not sufficient permissions
/// for
Looks at the user permissions and filters out trainings the user has not sufficient permissions
for
Looks at the user permissions and filters out trainings the user has not sufficient permissions for
pub async fn filter_trainings(
    ctx: &Context,
    trainings: Vec<db::Training>,
    user: &User,
) -> Result<Vec<db::Training>> {
    let roles = {
        let guild = ctx
            .data
            .read()
            .await
            .get::<ConfigValuesData>()
            .unwrap()
            .main_guild_id;
        guild.member(ctx, user.id).await?.roles
    };
    let tiers = db::Tier::all(ctx).await?;
    let mut tier_map: HashMap<i32, Vec<db::TierMapping>> = HashMap::new();
    for t in tiers {
        let t = Arc::new(t);
        let discord_roles = t.get_discord_roles(ctx).await?;
        tier_map.insert(t.id, discord_roles);
    }
    Ok(trainings
        .into_iter()
        .filter(|tier| match tier.tier_id {
            None => true,
            Some(id) => match tier_map.get(&id) {
                None => false,
                Some(tm_vec) => tm_vec
                    .iter()
                    .any(|tm| roles.iter().any(|r| *r == (tm.discord_role_id as u64))),
            },
        })
        .collect())
}
pub async fn filter_trainings ( ctx : & Context , trainings : Vec < db :: Training > , user : & User , ) -> Result < Vec < db :: Training > > { let roles = { let guild = ctx . data . read ( ) . await . get :: < ConfigValuesData > ( ) . unwrap ( ) . main_guild_id ; guild . member ( ctx , user . id ) . await ? . roles } ; let tiers = db :: Tier :: all ( ctx ) . await ? ; let mut tier_map : HashMap < i32 , Vec < db :: TierMapping > > = HashMap :: new ( ) ; for t in tiers { let t = Arc :: new ( t ) ; let discord_roles = t . get_discord_roles ( ctx ) . await ? ; tier_map . insert ( t . id , discord_roles ) ; } Ok ( trainings . into_iter ( ) . filter ( | tier | match tier . tier_id { None => true , Some ( id ) => match tier_map . get ( & id ) { None => false , Some ( tm_vec ) => tm_vec . iter ( ) . any ( | tm | roles . iter ( ) . any ( | r | * r == ( tm . discord_role_id as u64 ) ) ) , } , } ) . collect ( ) ) }
Looks at the user permissions and filters out trainings the user has not sufficient permissions
for
Looks at the user permissions and filters out trainings the user has not sufficient permissions for
ctx
&Context
trainings
Vec<db::Training>
user
&User