Compare commits
4 Commits
9d1d0eaecd
...
06ed48f4cf
Author | SHA1 | Date |
---|---|---|
|
06ed48f4cf | |
|
bd3d71ae16 | |
|
a15a1820a8 | |
|
b1be0d0d77 |
|
@ -23,6 +23,7 @@ pub enum Tag {
|
||||||
Dialog,
|
Dialog,
|
||||||
Article,
|
Article,
|
||||||
Header,
|
Header,
|
||||||
|
Footer,
|
||||||
|
|
||||||
Div,
|
Div,
|
||||||
|
|
||||||
|
@ -51,6 +52,7 @@ pub enum Tag {
|
||||||
Anchor,
|
Anchor,
|
||||||
Button,
|
Button,
|
||||||
|
|
||||||
|
HeaderGroup,
|
||||||
Header1,
|
Header1,
|
||||||
Header2,
|
Header2,
|
||||||
Header3,
|
Header3,
|
||||||
|
@ -85,6 +87,7 @@ impl Tag {
|
||||||
Self::Dialog => "dialog",
|
Self::Dialog => "dialog",
|
||||||
Self::Article => "article",
|
Self::Article => "article",
|
||||||
Self::Header => "header",
|
Self::Header => "header",
|
||||||
|
Self::Footer => "footer",
|
||||||
|
|
||||||
Self::Div => "div",
|
Self::Div => "div",
|
||||||
|
|
||||||
|
@ -111,6 +114,7 @@ impl Tag {
|
||||||
Self::Anchor => "a",
|
Self::Anchor => "a",
|
||||||
Self::Button => "button",
|
Self::Button => "button",
|
||||||
|
|
||||||
|
Self::HeaderGroup => "hgroup",
|
||||||
Self::Header1 => "h1",
|
Self::Header1 => "h1",
|
||||||
Self::Header2 => "h2",
|
Self::Header2 => "h2",
|
||||||
Self::Header3 => "h3",
|
Self::Header3 => "h3",
|
||||||
|
|
209
src/page.rs
209
src/page.rs
|
@ -1,7 +1,7 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
auth::{self, User},
|
auth::{self, User},
|
||||||
htmx::{Build, HtmxBuilder, SwapMethod, Tag},
|
htmx::{Build, HtmxBuilder, Tag},
|
||||||
settings::{ApiState, Intro, IntroFriendlyName},
|
settings::{ApiState, GuildSettings, Intro, IntroFriendlyName},
|
||||||
};
|
};
|
||||||
use axum::{
|
use axum::{
|
||||||
extract::{Path, State},
|
extract::{Path, State},
|
||||||
|
@ -9,14 +9,64 @@ use axum::{
|
||||||
};
|
};
|
||||||
use tracing::error;
|
use tracing::error;
|
||||||
|
|
||||||
pub(crate) async fn home(user: Option<User>) -> Redirect {
|
fn page_header(title: &str) -> HtmxBuilder {
|
||||||
if user.is_some() {
|
HtmxBuilder::new(Tag::Html).head(|b| {
|
||||||
Redirect::to("/guild/588149178912473103")
|
b.title(title)
|
||||||
|
.script(
|
||||||
|
"https://unpkg.com/htmx.org@1.9.3",
|
||||||
|
Some("sha384-lVb3Rd/Ca0AxaoZg5sACe8FJKF0tnUgR2Kd7ehUOG5GCcROv5uBIZsOqovBAcWua"),
|
||||||
|
)
|
||||||
|
// Not currently using
|
||||||
|
// .script("https://unpkg.com/hyperscript.org@0.9.9", None)
|
||||||
|
.style_link("https://cdn.jsdelivr.net/npm/@picocss/pico@1/css/pico.min.css")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn home(
|
||||||
|
State(state): State<ApiState>,
|
||||||
|
user: Option<User>,
|
||||||
|
) -> Result<Html<String>, Redirect> {
|
||||||
|
if let Some(user) = user {
|
||||||
|
let settings = state.settings.lock().await;
|
||||||
|
|
||||||
|
let guild = settings
|
||||||
|
.guilds
|
||||||
|
.iter()
|
||||||
|
.filter(|(_, guild_settings)| guild_settings.users.contains_key(&user.name));
|
||||||
|
|
||||||
|
Ok(Html(
|
||||||
|
page_header("MemeJoin - Home")
|
||||||
|
.builder(Tag::Div, |b| {
|
||||||
|
b.attribute("class", "container")
|
||||||
|
.builder_text(Tag::Header2, "Choose a Guild")
|
||||||
|
.push_builder(guild_list(&state.origin, guild))
|
||||||
|
})
|
||||||
|
.build(),
|
||||||
|
))
|
||||||
} else {
|
} else {
|
||||||
Redirect::to("/login")
|
Err(Redirect::to(&format!("{}/login", state.origin)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn guild_list<'a>(
|
||||||
|
origin: &str,
|
||||||
|
guilds: impl Iterator<Item = (&'a u64, &'a GuildSettings)>,
|
||||||
|
) -> HtmxBuilder {
|
||||||
|
HtmxBuilder::new(Tag::Empty).ul(|b| {
|
||||||
|
let mut b = b;
|
||||||
|
for (guild_id, guild_settings) in guilds {
|
||||||
|
b = b.li(|b| {
|
||||||
|
b.link(
|
||||||
|
&guild_settings.name,
|
||||||
|
&format!("{}/guild/{}", origin, guild_id),
|
||||||
|
)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
b
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
fn intro_list<'a>(
|
fn intro_list<'a>(
|
||||||
intros: impl Iterator<Item = (&'a String, &'a Intro)>,
|
intros: impl Iterator<Item = (&'a String, &'a Intro)>,
|
||||||
label: &str,
|
label: &str,
|
||||||
|
@ -29,7 +79,7 @@ fn intro_list<'a>(
|
||||||
.builder(Tag::FieldSet, |b| {
|
.builder(Tag::FieldSet, |b| {
|
||||||
let mut b = b
|
let mut b = b
|
||||||
.attribute("class", "container")
|
.attribute("class", "container")
|
||||||
.attribute("style", "max-height: 20%; overflow-y: scroll");
|
.attribute("style", "max-height: 50%; overflow-y: scroll");
|
||||||
for intro in intros {
|
for intro in intros {
|
||||||
b = b.builder(Tag::Label, |b| {
|
b = b.builder(Tag::Label, |b| {
|
||||||
b.builder(Tag::Input, |b| {
|
b.builder(Tag::Input, |b| {
|
||||||
|
@ -54,107 +104,110 @@ pub(crate) async fn guild_dashboard(
|
||||||
|
|
||||||
let Some(guild) = settings.guilds.get(&guild_id) else {
|
let Some(guild) = settings.guilds.get(&guild_id) else {
|
||||||
error!(%guild_id, "no such guild");
|
error!(%guild_id, "no such guild");
|
||||||
return Err(Redirect::to("/"));
|
return Err(Redirect::to(&format!("{}/", state.origin)));
|
||||||
};
|
};
|
||||||
let Some(guild_user) = guild.users.get(&user.name) else {
|
let Some(guild_user) = guild.users.get(&user.name) else {
|
||||||
error!(%guild_id, %user.name, "no user in guild");
|
error!(%guild_id, %user.name, "no user in guild");
|
||||||
return Err(Redirect::to("/"));
|
return Err(Redirect::to(&format!("{}/", state.origin)));
|
||||||
};
|
};
|
||||||
|
|
||||||
let is_moderator = guild_user.permissions.can(auth::Permission::DeleteSounds);
|
let is_moderator = guild_user.permissions.can(auth::Permission::DeleteSounds);
|
||||||
|
|
||||||
Ok(Html(
|
Ok(Html(
|
||||||
HtmxBuilder::new(Tag::Html)
|
HtmxBuilder::new(Tag::Html)
|
||||||
.head(|b| {
|
.push_builder(page_header("MemeJoin - Dashboard"))
|
||||||
b.title("MemeJoin - Dashboard")
|
|
||||||
.script(
|
|
||||||
"https://unpkg.com/htmx.org@1.9.3",
|
|
||||||
Some("sha384-lVb3Rd/Ca0AxaoZg5sACe8FJKF0tnUgR2Kd7ehUOG5GCcROv5uBIZsOqovBAcWua"),
|
|
||||||
)
|
|
||||||
.script("https://unpkg.com/hyperscript.org@0.9.9", None)
|
|
||||||
.style_link("https://cdn.jsdelivr.net/npm/@picocss/pico@1/css/pico.min.css")
|
|
||||||
})
|
|
||||||
.builder(Tag::Nav, |b| {
|
.builder(Tag::Nav, |b| {
|
||||||
b.builder(Tag::Header1, |b| b.text("MemeJoin - A bot for user intros"))
|
b.builder(Tag::HeaderGroup, |b| {
|
||||||
.builder_text(Tag::Paragraph, &user.name)
|
b.attribute("class", "container")
|
||||||
|
.builder(Tag::Header1, |b| b.text("MemeJoin - A bot for user intros"))
|
||||||
|
.builder_text(Tag::Header6, &user.name)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
.builder(Tag::Main, |b| {
|
.builder(Tag::Empty, |b| {
|
||||||
if is_moderator {
|
if is_moderator {
|
||||||
b.builder(Tag::Article, |b| {
|
b.builder(Tag::Div, |b| {
|
||||||
b.builder_text(Tag::Header, "Wow, you're a moderator")
|
b.attribute("class", "container")
|
||||||
|
.builder(Tag::Article, |b| {
|
||||||
|
b.builder_text(Tag::Header, "Wow, you're a moderator")
|
||||||
|
.push_builder(moderator_dashboard())
|
||||||
|
.builder_text(Tag::Footer, "End of super cool mod section")
|
||||||
|
})
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
b
|
b
|
||||||
}
|
}
|
||||||
.builder(Tag::Article, |b| {
|
.builder(Tag::Div, |b| {
|
||||||
let mut b = b.builder_text(Tag::Header, "Guild Settings");
|
b.attribute("class", "container")
|
||||||
|
.builder(Tag::Article, |b| {
|
||||||
|
let mut b = b.builder_text(Tag::Header, "Guild Settings");
|
||||||
|
|
||||||
for (channel_name, channel_settings) in &guild.channels {
|
for (channel_name, channel_settings) in &guild.channels {
|
||||||
if let Some(channel_user) = channel_settings.users.get(&user.name) {
|
if let Some(channel_user) = channel_settings.users.get(&user.name) {
|
||||||
let current_intros =
|
let current_intros =
|
||||||
channel_user.intros.iter().filter_map(|intro_index| {
|
channel_user.intros.iter().filter_map(|intro_index| {
|
||||||
Some((
|
Some((
|
||||||
&intro_index.index,
|
&intro_index.index,
|
||||||
guild.intros.get(&intro_index.index)?,
|
guild.intros.get(&intro_index.index)?,
|
||||||
))
|
))
|
||||||
});
|
});
|
||||||
let available_intros = guild.intros.iter().filter_map(|intro| {
|
let available_intros =
|
||||||
if !channel_user
|
guild.intros.iter().filter_map(|intro| {
|
||||||
.intros
|
if !channel_user
|
||||||
.iter()
|
.intros
|
||||||
.any(|intro_index| intro.0 == &intro_index.index)
|
.iter()
|
||||||
{
|
.any(|intro_index| intro.0 == &intro_index.index)
|
||||||
Some((intro.0, intro.1))
|
{
|
||||||
} else {
|
Some((intro.0, intro.1))
|
||||||
None
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
});
|
||||||
|
b = b.builder(Tag::Article, |b| {
|
||||||
|
b.builder_text(Tag::Header, channel_name).builder(
|
||||||
|
Tag::Div,
|
||||||
|
|b| {
|
||||||
|
b.builder_text(Tag::Strong, "Your Current Intros")
|
||||||
|
.push_builder(intro_list(
|
||||||
|
current_intros,
|
||||||
|
"Remove Intro",
|
||||||
|
&format!(
|
||||||
|
"{}/v2/intros/remove/{}/{}",
|
||||||
|
state.origin, guild_id, channel_name
|
||||||
|
),
|
||||||
|
))
|
||||||
|
.builder_text(Tag::Strong, "Select Intros")
|
||||||
|
.push_builder(intro_list(
|
||||||
|
available_intros,
|
||||||
|
"Add Intro",
|
||||||
|
&format!(
|
||||||
|
"{}/v2/intros/add/{}/{}",
|
||||||
|
state.origin, guild_id, channel_name
|
||||||
|
),
|
||||||
|
))
|
||||||
|
},
|
||||||
|
)
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
b = b
|
|
||||||
.builder_text(Tag::Strong, channel_name)
|
|
||||||
.builder(Tag::Div, |b| {
|
|
||||||
b.builder_text(Tag::Strong, "Your Current Intros")
|
|
||||||
.push_builder(intro_list(
|
|
||||||
current_intros,
|
|
||||||
"Remove Intro",
|
|
||||||
&format!(
|
|
||||||
"/v2/intros/remove/{}/{}",
|
|
||||||
guild_id, channel_name
|
|
||||||
),
|
|
||||||
))
|
|
||||||
.builder_text(Tag::Strong, "Select Intros")
|
|
||||||
.push_builder(intro_list(
|
|
||||||
available_intros,
|
|
||||||
"Add Intro",
|
|
||||||
&format!(
|
|
||||||
"/v2/intros/add/{}/{}",
|
|
||||||
guild_id, channel_name
|
|
||||||
),
|
|
||||||
))
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
b
|
b
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.build(),
|
.build(),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn moderator_dashboard() -> HtmxBuilder {
|
||||||
|
HtmxBuilder::new(Tag::Empty)
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) async fn login(State(state): State<ApiState>) -> Html<String> {
|
pub(crate) async fn login(State(state): State<ApiState>) -> Html<String> {
|
||||||
let authorize_uri = format!("https://discord.com/api/oauth2/authorize?client_id={}&redirect_uri={}/v2/auth&response_type=code&scope=guilds.members.read%20guilds%20identify", state.secrets.client_id, state.origin);
|
let authorize_uri = format!("https://discord.com/api/oauth2/authorize?client_id={}&redirect_uri={}/v2/auth&response_type=code&scope=guilds.members.read%20guilds%20identify", state.secrets.client_id, state.origin);
|
||||||
|
|
||||||
Html(
|
Html(
|
||||||
HtmxBuilder::new(Tag::Html)
|
HtmxBuilder::new(Tag::Html)
|
||||||
.head(|b| {
|
.push_builder(page_header("MemeJoin - Login"))
|
||||||
b.title("MemeJoin - Login")
|
|
||||||
.script(
|
|
||||||
"https://unpkg.com/htmx.org@1.9.3",
|
|
||||||
Some("sha384-lVb3Rd/Ca0AxaoZg5sACe8FJKF0tnUgR2Kd7ehUOG5GCcROv5uBIZsOqovBAcWua"),
|
|
||||||
)
|
|
||||||
.script("https://unpkg.com/hyperscript.org@0.9.9", None)
|
|
||||||
.style_link("https://cdn.jsdelivr.net/npm/@picocss/pico@1/css/pico.min.css")
|
|
||||||
})
|
|
||||||
.link("Login", &authorize_uri)
|
.link("Login", &authorize_uri)
|
||||||
.build(),
|
.build(),
|
||||||
)
|
)
|
||||||
|
|
|
@ -240,7 +240,7 @@ pub(crate) async fn auth(
|
||||||
|
|
||||||
let mut data = HashMap::new();
|
let mut data = HashMap::new();
|
||||||
|
|
||||||
let redirect_uri = format!("{}/auth", state.origin);
|
let redirect_uri = format!("{}/old/auth", state.origin);
|
||||||
data.insert("client_id", state.secrets.client_id.as_str());
|
data.insert("client_id", state.secrets.client_id.as_str());
|
||||||
data.insert("client_secret", state.secrets.client_secret.as_str());
|
data.insert("client_secret", state.secrets.client_secret.as_str());
|
||||||
data.insert("grant_type", "authorization_code");
|
data.insert("grant_type", "authorization_code");
|
||||||
|
|
Loading…
Reference in New Issue