]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/voting/undo_vote.rs
Add cargo feature for building lemmy_api_common with mininum deps (#2243)
[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_activity,
6     verify_is_public,
7     verify_person_in_community,
8     voting::{undo_vote_comment, undo_vote_post},
9   },
10   activity_lists::AnnouncableActivities,
11   objects::{community::ApubCommunity, person::ApubPerson},
12   protocol::activities::voting::{
13     undo_vote::UndoVote,
14     vote::{Vote, VoteType},
15   },
16   PostOrComment,
17 };
18 use activitystreams_kinds::{activity::UndoType, public};
19 use lemmy_api_common::utils::blocking;
20 use lemmy_apub_lib::{
21   data::Data,
22   object_id::ObjectId,
23   traits::{ActivityHandler, ActorType},
24   verify::verify_urls_match,
25 };
26 use lemmy_db_schema::{newtypes::CommunityId, source::community::Community, traits::Crud};
27 use lemmy_utils::LemmyError;
28 use lemmy_websocket::LemmyContext;
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 = blocking(context.pool(), move |conn| {
43       Community::read(conn, community_id)
44     })
45     .await??
46     .into();
47
48     let object = Vote::new(object, actor, &community, kind.clone(), context)?;
49     let id = generate_activity_id(
50       UndoType::Undo,
51       &context.settings().get_protocol_and_hostname(),
52     )?;
53     let undo_vote = UndoVote {
54       actor: ObjectId::new(actor.actor_id()),
55       to: vec![community.actor_id()],
56       object,
57       cc: vec![public()],
58       kind: UndoType::Undo,
59       id: id.clone(),
60       unparsed: Default::default(),
61     };
62     let activity = AnnouncableActivities::UndoVote(undo_vote);
63     send_activity_in_community(activity, &id, actor, &community, vec![], context).await
64   }
65 }
66
67 #[async_trait::async_trait(?Send)]
68 impl ActivityHandler for UndoVote {
69   type DataType = LemmyContext;
70
71   #[tracing::instrument(skip_all)]
72   async fn verify(
73     &self,
74     context: &Data<LemmyContext>,
75     request_counter: &mut i32,
76   ) -> Result<(), LemmyError> {
77     verify_is_public(&self.to, &self.cc)?;
78     verify_activity(&self.id, self.actor.inner(), &context.settings())?;
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, context.client(), request_counter)
95       .await?;
96     let object = self
97       .object
98       .object
99       .dereference(context, context.client(), 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 }