2025-03-26 21:46:21 -04:00
|
|
|
use super::{PaginatedQuery, render_error};
|
2025-03-25 23:58:27 -04:00
|
|
|
use crate::{State, assets::initial_context, get_lang, get_user_from_token};
|
|
|
|
use axum::{
|
|
|
|
Extension,
|
2025-03-26 21:46:21 -04:00
|
|
|
extract::{Path, Query},
|
2025-03-25 23:58:27 -04:00
|
|
|
response::{Html, IntoResponse},
|
|
|
|
};
|
|
|
|
use axum_extra::extract::CookieJar;
|
2025-03-27 18:10:47 -04:00
|
|
|
use tera::Context;
|
2025-03-29 00:26:56 -04:00
|
|
|
use tetratto_core::model::{Error, auth::User, communities::Community};
|
2025-03-27 18:10:47 -04:00
|
|
|
|
2025-03-31 11:45:34 -04:00
|
|
|
/// `/settings`
|
|
|
|
pub async fn settings_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 settings = user.settings.clone();
|
|
|
|
let tokens = user.tokens.clone();
|
|
|
|
|
|
|
|
let lang = get_lang!(jar, data.0);
|
|
|
|
let mut context = initial_context(&data.0.0, lang, &Some(user)).await;
|
|
|
|
|
|
|
|
context.insert(
|
|
|
|
"user_settings_serde",
|
|
|
|
&serde_json::to_string(&settings)
|
|
|
|
.unwrap()
|
|
|
|
.replace("\"", "\\\""),
|
|
|
|
);
|
|
|
|
context.insert(
|
|
|
|
"user_tokens_serde",
|
|
|
|
&serde_json::to_string(&tokens)
|
|
|
|
.unwrap()
|
|
|
|
.replace("\"", "\\\""),
|
|
|
|
);
|
|
|
|
|
|
|
|
// return
|
|
|
|
Ok(Html(
|
|
|
|
data.1
|
|
|
|
.render("profile/settings.html", &mut context)
|
|
|
|
.unwrap(),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2025-03-29 00:26:56 -04:00
|
|
|
pub fn profile_context(
|
|
|
|
context: &mut Context,
|
|
|
|
profile: &User,
|
|
|
|
communities: &Vec<Community>,
|
|
|
|
is_self: bool,
|
|
|
|
is_following: bool,
|
|
|
|
) {
|
2025-03-27 18:10:47 -04:00
|
|
|
context.insert("profile", &profile);
|
2025-03-29 00:26:56 -04:00
|
|
|
context.insert("communities", &communities);
|
2025-03-27 18:10:47 -04:00
|
|
|
context.insert("is_self", &is_self);
|
|
|
|
context.insert("is_following", &is_following);
|
|
|
|
}
|
2025-03-25 23:58:27 -04:00
|
|
|
|
|
|
|
/// `/user/{username}`
|
|
|
|
pub async fn posts_request(
|
|
|
|
jar: CookieJar,
|
|
|
|
Path(username): Path<String>,
|
2025-03-26 21:46:21 -04:00
|
|
|
Query(props): Query<PaginatedQuery>,
|
2025-03-25 23:58:27 -04:00
|
|
|
Extension(data): Extension<State>,
|
|
|
|
) -> impl IntoResponse {
|
|
|
|
let data = data.read().await;
|
|
|
|
let user = get_user_from_token!(jar, data.0);
|
|
|
|
|
|
|
|
let other_user = match data.0.get_user_by_username(&username).await {
|
|
|
|
Ok(ua) => ua,
|
2025-03-27 18:10:47 -04:00
|
|
|
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
|
2025-03-26 21:46:21 -04:00
|
|
|
};
|
|
|
|
|
2025-03-27 18:10:47 -04:00
|
|
|
// 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,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-29 00:26:56 -04:00
|
|
|
// fetch data
|
|
|
|
let posts = match data
|
|
|
|
.0
|
|
|
|
.get_posts_by_user(other_user.id, 12, props.page)
|
|
|
|
.await
|
|
|
|
{
|
|
|
|
Ok(p) => match data.0.fill_posts_with_community(p).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 communities = match data.0.get_memberships_by_owner(other_user.id).await {
|
|
|
|
Ok(m) => match data.0.fill_communities(m).await {
|
|
|
|
Ok(m) => m,
|
|
|
|
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
|
|
|
|
},
|
|
|
|
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
|
|
|
|
};
|
|
|
|
|
2025-03-27 18:10:47 -04:00
|
|
|
// init context
|
2025-03-25 23:58:27 -04:00
|
|
|
let lang = get_lang!(jar, data.0);
|
2025-03-27 18:10:47 -04:00
|
|
|
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
|
|
|
|
};
|
2025-03-26 21:46:21 -04:00
|
|
|
|
|
|
|
context.insert("posts", &posts);
|
2025-03-29 00:26:56 -04:00
|
|
|
profile_context(
|
|
|
|
&mut context,
|
|
|
|
&other_user,
|
|
|
|
&communities,
|
|
|
|
is_self,
|
|
|
|
is_following,
|
|
|
|
);
|
2025-03-25 23:58:27 -04:00
|
|
|
|
2025-03-27 18:10:47 -04:00
|
|
|
// return
|
2025-03-25 23:58:27 -04:00
|
|
|
Ok(Html(
|
|
|
|
data.1.render("profile/posts.html", &mut context).unwrap(),
|
|
|
|
))
|
|
|
|
}
|