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;
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(),
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;
74 #[tracing::instrument(skip_all)]
77 context: &Data<LemmyContext>,
78 request_counter: &mut i32,
79 ) -> Result<(), LemmyError> {
80 verify_is_public(&self.to, &self.cc)?;
81 verify_activity(&self.id, self.actor.inner(), &context.settings())?;
82 let community = self.get_community(context, request_counter).await?;
83 verify_person_in_community(&self.actor, &community, context, request_counter).await?;
87 #[tracing::instrument(skip_all)]
90 context: &Data<LemmyContext>,
91 request_counter: &mut i32,
92 ) -> Result<(), LemmyError> {
93 let actor = self.actor.dereference(context, request_counter).await?;
94 let object = self.object.dereference(context, request_counter).await?;
96 PostOrComment::Post(p) => vote_post(&self.kind, actor, &p, context).await,
97 PostOrComment::Comment(c) => vote_comment(&self.kind, actor, &c, context).await,
102 #[async_trait::async_trait(?Send)]
103 impl GetCommunity for Vote {
104 #[tracing::instrument(skip_all)]
105 async fn get_community(
107 context: &LemmyContext,
108 request_counter: &mut i32,
109 ) -> Result<ApubCommunity, LemmyError> {
110 let object = self.object.dereference(context, request_counter).await?;
111 let cid = match object {
112 PostOrComment::Post(p) => p.community_id,
113 PostOrComment::Comment(c) => {
114 blocking(context.pool(), move |conn| Post::read(conn, c.post_id))
119 let community = blocking(context.pool(), move |conn| Community::read(conn, cid)).await??;