]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/voting/vote.rs
Remove ActivityFields trait, deserialize into another struct instead
[lemmy.git] / crates / apub / src / activities / voting / vote.rs
1 use crate::{
2   activities::{
3     community::{announce::GetCommunity, send_to_community},
4     generate_activity_id,
5     verify_activity,
6     verify_is_public,
7     verify_person_in_community,
8     voting::{vote_comment, vote_post},
9   },
10   activity_lists::AnnouncableActivities,
11   fetcher::object_id::ObjectId,
12   objects::{community::ApubCommunity, person::ApubPerson},
13   protocol::activities::voting::vote::{Vote, VoteType},
14   PostOrComment,
15 };
16 use activitystreams::public;
17 use lemmy_api_common::blocking;
18 use lemmy_apub_lib::{
19   data::Data,
20   traits::{ActivityHandler, ActorType},
21 };
22 use lemmy_db_schema::{
23   newtypes::CommunityId,
24   source::{community::Community, post::Post},
25   traits::Crud,
26 };
27 use lemmy_utils::LemmyError;
28 use lemmy_websocket::LemmyContext;
29 use std::ops::Deref;
30
31 impl Vote {
32   pub(in crate::activities::voting) fn new(
33     object: &PostOrComment,
34     actor: &ApubPerson,
35     community: &ApubCommunity,
36     kind: VoteType,
37     context: &LemmyContext,
38   ) -> Result<Vote, LemmyError> {
39     Ok(Vote {
40       actor: ObjectId::new(actor.actor_id()),
41       to: vec![public()],
42       object: ObjectId::new(object.ap_id()),
43       cc: vec![community.actor_id()],
44       kind: kind.clone(),
45       id: generate_activity_id(kind, &context.settings().get_protocol_and_hostname())?,
46       unparsed: Default::default(),
47     })
48   }
49
50   pub async fn send(
51     object: &PostOrComment,
52     actor: &ApubPerson,
53     community_id: CommunityId,
54     kind: VoteType,
55     context: &LemmyContext,
56   ) -> Result<(), LemmyError> {
57     let community = blocking(context.pool(), move |conn| {
58       Community::read(conn, community_id)
59     })
60     .await??
61     .into();
62     let vote = Vote::new(object, actor, &community, kind, context)?;
63     let vote_id = vote.id.clone();
64
65     let activity = AnnouncableActivities::Vote(vote);
66     send_to_community(activity, &vote_id, actor, &community, vec![], context).await
67   }
68 }
69
70 #[async_trait::async_trait(?Send)]
71 impl ActivityHandler for Vote {
72   type DataType = LemmyContext;
73   async fn verify(
74     &self,
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?;
82     Ok(())
83   }
84
85   async fn receive(
86     self,
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?;
92     match object {
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,
95     }
96   }
97 }
98
99 #[async_trait::async_trait(?Send)]
100 impl GetCommunity for Vote {
101   async fn get_community(
102     &self,
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))
111           .await??
112           .community_id
113       }
114     };
115     let community = blocking(context.pool(), move |conn| Community::read(conn, cid)).await??;
116     Ok(community.into())
117   }
118 }