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