add reposts/quotes

fix #2
This commit is contained in:
trisua 2025-04-10 18:16:52 -04:00
parent 15e24b9a61
commit df32b9d65e
43 changed files with 708 additions and 234 deletions

View file

@ -50,7 +50,7 @@ pub async fn register_request(
.to_string();
// check for ip ban
if let Ok(_) = data.get_ipban_by_ip(&real_ip).await {
if data.get_ipban_by_ip(&real_ip).await.is_ok() {
return (None, Json(Error::NotAllowed.into()));
}
@ -126,7 +126,7 @@ pub async fn login_request(
.to_string();
// check for ip ban
if let Ok(_) = data.get_ipban_by_ip(&real_ip).await {
if data.get_ipban_by_ip(&real_ip).await.is_ok() {
return (None, Json(Error::NotAllowed.into()));
}

View file

@ -330,7 +330,7 @@ pub async fn disable_totp_request(
}
// ...
match data.update_user_totp(id, &String::new(), &Vec::new()).await {
match data.update_user_totp(id, "", &Vec::new()).await {
Ok(()) => Json(ApiReturn {
ok: true,
message: "TOTP disabled".to_string(),

View file

@ -3,8 +3,9 @@ use axum_extra::extract::CookieJar;
use tetratto_core::model::{ApiReturn, Error, communities::Post};
use crate::{
State, get_user_from_token,
routes::api::v1::{CreatePost, UpdatePostContent, UpdatePostContext},
get_user_from_token,
routes::api::v1::{CreatePost, CreateRepost, UpdatePostContent, UpdatePostContext},
State,
};
pub async fn create_request(
@ -46,6 +47,39 @@ pub async fn create_request(
}
}
pub async fn create_repost_request(
jar: CookieJar,
Extension(data): Extension<State>,
Path(id): Path<usize>,
Json(req): Json<CreateRepost>,
) -> impl IntoResponse {
let data = &(data.read().await).0;
let user = match get_user_from_token!(jar, data) {
Some(ua) => ua,
None => return Json(Error::NotAllowed.into()),
};
match data
.create_post(Post::repost(
req.content,
match req.community.parse::<usize>() {
Ok(x) => x,
Err(e) => return Json(Error::MiscError(e.to_string()).into()),
},
user.id,
id,
))
.await
{
Ok(id) => Json(ApiReturn {
ok: true,
message: "Post reposted".to_string(),
payload: Some(id.to_string()),
}),
Err(e) => Json(e.into()),
}
}
pub async fn delete_request(
jar: CookieJar,
Extension(data): Extension<State>,

View file

@ -81,6 +81,10 @@ pub fn routes() -> Router {
// posts
.route("/posts", post(communities::posts::create_request))
.route("/posts/{id}", delete(communities::posts::delete_request))
.route(
"/posts/{id}/repost",
post(communities::posts::create_repost_request),
)
.route(
"/posts/{id}/content",
post(communities::posts::update_content_request),
@ -247,6 +251,12 @@ pub struct CreatePost {
pub replying_to: Option<String>,
}
#[derive(Deserialize)]
pub struct CreateRepost {
pub content: String,
pub community: String,
}
#[derive(Deserialize)]
pub struct UpdatePostContent {
pub content: String,

View file

@ -1,5 +1,5 @@
use super::{PaginatedQuery, render_error};
use crate::{State, assets::initial_context, get_lang, get_user_from_token};
use crate::{assets::initial_context, get_lang, get_user_from_token, sanitize::clean_context, State};
use axum::{
Extension,
extract::{Path, Query},
@ -323,9 +323,7 @@ pub async fn settings_request(
context.insert("community", &community);
context.insert(
"community_context_serde",
&serde_json::to_string(&community.context)
.unwrap()
.replace("\"", "\\\""),
&clean_context(&community.context),
);
// return
@ -347,15 +345,38 @@ pub async fn post_request(
let user = get_user_from_token!(jar, data.0);
let post = match data.0.get_post_by_id(id).await {
Ok(ua) => ua,
Ok(p) => p,
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
};
let community = match data.0.get_community_by_id(post.community).await {
Ok(ua) => ua,
Ok(c) => c,
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
};
// check repost
let reposting = if let Some(ref repost) = post.context.repost {
if let Some(reposting) = repost.reposting {
let mut x = match data.0.get_post_by_id(reposting).await {
Ok(p) => p,
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
};
x.mark_as_repost();
Some((
match data.0.get_user_by_id(x.owner).await {
Ok(ua) => ua,
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
},
x,
))
} else {
None
}
} else {
None
};
// check permissions
let (can_read, can_manage_pins) = check_permissions!(community, jar, data, user);
@ -383,6 +404,7 @@ pub async fn post_request(
) = community_context_bools!(data, user, community);
context.insert("post", &post);
context.insert("reposting", &reposting);
context.insert("replies", &feed);
context.insert("page", &props.page);
context.insert(

View file

@ -47,7 +47,7 @@ pub async fn index_request(
.get_posts_from_user_communities(user.id, 12, req.page)
.await
{
Ok(l) => match data.0.fill_posts_with_community(l).await {
Ok(l) => match data.0.fill_posts_with_community(l, user.id).await {
Ok(l) => l,
Err(e) => return Html(render_error(e, &jar, &data, &Some(user)).await),
},
@ -83,7 +83,7 @@ pub async fn following_request(
.get_posts_from_user_following(user.id, 12, req.page)
.await
{
Ok(l) => match data.0.fill_posts_with_community(l).await {
Ok(l) => match data.0.fill_posts_with_community(l, user.id).await {
Ok(l) => l,
Err(e) => return Err(Html(render_error(e, &jar, &data, &Some(user)).await)),
},
@ -110,7 +110,11 @@ pub async fn popular_request(
let user = get_user_from_token!(jar, data.0);
let list = match data.0.get_popular_posts(12, req.page).await {
Ok(l) => match data.0.fill_posts_with_community(l).await {
Ok(l) => match data
.0
.fill_posts_with_community(l, if let Some(ref ua) = user { ua.id } else { 0 })
.await
{
Ok(l) => l,
Err(e) => return Html(render_error(e, &jar, &data, &user).await),
},

View file

@ -1,5 +1,5 @@
use super::{PaginatedQuery, render_error};
use crate::{State, assets::initial_context, get_lang, get_user_from_token};
use crate::{assets::initial_context, get_lang, get_user_from_token, sanitize::clean_settings, State};
use axum::{
Extension,
extract::{Path, Query},
@ -44,19 +44,13 @@ pub async fn settings_request(
}
};
let settings = profile.settings.clone();
let tokens = profile.tokens.clone();
let lang = get_lang!(jar, data.0);
let mut context = initial_context(&data.0.0, lang, &Some(user)).await;
context.insert("profile", &profile);
context.insert(
"user_settings_serde",
&serde_json::to_string(&settings)
.unwrap()
.replace("\"", "\\\""),
);
context.insert("user_settings_serde", &clean_settings(&profile.settings));
context.insert(
"user_tokens_serde",
&serde_json::to_string(&tokens)
@ -158,7 +152,11 @@ pub async fn posts_request(
.get_posts_by_user(other_user.id, 12, props.page)
.await
{
Ok(p) => match data.0.fill_posts_with_community(p).await {
Ok(p) => match data
.0
.fill_posts_with_community(p, if let Some(ref ua) = user { ua.id } else { 0 })
.await
{
Ok(p) => p,
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
},
@ -166,7 +164,11 @@ pub async fn posts_request(
};
let pinned = match data.0.get_pinned_posts_by_user(other_user.id).await {
Ok(p) => match data.0.fill_posts_with_community(p).await {
Ok(p) => match data
.0
.fill_posts_with_community(p, if let Some(ref ua) = user { ua.id } else { 0 })
.await
{
Ok(p) => p,
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
},
@ -219,8 +221,8 @@ pub async fn posts_request(
};
context.insert("posts", &posts);
context.insert("page", &props.page);
context.insert("pinned", &pinned);
context.insert("page", &props.page);
profile_context(
&mut context,
&user,