]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/voting/vote.rs
Extract Activitypub logic into separate library (#2288)
[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_is_public,
6     verify_person_in_community,
7     voting::{vote_comment, vote_post},
8   },
9   activity_lists::AnnouncableActivities,
10   local_instance,
11   objects::{community::ApubCommunity, person::ApubPerson},
12   protocol::activities::voting::vote::{Vote, VoteType},
13   ActorType,
14   PostOrComment,
15 };
16 use activitypub_federation::{core::object_id::ObjectId, data::Data, traits::ActivityHandler};
17 use activitystreams_kinds::public;
18 use anyhow::anyhow;
19 use lemmy_api_common::utils::blocking;
20 use lemmy_db_schema::{
21   newtypes::CommunityId,
22   source::{community::Community, post::Post, site::Site},
23   traits::Crud,
24 };
25 use lemmy_utils::error::LemmyError;
26 use lemmy_websocket::LemmyContext;
27 use url::Url;
28
29 /// Vote has as:Public value in cc field, unlike other activities. This indicates to other software
30 /// (like GNU social, or presumably Mastodon), that the like actor should not be disclosed.
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![community.actor_id()],
42       object: ObjectId::new(object.ap_id()),
43       cc: vec![public()],
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   type Error = LemmyError;
75
76   fn id(&self) -> &Url {
77     &self.id
78   }
79
80   fn actor(&self) -> &Url {
81     self.actor.inner()
82   }
83
84   #[tracing::instrument(skip_all)]
85   async fn verify(
86     &self,
87     context: &Data<LemmyContext>,
88     request_counter: &mut i32,
89   ) -> Result<(), LemmyError> {
90     verify_is_public(&self.to, &self.cc)?;
91     let community = self.get_community(context, request_counter).await?;
92     verify_person_in_community(&self.actor, &community, context, request_counter).await?;
93     let site = blocking(context.pool(), Site::read_local_site).await??;
94     if self.kind == VoteType::Dislike && !site.enable_downvotes {
95       return Err(anyhow!("Downvotes disabled").into());
96     }
97     Ok(())
98   }
99
100   #[tracing::instrument(skip_all)]
101   async fn receive(
102     self,
103     context: &Data<LemmyContext>,
104     request_counter: &mut i32,
105   ) -> Result<(), LemmyError> {
106     let actor = self
107       .actor
108       .dereference::<LemmyError>(context, local_instance(context), request_counter)
109       .await?;
110     let object = self
111       .object
112       .dereference::<LemmyError>(context, local_instance(context), request_counter)
113       .await?;
114     match object {
115       PostOrComment::Post(p) => vote_post(&self.kind, actor, &p, context).await,
116       PostOrComment::Comment(c) => vote_comment(&self.kind, actor, &c, context).await,
117     }
118   }
119 }
120
121 #[async_trait::async_trait(?Send)]
122 impl GetCommunity for Vote {
123   #[tracing::instrument(skip_all)]
124   async fn get_community(
125     &self,
126     context: &LemmyContext,
127     request_counter: &mut i32,
128   ) -> Result<ApubCommunity, LemmyError> {
129     let object = self
130       .object
131       .dereference::<LemmyError>(context, local_instance(context), request_counter)
132       .await?;
133     let cid = match object {
134       PostOrComment::Post(p) => p.community_id,
135       PostOrComment::Comment(c) => {
136         blocking(context.pool(), move |conn| Post::read(conn, c.post_id))
137           .await??
138           .community_id
139       }
140     };
141     let community = blocking(context.pool(), move |conn| Community::read(conn, cid)).await??;
142     Ok(community.into())
143   }
144 }