Bump package lock. Add attachment serializer

This commit is contained in:
jude 2023-11-12 09:39:45 +00:00
parent e0c60e2ce3
commit d36438c6ce
4 changed files with 458 additions and 474 deletions

857
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -33,11 +33,9 @@ impl<'r> FromRequest<'r> for Transaction<'r> {
match request.guard::<&State<Pool<Database>>>().await { match request.guard::<&State<Pool<Database>>>().await {
Outcome::Success(pool) => match pool.begin().await { Outcome::Success(pool) => match pool.begin().await {
Ok(transaction) => Outcome::Success(Transaction(transaction)), Ok(transaction) => Outcome::Success(Transaction(transaction)),
Err(e) => { Err(e) => Outcome::Error((Status::InternalServerError, TransactionError::Error(e))),
Outcome::Failure((Status::InternalServerError, TransactionError::Error(e)))
}
}, },
Outcome::Failure(e) => Outcome::Failure((e.0, TransactionError::Missing)), Outcome::Error(e) => Outcome::Error((e.0, TransactionError::Missing)),
Outcome::Forward(f) => Outcome::Forward(f), Outcome::Forward(f) => Outcome::Forward(f),
} }
} }

View File

@ -4,7 +4,7 @@ use chrono::{naive::NaiveDateTime, Utc};
use rand::{rngs::OsRng, seq::IteratorRandom}; use rand::{rngs::OsRng, seq::IteratorRandom};
use rocket::{http::CookieJar, response::Redirect, serde::json::json}; use rocket::{http::CookieJar, response::Redirect, serde::json::json};
use rocket_dyn_templates::Template; use rocket_dyn_templates::Template;
use serde::{de, Deserialize, Deserializer, Serialize}; use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
use serenity::{ use serenity::{
client::Context, client::Context,
http::Http, http::Http,
@ -65,6 +65,15 @@ impl<'de> Deserialize<'de> for Attachment {
} }
} }
impl Serialize for Attachment {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.collect_str(&base64::encode(&self.0))
}
}
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub struct ReminderTemplate { pub struct ReminderTemplate {
#[serde(default = "id_default")] #[serde(default = "id_default")]
@ -73,7 +82,7 @@ pub struct ReminderTemplate {
guild_id: u32, guild_id: u32,
#[serde(default = "template_name_default")] #[serde(default = "template_name_default")]
name: String, name: String,
attachment: Option<Vec<u8>>, attachment: Option<Attachment>,
attachment_name: Option<String>, attachment_name: Option<String>,
avatar: Option<String>, avatar: Option<String>,
content: String, content: String,
@ -98,7 +107,7 @@ pub struct ReminderTemplate {
pub struct ReminderTemplateCsv { pub struct ReminderTemplateCsv {
#[serde(default = "template_name_default")] #[serde(default = "template_name_default")]
name: String, name: String,
attachment: Option<Vec<u8>>, attachment: Option<Attachment>,
attachment_name: Option<String>, attachment_name: Option<String>,
avatar: Option<String>, avatar: Option<String>,
content: String, content: String,
@ -133,8 +142,7 @@ pub struct EmbedField {
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub struct Reminder { pub struct Reminder {
#[serde(with = "base64s")] attachment: Option<Attachment>,
attachment: Option<Vec<u8>>,
attachment_name: Option<String>, attachment_name: Option<String>,
avatar: Option<String>, avatar: Option<String>,
#[serde(with = "string")] #[serde(with = "string")]
@ -167,8 +175,7 @@ pub struct Reminder {
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub struct ReminderCsv { pub struct ReminderCsv {
#[serde(with = "base64s")] attachment: Option<Attachment>,
attachment: Option<Vec<u8>>,
attachment_name: Option<String>, attachment_name: Option<String>,
avatar: Option<String>, avatar: Option<String>,
channel: String, channel: String,
@ -329,29 +336,6 @@ mod string {
} }
} }
mod base64s {
use serde::{de, Deserialize, Deserializer, Serializer};
pub fn serialize<S>(value: &Option<Vec<u8>>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
if let Some(opt) = value {
serializer.collect_str(&base64::encode(opt))
} else {
serializer.serialize_none()
}
}
pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<Vec<u8>>, D::Error>
where
D: Deserializer<'de>,
{
let string = Option::<String>::deserialize(deserializer)?;
Some(string.map(|b| base64::decode(b).map_err(de::Error::custom))).flatten().transpose()
}
}
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct DeleteReminder { pub struct DeleteReminder {
uid: String, uid: String,

View File

@ -31,22 +31,20 @@ pub async fn discord_login(
// store the pkce secret to verify the authorization later // store the pkce secret to verify the authorization later
cookies.add_private( cookies.add_private(
Cookie::build("verify", pkce_verifier.secret().to_string()) Cookie::build(("verify", pkce_verifier.secret().to_string()))
.http_only(true) .http_only(true)
.path("/login") .path("/login")
.same_site(SameSite::Lax) .same_site(SameSite::Lax)
.expires(Expiration::Session) .expires(Expiration::Session),
.finish(),
); );
// store the csrf token to verify no interference // store the csrf token to verify no interference
cookies.add_private( cookies.add_private(
Cookie::build("csrf", csrf_token.secret().to_string()) Cookie::build(("csrf", csrf_token.secret().to_string()))
.http_only(true) .http_only(true)
.path("/login") .path("/login")
.same_site(SameSite::Lax) .same_site(SameSite::Lax)
.expires(Expiration::Session) .expires(Expiration::Session),
.finish(),
); );
Redirect::to(auth_url.to_string()) Redirect::to(auth_url.to_string())
@ -54,9 +52,9 @@ pub async fn discord_login(
#[get("/discord/logout")] #[get("/discord/logout")]
pub async fn discord_logout(cookies: &CookieJar<'_>) -> Redirect { pub async fn discord_logout(cookies: &CookieJar<'_>) -> Redirect {
cookies.remove_private(Cookie::named("username")); cookies.remove_private(Cookie::from("username"));
cookies.remove_private(Cookie::named("userid")); cookies.remove_private(Cookie::from("userid"));
cookies.remove_private(Cookie::named("access_token")); cookies.remove_private(Cookie::from("access_token"));
Redirect::to(uri!(routes::index)) Redirect::to(uri!(routes::index))
} }
@ -80,17 +78,16 @@ pub async fn discord_callback(
.request_async(async_http_client) .request_async(async_http_client)
.await; .await;
cookies.remove_private(Cookie::named("verify")); cookies.remove_private(Cookie::from("verify"));
cookies.remove_private(Cookie::named("csrf")); cookies.remove_private(Cookie::from("csrf"));
match token_result { match token_result {
Ok(token) => { Ok(token) => {
cookies.add_private( cookies.add_private(
Cookie::build("access_token", token.access_token().secret().to_string()) Cookie::build(("access_token", token.access_token().secret().to_string()))
.secure(true) .secure(true)
.http_only(true) .http_only(true)
.path("/dashboard") .path("/dashboard"),
.finish(),
); );
let request_res = reqwest_client let request_res = reqwest_client