fix: don't toggle follow when following back
This commit is contained in:
parent
70ecc6f96e
commit
0256f38e5d
7 changed files with 78 additions and 7 deletions
|
@ -71,7 +71,6 @@
|
|||
("name" "content")
|
||||
("id" "content")
|
||||
("placeholder" "content")
|
||||
("required" "")
|
||||
("minlength" "2")
|
||||
("maxlength" "4096")))
|
||||
(div
|
||||
|
|
|
@ -290,7 +290,7 @@
|
|||
]);
|
||||
|
||||
fetch(
|
||||
\"/api/v1/auth/user/{{ profile.id }}/follow\",
|
||||
\"/api/v1/auth/user/{{ profile.id }}/follow/toggle\",
|
||||
{
|
||||
method: \"POST\",
|
||||
},
|
||||
|
|
|
@ -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())
|
||||
|
|
|
@ -193,9 +193,13 @@
|
|||
like.classList.add("green");
|
||||
like.querySelector("svg").classList.add("filled");
|
||||
|
||||
if (dislike) {
|
||||
dislike.classList.remove("red");
|
||||
}
|
||||
} else {
|
||||
if (dislike) {
|
||||
dislike.classList.add("red");
|
||||
}
|
||||
|
||||
like.classList.remove("green");
|
||||
like.querySelector("svg").classList.remove("filled");
|
||||
|
|
|
@ -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>,
|
||||
|
|
|
@ -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),
|
||||
|
|
1
justfile
1
justfile
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue