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