add: communities list page

TODO(ui): implement community creation, community viewing, community posting
TODO(ui): implement profile following, followers, and posts feed
This commit is contained in:
trisua 2025-03-27 18:10:47 -04:00
parent 5cfca49793
commit d6fbfc3cd6
28 changed files with 497 additions and 313 deletions

View file

@ -15,7 +15,7 @@ pub async fn login_request(jar: CookieJar, Extension(data): Extension<State>) ->
}
let lang = get_lang!(jar, data.0);
let mut context = initial_context(&data.0.0, lang, &user);
let mut context = initial_context(&data.0.0, lang, &user).await;
Ok(Html(
data.1.render("auth/login.html", &mut context).unwrap(),
@ -35,7 +35,7 @@ pub async fn register_request(
}
let lang = get_lang!(jar, data.0);
let mut context = initial_context(&data.0.0, lang, &user);
let mut context = initial_context(&data.0.0, lang, &user).await;
Ok(Html(
data.1.render("auth/register.html", &mut context).unwrap(),

View file

@ -0,0 +1,37 @@
use super::render_error;
use crate::{State, assets::initial_context, get_lang, get_user_from_token};
use axum::{
Extension,
response::{Html, IntoResponse},
};
use axum_extra::extract::CookieJar;
use tetratto_core::model::Error;
/// `/communities`
pub async fn list_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 posts = 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 lang = get_lang!(jar, data.0);
let mut context = initial_context(&data.0.0, lang, &Some(user)).await;
context.insert("posts", &posts);
// return
Ok(Html(
data.1
.render("communities/list.html", &mut context)
.unwrap(),
))
}

View file

@ -11,7 +11,7 @@ pub async fn index_request(jar: CookieJar, Extension(data): Extension<State>) ->
let user = get_user_from_token!(jar, data.0);
let lang = get_lang!(jar, data.0);
let mut context = initial_context(&data.0.0, lang, &user);
let mut context = initial_context(&data.0.0, lang, &user).await;
Html(data.1.render("misc/index.html", &mut context).unwrap())
}

View file

@ -1,4 +1,5 @@
pub mod auth;
pub mod communities;
pub mod misc;
pub mod profile;
@ -21,16 +22,18 @@ pub fn routes() -> Router {
.route("/auth/login", get(auth::login_request))
// profile
.route("/user/{username}", get(profile::posts_request))
// communities
.route("/communities", get(communities::list_request))
}
pub fn render_error(
pub async fn render_error(
e: Error,
jar: &CookieJar,
data: &(DataManager, tera::Tera),
user: &Option<User>,
) -> String {
let lang = get_lang!(jar, data.0);
let mut context = initial_context(&data.0.0, lang, &user);
let mut context = initial_context(&data.0.0, lang, &user).await;
context.insert("error_text", &e.to_string());
data.1.render("misc/error.html", &mut context).unwrap()
}

View file

@ -6,6 +6,14 @@ use axum::{
response::{Html, IntoResponse},
};
use axum_extra::extract::CookieJar;
use tera::Context;
use tetratto_core::model::{Error, auth::User};
pub fn profile_context(context: &mut Context, profile: &User, is_self: bool, is_following: bool) {
context.insert("profile", &profile);
context.insert("is_self", &is_self);
context.insert("is_following", &is_following);
}
/// `/user/{username}`
pub async fn posts_request(
@ -19,7 +27,7 @@ pub async fn posts_request(
let other_user = match data.0.get_user_by_username(&username).await {
Ok(ua) => ua,
Err(e) => return Err(Html(render_error(e, &jar, &data, &user))),
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
};
let posts = match data
@ -28,15 +36,46 @@ pub async fn posts_request(
.await
{
Ok(p) => p,
Err(e) => return Err(Html(render_error(e, &jar, &data, &user))),
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
};
// check if we're blocked
if let Some(ref ua) = user {
if data
.0
.get_userblock_by_initiator_receiver(other_user.id, ua.id)
.await
.is_ok()
{
return Err(Html(
render_error(Error::NotAllowed, &jar, &data, &user).await,
));
}
}
// init context
let lang = get_lang!(jar, data.0);
let mut context = initial_context(&data.0.0, lang, &user);
let mut context = initial_context(&data.0.0, lang, &user).await;
let is_self = if let Some(ref ua) = user {
ua.id == other_user.id
} else {
false
};
let is_following = if let Some(ref ua) = user {
data.0
.get_userfollow_by_initiator_receiver(ua.id, other_user.id)
.await
.is_ok()
} else {
false
};
context.insert("profile", &other_user);
context.insert("posts", &posts);
profile_context(&mut context, &other_user, is_self, is_following);
// return
Ok(Html(
data.1.render("profile/posts.html", &mut context).unwrap(),
))