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