]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/voting/undo_vote.rs
Moving settings to Database. (#2492)
[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   check_apub_id_valid,
10   fetch_local_site_data,
11   local_instance,
12   objects::{community::ApubCommunity, person::ApubPerson},
13   protocol::activities::voting::{
14     undo_vote::UndoVote,
15     vote::{Vote, VoteType},
16   },
17   ActorType,
18   PostOrComment,
19 };
20 use activitypub_federation::{
21   core::object_id::ObjectId,
22   data::Data,
23   traits::ActivityHandler,
24   utils::verify_urls_match,
25 };
26 use activitystreams_kinds::activity::UndoType;
27 use lemmy_api_common::utils::blocking;
28 use lemmy_db_schema::{newtypes::CommunityId, source::community::Community, traits::Crud};
29 use lemmy_utils::error::LemmyError;
30 use lemmy_websocket::LemmyContext;
31 use url::Url;
32
33 impl UndoVote {
34   /// UndoVote has as:Public value in cc field, unlike other activities. This indicates to other
35   /// software (like GNU social, or presumably Mastodon), that the like actor should not be
36   /// disclosed.
37   #[tracing::instrument(skip_all)]
38   pub async fn send(
39     object: &PostOrComment,
40     actor: &ApubPerson,
41     community_id: CommunityId,
42     kind: VoteType,
43     context: &LemmyContext,
44   ) -> Result<(), LemmyError> {
45     let community: ApubCommunity = blocking(context.pool(), move |conn| {
46       Community::read(conn, community_id)
47     })
48     .await??
49     .into();
50
51     let object = Vote::new(object, actor, kind.clone(), context)?;
52     let id = generate_activity_id(
53       UndoType::Undo,
54       &context.settings().get_protocol_and_hostname(),
55     )?;
56     let undo_vote = UndoVote {
57       actor: ObjectId::new(actor.actor_id()),
58       object,
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, 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 local_site_data = blocking(context.pool(), fetch_local_site_data).await??;
88     check_apub_id_valid(self.id(), &local_site_data, context.settings())
89       .map_err(LemmyError::from_message)?;
90     let community = self.get_community(context, request_counter).await?;
91     verify_person_in_community(&self.actor, &community, context, request_counter).await?;
92     verify_urls_match(self.actor.inner(), self.object.actor.inner())?;
93     self.object.verify(context, request_counter).await?;
94     Ok(())
95   }
96
97   #[tracing::instrument(skip_all)]
98   async fn receive(
99     self,
100     context: &Data<LemmyContext>,
101     request_counter: &mut i32,
102   ) -> Result<(), LemmyError> {
103     let actor = self
104       .actor
105       .dereference(context, local_instance(context), request_counter)
106       .await?;
107     let object = self
108       .object
109       .object
110       .dereference(context, local_instance(context), request_counter)
111       .await?;
112     match object {
113       PostOrComment::Post(p) => undo_vote_post(actor, &p, context).await,
114       PostOrComment::Comment(c) => undo_vote_comment(actor, &c, context).await,
115     }
116   }
117 }
118
119 #[async_trait::async_trait(?Send)]
120 impl GetCommunity for UndoVote {
121   #[tracing::instrument(skip_all)]
122   async fn get_community(
123     &self,
124     context: &LemmyContext,
125     request_counter: &mut i32,
126   ) -> Result<ApubCommunity, LemmyError> {
127     self.object.get_community(context, request_counter).await
128   }
129 }