]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/voting/undo_vote.rs
Use audience field to federate items in groups (fixes #2464) (#2584)
[lemmy.git] / crates / apub / src / activities / voting / undo_vote.rs
1 use crate::{
2   activities::{
3     community::send_activity_in_community,
4     generate_activity_id,
5     verify_person_in_community,
6     voting::{undo_vote_comment, undo_vote_post},
7   },
8   activity_lists::AnnouncableActivities,
9   local_instance,
10   objects::{community::ApubCommunity, person::ApubPerson},
11   protocol::{
12     activities::voting::{
13       undo_vote::UndoVote,
14       vote::{Vote, VoteType},
15     },
16     InCommunity,
17   },
18   ActorType,
19   PostOrComment,
20 };
21 use activitypub_federation::{
22   core::object_id::ObjectId,
23   data::Data,
24   traits::ActivityHandler,
25   utils::verify_urls_match,
26 };
27 use activitystreams_kinds::activity::UndoType;
28 use lemmy_db_schema::{newtypes::CommunityId, source::community::Community, traits::Crud};
29 use lemmy_utils::error::LemmyError;
30 use lemmy_websocket::LemmyContext;
31 use url::Url;
32
33 impl UndoVote {
34   /// UndoVote has as:Public value in cc field, unlike other activities. This indicates to other
35   /// software (like GNU social, or presumably Mastodon), that the like actor should not be
36   /// disclosed.
37   #[tracing::instrument(skip_all)]
38   pub async fn send(
39     object: &PostOrComment,
40     actor: &ApubPerson,
41     community_id: CommunityId,
42     kind: VoteType,
43     context: &LemmyContext,
44   ) -> Result<(), LemmyError> {
45     let community: ApubCommunity = Community::read(context.pool(), community_id).await?.into();
46
47     let object = Vote::new(object, actor, &community, kind.clone(), context)?;
48     let id = generate_activity_id(
49       UndoType::Undo,
50       &context.settings().get_protocol_and_hostname(),
51     )?;
52     let undo_vote = UndoVote {
53       actor: ObjectId::new(actor.actor_id()),
54       object,
55       kind: UndoType::Undo,
56       id: id.clone(),
57       audience: Some(ObjectId::new(community.actor_id())),
58     };
59     let activity = AnnouncableActivities::UndoVote(undo_vote);
60     send_activity_in_community(activity, actor, &community, vec![], false, context).await
61   }
62 }
63
64 #[async_trait::async_trait(?Send)]
65 impl ActivityHandler for UndoVote {
66   type DataType = LemmyContext;
67   type Error = LemmyError;
68
69   fn id(&self) -> &Url {
70     &self.id
71   }
72
73   fn actor(&self) -> &Url {
74     self.actor.inner()
75   }
76
77   #[tracing::instrument(skip_all)]
78   async fn verify(
79     &self,
80     context: &Data<LemmyContext>,
81     request_counter: &mut i32,
82   ) -> Result<(), LemmyError> {
83     let community = self.community(context, request_counter).await?;
84     verify_person_in_community(&self.actor, &community, context, request_counter).await?;
85     verify_urls_match(self.actor.inner(), self.object.actor.inner())?;
86     self.object.verify(context, request_counter).await?;
87     Ok(())
88   }
89
90   #[tracing::instrument(skip_all)]
91   async fn receive(
92     self,
93     context: &Data<LemmyContext>,
94     request_counter: &mut i32,
95   ) -> Result<(), LemmyError> {
96     let actor = self
97       .actor
98       .dereference(context, local_instance(context).await, request_counter)
99       .await?;
100     let object = self
101       .object
102       .object
103       .dereference(context, local_instance(context).await, request_counter)
104       .await?;
105     match object {
106       PostOrComment::Post(p) => undo_vote_post(actor, &p, context).await,
107       PostOrComment::Comment(c) => undo_vote_comment(actor, &c, context).await,
108     }
109   }
110 }