Fix handling of apub downvote (fixes #4545) (#4551)

* Fix handling of apub downvote (fixes #4545)

* fmt

---------

Co-authored-by: SleeplessOne1917 <28871516+SleeplessOne1917@users.noreply.github.com>
This commit is contained in:
Nutomic 2024-03-22 23:41:09 +01:00 committed by GitHub
parent baf5921d2c
commit 21547dedf7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2,7 +2,7 @@ use crate::{
activities::{ activities::{
generate_activity_id, generate_activity_id,
verify_person_in_community, verify_person_in_community,
voting::{vote_comment, vote_post}, voting::{undo_vote_comment, undo_vote_post, vote_comment, vote_post},
}, },
insert_received_activity, insert_received_activity,
objects::{community::ApubCommunity, person::ApubPerson}, objects::{community::ApubCommunity, person::ApubPerson},
@ -17,7 +17,6 @@ use activitypub_federation::{
fetch::object_id::ObjectId, fetch::object_id::ObjectId,
traits::{ActivityHandler, Actor}, traits::{ActivityHandler, Actor},
}; };
use anyhow::anyhow;
use lemmy_api_common::{context::LemmyContext, utils::check_bot_account}; use lemmy_api_common::{context::LemmyContext, utils::check_bot_account};
use lemmy_db_schema::source::local_site::LocalSite; use lemmy_db_schema::source::local_site::LocalSite;
use lemmy_utils::error::LemmyError; use lemmy_utils::error::LemmyError;
@ -58,16 +57,8 @@ impl ActivityHandler for Vote {
async fn verify(&self, context: &Data<LemmyContext>) -> Result<(), LemmyError> { async fn verify(&self, context: &Data<LemmyContext>) -> Result<(), LemmyError> {
let community = self.community(context).await?; let community = self.community(context).await?;
verify_person_in_community(&self.actor, &community, context).await?; verify_person_in_community(&self.actor, &community, context).await?;
let enable_downvotes = LocalSite::read(&mut context.pool())
.await
.map(|l| l.enable_downvotes)
.unwrap_or(true);
if self.kind == VoteType::Dislike && !enable_downvotes {
Err(anyhow!("Downvotes disabled").into())
} else {
Ok(()) Ok(())
} }
}
#[tracing::instrument(skip_all)] #[tracing::instrument(skip_all)]
async fn receive(self, context: &Data<LemmyContext>) -> Result<(), LemmyError> { async fn receive(self, context: &Data<LemmyContext>) -> Result<(), LemmyError> {
@ -77,9 +68,22 @@ impl ActivityHandler for Vote {
check_bot_account(&actor.0)?; check_bot_account(&actor.0)?;
let enable_downvotes = LocalSite::read(&mut context.pool())
.await
.map(|l| l.enable_downvotes)
.unwrap_or(true);
if self.kind == VoteType::Dislike && !enable_downvotes {
// If this is a downvote but downvotes are ignored, only undo any existing vote
match object {
PostOrComment::Post(p) => undo_vote_post(actor, &p, context).await,
PostOrComment::Comment(c) => undo_vote_comment(actor, &c, context).await,
}
} else {
// Otherwise apply the vote normally
match object { match object {
PostOrComment::Post(p) => vote_post(&self.kind, actor, &p, context).await, PostOrComment::Post(p) => vote_post(&self.kind, actor, &p, context).await,
PostOrComment::Comment(c) => vote_comment(&self.kind, actor, &c, context).await, PostOrComment::Comment(c) => vote_comment(&self.kind, actor, &c, context).await,
} }
} }
}
} }