diff --git a/Cargo.lock b/Cargo.lock index 42bc0cf..dfb337e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1272,6 +1272,7 @@ dependencies = [ "color-eyre", "cookie", "envious", + "futures-util", "jwt-simple", "once_cell", "openidconnect", diff --git a/Cargo.toml b/Cargo.toml index fe6bcc9..4773c91 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ base64 = "0.21.2" color-eyre = "0.6.2" cookie = "0.17.0" envious = "0.2.2" +futures-util = "0.3.28" jwt-simple = "0.11.6" once_cell = "1.18.0" openidconnect = "3.3.0" diff --git a/src/main.rs b/src/main.rs index 617e944..b41ad2b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,6 +17,7 @@ use axum_extra::extract::{cookie::Cookie, CookieJar}; use base64::{engine::general_purpose, engine::Engine}; use color_eyre::eyre; use cookie::{time::OffsetDateTime, SameSite}; +use futures_util::TryStreamExt; use jwt_simple::prelude::*; use once_cell::sync::Lazy; use openidconnect::{ @@ -500,6 +501,17 @@ struct HomeQuery { user_error: Option, } +struct AliasPart { + mail: String, + recipient: String, +} + +#[derive(Serialize)] +struct Alias { + mail: String, + recipients: Vec, +} + async fn home( state: State>, User(user): User, @@ -513,9 +525,39 @@ async fn home( .fetch_all(&state.db) .await?; + let aliases = sqlx::query_as!( + Mail, + "SELECT mail FROM emails WHERE id = $1 AND alias = true", + user + ) + .fetch_all(&state.db) + .await?; + + let mut alias_stream = sqlx::query_as!( + AliasPart, + r#" + SELECT alias_recipient.mail,recipient + FROM emails,alias_recipient + WHERE id = $1 AND alias = true AND emails.mail = alias_recipient.mail + "#, + user + ) + .fetch(&state.db); + + let mut aliases: HashMap<_, _> = aliases.into_iter().map(|a| (a.mail, Vec::new())).collect(); + while let Some(alias) = alias_stream.try_next().await? { + aliases.get_mut(&alias.mail).unwrap().push(alias.recipient); + } + + let aliases: Vec<_> = aliases + .into_iter() + .map(|(mail, recipients)| Alias { mail, recipients }) + .collect(); + let mut context = tera::Context::new(); context.insert("mails", &mails); context.insert("mail_domain", &state.mail_domain); + context.insert("aliases", &aliases); if let Some(err) = query.user_error { tracing::info!("User error: {err:?}"); context.insert("user_error", &err.to_string()); diff --git a/templates/home.html b/templates/home.html index 5e85a71..b0b635d 100644 --- a/templates/home.html +++ b/templates/home.html @@ -81,13 +81,104 @@ +

Aliases

+
    + {% for alias in aliases %} +
  • +
    + {{ alias.mail }} + + +
    +
      + {% set alias_idx = loop.index %} + {% for recpt in alias.recipients %}
    • {{ recpt }}
    • {% endfor %} +
    + + +
  • + {% endfor %} +
{% endblock content %}