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