]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/voting/vote.rs
Moving settings to Database. (#2492)
[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   check_apub_id_valid,
10   fetch_local_site_data,
11   local_instance,
12   objects::{community::ApubCommunity, person::ApubPerson},
13   protocol::activities::voting::vote::{Vote, VoteType},
14   ActorType,
15   PostOrComment,
16 };
17 use activitypub_federation::{core::object_id::ObjectId, data::Data, traits::ActivityHandler};
18 use anyhow::anyhow;
19 use lemmy_api_common::utils::blocking;
20 use lemmy_db_schema::{
21   newtypes::CommunityId,
22   source::{community::Community, local_site::LocalSite, post::Post},
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     kind: VoteType,
36     context: &LemmyContext,
37   ) -> Result<Vote, LemmyError> {
38     Ok(Vote {
39       actor: ObjectId::new(actor.actor_id()),
40       object: ObjectId::new(object.ap_id()),
41       kind: kind.clone(),
42       id: generate_activity_id(kind, &context.settings().get_protocol_and_hostname())?,
43       unparsed: Default::default(),
44     })
45   }
46
47   #[tracing::instrument(skip_all)]
48   pub async fn send(
49     object: &PostOrComment,
50     actor: &ApubPerson,
51     community_id: CommunityId,
52     kind: VoteType,
53     context: &LemmyContext,
54   ) -> Result<(), LemmyError> {
55     let community = blocking(context.pool(), move |conn| {
56       Community::read(conn, community_id)
57     })
58     .await??
59     .into();
60     let vote = Vote::new(object, actor, kind, context)?;
61
62     let activity = AnnouncableActivities::Vote(vote);
63     send_activity_in_community(activity, actor, &community, vec![], context).await
64   }
65 }
66
67 #[async_trait::async_trait(?Send)]
68 impl ActivityHandler for Vote {
69   type DataType = LemmyContext;
70   type Error = LemmyError;
71
72   fn id(&self) -> &Url {
73     &self.id
74   }
75
76   fn actor(&self) -> &Url {
77     self.actor.inner()
78   }
79
80   #[tracing::instrument(skip_all)]
81   async fn verify(
82     &self,
83     context: &Data<LemmyContext>,
84     request_counter: &mut i32,
85   ) -> Result<(), LemmyError> {
86     let local_site_data = blocking(context.pool(), fetch_local_site_data).await??;
87     check_apub_id_valid(self.id(), &local_site_data, context.settings())
88       .map_err(LemmyError::from_message)?;
89     let community = self.get_community(context, request_counter).await?;
90     verify_person_in_community(&self.actor, &community, context, request_counter).await?;
91     let enable_downvotes = blocking(context.pool(), LocalSite::read)
92       .await?
93       .map(|l| l.enable_downvotes)
94       .unwrap_or(true);
95     if self.kind == VoteType::Dislike && !enable_downvotes {
96       return Err(anyhow!("Downvotes disabled").into());
97     }
98     Ok(())
99   }
100
101   #[tracing::instrument(skip_all)]
102   async fn receive(
103     self,
104     context: &Data<LemmyContext>,
105     request_counter: &mut i32,
106   ) -> Result<(), LemmyError> {
107     let actor = self
108       .actor
109       .dereference(context, local_instance(context), request_counter)
110       .await?;
111     let object = self
112       .object
113       .dereference(context, local_instance(context), request_counter)
114       .await?;
115     match object {
116       PostOrComment::Post(p) => vote_post(&self.kind, actor, &p, context).await,
117       PostOrComment::Comment(c) => vote_comment(&self.kind, actor, &c, context).await,
118     }
119   }
120 }
121
122 #[async_trait::async_trait(?Send)]
123 impl GetCommunity for Vote {
124   #[tracing::instrument(skip_all)]
125   async fn get_community(
126     &self,
127     context: &LemmyContext,
128     request_counter: &mut i32,
129   ) -> Result<ApubCommunity, LemmyError> {
130     let object = self
131       .object
132       .dereference(context, local_instance(context), request_counter)
133       .await?;
134     let cid = match object {
135       PostOrComment::Post(p) => p.community_id,
136       PostOrComment::Comment(c) => {
137         blocking(context.pool(), move |conn| Post::read(conn, c.post_id))
138           .await??
139           .community_id
140       }
141     };
142     let community = blocking(context.pool(), move |conn| Community::read(conn, cid)).await??;
143     Ok(community.into())
144   }
145 }