3 community::{announce::GetCommunity, send_activity_in_community},
6 verify_person_in_community,
7 voting::{vote_comment, vote_post},
9 activity_lists::AnnouncableActivities,
11 objects::{community::ApubCommunity, person::ApubPerson},
12 protocol::activities::voting::vote::{Vote, VoteType},
16 use activitypub_federation::{core::object_id::ObjectId, data::Data, traits::ActivityHandler};
17 use activitystreams_kinds::public;
19 use lemmy_api_common::utils::blocking;
20 use lemmy_db_schema::{
21 newtypes::CommunityId,
22 source::{community::Community, post::Post, site::Site},
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,
35 community: &ApubCommunity,
37 context: &LemmyContext,
38 ) -> Result<Vote, LemmyError> {
40 actor: ObjectId::new(actor.actor_id()),
41 to: vec![community.actor_id()],
42 object: ObjectId::new(object.ap_id()),
45 id: generate_activity_id(kind, &context.settings().get_protocol_and_hostname())?,
46 unparsed: Default::default(),
50 #[tracing::instrument(skip_all)]
52 object: &PostOrComment,
54 community_id: CommunityId,
56 context: &LemmyContext,
57 ) -> Result<(), LemmyError> {
58 let community = blocking(context.pool(), move |conn| {
59 Community::read(conn, community_id)
63 let vote = Vote::new(object, actor, &community, kind, context)?;
64 let vote_id = vote.id.clone();
66 let activity = AnnouncableActivities::Vote(vote);
67 send_activity_in_community(activity, &vote_id, actor, &community, vec![], context).await
71 #[async_trait::async_trait(?Send)]
72 impl ActivityHandler for Vote {
73 type DataType = LemmyContext;
74 type Error = LemmyError;
76 fn id(&self) -> &Url {
80 fn actor(&self) -> &Url {
84 #[tracing::instrument(skip_all)]
87 context: &Data<LemmyContext>,
88 request_counter: &mut i32,
89 ) -> Result<(), LemmyError> {
90 verify_is_public(&self.to, &self.cc)?;
91 let community = self.get_community(context, request_counter).await?;
92 verify_person_in_community(&self.actor, &community, context, request_counter).await?;
93 let site = blocking(context.pool(), Site::read_local_site).await??;
94 if self.kind == VoteType::Dislike && !site.enable_downvotes {
95 return Err(anyhow!("Downvotes disabled").into());
100 #[tracing::instrument(skip_all)]
103 context: &Data<LemmyContext>,
104 request_counter: &mut i32,
105 ) -> Result<(), LemmyError> {
108 .dereference::<LemmyError>(context, local_instance(context), request_counter)
112 .dereference::<LemmyError>(context, local_instance(context), request_counter)
115 PostOrComment::Post(p) => vote_post(&self.kind, actor, &p, context).await,
116 PostOrComment::Comment(c) => vote_comment(&self.kind, actor, &c, context).await,
121 #[async_trait::async_trait(?Send)]
122 impl GetCommunity for Vote {
123 #[tracing::instrument(skip_all)]
124 async fn get_community(
126 context: &LemmyContext,
127 request_counter: &mut i32,
128 ) -> Result<ApubCommunity, LemmyError> {
131 .dereference::<LemmyError>(context, local_instance(context), request_counter)
133 let cid = match object {
134 PostOrComment::Post(p) => p.community_id,
135 PostOrComment::Comment(c) => {
136 blocking(context.pool(), move |conn| Post::read(conn, c.post_id))
141 let community = blocking(context.pool(), move |conn| Community::read(conn, cid)).await??;