]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/voting/vote.rs
Merge pull request #1897 from LemmyNet/mastodon-compat
[lemmy.git] / crates / apub / src / activities / voting / vote.rs
1 use crate::{
2   activities::{
3     community::{announce::GetCommunity, send_activity_in_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   objects::{community::ApubCommunity, person::ApubPerson},
12   protocol::activities::voting::vote::{Vote, VoteType},
13   PostOrComment,
14 };
15 use activitystreams::public;
16 use lemmy_api_common::blocking;
17 use lemmy_apub_lib::{
18   data::Data,
19   object_id::ObjectId,
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
30 impl Vote {
31   pub(in crate::activities::voting) fn new(
32     object: &PostOrComment,
33     actor: &ApubPerson,
34     community: &ApubCommunity,
35     kind: VoteType,
36     context: &LemmyContext,
37   ) -> Result<Vote, LemmyError> {
38     Ok(Vote {
39       actor: ObjectId::new(actor.actor_id()),
40       to: vec![public()],
41       object: ObjectId::new(object.ap_id()),
42       cc: vec![community.actor_id()],
43       kind: kind.clone(),
44       id: generate_activity_id(kind, &context.settings().get_protocol_and_hostname())?,
45       unparsed: Default::default(),
46     })
47   }
48
49   pub async fn send(
50     object: &PostOrComment,
51     actor: &ApubPerson,
52     community_id: CommunityId,
53     kind: VoteType,
54     context: &LemmyContext,
55   ) -> Result<(), LemmyError> {
56     let community = blocking(context.pool(), move |conn| {
57       Community::read(conn, community_id)
58     })
59     .await??
60     .into();
61     let vote = Vote::new(object, actor, &community, kind, context)?;
62     let vote_id = vote.id.clone();
63
64     let activity = AnnouncableActivities::Vote(vote);
65     send_activity_in_community(activity, &vote_id, actor, &community, vec![], context).await
66   }
67 }
68
69 #[async_trait::async_trait(?Send)]
70 impl ActivityHandler for Vote {
71   type DataType = LemmyContext;
72   async fn verify(
73     &self,
74     context: &Data<LemmyContext>,
75     request_counter: &mut i32,
76   ) -> Result<(), LemmyError> {
77     verify_is_public(&self.to, &self.cc)?;
78     verify_activity(&self.id, self.actor.inner(), &context.settings())?;
79     let community = self.get_community(context, request_counter).await?;
80     verify_person_in_community(&self.actor, &community, context, request_counter).await?;
81     Ok(())
82   }
83
84   async fn receive(
85     self,
86     context: &Data<LemmyContext>,
87     request_counter: &mut i32,
88   ) -> Result<(), LemmyError> {
89     let actor = self.actor.dereference(context, request_counter).await?;
90     let object = self.object.dereference(context, request_counter).await?;
91     match object {
92       PostOrComment::Post(p) => vote_post(&self.kind, actor, &p, context).await,
93       PostOrComment::Comment(c) => vote_comment(&self.kind, actor, &c, context).await,
94     }
95   }
96 }
97
98 #[async_trait::async_trait(?Send)]
99 impl GetCommunity for Vote {
100   async fn get_community(
101     &self,
102     context: &LemmyContext,
103     request_counter: &mut i32,
104   ) -> Result<ApubCommunity, LemmyError> {
105     let object = self.object.dereference(context, request_counter).await?;
106     let cid = match object {
107       PostOrComment::Post(p) => p.community_id,
108       PostOrComment::Comment(c) => {
109         blocking(context.pool(), move |conn| Post::read(conn, c.post_id))
110           .await??
111           .community_id
112       }
113     };
114     let community = blocking(context.pool(), move |conn| Community::read(conn, cid)).await??;
115     Ok(community.into())
116   }
117 }