127 lines
3.5 KiB
Rust
127 lines
3.5 KiB
Rust
|
use super::{communities::community_context, render_error};
|
||
|
use crate::{
|
||
|
assets::initial_context, check_community_permissions, community_context_bools, get_lang,
|
||
|
get_user_from_token, State,
|
||
|
};
|
||
|
use axum::{
|
||
|
response::{Html, IntoResponse},
|
||
|
extract::Path,
|
||
|
Extension,
|
||
|
};
|
||
|
use axum_extra::extract::CookieJar;
|
||
|
use tetratto_core::model::{communities::Community, Error};
|
||
|
|
||
|
/// `/forges`
|
||
|
pub async fn home_request(jar: CookieJar, Extension(data): Extension<State>) -> impl IntoResponse {
|
||
|
let data = data.read().await;
|
||
|
let user = match get_user_from_token!(jar, data.0) {
|
||
|
Some(ua) => ua,
|
||
|
None => {
|
||
|
return Err(Html(
|
||
|
render_error(Error::NotAllowed, &jar, &data, &None).await,
|
||
|
));
|
||
|
}
|
||
|
};
|
||
|
|
||
|
let list = match data.0.get_memberships_by_owner(user.id).await {
|
||
|
Ok(p) => p,
|
||
|
Err(e) => return Err(Html(render_error(e, &jar, &data, &Some(user)).await)),
|
||
|
};
|
||
|
|
||
|
let mut communities: Vec<Community> = Vec::new();
|
||
|
for membership in &list {
|
||
|
match data
|
||
|
.0
|
||
|
.get_community_by_id_no_void(membership.community)
|
||
|
.await
|
||
|
{
|
||
|
Ok(c) => {
|
||
|
if c.is_forge {
|
||
|
communities.push(c)
|
||
|
} else {
|
||
|
// we only want to show forges here
|
||
|
continue;
|
||
|
}
|
||
|
}
|
||
|
Err(_) => {
|
||
|
// delete membership; community doesn't exist
|
||
|
if let Err(e) = data.0.delete_membership(membership.id, &user).await {
|
||
|
return Err(Html(render_error(e, &jar, &data, &Some(user)).await));
|
||
|
}
|
||
|
|
||
|
continue;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
let lang = get_lang!(jar, data.0);
|
||
|
let mut context = initial_context(&data.0.0.0, lang, &Some(user)).await;
|
||
|
context.insert("list", &communities);
|
||
|
|
||
|
// return
|
||
|
Ok(Html(data.1.render("forge/home.html", &context).unwrap()))
|
||
|
}
|
||
|
|
||
|
/// `/forge/{title}`
|
||
|
pub async fn info_request(
|
||
|
jar: CookieJar,
|
||
|
Path(title): Path<String>,
|
||
|
Extension(data): Extension<State>,
|
||
|
) -> impl IntoResponse {
|
||
|
let data = data.read().await;
|
||
|
let user = get_user_from_token!(jar, data.0);
|
||
|
|
||
|
let community = match data.0.get_community_by_title(&title.to_lowercase()).await {
|
||
|
Ok(ua) => ua,
|
||
|
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
|
||
|
};
|
||
|
|
||
|
if community.id == 0 {
|
||
|
// don't show page for void community
|
||
|
return Err(Html(
|
||
|
render_error(
|
||
|
Error::GeneralNotFound("community".to_string()),
|
||
|
&jar,
|
||
|
&data,
|
||
|
&user,
|
||
|
)
|
||
|
.await,
|
||
|
));
|
||
|
}
|
||
|
|
||
|
// check permissions
|
||
|
let (can_read, _) = check_community_permissions!(community, jar, data, user);
|
||
|
|
||
|
// init context
|
||
|
let lang = get_lang!(jar, data.0);
|
||
|
let mut context = initial_context(&data.0.0.0, lang, &user).await;
|
||
|
|
||
|
let (
|
||
|
is_owner,
|
||
|
is_joined,
|
||
|
is_pending,
|
||
|
can_post,
|
||
|
can_manage_posts,
|
||
|
can_manage_community,
|
||
|
can_manage_roles,
|
||
|
can_manage_questions,
|
||
|
) = community_context_bools!(data, user, community);
|
||
|
|
||
|
community_context(
|
||
|
&mut context,
|
||
|
&community,
|
||
|
is_owner,
|
||
|
is_joined,
|
||
|
is_pending,
|
||
|
can_post,
|
||
|
can_read,
|
||
|
can_manage_posts,
|
||
|
can_manage_community,
|
||
|
can_manage_roles,
|
||
|
can_manage_questions,
|
||
|
);
|
||
|
|
||
|
// return
|
||
|
Ok(Html(data.1.render("forge/info.html", &context).unwrap()))
|
||
|
}
|