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