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