fix: user follows, user blocks, private profile setting

This commit is contained in:
trisua 2025-03-31 20:02:09 -04:00
parent de53eec0e8
commit 17564ede49
8 changed files with 220 additions and 43 deletions

View file

@ -4,7 +4,7 @@ use crate::{
};
use axum::{Extension, Json, extract::Path, response::IntoResponse};
use axum_extra::extract::CookieJar;
use tetratto_core::model::auth::{UserBlock, UserFollow};
use tetratto_core::model::auth::{Notification, UserBlock, UserFollow};
/// Toggle following on the given user.
pub async fn follow_request(
@ -20,7 +20,7 @@ pub async fn follow_request(
if let Ok(userfollow) = data.get_userfollow_by_initiator_receiver(user.id, id).await {
// delete
match data.delete_userfollow(userfollow.id, user).await {
match data.delete_userfollow(userfollow.id, &user).await {
Ok(_) => Json(ApiReturn {
ok: true,
message: "User unfollowed".to_string(),
@ -31,11 +31,27 @@ pub async fn follow_request(
} else {
// create
match data.create_userfollow(UserFollow::new(user.id, id)).await {
Ok(_) => Json(ApiReturn {
ok: true,
message: "User followed".to_string(),
payload: (),
}),
Ok(_) => {
if let Err(e) = data
.create_notification(Notification::new(
"Somebody has followed you!".to_string(),
format!(
"You have been followed by [@{}](/api/v1/auth/profile/find/{}).",
user.username, user.id
),
id,
))
.await
{
return Json(e.into());
};
Json(ApiReturn {
ok: true,
message: "User followed".to_string(),
payload: (),
})
}
Err(e) => Json(e.into()),
}
}
@ -70,10 +86,22 @@ pub async fn block_request(
if let Ok(userfollow) = data.get_userfollow_by_initiator_receiver(user.id, id).await
{
// automatically unfollow
match data.delete_userfollow(userfollow.id, user).await {
match data.delete_userfollow(userfollow.id, &user).await {
Ok(_) => Json(ApiReturn {
ok: true,
message: "User unfollowed".to_string(),
message: "User blocked".to_string(),
payload: (),
}),
Err(e) => Json(e.into()),
}
} else if let Ok(userfollow) =
data.get_userfollow_by_receiver_initiator(user.id, id).await
{
// automatically unfollow
match data.delete_userfollow(userfollow.id, &user).await {
Ok(_) => Json(ApiReturn {
ok: true,
message: "User blocked".to_string(),
payload: (),
}),
Err(e) => Json(e.into()),

View file

@ -55,11 +55,15 @@ pub fn profile_context(
communities: &Vec<Community>,
is_self: bool,
is_following: bool,
is_following_you: bool,
is_blocking: bool,
) {
context.insert("profile", &profile);
context.insert("communities", &communities);
context.insert("is_self", &is_self);
context.insert("is_following", &is_following);
context.insert("is_following_you", &is_following_you);
context.insert("is_blocking", &is_blocking);
}
/// `/user/{username}`
@ -94,15 +98,17 @@ pub async fn posts_request(
// check for private profile
if other_user.settings.private_profile {
if let Some(ref ua) = user {
if data
.0
.get_userfollow_by_initiator_receiver(other_user.id, ua.id)
.await
.is_err()
{
return Err(Html(
render_error(Error::NotAllowed, &jar, &data, &user).await,
));
if ua.id != other_user.id {
if data
.0
.get_userfollow_by_initiator_receiver(other_user.id, ua.id)
.await
.is_err()
{
return Err(Html(
render_error(Error::NotAllowed, &jar, &data, &user).await,
));
}
}
} else {
return Err(Html(
@ -151,6 +157,24 @@ pub async fn posts_request(
false
};
let is_following_you = if let Some(ref ua) = user {
data.0
.get_userfollow_by_receiver_initiator(ua.id, other_user.id)
.await
.is_ok()
} else {
false
};
let is_blocking = if let Some(ref ua) = user {
data.0
.get_userblock_by_initiator_receiver(ua.id, other_user.id)
.await
.is_ok()
} else {
false
};
context.insert("posts", &posts);
profile_context(
&mut context,
@ -158,6 +182,8 @@ pub async fn posts_request(
&communities,
is_self,
is_following,
is_following_you,
is_blocking,
);
// return