3 community::{announce::GetCommunity, send_activity_in_community},
5 verify_person_in_community,
6 voting::{vote_comment, vote_post},
8 activity_lists::AnnouncableActivities,
10 fetch_local_site_data,
12 objects::{community::ApubCommunity, person::ApubPerson},
13 protocol::activities::voting::vote::{Vote, VoteType},
17 use activitypub_federation::{core::object_id::ObjectId, data::Data, traits::ActivityHandler};
19 use lemmy_api_common::utils::blocking;
20 use lemmy_db_schema::{
21 newtypes::CommunityId,
22 source::{community::Community, local_site::LocalSite, post::Post},
25 use lemmy_utils::error::LemmyError;
26 use lemmy_websocket::LemmyContext;
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.
32 pub(in crate::activities::voting) fn new(
33 object: &PostOrComment,
36 context: &LemmyContext,
37 ) -> Result<Vote, LemmyError> {
39 actor: ObjectId::new(actor.actor_id()),
40 object: ObjectId::new(object.ap_id()),
42 id: generate_activity_id(kind, &context.settings().get_protocol_and_hostname())?,
43 unparsed: Default::default(),
47 #[tracing::instrument(skip_all)]
49 object: &PostOrComment,
51 community_id: CommunityId,
53 context: &LemmyContext,
54 ) -> Result<(), LemmyError> {
55 let community = blocking(context.pool(), move |conn| {
56 Community::read(conn, community_id)
60 let vote = Vote::new(object, actor, kind, context)?;
62 let activity = AnnouncableActivities::Vote(vote);
63 send_activity_in_community(activity, actor, &community, vec![], context).await
67 #[async_trait::async_trait(?Send)]
68 impl ActivityHandler for Vote {
69 type DataType = LemmyContext;
70 type Error = LemmyError;
72 fn id(&self) -> &Url {
76 fn actor(&self) -> &Url {
80 #[tracing::instrument(skip_all)]
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)
93 .map(|l| l.enable_downvotes)
95 if self.kind == VoteType::Dislike && !enable_downvotes {
96 return Err(anyhow!("Downvotes disabled").into());
101 #[tracing::instrument(skip_all)]
104 context: &Data<LemmyContext>,
105 request_counter: &mut i32,
106 ) -> Result<(), LemmyError> {
109 .dereference(context, local_instance(context), request_counter)
113 .dereference(context, local_instance(context), request_counter)
116 PostOrComment::Post(p) => vote_post(&self.kind, actor, &p, context).await,
117 PostOrComment::Comment(c) => vote_comment(&self.kind, actor, &c, context).await,
122 #[async_trait::async_trait(?Send)]
123 impl GetCommunity for Vote {
124 #[tracing::instrument(skip_all)]
125 async fn get_community(
127 context: &LemmyContext,
128 request_counter: &mut i32,
129 ) -> Result<ApubCommunity, LemmyError> {
132 .dereference(context, local_instance(context), request_counter)
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))
142 let community = blocking(context.pool(), move |conn| Community::read(conn, cid)).await??;