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};
16 use activitystreams_kinds::public;
18 use lemmy_api_common::utils::blocking;
19 use lemmy_db_schema::{
20 newtypes::CommunityId,
21 source::{community::Community, post::Post, site::Site},
24 use lemmy_utils::error::LemmyError;
25 use lemmy_websocket::LemmyContext;
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.
31 pub(in crate::activities::voting) fn new(
32 object: &PostOrComment,
35 context: &LemmyContext,
36 ) -> Result<Vote, LemmyError> {
38 actor: ObjectId::new(actor.actor_id()),
39 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 community = self.get_community(context, request_counter).await?;
87 verify_person_in_community(&self.actor, &community, context, request_counter).await?;
88 let site = blocking(context.pool(), Site::read_local_site).await??;
89 if self.kind == VoteType::Dislike && !site.enable_downvotes {
90 return Err(anyhow!("Downvotes disabled").into());
95 #[tracing::instrument(skip_all)]
98 context: &Data<LemmyContext>,
99 request_counter: &mut i32,
100 ) -> Result<(), LemmyError> {
103 .dereference(context, local_instance(context), request_counter)
107 .dereference(context, local_instance(context), request_counter)
110 PostOrComment::Post(p) => vote_post(&self.kind, actor, &p, context).await,
111 PostOrComment::Comment(c) => vote_comment(&self.kind, actor, &c, context).await,
116 #[async_trait::async_trait(?Send)]
117 impl GetCommunity for Vote {
118 #[tracing::instrument(skip_all)]
119 async fn get_community(
121 context: &LemmyContext,
122 request_counter: &mut i32,
123 ) -> Result<ApubCommunity, LemmyError> {
126 .dereference(context, local_instance(context), request_counter)
128 let cid = match object {
129 PostOrComment::Post(p) => p.community_id,
130 PostOrComment::Comment(c) => {
131 blocking(context.pool(), move |conn| Post::read(conn, c.post_id))
136 let community = blocking(context.pool(), move |conn| Community::read(conn, cid)).await??;