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