]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/voting/vote.rs
4d6697283b9ffa7ce2b706e151b54b348fc6beeb
[lemmy.git] / crates / apub / src / activities / voting / vote.rs
1 use crate::{
2   activities::{
3     community::{
4       announce::{AnnouncableActivities, GetCommunity},
5       send_to_community,
6     },
7     generate_activity_id,
8     verify_activity,
9     verify_is_public,
10     verify_person_in_community,
11     voting::{vote_comment, vote_post},
12   },
13   context::lemmy_context,
14   fetcher::object_id::ObjectId,
15   objects::{community::ApubCommunity, person::ApubPerson},
16   PostOrComment,
17 };
18 use activitystreams::{base::AnyBase, primitives::OneOrMany, public, unparsed::Unparsed};
19 use anyhow::anyhow;
20 use lemmy_api_common::blocking;
21 use lemmy_apub_lib::{
22   data::Data,
23   traits::{ActivityFields, ActivityHandler, ActorType},
24 };
25 use lemmy_db_schema::{
26   newtypes::CommunityId,
27   source::{community::Community, post::Post},
28   traits::Crud,
29 };
30 use lemmy_utils::LemmyError;
31 use lemmy_websocket::LemmyContext;
32 use serde::{Deserialize, Serialize};
33 use std::{convert::TryFrom, ops::Deref};
34 use strum_macros::ToString;
35 use url::Url;
36
37 #[derive(Clone, Debug, ToString, Deserialize, Serialize)]
38 pub enum VoteType {
39   Like,
40   Dislike,
41 }
42
43 impl TryFrom<i16> for VoteType {
44   type Error = LemmyError;
45
46   fn try_from(value: i16) -> Result<Self, Self::Error> {
47     match value {
48       1 => Ok(VoteType::Like),
49       -1 => Ok(VoteType::Dislike),
50       _ => Err(anyhow!("invalid vote value").into()),
51     }
52   }
53 }
54
55 impl From<&VoteType> for i16 {
56   fn from(value: &VoteType) -> i16 {
57     match value {
58       VoteType::Like => 1,
59       VoteType::Dislike => -1,
60     }
61   }
62 }
63
64 #[derive(Clone, Debug, Deserialize, Serialize, ActivityFields)]
65 #[serde(rename_all = "camelCase")]
66 pub struct Vote {
67   actor: ObjectId<ApubPerson>,
68   to: Vec<Url>,
69   pub(in crate::activities::voting) object: ObjectId<PostOrComment>,
70   cc: [ObjectId<ApubCommunity>; 1],
71   #[serde(rename = "type")]
72   pub(in crate::activities::voting) kind: VoteType,
73   id: Url,
74   #[serde(rename = "@context")]
75   context: OneOrMany<AnyBase>,
76   #[serde(flatten)]
77   unparsed: Unparsed,
78 }
79
80 impl Vote {
81   pub(in crate::activities::voting) fn new(
82     object: &PostOrComment,
83     actor: &ApubPerson,
84     community: &ApubCommunity,
85     kind: VoteType,
86     context: &LemmyContext,
87   ) -> Result<Vote, LemmyError> {
88     Ok(Vote {
89       actor: ObjectId::new(actor.actor_id()),
90       to: vec![public()],
91       object: ObjectId::new(object.ap_id()),
92       cc: [ObjectId::new(community.actor_id())],
93       kind: kind.clone(),
94       id: generate_activity_id(kind, &context.settings().get_protocol_and_hostname())?,
95       context: lemmy_context(),
96       unparsed: Default::default(),
97     })
98   }
99
100   pub async fn send(
101     object: &PostOrComment,
102     actor: &ApubPerson,
103     community_id: CommunityId,
104     kind: VoteType,
105     context: &LemmyContext,
106   ) -> Result<(), LemmyError> {
107     let community = blocking(context.pool(), move |conn| {
108       Community::read(conn, community_id)
109     })
110     .await??
111     .into();
112     let vote = Vote::new(object, actor, &community, kind, context)?;
113     let vote_id = vote.id.clone();
114
115     let activity = AnnouncableActivities::Vote(vote);
116     send_to_community(activity, &vote_id, actor, &community, vec![], context).await
117   }
118 }
119
120 #[async_trait::async_trait(?Send)]
121 impl ActivityHandler for Vote {
122   type DataType = LemmyContext;
123   async fn verify(
124     &self,
125     context: &Data<LemmyContext>,
126     request_counter: &mut i32,
127   ) -> Result<(), LemmyError> {
128     verify_is_public(&self.to)?;
129     verify_activity(self, &context.settings())?;
130     let community = self.get_community(context, request_counter).await?;
131     verify_person_in_community(&self.actor, &community, context, request_counter).await?;
132     Ok(())
133   }
134
135   async fn receive(
136     self,
137     context: &Data<LemmyContext>,
138     request_counter: &mut i32,
139   ) -> Result<(), LemmyError> {
140     let actor = self.actor.dereference(context, request_counter).await?;
141     let object = self.object.dereference(context, request_counter).await?;
142     match object {
143       PostOrComment::Post(p) => vote_post(&self.kind, actor, p.deref(), context).await,
144       PostOrComment::Comment(c) => vote_comment(&self.kind, actor, &c, context).await,
145     }
146   }
147 }
148
149 #[async_trait::async_trait(?Send)]
150 impl GetCommunity for Vote {
151   async fn get_community(
152     &self,
153     context: &LemmyContext,
154     request_counter: &mut i32,
155   ) -> Result<ApubCommunity, LemmyError> {
156     let object = self.object.dereference(context, request_counter).await?;
157     let cid = match object {
158       PostOrComment::Post(p) => p.community_id,
159       PostOrComment::Comment(c) => {
160         blocking(context.pool(), move |conn| Post::read(conn, c.post_id))
161           .await??
162           .community_id
163       }
164     };
165     let community = blocking(context.pool(), move |conn| Community::read(conn, cid)).await??;
166     Ok(community.into())
167   }
168 }