use super::{communities::community_context, render_error, PaginatedQuery}; use crate::{ assets::initial_context, check_community_permissions, community_context_bools, get_lang, get_user_from_token, State, }; use axum::{ extract::{Path, Query}, response::{Html, IntoResponse}, Extension, }; use axum_extra::extract::CookieJar; use tetratto_core::model::{communities::Community, Error}; /// `/forges` pub async fn home_request(jar: CookieJar, Extension(data): Extension) -> 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 = 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, Extension(data): Extension, ) -> 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())) } /// `/forge/{title}/tickets` pub async fn tickets_request( jar: CookieJar, Path(title): Path, Query(props): Query, Extension(data): Extension, ) -> 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); // ... let ignore_users = crate::ignore_users_gen!(user, data); let feed = match data .0 .get_posts_by_community(community.id, 12, props.page) .await { Ok(p) => match data.0.fill_posts(p, &ignore_users, &user).await { Ok(p) => p, Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)), }, Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)), }; // let pinned = match data.0.get_pinned_posts_by_community(community.id).await { // Ok(p) => match data.0.fill_posts(p, &ignore_users, &user).await { // Ok(p) => p, // Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)), // }, // Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)), // }; // 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); context.insert("feed", &feed); // context.insert("pinned", &pinned); context.insert("page", &props.page); 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/tickets.html", &context).unwrap())) }