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