Working on user preferences for dashboards
This commit is contained in:
@ -6,9 +6,7 @@ use sqlx::MySqlPool;
|
||||
use crate::consts::LOCAL_TIMEZONE;
|
||||
|
||||
pub struct UserData {
|
||||
pub id: u32,
|
||||
#[allow(dead_code)]
|
||||
pub user: u64,
|
||||
pub id: u64,
|
||||
pub dm_channel: u32,
|
||||
pub timezone: String,
|
||||
pub allowed_dm: bool,
|
||||
@ -23,7 +21,9 @@ impl UserData {
|
||||
|
||||
match sqlx::query!(
|
||||
"
|
||||
SELECT IFNULL(timezone, 'UTC') AS timezone FROM users WHERE user = ?
|
||||
SELECT IFNULL(timezone, 'UTC') AS timezone
|
||||
FROM users
|
||||
WHERE id = ?
|
||||
",
|
||||
user_id
|
||||
)
|
||||
@ -48,7 +48,9 @@ impl UserData {
|
||||
match sqlx::query_as_unchecked!(
|
||||
Self,
|
||||
"
|
||||
SELECT id, user, dm_channel, IF(timezone IS NULL, ?, timezone) AS timezone, allowed_dm FROM users WHERE user = ?
|
||||
SELECT id, dm_channel, IF(timezone IS NULL, ?, timezone) AS timezone, allowed_dm
|
||||
FROM users
|
||||
WHERE id = ?
|
||||
",
|
||||
*LOCAL_TIMEZONE,
|
||||
user_id.get()
|
||||
@ -64,7 +66,8 @@ SELECT id, user, dm_channel, IF(timezone IS NULL, ?, timezone) AS timezone, allo
|
||||
|
||||
sqlx::query!(
|
||||
"
|
||||
INSERT IGNORE INTO channels (channel) VALUES (?)
|
||||
INSERT IGNORE INTO channels (channel)
|
||||
VALUES (?)
|
||||
",
|
||||
dm_channel.id.get()
|
||||
)
|
||||
@ -73,7 +76,8 @@ INSERT IGNORE INTO channels (channel) VALUES (?)
|
||||
|
||||
sqlx::query!(
|
||||
"
|
||||
INSERT INTO users (name, user, dm_channel, timezone) VALUES ('', ?, (SELECT id FROM channels WHERE channel = ?), ?)
|
||||
INSERT INTO users (id, dm_channel, timezone)
|
||||
VALUES (?, (SELECT id FROM channels WHERE channel = ?), ?)
|
||||
",
|
||||
user_id.get(),
|
||||
dm_channel.id.get(),
|
||||
@ -85,7 +89,9 @@ INSERT INTO users (name, user, dm_channel, timezone) VALUES ('', ?, (SELECT id F
|
||||
Ok(sqlx::query_as_unchecked!(
|
||||
Self,
|
||||
"
|
||||
SELECT id, user, dm_channel, timezone, allowed_dm FROM users WHERE user = ?
|
||||
SELECT id, dm_channel, timezone, allowed_dm
|
||||
FROM users
|
||||
WHERE id = ?
|
||||
",
|
||||
user_id.get()
|
||||
)
|
||||
@ -104,7 +110,9 @@ SELECT id, user, dm_channel, timezone, allowed_dm FROM users WHERE user = ?
|
||||
pub async fn commit_changes(&self, pool: &MySqlPool) {
|
||||
sqlx::query!(
|
||||
"
|
||||
UPDATE users SET timezone = ?, allowed_dm = ? WHERE id = ?
|
||||
UPDATE users
|
||||
SET timezone = ?, allowed_dm = ?
|
||||
WHERE id = ?
|
||||
",
|
||||
self.timezone,
|
||||
self.allowed_dm,
|
||||
|
@ -21,16 +21,29 @@ use serenity::{
|
||||
};
|
||||
use sqlx::{MySql, Pool};
|
||||
|
||||
use crate::web::guards::transaction::Transaction;
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct UserPreferences {
|
||||
timezone: String,
|
||||
use_browser_timezone: bool,
|
||||
dashboard_color_scheme: String,
|
||||
reset_inputs_on_create: bool,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct UserInfo {
|
||||
name: String,
|
||||
patreon: bool,
|
||||
timezone: Option<String>,
|
||||
preferences: UserPreferences,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct UpdateUser {
|
||||
timezone: String,
|
||||
pub struct UpdateUserPreferences {
|
||||
timezone: Option<String>,
|
||||
use_browser_timezone: Option<bool>,
|
||||
dashboard_color_scheme: Option<String>,
|
||||
reset_inputs_on_create: Option<bool>,
|
||||
}
|
||||
|
||||
#[get("/api/user")]
|
||||
@ -39,7 +52,16 @@ pub async fn get_user_info(
|
||||
ctx: &State<Context>,
|
||||
pool: &State<Pool<MySql>>,
|
||||
) -> JsonValue {
|
||||
offline!(json!(UserInfo { name: "Discord".to_string(), patreon: true, timezone: None }));
|
||||
offline!(json!(UserInfo {
|
||||
name: "Discord".to_string(),
|
||||
patreon: true,
|
||||
preferences: UserPreferences {
|
||||
timezone: "UTC".to_string(),
|
||||
use_browser_timezone: false,
|
||||
dashboard_color_scheme: "system".to_string(),
|
||||
reset_inputs_on_create: false,
|
||||
}
|
||||
}));
|
||||
|
||||
if let Some(user_id) =
|
||||
cookies.get_private("userid").map(|u| u.value().parse::<u64>().ok()).flatten()
|
||||
@ -48,8 +70,16 @@ pub async fn get_user_info(
|
||||
.member(&ctx.inner(), user_id)
|
||||
.await;
|
||||
|
||||
let timezone = sqlx::query!(
|
||||
"SELECT IFNULL(timezone, 'UTC') AS timezone FROM users WHERE user = ?",
|
||||
let prefs = sqlx::query!(
|
||||
"
|
||||
SELECT
|
||||
IFNULL(timezone, 'UTC') AS timezone,
|
||||
use_browser_timezone,
|
||||
dashboard_color_scheme,
|
||||
reset_inputs_on_create
|
||||
FROM users
|
||||
WHERE id = ?
|
||||
",
|
||||
user_id
|
||||
)
|
||||
.fetch_one(pool.inner())
|
||||
@ -65,7 +95,12 @@ pub async fn get_user_info(
|
||||
.roles
|
||||
.contains(&RoleId::new(env::var("PATREON_ROLE_ID").unwrap().parse().unwrap()))
|
||||
}),
|
||||
timezone,
|
||||
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,
|
||||
},
|
||||
};
|
||||
|
||||
json!(user_info)
|
||||
@ -74,28 +109,55 @@ pub async fn get_user_info(
|
||||
}
|
||||
}
|
||||
|
||||
#[patch("/api/user", data = "<user>")]
|
||||
#[patch("/api/user", data = "<preferences>")]
|
||||
pub async fn update_user_info(
|
||||
cookies: &CookieJar<'_>,
|
||||
user: Json<UpdateUser>,
|
||||
pool: &State<Pool<MySql>>,
|
||||
preferences: Json<UpdateUserPreferences>,
|
||||
mut transaction: Transaction<'_>,
|
||||
) -> JsonValue {
|
||||
if let Some(user_id) =
|
||||
cookies.get_private("userid").map(|u| u.value().parse::<u64>().ok()).flatten()
|
||||
{
|
||||
if user.timezone.parse::<Tz>().is_ok() {
|
||||
let _ = sqlx::query!(
|
||||
"UPDATE users SET timezone = ? WHERE user = ?",
|
||||
user.timezone,
|
||||
user_id,
|
||||
)
|
||||
.execute(pool.inner())
|
||||
.await;
|
||||
|
||||
json!({})
|
||||
} else {
|
||||
json!({"error": "Timezone not recognized"})
|
||||
if let Some(timezone) = &preferences.timezone {
|
||||
if timezone.parse::<Tz>().is_ok() {
|
||||
let _ = sqlx::query!(
|
||||
"
|
||||
UPDATE users
|
||||
SET timezone = ?
|
||||
WHERE id = ?
|
||||
",
|
||||
timezone,
|
||||
user_id,
|
||||
)
|
||||
.execute(transaction.executor())
|
||||
.await;
|
||||
} else {
|
||||
return json!({"error": "Timezone not recognised"});
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(dashboard_color_scheme) = &preferences.dashboard_color_scheme {
|
||||
if vec!["system", "light", "dark"].contains(dashboard_color_scheme) {
|
||||
let _ = sqlx::query!(
|
||||
"
|
||||
UPDATE users
|
||||
SET dashboard_color_scheme = ?
|
||||
WHERE id = ?
|
||||
",
|
||||
dashboard_color_scheme,
|
||||
user_id,
|
||||
)
|
||||
.execute(transaction.executor())
|
||||
.await;
|
||||
} else {
|
||||
return json!({"error": "Color scheme not recognised"});
|
||||
}
|
||||
}
|
||||
|
||||
// todo handle other two options
|
||||
|
||||
transaction.commit().await;
|
||||
json!({})
|
||||
} else {
|
||||
json!({"error": "Not authorized"})
|
||||
}
|
||||
|
Reference in New Issue
Block a user