fix: don't toggle follow when following back

This commit is contained in:
trisua 2025-07-15 15:59:05 -04:00
parent 70ecc6f96e
commit 0256f38e5d
7 changed files with 78 additions and 7 deletions

View file

@ -71,7 +71,6 @@
("name" "content")
("id" "content")
("placeholder" "content")
("required" "")
("minlength" "2")
("maxlength" "4096")))
(div

View file

@ -290,7 +290,7 @@
]);
fetch(
\"/api/v1/auth/user/{{ profile.id }}/follow\",
\"/api/v1/auth/user/{{ profile.id }}/follow/toggle\",
{
method: \"POST\",
},

View file

@ -81,7 +81,7 @@
(script
(text "globalThis.toggle_follow_user = async (e) => {
await trigger(\"atto::debounce\", [\"users::follow\"]);
fetch(\"/api/v1/auth/user/{{ profile.id }}/follow\", {
fetch(\"/api/v1/auth/user/{{ profile.id }}/follow/toggle\", {
method: \"POST\",
})
.then((res) => res.json())

View file

@ -193,9 +193,13 @@
like.classList.add("green");
like.querySelector("svg").classList.add("filled");
dislike.classList.remove("red");
if (dislike) {
dislike.classList.remove("red");
}
} else {
dislike.classList.add("red");
if (dislike) {
dislike.classList.add("red");
}
like.classList.remove("green");
like.querySelector("svg").classList.remove("filled");

View file

@ -17,7 +17,7 @@ use tetratto_core::model::{
};
/// Toggle following on the given user.
pub async fn follow_request(
pub async fn toggle_follow_request(
jar: CookieJar,
Path(id): Path<usize>,
Extension(data): Extension<State>,
@ -154,6 +154,71 @@ pub async fn accept_follow_request(
}
}
pub async fn follow_request(
jar: CookieJar,
Path(id): Path<usize>,
Extension(data): Extension<State>,
) -> impl IntoResponse {
let data = &(data.read().await).0;
let mut user = match get_user_from_token!(jar, data, oauth::AppScope::UserManageFollowing) {
Some(ua) => ua,
None => return Json(Error::NotAllowed.into()),
};
if data
.get_userfollow_by_initiator_receiver(user.id, id)
.await
.is_ok()
{
return Json(Error::MiscError("Already following user".to_string()).into());
} else {
match data
.create_userfollow(UserFollow::new(user.id, id), &user, false)
.await
{
Ok(r) => {
if r == FollowResult::Followed {
if let Err(e) = data
.create_notification(Notification::new(
"Somebody has followed you!".to_string(),
format!(
"You have been followed by [@{}](/api/v1/auth/user/find/{}).",
user.username, user.id
),
id,
))
.await
{
return Json(e.into());
};
// award achievement
if let Err(e) = data
.add_achievement(&mut user, AchievementName::FollowUser.into(), true)
.await
{
return Json(e.into());
}
// ...
Json(ApiReturn {
ok: true,
message: "User followed".to_string(),
payload: (),
})
} else {
Json(ApiReturn {
ok: true,
message: "Asked to follow user".to_string(),
payload: (),
})
}
}
Err(e) => Json(e.into()),
}
}
}
pub async fn force_unfollow_me_request(
jar: CookieJar,
Path(id): Path<usize>,

View file

@ -285,6 +285,10 @@ pub fn routes() -> Router {
.route("/auth/user/{id}/avatar", get(auth::images::avatar_request))
.route("/auth/user/{id}/banner", get(auth::images::banner_request))
.route("/auth/user/{id}/follow", post(auth::social::follow_request))
.route(
"/auth/user/{id}/follow/toggle",
post(auth::social::toggle_follow_request),
)
.route(
"/auth/user/{id}/follow/cancel",
post(auth::social::cancel_follow_request),

View file

@ -10,6 +10,5 @@ doc:
cargo doc --document-private-items --no-deps
test:
sudo pkill -e tetratto
cd example && LITTLEWEB=true PORT=4119 cargo run &
cd example && cargo run