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