]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/voting/vote.rs
8a022f7d249f88c430f024a39f991f3344f19d21
[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       unparsed: Default::default(),
41     })
42   }
43
44   #[tracing::instrument(skip_all)]
45   pub async fn send(
46     object: &PostOrComment,
47     actor: &ApubPerson,
48     community_id: CommunityId,
49     kind: VoteType,
50     context: &LemmyContext,
51   ) -> Result<(), LemmyError> {
52     let community = Community::read(context.pool(), community_id).await?.into();
53     let vote = Vote::new(object, actor, kind, context)?;
54
55     let activity = AnnouncableActivities::Vote(vote);
56     send_activity_in_community(activity, actor, &community, vec![], context).await
57   }
58 }
59
60 #[async_trait::async_trait(?Send)]
61 impl ActivityHandler for Vote {
62   type DataType = LemmyContext;
63   type Error = LemmyError;
64
65   fn id(&self) -> &Url {
66     &self.id
67   }
68
69   fn actor(&self) -> &Url {
70     self.actor.inner()
71   }
72
73   #[tracing::instrument(skip_all)]
74   async fn verify(
75     &self,
76     context: &Data<LemmyContext>,
77     request_counter: &mut i32,
78   ) -> Result<(), LemmyError> {
79     let community = self.get_community(context, request_counter).await?;
80     verify_person_in_community(&self.actor, &community, context, request_counter).await?;
81     let enable_downvotes = LocalSite::read(context.pool())
82       .await
83       .map(|l| l.enable_downvotes)
84       .unwrap_or(true);
85     if self.kind == VoteType::Dislike && !enable_downvotes {
86       return Err(anyhow!("Downvotes disabled").into());
87     }
88     Ok(())
89   }
90
91   #[tracing::instrument(skip_all)]
92   async fn receive(
93     self,
94     context: &Data<LemmyContext>,
95     request_counter: &mut i32,
96   ) -> Result<(), LemmyError> {
97     let actor = self
98       .actor
99       .dereference(context, local_instance(context).await, request_counter)
100       .await?;
101     let object = self
102       .object
103       .dereference(context, local_instance(context).await, request_counter)
104       .await?;
105     match object {
106       PostOrComment::Post(p) => vote_post(&self.kind, actor, &p, context).await,
107       PostOrComment::Comment(c) => vote_comment(&self.kind, actor, &c, context).await,
108     }
109   }
110 }
111
112 #[async_trait::async_trait(?Send)]
113 impl GetCommunity for Vote {
114   #[tracing::instrument(skip_all)]
115   async fn get_community(
116     &self,
117     context: &LemmyContext,
118     request_counter: &mut i32,
119   ) -> Result<ApubCommunity, LemmyError> {
120     let object = self
121       .object
122       .dereference(context, local_instance(context).await, request_counter)
123       .await?;
124     let cid = match object {
125       PostOrComment::Post(p) => p.community_id,
126       PostOrComment::Comment(c) => Post::read(context.pool(), c.post_id).await?.community_id,
127     };
128     let community = Community::read(context.pool(), cid).await?;
129     Ok(community.into())
130   }
131 }