add: forums ui

This commit is contained in:
trisua 2025-08-04 12:12:04 -04:00
parent 2be87c397d
commit 9ec52abfe4
24 changed files with 770 additions and 64 deletions

View file

@ -16,7 +16,7 @@ use tera::Context;
use tetratto_core::model::{
addr::RemoteAddr,
auth::User,
communities::Community,
communities::{Community, ForumTopic},
communities_permissions::CommunityPermission,
permissions::FinePermission,
stacks::{StackMode, UserStack},
@ -424,6 +424,47 @@ pub async fn feed_request(
// check permissions
let (can_read, _) = check_community_permissions!(community, jar, data, user);
// is this is a forum, just show topics
if community.is_forum {
// 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);
let mut sorted: Vec<(&usize, &ForumTopic)> = community.topics.iter().collect();
sorted.sort_by(|x, y| x.1.position.cmp(&y.1.position));
context.insert("topics_sorted", &sorted);
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
return Ok(Html(
data.1.render("communities/topics.html", &context).unwrap(),
));
}
// ...
let ignore_users = crate::ignore_users_gen!(user, data);
@ -485,6 +526,119 @@ pub async fn feed_request(
))
}
/// `/community/{title}/topic/{id}`
pub async fn topic_feed_request(
jar: CookieJar,
Path((title, topic_id)): Path<(String, usize)>,
Query(props): Query<PaginatedQuery>,
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,
));
}
let topic = match community.topics.get(&topic_id) {
Some(x) => x,
None => {
return Err(Html(
render_error(
Error::GeneralNotFound("topic".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_topic(community.id, topic_id, 12, props.page, &user)
.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_topic(community.id, topic_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);
context.insert("topic", &topic);
context.insert("topic_id", &topic_id);
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("communities/topic.html", &context).unwrap(),
))
}
/// `/community/{title}/questions`
pub async fn questions_request(
jar: CookieJar,