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;
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()),
42 object: ObjectId::new(object.ap_id()),
43 cc: vec![community.actor_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;
75 #[tracing::instrument(skip_all)]
78 context: &Data<LemmyContext>,
79 request_counter: &mut i32,
80 ) -> Result<(), LemmyError> {
81 verify_is_public(&self.to, &self.cc)?;
82 verify_activity(&self.id, self.actor.inner(), &context.settings())?;
83 let community = self.get_community(context, request_counter).await?;
84 verify_person_in_community(&self.actor, &community, context, request_counter).await?;
85 let site = blocking(context.pool(), Site::read_local_site).await??;
86 if self.kind == VoteType::Dislike && !site.enable_downvotes {
87 return Err(anyhow!("Downvotes disabled").into());
92 #[tracing::instrument(skip_all)]
95 context: &Data<LemmyContext>,
96 request_counter: &mut i32,
97 ) -> Result<(), LemmyError> {
100 .dereference(context, context.client(), request_counter)
104 .dereference(context, context.client(), request_counter)
107 PostOrComment::Post(p) => vote_post(&self.kind, actor, &p, context).await,
108 PostOrComment::Comment(c) => vote_comment(&self.kind, actor, &c, context).await,
113 #[async_trait::async_trait(?Send)]
114 impl GetCommunity for Vote {
115 #[tracing::instrument(skip_all)]
116 async fn get_community(
118 context: &LemmyContext,
119 request_counter: &mut i32,
120 ) -> Result<ApubCommunity, LemmyError> {
123 .dereference(context, context.client(), request_counter)
125 let cid = match object {
126 PostOrComment::Post(p) => p.community_id,
127 PostOrComment::Comment(c) => {
128 blocking(context.pool(), move |conn| Post::read(conn, c.post_id))
133 let community = blocking(context.pool(), move |conn| Community::read(conn, cid)).await??;