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
|
@ -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>,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue