3 use activitystreams::public;
5 use lemmy_api_common::blocking;
8 traits::{ActivityHandler, ActorType},
10 use lemmy_db_schema::{
11 newtypes::CommunityId,
12 source::{community::Community, post::Post},
15 use lemmy_utils::LemmyError;
16 use lemmy_websocket::LemmyContext;
20 community::{announce::GetCommunity, send_to_community},
24 verify_person_in_community,
25 voting::{vote_comment, vote_post},
27 activity_lists::AnnouncableActivities,
28 fetcher::object_id::ObjectId,
29 objects::{community::ApubCommunity, person::ApubPerson},
30 protocol::activities::voting::vote::{Vote, VoteType},
35 pub(in crate::activities::voting) fn new(
36 object: &PostOrComment,
38 community: &ApubCommunity,
40 context: &LemmyContext,
41 ) -> Result<Vote, LemmyError> {
43 actor: ObjectId::new(actor.actor_id()),
45 object: ObjectId::new(object.ap_id()),
46 cc: vec![community.actor_id()],
48 id: generate_activity_id(kind, &context.settings().get_protocol_and_hostname())?,
49 unparsed: Default::default(),
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_to_community(activity, &vote_id, actor, &community, vec![], context).await
73 #[async_trait::async_trait(?Send)]
74 impl ActivityHandler for Vote {
75 type DataType = LemmyContext;
78 context: &Data<LemmyContext>,
79 request_counter: &mut i32,
80 ) -> Result<(), LemmyError> {
81 verify_is_public(&self.to)?;
82 verify_activity(self, &context.settings())?;
83 let community = self.get_community(context, request_counter).await?;
84 verify_person_in_community(&self.actor, &community, context, request_counter).await?;
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.deref(), 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 async fn get_community(
106 context: &LemmyContext,
107 request_counter: &mut i32,
108 ) -> Result<ApubCommunity, LemmyError> {
109 let object = self.object.dereference(context, request_counter).await?;
110 let cid = match object {
111 PostOrComment::Post(p) => p.community_id,
112 PostOrComment::Comment(c) => {
113 blocking(context.pool(), move |conn| Post::read(conn, c.post_id))
118 let community = blocking(context.pool(), move |conn| Community::read(conn, cid)).await??;