]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/voting/vote.rs
GNU social compatibility (#2100)
[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 /// Vote has as:Public value in cc field, unlike other activities. This indicates to other software
32 /// (like GNU social, or presumably Mastodon), that the like actor should not be disclosed.
33 impl Vote {
34   pub(in crate::activities::voting) fn new(
35     object: &PostOrComment,
36     actor: &ApubPerson,
37     community: &ApubCommunity,
38     kind: VoteType,
39     context: &LemmyContext,
40   ) -> Result<Vote, LemmyError> {
41     Ok(Vote {
42       actor: ObjectId::new(actor.actor_id()),
43       to: vec![community.actor_id()],
44       object: ObjectId::new(object.ap_id()),
45       cc: vec![public()],
46       kind: kind.clone(),
47       id: generate_activity_id(kind, &context.settings().get_protocol_and_hostname())?,
48       unparsed: Default::default(),
49     })
50   }
51
52   #[tracing::instrument(skip_all)]
53   pub async fn send(
54     object: &PostOrComment,
55     actor: &ApubPerson,
56     community_id: CommunityId,
57     kind: VoteType,
58     context: &LemmyContext,
59   ) -> Result<(), LemmyError> {
60     let community = blocking(context.pool(), move |conn| {
61       Community::read(conn, community_id)
62     })
63     .await??
64     .into();
65     let vote = Vote::new(object, actor, &community, kind, context)?;
66     let vote_id = vote.id.clone();
67
68     let activity = AnnouncableActivities::Vote(vote);
69     send_activity_in_community(activity, &vote_id, actor, &community, vec![], context).await
70   }
71 }
72
73 #[async_trait::async_trait(?Send)]
74 impl ActivityHandler for Vote {
75   type DataType = LemmyContext;
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     verify_is_public(&self.to, &self.cc)?;
84     verify_activity(&self.id, self.actor.inner(), &context.settings())?;
85     let community = self.get_community(context, request_counter).await?;
86     verify_person_in_community(&self.actor, &community, context, request_counter).await?;
87     let site = blocking(context.pool(), Site::read_local_site).await??;
88     if self.kind == VoteType::Dislike && !site.enable_downvotes {
89       return Err(anyhow!("Downvotes disabled").into());
90     }
91     Ok(())
92   }
93
94   #[tracing::instrument(skip_all)]
95   async fn receive(
96     self,
97     context: &Data<LemmyContext>,
98     request_counter: &mut i32,
99   ) -> Result<(), LemmyError> {
100     let actor = self
101       .actor
102       .dereference(context, context.client(), request_counter)
103       .await?;
104     let object = self
105       .object
106       .dereference(context, context.client(), request_counter)
107       .await?;
108     match object {
109       PostOrComment::Post(p) => vote_post(&self.kind, actor, &p, context).await,
110       PostOrComment::Comment(c) => vote_comment(&self.kind, actor, &c, context).await,
111     }
112   }
113 }
114
115 #[async_trait::async_trait(?Send)]
116 impl GetCommunity for Vote {
117   #[tracing::instrument(skip_all)]
118   async fn get_community(
119     &self,
120     context: &LemmyContext,
121     request_counter: &mut i32,
122   ) -> Result<ApubCommunity, LemmyError> {
123     let object = self
124       .object
125       .dereference(context, context.client(), request_counter)
126       .await?;
127     let cid = match object {
128       PostOrComment::Post(p) => p.community_id,
129       PostOrComment::Comment(c) => {
130         blocking(context.pool(), move |conn| Post::read(conn, c.post_id))
131           .await??
132           .community_id
133       }
134     };
135     let community = blocking(context.pool(), move |conn| Community::read(conn, cid)).await??;
136     Ok(community.into())
137   }
138 }