3 community::{announce::GetCommunity, send_to_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::public;
16 use lemmy_api_common::blocking;
20 traits::{ActivityHandler, ActorType},
22 use lemmy_db_schema::{
23 newtypes::CommunityId,
24 source::{community::Community, post::Post},
27 use lemmy_utils::LemmyError;
28 use lemmy_websocket::LemmyContext;
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()),
41 object: ObjectId::new(object.ap_id()),
42 cc: vec![community.actor_id()],
44 id: generate_activity_id(kind, &context.settings().get_protocol_and_hostname())?,
45 unparsed: Default::default(),
50 object: &PostOrComment,
52 community_id: CommunityId,
54 context: &LemmyContext,
55 ) -> Result<(), LemmyError> {
56 let community = blocking(context.pool(), move |conn| {
57 Community::read(conn, community_id)
61 let vote = Vote::new(object, actor, &community, kind, context)?;
62 let vote_id = vote.id.clone();
64 let activity = AnnouncableActivities::Vote(vote);
65 send_to_community(activity, &vote_id, actor, &community, vec![], context).await
69 #[async_trait::async_trait(?Send)]
70 impl ActivityHandler for Vote {
71 type DataType = LemmyContext;
74 context: &Data<LemmyContext>,
75 request_counter: &mut i32,
76 ) -> Result<(), LemmyError> {
77 verify_is_public(&self.to, &self.cc)?;
78 verify_activity(&self.id, self.actor.inner(), &context.settings())?;
79 let community = self.get_community(context, request_counter).await?;
80 verify_person_in_community(&self.actor, &community, context, request_counter).await?;
86 context: &Data<LemmyContext>,
87 request_counter: &mut i32,
88 ) -> Result<(), LemmyError> {
89 let actor = self.actor.dereference(context, request_counter).await?;
90 let object = self.object.dereference(context, request_counter).await?;
92 PostOrComment::Post(p) => vote_post(&self.kind, actor, &p, context).await,
93 PostOrComment::Comment(c) => vote_comment(&self.kind, actor, &c, context).await,
98 #[async_trait::async_trait(?Send)]
99 impl GetCommunity for Vote {
100 async fn get_community(
102 context: &LemmyContext,
103 request_counter: &mut i32,
104 ) -> Result<ApubCommunity, LemmyError> {
105 let object = self.object.dereference(context, request_counter).await?;
106 let cid = match object {
107 PostOrComment::Post(p) => p.community_id,
108 PostOrComment::Comment(c) => {
109 blocking(context.pool(), move |conn| Post::read(conn, c.post_id))
114 let community = blocking(context.pool(), move |conn| Community::read(conn, cid)).await??;