]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/voting/vote.rs
e08e4bb69c9db1bf70e0e7b5dbed6021c6ef1311
[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     let vote_id = vote.id.clone();
64
65     let activity = AnnouncableActivities::Vote(vote);
66     send_activity_in_community(activity, &vote_id, actor, &community, vec![], context).await
67   }
68 }
69
70 #[async_trait::async_trait(?Send)]
71 impl ActivityHandler for Vote {
72   type DataType = LemmyContext;
73   type Error = LemmyError;
74
75   fn id(&self) -> &Url {
76     &self.id
77   }
78
79   fn actor(&self) -> &Url {
80     self.actor.inner()
81   }
82
83   #[tracing::instrument(skip_all)]
84   async fn verify(
85     &self,
86     context: &Data<LemmyContext>,
87     request_counter: &mut i32,
88   ) -> Result<(), LemmyError> {
89     let community = self.get_community(context, request_counter).await?;
90     verify_person_in_community(&self.actor, &community, context, request_counter).await?;
91     let site = blocking(context.pool(), Site::read_local_site).await??;
92     if self.kind == VoteType::Dislike && !site.enable_downvotes {
93       return Err(anyhow!("Downvotes disabled").into());
94     }
95     Ok(())
96   }
97
98   #[tracing::instrument(skip_all)]
99   async fn receive(
100     self,
101     context: &Data<LemmyContext>,
102     request_counter: &mut i32,
103   ) -> Result<(), LemmyError> {
104     let actor = self
105       .actor
106       .dereference::<LemmyError>(context, local_instance(context), request_counter)
107       .await?;
108     let object = self
109       .object
110       .dereference::<LemmyError>(context, local_instance(context), request_counter)
111       .await?;
112     match object {
113       PostOrComment::Post(p) => vote_post(&self.kind, actor, &p, context).await,
114       PostOrComment::Comment(c) => vote_comment(&self.kind, actor, &c, context).await,
115     }
116   }
117 }
118
119 #[async_trait::async_trait(?Send)]
120 impl GetCommunity for Vote {
121   #[tracing::instrument(skip_all)]
122   async fn get_community(
123     &self,
124     context: &LemmyContext,
125     request_counter: &mut i32,
126   ) -> Result<ApubCommunity, LemmyError> {
127     let object = self
128       .object
129       .dereference::<LemmyError>(context, local_instance(context), request_counter)
130       .await?;
131     let cid = match object {
132       PostOrComment::Post(p) => p.community_id,
133       PostOrComment::Comment(c) => {
134         blocking(context.pool(), move |conn| Post::read(conn, c.post_id))
135           .await??
136           .community_id
137       }
138     };
139     let community = blocking(context.pool(), move |conn| Community::read(conn, cid)).await??;
140     Ok(community.into())
141   }
142 }