]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/voting/vote.rs
Consolidate reqwest clients, use reqwest-middleware for tracing
[lemmy.git] / crates / apub / src / activities / voting / 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::{vote_comment, vote_post},
9   },
10   activity_lists::AnnouncableActivities,
11   objects::{community::ApubCommunity, person::ApubPerson},
12   protocol::activities::voting::vote::{Vote, VoteType},
13   PostOrComment,
14 };
15 use activitystreams_kinds::public;
16 use lemmy_api_common::blocking;
17 use lemmy_apub_lib::{
18   data::Data,
19   object_id::ObjectId,
20   traits::{ActivityHandler, ActorType},
21 };
22 use lemmy_db_schema::{
23   newtypes::CommunityId,
24   source::{community::Community, post::Post},
25   traits::Crud,
26 };
27 use lemmy_utils::LemmyError;
28 use lemmy_websocket::LemmyContext;
29
30 impl Vote {
31   pub(in crate::activities::voting) fn new(
32     object: &PostOrComment,
33     actor: &ApubPerson,
34     community: &ApubCommunity,
35     kind: VoteType,
36     context: &LemmyContext,
37   ) -> Result<Vote, LemmyError> {
38     Ok(Vote {
39       actor: ObjectId::new(actor.actor_id()),
40       to: vec![public()],
41       object: ObjectId::new(object.ap_id()),
42       cc: vec![community.actor_id()],
43       kind: kind.clone(),
44       id: generate_activity_id(kind, &context.settings().get_protocol_and_hostname())?,
45       unparsed: Default::default(),
46     })
47   }
48
49   #[tracing::instrument(skip_all)]
50   pub async fn send(
51     object: &PostOrComment,
52     actor: &ApubPerson,
53     community_id: CommunityId,
54     kind: VoteType,
55     context: &LemmyContext,
56   ) -> Result<(), LemmyError> {
57     let community = blocking(context.pool(), move |conn| {
58       Community::read(conn, community_id)
59     })
60     .await??
61     .into();
62     let vote = Vote::new(object, actor, &community, kind, context)?;
63     let vote_id = vote.id.clone();
64
65     let activity = AnnouncableActivities::Vote(vote);
66     send_activity_in_community(activity, &vote_id, actor, &community, vec![], context).await
67   }
68 }
69
70 #[async_trait::async_trait(?Send)]
71 impl ActivityHandler for Vote {
72   type DataType = LemmyContext;
73
74   #[tracing::instrument(skip_all)]
75   async fn verify(
76     &self,
77     context: &Data<LemmyContext>,
78     request_counter: &mut i32,
79   ) -> Result<(), LemmyError> {
80     verify_is_public(&self.to, &self.cc)?;
81     verify_activity(&self.id, self.actor.inner(), &context.settings())?;
82     let community = self.get_community(context, request_counter).await?;
83     verify_person_in_community(&self.actor, &community, context, request_counter).await?;
84     Ok(())
85   }
86
87   #[tracing::instrument(skip_all)]
88   async fn receive(
89     self,
90     context: &Data<LemmyContext>,
91     request_counter: &mut i32,
92   ) -> Result<(), LemmyError> {
93     let actor = self
94       .actor
95       .dereference(context, context.client(), request_counter)
96       .await?;
97     let object = self
98       .object
99       .dereference(context, context.client(), request_counter)
100       .await?;
101     match object {
102       PostOrComment::Post(p) => vote_post(&self.kind, actor, &p, context).await,
103       PostOrComment::Comment(c) => vote_comment(&self.kind, actor, &c, context).await,
104     }
105   }
106 }
107
108 #[async_trait::async_trait(?Send)]
109 impl GetCommunity for Vote {
110   #[tracing::instrument(skip_all)]
111   async fn get_community(
112     &self,
113     context: &LemmyContext,
114     request_counter: &mut i32,
115   ) -> Result<ApubCommunity, LemmyError> {
116     let object = self
117       .object
118       .dereference(context, context.client(), request_counter)
119       .await?;
120     let cid = match object {
121       PostOrComment::Post(p) => p.community_id,
122       PostOrComment::Comment(c) => {
123         blocking(context.pool(), move |conn| Post::read(conn, c.post_id))
124           .await??
125           .community_id
126       }
127     };
128     let community = blocking(context.pool(), move |conn| Community::read(conn, cid)).await??;
129     Ok(community.into())
130   }
131 }