]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/voting/vote.rs
Implement federated user following (fixes #752) (#2577)
[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_person_in_community,
6     voting::{vote_comment, vote_post},
7   },
8   activity_lists::AnnouncableActivities,
9   local_instance,
10   objects::{community::ApubCommunity, person::ApubPerson},
11   protocol::activities::voting::vote::{Vote, VoteType},
12   ActorType,
13   PostOrComment,
14 };
15 use activitypub_federation::{core::object_id::ObjectId, data::Data, traits::ActivityHandler};
16 use anyhow::anyhow;
17 use lemmy_db_schema::{
18   newtypes::CommunityId,
19   source::{community::Community, local_site::LocalSite, post::Post},
20   traits::Crud,
21 };
22 use lemmy_utils::error::LemmyError;
23 use lemmy_websocket::LemmyContext;
24 use url::Url;
25
26 /// Vote has as:Public value in cc field, unlike other activities. This indicates to other software
27 /// (like GNU social, or presumably Mastodon), that the like actor should not be disclosed.
28 impl Vote {
29   pub(in crate::activities::voting) fn new(
30     object: &PostOrComment,
31     actor: &ApubPerson,
32     kind: VoteType,
33     context: &LemmyContext,
34   ) -> Result<Vote, LemmyError> {
35     Ok(Vote {
36       actor: ObjectId::new(actor.actor_id()),
37       object: ObjectId::new(object.ap_id()),
38       kind: kind.clone(),
39       id: generate_activity_id(kind, &context.settings().get_protocol_and_hostname())?,
40     })
41   }
42
43   #[tracing::instrument(skip_all)]
44   pub async fn send(
45     object: &PostOrComment,
46     actor: &ApubPerson,
47     community_id: CommunityId,
48     kind: VoteType,
49     context: &LemmyContext,
50   ) -> Result<(), LemmyError> {
51     let community = Community::read(context.pool(), community_id).await?.into();
52     let vote = Vote::new(object, actor, kind, context)?;
53
54     let activity = AnnouncableActivities::Vote(vote);
55     send_activity_in_community(activity, actor, &community, vec![], false, context).await
56   }
57 }
58
59 #[async_trait::async_trait(?Send)]
60 impl ActivityHandler for Vote {
61   type DataType = LemmyContext;
62   type Error = LemmyError;
63
64   fn id(&self) -> &Url {
65     &self.id
66   }
67
68   fn actor(&self) -> &Url {
69     self.actor.inner()
70   }
71
72   #[tracing::instrument(skip_all)]
73   async fn verify(
74     &self,
75     context: &Data<LemmyContext>,
76     request_counter: &mut i32,
77   ) -> Result<(), LemmyError> {
78     let community = self.get_community(context, request_counter).await?;
79     verify_person_in_community(&self.actor, &community, context, request_counter).await?;
80     let enable_downvotes = LocalSite::read(context.pool())
81       .await
82       .map(|l| l.enable_downvotes)
83       .unwrap_or(true);
84     if self.kind == VoteType::Dislike && !enable_downvotes {
85       return Err(anyhow!("Downvotes disabled").into());
86     }
87     Ok(())
88   }
89
90   #[tracing::instrument(skip_all)]
91   async fn receive(
92     self,
93     context: &Data<LemmyContext>,
94     request_counter: &mut i32,
95   ) -> Result<(), LemmyError> {
96     let actor = self
97       .actor
98       .dereference(context, local_instance(context).await, request_counter)
99       .await?;
100     let object = self
101       .object
102       .dereference(context, local_instance(context).await, request_counter)
103       .await?;
104     match object {
105       PostOrComment::Post(p) => vote_post(&self.kind, actor, &p, context).await,
106       PostOrComment::Comment(c) => vote_comment(&self.kind, actor, &c, context).await,
107     }
108   }
109 }
110
111 #[async_trait::async_trait(?Send)]
112 impl GetCommunity for Vote {
113   #[tracing::instrument(skip_all)]
114   async fn get_community(
115     &self,
116     context: &LemmyContext,
117     request_counter: &mut i32,
118   ) -> Result<ApubCommunity, LemmyError> {
119     let object = self
120       .object
121       .dereference(context, local_instance(context).await, request_counter)
122       .await?;
123     let cid = match object {
124       PostOrComment::Post(p) => p.community_id,
125       PostOrComment::Comment(c) => Post::read(context.pool(), c.post_id).await?.community_id,
126     };
127     let community = Community::read(context.pool(), cid).await?;
128     Ok(community.into())
129   }
130 }