3 community::{announce::GetCommunity, send_to_community},
7 verify_person_in_community,
8 voting::{vote_comment, vote_post},
10 activity_lists::AnnouncableActivities,
11 fetcher::object_id::ObjectId,
12 objects::{community::ApubCommunity, person::ApubPerson},
13 protocol::activities::voting::vote::{Vote, VoteType},
16 use activitystreams::public;
17 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;
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(),
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_to_community(activity, &vote_id, actor, &community, vec![], context).await
70 #[async_trait::async_trait(?Send)]
71 impl ActivityHandler for Vote {
72 type DataType = LemmyContext;
75 context: &Data<LemmyContext>,
76 request_counter: &mut i32,
77 ) -> Result<(), LemmyError> {
78 verify_is_public(&self.to)?;
79 verify_activity(&self.id, self.actor.inner(), &context.settings())?;
80 let community = self.get_community(context, request_counter).await?;
81 verify_person_in_community(&self.actor, &community, context, request_counter).await?;
87 context: &Data<LemmyContext>,
88 request_counter: &mut i32,
89 ) -> Result<(), LemmyError> {
90 let actor = self.actor.dereference(context, request_counter).await?;
91 let object = self.object.dereference(context, request_counter).await?;
93 PostOrComment::Post(p) => vote_post(&self.kind, actor, p.deref(), context).await,
94 PostOrComment::Comment(c) => vote_comment(&self.kind, actor, &c, context).await,
99 #[async_trait::async_trait(?Send)]
100 impl GetCommunity for Vote {
101 async fn get_community(
103 context: &LemmyContext,
104 request_counter: &mut i32,
105 ) -> Result<ApubCommunity, LemmyError> {
106 let object = self.object.dereference(context, request_counter).await?;
107 let cid = match object {
108 PostOrComment::Post(p) => p.community_id,
109 PostOrComment::Comment(c) => {
110 blocking(context.pool(), move |conn| Post::read(conn, c.post_id))
115 let community = blocking(context.pool(), move |conn| Community::read(conn, cid)).await??;