3 community::{announce::GetCommunity, send_activity_in_community},
5 verify_person_in_community,
6 voting::{vote_comment, vote_post},
8 activity_lists::AnnouncableActivities,
10 objects::{community::ApubCommunity, person::ApubPerson},
11 protocol::activities::voting::vote::{Vote, VoteType},
15 use activitypub_federation::{core::object_id::ObjectId, data::Data, traits::ActivityHandler};
17 use lemmy_api_common::utils::blocking;
18 use lemmy_db_schema::{
19 newtypes::CommunityId,
20 source::{community::Community, post::Post, site::Site},
23 use lemmy_utils::error::LemmyError;
24 use lemmy_websocket::LemmyContext;
27 /// Vote has as:Public value in cc field, unlike other activities. This indicates to other software
28 /// (like GNU social, or presumably Mastodon), that the like actor should not be disclosed.
30 pub(in crate::activities::voting) fn new(
31 object: &PostOrComment,
34 context: &LemmyContext,
35 ) -> Result<Vote, LemmyError> {
37 actor: ObjectId::new(actor.actor_id()),
38 object: ObjectId::new(object.ap_id()),
40 id: generate_activity_id(kind, &context.settings().get_protocol_and_hostname())?,
41 unparsed: Default::default(),
45 #[tracing::instrument(skip_all)]
47 object: &PostOrComment,
49 community_id: CommunityId,
51 context: &LemmyContext,
52 ) -> Result<(), LemmyError> {
53 let community = blocking(context.pool(), move |conn| {
54 Community::read(conn, community_id)
58 let vote = Vote::new(object, actor, kind, context)?;
60 let activity = AnnouncableActivities::Vote(vote);
61 send_activity_in_community(activity, actor, &community, vec![], context).await
65 #[async_trait::async_trait(?Send)]
66 impl ActivityHandler for Vote {
67 type DataType = LemmyContext;
68 type Error = LemmyError;
70 fn id(&self) -> &Url {
74 fn actor(&self) -> &Url {
78 #[tracing::instrument(skip_all)]
81 context: &Data<LemmyContext>,
82 request_counter: &mut i32,
83 ) -> Result<(), LemmyError> {
84 let community = self.get_community(context, request_counter).await?;
85 verify_person_in_community(&self.actor, &community, context, request_counter).await?;
86 let site = blocking(context.pool(), Site::read_local).await??;
87 if self.kind == VoteType::Dislike && !site.enable_downvotes {
88 return Err(anyhow!("Downvotes disabled").into());
93 #[tracing::instrument(skip_all)]
96 context: &Data<LemmyContext>,
97 request_counter: &mut i32,
98 ) -> Result<(), LemmyError> {
101 .dereference(context, local_instance(context), request_counter)
105 .dereference(context, local_instance(context), request_counter)
108 PostOrComment::Post(p) => vote_post(&self.kind, actor, &p, context).await,
109 PostOrComment::Comment(c) => vote_comment(&self.kind, actor, &c, context).await,
114 #[async_trait::async_trait(?Send)]
115 impl GetCommunity for Vote {
116 #[tracing::instrument(skip_all)]
117 async fn get_community(
119 context: &LemmyContext,
120 request_counter: &mut i32,
121 ) -> Result<ApubCommunity, LemmyError> {
124 .dereference(context, local_instance(context), request_counter)
126 let cid = match object {
127 PostOrComment::Post(p) => p.community_id,
128 PostOrComment::Comment(c) => {
129 blocking(context.pool(), move |conn| Post::read(conn, c.post_id))
134 let community = blocking(context.pool(), move |conn| Community::read(conn, cid)).await??;