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,
34 community: &ApubCommunity,
36 context: &LemmyContext,
37 ) -> Result<Vote, LemmyError> {
39 actor: ObjectId::new(actor.actor_id()),
40 to: vec![community.actor_id()],
41 object: ObjectId::new(object.ap_id()),
44 id: generate_activity_id(kind, &context.settings().get_protocol_and_hostname())?,
45 unparsed: Default::default(),
49 #[tracing::instrument(skip_all)]
51 object: &PostOrComment,
53 community_id: CommunityId,
55 context: &LemmyContext,
56 ) -> Result<(), LemmyError> {
57 let community = blocking(context.pool(), move |conn| {
58 Community::read(conn, community_id)
62 let vote = Vote::new(object, actor, &community, kind, context)?;
63 let vote_id = vote.id.clone();
65 let activity = AnnouncableActivities::Vote(vote);
66 send_activity_in_community(activity, &vote_id, actor, &community, vec![], context).await
70 #[async_trait::async_trait(?Send)]
71 impl ActivityHandler for Vote {
72 type DataType = LemmyContext;
73 type Error = LemmyError;
75 fn id(&self) -> &Url {
79 fn actor(&self) -> &Url {
83 #[tracing::instrument(skip_all)]
86 context: &Data<LemmyContext>,
87 request_counter: &mut i32,
88 ) -> Result<(), LemmyError> {
89 let community = self.get_community(context, request_counter).await?;
90 verify_person_in_community(&self.actor, &community, context, request_counter).await?;
91 let site = blocking(context.pool(), Site::read_local_site).await??;
92 if self.kind == VoteType::Dislike && !site.enable_downvotes {
93 return Err(anyhow!("Downvotes disabled").into());
98 #[tracing::instrument(skip_all)]
101 context: &Data<LemmyContext>,
102 request_counter: &mut i32,
103 ) -> Result<(), LemmyError> {
106 .dereference::<LemmyError>(context, local_instance(context), request_counter)
110 .dereference::<LemmyError>(context, local_instance(context), request_counter)
113 PostOrComment::Post(p) => vote_post(&self.kind, actor, &p, context).await,
114 PostOrComment::Comment(c) => vote_comment(&self.kind, actor, &c, context).await,
119 #[async_trait::async_trait(?Send)]
120 impl GetCommunity for Vote {
121 #[tracing::instrument(skip_all)]
122 async fn get_community(
124 context: &LemmyContext,
125 request_counter: &mut i32,
126 ) -> Result<ApubCommunity, LemmyError> {
129 .dereference::<LemmyError>(context, local_instance(context), request_counter)
131 let cid = match object {
132 PostOrComment::Post(p) => p.community_id,
133 PostOrComment::Comment(c) => {
134 blocking(context.pool(), move |conn| Post::read(conn, c.post_id))
139 let community = blocking(context.pool(), move |conn| Community::read(conn, cid)).await??;