]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/voting/undo_vote.rs
Handle Like, Undo/Like activities from Mastodon, add tests (fixes #2378) (#2380)
[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, 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       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, actor, &community, vec![], context).await
64   }
65 }
66
67 #[async_trait::async_trait(?Send)]
68 impl ActivityHandler for UndoVote {
69   type DataType = LemmyContext;
70   type Error = LemmyError;
71
72   fn id(&self) -> &Url {
73     &self.id
74   }
75
76   fn actor(&self) -> &Url {
77     self.actor.inner()
78   }
79
80   #[tracing::instrument(skip_all)]
81   async fn verify(
82     &self,
83     context: &Data<LemmyContext>,
84     request_counter: &mut i32,
85   ) -> Result<(), LemmyError> {
86     let community = self.get_community(context, request_counter).await?;
87     verify_person_in_community(&self.actor, &community, context, request_counter).await?;
88     verify_urls_match(self.actor.inner(), self.object.actor.inner())?;
89     self.object.verify(context, request_counter).await?;
90     Ok(())
91   }
92
93   #[tracing::instrument(skip_all)]
94   async fn receive(
95     self,
96     context: &Data<LemmyContext>,
97     request_counter: &mut i32,
98   ) -> Result<(), LemmyError> {
99     let actor = self
100       .actor
101       .dereference(context, local_instance(context), request_counter)
102       .await?;
103     let object = self
104       .object
105       .object
106       .dereference(context, local_instance(context), request_counter)
107       .await?;
108     match object {
109       PostOrComment::Post(p) => undo_vote_post(actor, &p, context).await,
110       PostOrComment::Comment(c) => undo_vote_comment(actor, &c, context).await,
111     }
112   }
113 }
114
115 #[async_trait::async_trait(?Send)]
116 impl GetCommunity for UndoVote {
117   #[tracing::instrument(skip_all)]
118   async fn get_community(
119     &self,
120     context: &LemmyContext,
121     request_counter: &mut i32,
122   ) -> Result<ApubCommunity, LemmyError> {
123     self.object.get_community(context, request_counter).await
124   }
125 }