Update schemas and resolve some warnings
This commit is contained in:
		@@ -16,17 +16,15 @@ pub async fn get_guild_roles(id: u64, cookies: &CookieJar<'_>, ctx: &State<Conte
 | 
			
		||||
    offline!(Ok(json!(vec![RoleInfo { name: "@everyone".to_string(), id: "1".to_string() }])));
 | 
			
		||||
    check_authorization(cookies, ctx.inner(), id).await?;
 | 
			
		||||
 | 
			
		||||
    let roles_res = ctx.cache.guild_roles(id);
 | 
			
		||||
    let roles_res = ctx.cache.guild(id).map(|g| {
 | 
			
		||||
        g.roles
 | 
			
		||||
            .iter()
 | 
			
		||||
            .map(|(_, r)| RoleInfo { id: r.id.to_string(), name: r.name.to_string() })
 | 
			
		||||
            .collect::<Vec<RoleInfo>>()
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    match roles_res {
 | 
			
		||||
        Some(roles) => {
 | 
			
		||||
            let roles = roles
 | 
			
		||||
                .iter()
 | 
			
		||||
                .map(|(_, r)| RoleInfo { id: r.id.to_string(), name: r.name.to_string() })
 | 
			
		||||
                .collect::<Vec<RoleInfo>>();
 | 
			
		||||
 | 
			
		||||
            Ok(json!(roles))
 | 
			
		||||
        }
 | 
			
		||||
        Some(roles) => Ok(json!(roles)),
 | 
			
		||||
        None => {
 | 
			
		||||
            warn!("Could not fetch roles from {}", id);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -6,6 +6,7 @@ use std::env;
 | 
			
		||||
 | 
			
		||||
use chrono_tz::Tz;
 | 
			
		||||
pub use guilds::*;
 | 
			
		||||
use log::warn;
 | 
			
		||||
pub use reminders::*;
 | 
			
		||||
use rocket::{
 | 
			
		||||
    get,
 | 
			
		||||
@@ -57,7 +58,7 @@ pub async fn get_user_info(
 | 
			
		||||
        patreon: true,
 | 
			
		||||
        preferences: UserPreferences {
 | 
			
		||||
            timezone: "UTC".to_string(),
 | 
			
		||||
            use_browser_timezone: false,
 | 
			
		||||
            use_browser_timezone: true,
 | 
			
		||||
            dashboard_color_scheme: "system".to_string(),
 | 
			
		||||
            reset_inputs_on_create: false,
 | 
			
		||||
        }
 | 
			
		||||
@@ -70,10 +71,10 @@ pub async fn get_user_info(
 | 
			
		||||
            .member(&ctx.inner(), user_id)
 | 
			
		||||
            .await;
 | 
			
		||||
 | 
			
		||||
        let prefs = sqlx::query!(
 | 
			
		||||
        let preferences = sqlx::query!(
 | 
			
		||||
            "
 | 
			
		||||
            SELECT
 | 
			
		||||
                IFNULL(timezone, 'UTC') AS timezone,
 | 
			
		||||
                timezone,
 | 
			
		||||
                use_browser_timezone,
 | 
			
		||||
                dashboard_color_scheme,
 | 
			
		||||
                reset_inputs_on_create
 | 
			
		||||
@@ -84,7 +85,20 @@ pub async fn get_user_info(
 | 
			
		||||
        )
 | 
			
		||||
        .fetch_one(pool.inner())
 | 
			
		||||
        .await
 | 
			
		||||
        .map_or(None, |q| Some(q.timezone));
 | 
			
		||||
        .map_or(
 | 
			
		||||
            UserPreferences {
 | 
			
		||||
                timezone: "UTC".to_string(),
 | 
			
		||||
                use_browser_timezone: false,
 | 
			
		||||
                dashboard_color_scheme: "system".to_string(),
 | 
			
		||||
                reset_inputs_on_create: false,
 | 
			
		||||
            },
 | 
			
		||||
            |q| UserPreferences {
 | 
			
		||||
                timezone: q.timezone,
 | 
			
		||||
                use_browser_timezone: q.use_browser_timezone != 0,
 | 
			
		||||
                dashboard_color_scheme: q.dashboard_color_scheme,
 | 
			
		||||
                reset_inputs_on_create: q.reset_inputs_on_create != 0,
 | 
			
		||||
            },
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        let user_info = UserInfo {
 | 
			
		||||
            name: cookies
 | 
			
		||||
@@ -95,12 +109,7 @@ pub async fn get_user_info(
 | 
			
		||||
                    .roles
 | 
			
		||||
                    .contains(&RoleId::new(env::var("PATREON_ROLE_ID").unwrap().parse().unwrap()))
 | 
			
		||||
            }),
 | 
			
		||||
            preferences: UserPreferences {
 | 
			
		||||
                timezone: prefs.timezone,
 | 
			
		||||
                use_browser_timezone: prefs.use_browser_timezone,
 | 
			
		||||
                dashboard_color_scheme: prefs.dashboard_color_scheme,
 | 
			
		||||
                reset_inputs_on_create: prefs.reset_inputs_on_create,
 | 
			
		||||
            },
 | 
			
		||||
            preferences,
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        json!(user_info)
 | 
			
		||||
@@ -137,7 +146,7 @@ pub async fn update_user_info(
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if let Some(dashboard_color_scheme) = &preferences.dashboard_color_scheme {
 | 
			
		||||
            if vec!["system", "light", "dark"].contains(dashboard_color_scheme) {
 | 
			
		||||
            if vec!["system", "light", "dark"].contains(&dashboard_color_scheme.as_str()) {
 | 
			
		||||
                let _ = sqlx::query!(
 | 
			
		||||
                    "
 | 
			
		||||
                    UPDATE users
 | 
			
		||||
@@ -154,10 +163,42 @@ pub async fn update_user_info(
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // todo handle other two options
 | 
			
		||||
        if let Some(reset_inputs_on_create) = &preferences.reset_inputs_on_create {
 | 
			
		||||
            let _ = sqlx::query!(
 | 
			
		||||
                "
 | 
			
		||||
                UPDATE users
 | 
			
		||||
                SET reset_inputs_on_create = ?
 | 
			
		||||
                WHERE id = ?
 | 
			
		||||
                ",
 | 
			
		||||
                reset_inputs_on_create,
 | 
			
		||||
                user_id,
 | 
			
		||||
            )
 | 
			
		||||
            .execute(transaction.executor())
 | 
			
		||||
            .await;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        transaction.commit().await;
 | 
			
		||||
        json!({})
 | 
			
		||||
        if let Some(use_browser_timezone) = &preferences.use_browser_timezone {
 | 
			
		||||
            let _ = sqlx::query!(
 | 
			
		||||
                "
 | 
			
		||||
                UPDATE users
 | 
			
		||||
                SET use_browser_timezone = ?
 | 
			
		||||
                WHERE id = ?
 | 
			
		||||
                ",
 | 
			
		||||
                use_browser_timezone,
 | 
			
		||||
                user_id,
 | 
			
		||||
            )
 | 
			
		||||
            .execute(transaction.executor())
 | 
			
		||||
            .await;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        match transaction.commit().await {
 | 
			
		||||
            Ok(_) => json!({}),
 | 
			
		||||
            Err(e) => {
 | 
			
		||||
                warn!("Error updating user preferences for {}: {:?}", user_id, e);
 | 
			
		||||
 | 
			
		||||
                json!({"error": "Couldn't update preferences"})
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    } else {
 | 
			
		||||
        json!({"error": "Not authorized"})
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -593,18 +593,11 @@ pub(crate) async fn create_reminder(
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn check_channel_matches_guild(ctx: &Context, channel_id: ChannelId, guild_id: GuildId) -> bool {
 | 
			
		||||
    // validate channel
 | 
			
		||||
    let channel = channel_id.to_channel_cached(&ctx.cache);
 | 
			
		||||
    let channel_exists = channel.is_some();
 | 
			
		||||
    return match ctx.cache.guild(guild_id) {
 | 
			
		||||
        Some(guild) => guild.channels.get(&channel_id).is_some(),
 | 
			
		||||
 | 
			
		||||
    if !channel_exists {
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    let channel_matches_guild =
 | 
			
		||||
        channel.map_or(false, |c| c.guild(&ctx.cache).map_or(false, |g| g.id == guild_id));
 | 
			
		||||
 | 
			
		||||
    channel_matches_guild
 | 
			
		||||
        None => false,
 | 
			
		||||
    };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async fn create_database_channel(
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user