3 generate_activity_id, verify_person_in_community,
4 voting::{vote_comment, vote_post},
6 insert_received_activity,
7 objects::{community::ApubCommunity, person::ApubPerson},
9 activities::voting::vote::{Vote, VoteType},
14 use activitypub_federation::{
16 fetch::object_id::ObjectId,
17 traits::{ActivityHandler, Actor},
20 use lemmy_api_common::context::LemmyContext;
21 use lemmy_db_schema::source::local_site::LocalSite;
22 use lemmy_utils::error::LemmyError;
26 pub(in crate::activities::voting) fn new(
27 object_id: ObjectId<PostOrComment>,
29 community: &ApubCommunity,
31 context: &Data<LemmyContext>,
32 ) -> Result<Vote, LemmyError> {
34 actor: actor.id().into(),
37 id: generate_activity_id(kind, &context.settings().get_protocol_and_hostname())?,
38 audience: Some(community.id().into()),
43 #[async_trait::async_trait]
44 impl ActivityHandler for Vote {
45 type DataType = LemmyContext;
46 type Error = LemmyError;
48 fn id(&self) -> &Url {
52 fn actor(&self) -> &Url {
56 #[tracing::instrument(skip_all)]
57 async fn verify(&self, context: &Data<LemmyContext>) -> Result<(), LemmyError> {
58 insert_received_activity(&self.id, context).await?;
59 let community = self.community(context).await?;
60 verify_person_in_community(&self.actor, &community, context).await?;
61 let enable_downvotes = LocalSite::read(&mut context.pool())
63 .map(|l| l.enable_downvotes)
65 let enable_federated_downvotes = LocalSite::read(&mut context.pool())
67 .map(|l| l.enable_federated_downvotes)
69 if self.kind == VoteType::Dislike && (!enable_downvotes || !enable_federated_downvotes) {
70 return Err(anyhow!("Downvotes disabled").into());
75 #[tracing::instrument(skip_all)]
76 async fn receive(self, context: &Data<LemmyContext>) -> Result<(), LemmyError> {
77 let actor = self.actor.dereference(context).await?;
78 let object = self.object.dereference(context).await?;
80 PostOrComment::Post(p) => vote_post(&self.kind, actor, &p, context).await,
81 PostOrComment::Comment(c) => vote_comment(&self.kind, actor, &c, context).await,