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

@ -538,7 +538,7 @@ pub async fn add_topic_request(
None => return Json(Error::NotAllowed.into()),
};
let mut community = match data.get_community_by_id(id).await {
let mut community = match data.get_community_by_id_no_void(id).await {
Ok(x) => x,
Err(e) => return Json(e.into()),
};
@ -547,8 +547,22 @@ pub async fn add_topic_request(
return Json(Error::DoesNotSupportField("community".to_string()).into());
}
let (id, topic) = ForumTopic::new(req.title, req.description, req.color);
community.topics.insert(id, topic);
// check lengths
if req.title.len() > 32 {
return Json(Error::DataTooLong("title".to_string()).into());
}
if req.title.len() < 2 {
return Json(Error::DataTooShort("title".to_string()).into());
}
if req.description.len() > 256 {
return Json(Error::DataTooLong("description".to_string()).into());
}
// ...
let (topic_id, topic) = ForumTopic::new(req.title, req.description, req.color, req.position);
community.topics.insert(topic_id, topic);
match data
.update_community_topics(id, &user, community.topics)
@ -575,7 +589,7 @@ pub async fn update_topic_request(
None => return Json(Error::NotAllowed.into()),
};
let mut community = match data.get_community_by_id(id).await {
let mut community = match data.get_community_by_id_no_void(id).await {
Ok(x) => x,
Err(e) => return Json(e.into()),
};
@ -584,10 +598,25 @@ pub async fn update_topic_request(
return Json(Error::DoesNotSupportField("community".to_string()).into());
}
// check lengths
if req.title.len() > 32 {
return Json(Error::DataTooLong("title".to_string()).into());
}
if req.title.len() < 2 {
return Json(Error::DataTooShort("title".to_string()).into());
}
if req.description.len() > 256 {
return Json(Error::DataTooLong("description".to_string()).into());
}
// ...
let topic = ForumTopic {
title: req.title,
description: req.description,
color: req.color,
position: req.position,
};
community.topics.insert(topic_id, topic);
@ -616,7 +645,7 @@ pub async fn delete_topic_request(
None => return Json(Error::NotAllowed.into()),
};
let mut community = match data.get_community_by_id(id).await {
let mut community = match data.get_community_by_id_no_void(id).await {
Ok(x) => x,
Err(e) => return Json(e.into()),
};
@ -631,11 +660,14 @@ pub async fn delete_topic_request(
.update_community_topics(id, &user, community.topics)
.await
{
Ok(_) => Json(ApiReturn {
ok: true,
message: "Community updated".to_string(),
payload: (),
}),
Ok(_) => match data.delete_topic_posts(id, topic_id).await {
Ok(_) => Json(ApiReturn {
ok: true,
message: "Community updated".to_string(),
payload: (),
}),
Err(e) => Json(e.into()),
},
Err(e) => Json(e.into()),
}
}

View file

@ -129,6 +129,10 @@ pub async fn create_request(
Ok(x) => x,
Err(e) => return Json(Error::MiscError(e.to_string()).into()),
};
props.topic = match req.topic.parse::<usize>() {
Ok(x) => x,
Err(e) => return Json(Error::MiscError(e.to_string()).into()),
};
}
// check sizes

View file

@ -795,6 +795,8 @@ pub struct AddTopic {
pub title: String,
pub description: String,
pub color: String,
#[serde(default)]
pub position: i32,
}
#[derive(Deserialize)]
@ -821,6 +823,8 @@ pub struct CreatePost {
pub title: String,
#[serde(default)]
pub stack: String,
#[serde(default)]
pub topic: String,
}
#[derive(Deserialize)]

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,

View file

@ -99,6 +99,10 @@ pub fn routes() -> Router {
get(communities::create_post_request),
)
.route("/community/{title}", get(communities::feed_request))
.route(
"/community/{title}/topic/{id}",
get(communities::topic_feed_request),
)
.route(
"/community/{title}/questions",
get(communities::questions_request),