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