Update schemas and resolve some warnings

This commit is contained in:
jude
2024-07-16 10:42:32 +01:00
parent 54ee3594eb
commit febd04c374
26 changed files with 221 additions and 164 deletions

View File

@ -17,10 +17,7 @@ impl Recordable for Options {
sqlx::query!(
"
INSERT INTO todos (user_id, value)
VALUES (
(SELECT id FROM users WHERE user = ?),
?
)
VALUES (?, ?)
",
ctx.author().id.get(),
self.task

View File

@ -14,8 +14,7 @@ impl Recordable for Options {
let values = sqlx::query!(
"
SELECT todos.id, value FROM todos
INNER JOIN users ON todos.user_id = users.id
WHERE users.user = ?
WHERE user_id = ?
",
ctx.author().id.get(),
)

View File

@ -191,8 +191,7 @@ impl ComponentDataModel {
sqlx::query!(
"
SELECT todos.id, value FROM todos
INNER JOIN users ON todos.user_id = users.id
WHERE users.user = ?
WHERE user_id = ?
",
uid,
)
@ -285,10 +284,9 @@ impl ComponentDataModel {
let values = if let Some(uid) = selector.user_id {
sqlx::query!(
"
SELECT todos.id, value FROM todos
INNER JOIN users ON todos.user_id = users.id
WHERE users.user = ?
",
SELECT todos.id, value FROM todos
WHERE user_id = ?
",
uid,
)
.fetch_all(&data.database)

View File

@ -1,6 +1,6 @@
use poise::{
serenity_prelude as serenity,
serenity_prelude::{ActivityData, CreateEmbed, CreateMessage, FullEvent},
serenity_prelude::{CreateEmbed, CreateMessage, FullEvent},
};
use crate::{

View File

@ -1,4 +1,4 @@
use poise::{serenity_prelude::model::channel::Channel, CommandInteractionType, CreateReply};
use poise::{CommandInteractionType, CreateReply};
use crate::{consts::MACRO_MAX_COMMANDS, models::command_macro::RecordedCommand, Context, Error};

View File

@ -85,17 +85,13 @@ impl Reminder {
reminders.enabled,
reminders.content,
reminders.embed_description,
users.user AS set_by
reminders.set_by
FROM
reminders
INNER JOIN
channels
ON
reminders.channel_id = channels.id
LEFT JOIN
users
ON
reminders.set_by = users.id
WHERE
reminders.uid = ?
",
@ -122,17 +118,13 @@ impl Reminder {
reminders.enabled,
reminders.content,
reminders.embed_description,
users.user AS set_by
reminders.set_by
FROM
reminders
INNER JOIN
channels
ON
reminders.channel_id = channels.id
LEFT JOIN
users
ON
reminders.set_by = users.id
WHERE
reminders.id = ?
",
@ -166,17 +158,13 @@ impl Reminder {
reminders.enabled,
reminders.content,
reminders.embed_description,
users.user AS set_by
reminders.set_by
FROM
reminders
INNER JOIN
channels
ON
reminders.channel_id = channels.id
LEFT JOIN
users
ON
reminders.set_by = users.id
WHERE
`status` = 'pending' AND
channels.channel = ? AND
@ -230,17 +218,13 @@ impl Reminder {
reminders.enabled,
reminders.content,
reminders.embed_description,
users.user AS set_by
reminders.set_by
FROM
reminders
LEFT JOIN
channels
ON
channels.id = reminders.channel_id
LEFT JOIN
users
ON
reminders.set_by = users.id
WHERE
`status` = 'pending' AND
FIND_IN_SET(channels.channel, ?)
@ -266,17 +250,13 @@ impl Reminder {
reminders.enabled,
reminders.content,
reminders.embed_description,
users.user AS set_by
reminders.set_by
FROM
reminders
LEFT JOIN
channels
ON
channels.id = reminders.channel_id
LEFT JOIN
users
ON
reminders.set_by = users.id
WHERE
`status` = 'pending' AND
channels.guild_id = (SELECT id FROM guilds WHERE guild = ?)
@ -303,20 +283,16 @@ impl Reminder {
reminders.enabled,
reminders.content,
reminders.embed_description,
users.user AS set_by
reminders.set_by
FROM
reminders
INNER JOIN
channels
ON
channels.id = reminders.channel_id
LEFT JOIN
users
ON
reminders.set_by = users.id
WHERE
`status` = 'pending' AND
channels.id = (SELECT dm_channel FROM users WHERE user = ?)
channels.id = (SELECT dm_channel FROM users WHERE id = ?)
",
user.get()
)

View File

@ -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);

View File

@ -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"})
}

View File

@ -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(