3 community::{announce::GetCommunity, send_activity_in_community},
7 verify_person_in_community,
8 voting::{vote_comment, vote_post},
10 activity_lists::AnnouncableActivities,
11 objects::{community::ApubCommunity, person::ApubPerson},
12 protocol::activities::voting::vote::{Vote, VoteType},
15 use activitystreams_kinds::public;
17 use lemmy_api_common::blocking;
21 traits::{ActivityHandler, ActorType},
23 use lemmy_db_schema::{
24 newtypes::CommunityId,
25 source::{community::Community, post::Post, site::Site},
28 use lemmy_utils::LemmyError;
29 use lemmy_websocket::LemmyContext;
31 /// Vote has as:Public value in cc field, unlike other activities. This indicates to other software
32 /// (like GNU social, or presumably Mastodon), that the like actor should not be disclosed.
34 pub(in crate::activities::voting) fn new(
35 object: &PostOrComment,
37 community: &ApubCommunity,
39 context: &LemmyContext,
40 ) -> Result<Vote, LemmyError> {
42 actor: ObjectId::new(actor.actor_id()),
43 to: vec![community.actor_id()],
44 object: ObjectId::new(object.ap_id()),
47 id: generate_activity_id(kind, &context.settings().get_protocol_and_hostname())?,
48 unparsed: Default::default(),
52 #[tracing::instrument(skip_all)]
54 object: &PostOrComment,
56 community_id: CommunityId,
58 context: &LemmyContext,
59 ) -> Result<(), LemmyError> {
60 let community = blocking(context.pool(), move |conn| {
61 Community::read(conn, community_id)
65 let vote = Vote::new(object, actor, &community, kind, context)?;
66 let vote_id = vote.id.clone();
68 let activity = AnnouncableActivities::Vote(vote);
69 send_activity_in_community(activity, &vote_id, actor, &community, vec![], context).await
73 #[async_trait::async_trait(?Send)]
74 impl ActivityHandler for Vote {
75 type DataType = LemmyContext;
77 #[tracing::instrument(skip_all)]
80 context: &Data<LemmyContext>,
81 request_counter: &mut i32,
82 ) -> Result<(), LemmyError> {
83 verify_is_public(&self.to, &self.cc)?;
84 verify_activity(&self.id, self.actor.inner(), &context.settings())?;
85 let community = self.get_community(context, request_counter).await?;
86 verify_person_in_community(&self.actor, &community, context, request_counter).await?;
87 let site = blocking(context.pool(), Site::read_local_site).await??;
88 if self.kind == VoteType::Dislike && !site.enable_downvotes {
89 return Err(anyhow!("Downvotes disabled").into());
94 #[tracing::instrument(skip_all)]
97 context: &Data<LemmyContext>,
98 request_counter: &mut i32,
99 ) -> Result<(), LemmyError> {
102 .dereference(context, context.client(), request_counter)
106 .dereference(context, context.client(), request_counter)
109 PostOrComment::Post(p) => vote_post(&self.kind, actor, &p, context).await,
110 PostOrComment::Comment(c) => vote_comment(&self.kind, actor, &c, context).await,
115 #[async_trait::async_trait(?Send)]
116 impl GetCommunity for Vote {
117 #[tracing::instrument(skip_all)]
118 async fn get_community(
120 context: &LemmyContext,
121 request_counter: &mut i32,
122 ) -> Result<ApubCommunity, LemmyError> {
125 .dereference(context, context.client(), request_counter)
127 let cid = match object {
128 PostOrComment::Post(p) => p.community_id,
129 PostOrComment::Comment(c) => {
130 blocking(context.pool(), move |conn| Post::read(conn, c.post_id))
135 let community = blocking(context.pool(), move |conn| Community::read(conn, cid)).await??;