]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/voting/undo_vote.rs
Remove ActivityFields trait, deserialize into another struct instead
[lemmy.git] / crates / apub / src / activities / voting / undo_vote.rs
1 use crate::{
2   activities::{
3     community::{announce::GetCommunity, send_to_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   fetcher::object_id::ObjectId,
12   objects::{community::ApubCommunity, person::ApubPerson},
13   protocol::activities::voting::{
14     undo_vote::UndoVote,
15     vote::{Vote, VoteType},
16   },
17   PostOrComment,
18 };
19 use activitystreams::{activity::kind::UndoType, public};
20 use lemmy_api_common::blocking;
21 use lemmy_apub_lib::{
22   data::Data,
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 use std::ops::Deref;
30
31 impl UndoVote {
32   pub async fn send(
33     object: &PostOrComment,
34     actor: &ApubPerson,
35     community_id: CommunityId,
36     kind: VoteType,
37     context: &LemmyContext,
38   ) -> Result<(), LemmyError> {
39     let community: ApubCommunity = blocking(context.pool(), move |conn| {
40       Community::read(conn, community_id)
41     })
42     .await??
43     .into();
44
45     let object = Vote::new(object, actor, &community, kind.clone(), context)?;
46     let id = generate_activity_id(
47       UndoType::Undo,
48       &context.settings().get_protocol_and_hostname(),
49     )?;
50     let undo_vote = UndoVote {
51       actor: ObjectId::new(actor.actor_id()),
52       to: vec![public()],
53       object,
54       cc: vec![community.actor_id()],
55       kind: UndoType::Undo,
56       id: id.clone(),
57       unparsed: Default::default(),
58     };
59     let activity = AnnouncableActivities::UndoVote(undo_vote);
60     send_to_community(activity, &id, actor, &community, vec![], context).await
61   }
62 }
63
64 #[async_trait::async_trait(?Send)]
65 impl ActivityHandler for UndoVote {
66   type DataType = LemmyContext;
67   async fn verify(
68     &self,
69     context: &Data<LemmyContext>,
70     request_counter: &mut i32,
71   ) -> Result<(), LemmyError> {
72     verify_is_public(&self.to)?;
73     verify_activity(&self.id, self.actor.inner(), &context.settings())?;
74     let community = self.get_community(context, request_counter).await?;
75     verify_person_in_community(&self.actor, &community, context, request_counter).await?;
76     verify_urls_match(self.actor.inner(), self.object.actor.inner())?;
77     self.object.verify(context, request_counter).await?;
78     Ok(())
79   }
80
81   async fn receive(
82     self,
83     context: &Data<LemmyContext>,
84     request_counter: &mut i32,
85   ) -> Result<(), LemmyError> {
86     let actor = self.actor.dereference(context, request_counter).await?;
87     let object = self
88       .object
89       .object
90       .dereference(context, request_counter)
91       .await?;
92     match object {
93       PostOrComment::Post(p) => undo_vote_post(actor, p.deref(), context).await,
94       PostOrComment::Comment(c) => undo_vote_comment(actor, &c, context).await,
95     }
96   }
97 }
98
99 #[async_trait::async_trait(?Send)]
100 impl GetCommunity for UndoVote {
101   async fn get_community(
102     &self,
103     context: &LemmyContext,
104     request_counter: &mut i32,
105   ) -> Result<ApubCommunity, LemmyError> {
106     self.object.get_community(context, request_counter).await
107   }
108 }