4 verify_person_in_community,
5 voting::{vote_comment, vote_post},
8 objects::{community::ApubCommunity, person::ApubPerson},
10 activities::voting::vote::{Vote, VoteType},
15 use activitypub_federation::{
17 fetch::object_id::ObjectId,
18 traits::{ActivityHandler, Actor},
21 use lemmy_api_common::context::LemmyContext;
22 use lemmy_db_schema::source::local_site::LocalSite;
23 use lemmy_utils::error::LemmyError;
27 pub(in crate::activities::voting) fn new(
28 object_id: ObjectId<PostOrComment>,
30 community: &ApubCommunity,
32 context: &Data<LemmyContext>,
33 ) -> Result<Vote, LemmyError> {
35 actor: actor.id().into(),
38 id: generate_activity_id(kind, &context.settings().get_protocol_and_hostname())?,
39 audience: Some(community.id().into()),
44 #[async_trait::async_trait]
45 impl ActivityHandler for Vote {
46 type DataType = LemmyContext;
47 type Error = LemmyError;
49 fn id(&self) -> &Url {
53 fn actor(&self) -> &Url {
57 #[tracing::instrument(skip_all)]
58 async fn verify(&self, context: &Data<LemmyContext>) -> Result<(), LemmyError> {
59 let community = self.community(context).await?;
60 verify_person_in_community(&self.actor, &community, context).await?;
61 let enable_downvotes = LocalSite::read(context.pool())
63 .map(|l| l.enable_downvotes)
65 if self.kind == VoteType::Dislike && !enable_downvotes {
66 return Err(anyhow!("Downvotes disabled").into());
71 #[tracing::instrument(skip_all)]
72 async fn receive(self, context: &Data<LemmyContext>) -> Result<(), LemmyError> {
73 insert_activity(&self.id, &self, false, true, context).await?;
74 let actor = self.actor.dereference(context).await?;
75 let object = self.object.dereference(context).await?;
77 PostOrComment::Post(p) => vote_post(&self.kind, actor, &p, context).await,
78 PostOrComment::Comment(c) => vote_comment(&self.kind, actor, &c, context).await,