]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/voting/undo_vote.rs
bcb8ee4068241c22083291feb6d9b2958de0cc37
[lemmy.git] / crates / apub / src / activities / voting / undo_vote.rs
1 use crate::{
2   activities::{
3     generate_activity_id,
4     verify_person_in_community,
5     voting::{undo_vote_comment, undo_vote_post},
6   },
7   insert_activity,
8   objects::{community::ApubCommunity, person::ApubPerson},
9   protocol::{
10     activities::voting::{undo_vote::UndoVote, vote::Vote},
11     InCommunity,
12   },
13   PostOrComment,
14 };
15 use activitypub_federation::{
16   config::Data,
17   kinds::activity::UndoType,
18   protocol::verification::verify_urls_match,
19   traits::{ActivityHandler, Actor},
20 };
21 use lemmy_api_common::context::LemmyContext;
22 use lemmy_utils::error::LemmyError;
23 use url::Url;
24
25 impl UndoVote {
26   pub(in crate::activities::voting) fn new(
27     vote: Vote,
28     actor: &ApubPerson,
29     community: &ApubCommunity,
30     context: &Data<LemmyContext>,
31   ) -> Result<Self, LemmyError> {
32     Ok(UndoVote {
33       actor: actor.id().into(),
34       object: vote,
35       kind: UndoType::Undo,
36       id: generate_activity_id(
37         UndoType::Undo,
38         &context.settings().get_protocol_and_hostname(),
39       )?,
40       audience: Some(community.id().into()),
41     })
42   }
43 }
44
45 #[async_trait::async_trait]
46 impl ActivityHandler for UndoVote {
47   type DataType = LemmyContext;
48   type Error = LemmyError;
49
50   fn id(&self) -> &Url {
51     &self.id
52   }
53
54   fn actor(&self) -> &Url {
55     self.actor.inner()
56   }
57
58   #[tracing::instrument(skip_all)]
59   async fn verify(&self, context: &Data<LemmyContext>) -> Result<(), LemmyError> {
60     let community = self.community(context).await?;
61     verify_person_in_community(&self.actor, &community, context).await?;
62     verify_urls_match(self.actor.inner(), self.object.actor.inner())?;
63     self.object.verify(context).await?;
64     Ok(())
65   }
66
67   #[tracing::instrument(skip_all)]
68   async fn receive(self, context: &Data<LemmyContext>) -> Result<(), LemmyError> {
69     insert_activity(&self.id, &self, false, true, context).await?;
70     let actor = self.actor.dereference(context).await?;
71     let object = self.object.object.dereference(context).await?;
72     match object {
73       PostOrComment::Post(p) => undo_vote_post(actor, &p, context).await,
74       PostOrComment::Comment(c) => undo_vote_comment(actor, &c, context).await,
75     }
76   }
77 }