]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/voting/vote.rs
Use audience field to federate items in groups (fixes #2464) (#2584)
[lemmy.git] / crates / apub / src / activities / voting / vote.rs
1 use crate::{
2   activities::{
3     community::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::{
12     activities::voting::vote::{Vote, VoteType},
13     InCommunity,
14   },
15   ActorType,
16   PostOrComment,
17 };
18 use activitypub_federation::{core::object_id::ObjectId, data::Data, traits::ActivityHandler};
19 use anyhow::anyhow;
20 use lemmy_db_schema::{
21   newtypes::CommunityId,
22   source::{community::Community, local_site::LocalSite},
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       object: ObjectId::new(object.ap_id()),
42       kind: kind.clone(),
43       id: generate_activity_id(kind, &context.settings().get_protocol_and_hostname())?,
44       audience: Some(ObjectId::new(community.actor_id())),
45     })
46   }
47
48   #[tracing::instrument(skip_all)]
49   pub async fn send(
50     object: &PostOrComment,
51     actor: &ApubPerson,
52     community_id: CommunityId,
53     kind: VoteType,
54     context: &LemmyContext,
55   ) -> Result<(), LemmyError> {
56     let community = Community::read(context.pool(), community_id).await?.into();
57     let vote = Vote::new(object, actor, &community, kind, context)?;
58
59     let activity = AnnouncableActivities::Vote(vote);
60     send_activity_in_community(activity, actor, &community, vec![], false, context).await
61   }
62 }
63
64 #[async_trait::async_trait(?Send)]
65 impl ActivityHandler for Vote {
66   type DataType = LemmyContext;
67   type Error = LemmyError;
68
69   fn id(&self) -> &Url {
70     &self.id
71   }
72
73   fn actor(&self) -> &Url {
74     self.actor.inner()
75   }
76
77   #[tracing::instrument(skip_all)]
78   async fn verify(
79     &self,
80     context: &Data<LemmyContext>,
81     request_counter: &mut i32,
82   ) -> Result<(), LemmyError> {
83     let community = self.community(context, request_counter).await?;
84     verify_person_in_community(&self.actor, &community, context, request_counter).await?;
85     let enable_downvotes = LocalSite::read(context.pool())
86       .await
87       .map(|l| l.enable_downvotes)
88       .unwrap_or(true);
89     if self.kind == VoteType::Dislike && !enable_downvotes {
90       return Err(anyhow!("Downvotes disabled").into());
91     }
92     Ok(())
93   }
94
95   #[tracing::instrument(skip_all)]
96   async fn receive(
97     self,
98     context: &Data<LemmyContext>,
99     request_counter: &mut i32,
100   ) -> Result<(), LemmyError> {
101     let actor = self
102       .actor
103       .dereference(context, local_instance(context).await, request_counter)
104       .await?;
105     let object = self
106       .object
107       .dereference(context, local_instance(context).await, request_counter)
108       .await?;
109     match object {
110       PostOrComment::Post(p) => vote_post(&self.kind, actor, &p, context).await,
111       PostOrComment::Comment(c) => vote_comment(&self.kind, actor, &c, context).await,
112     }
113   }
114 }