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