]> Untitled Git - lemmy.git/blob - crates/apub/src/protocol/activities/voting/vote.rs
Reject federated downvotes if downvotes are disabled (fixes #2124) (#2128)
[lemmy.git] / crates / apub / src / protocol / activities / voting / vote.rs
1 use crate::{
2   fetcher::post_or_comment::PostOrComment,
3   objects::person::ApubPerson,
4   protocol::Unparsed,
5 };
6 use lemmy_apub_lib::object_id::ObjectId;
7 use lemmy_utils::LemmyError;
8 use serde::{Deserialize, Serialize};
9 use std::convert::TryFrom;
10 use strum_macros::Display;
11 use url::Url;
12
13 #[derive(Clone, Debug, Deserialize, Serialize)]
14 #[serde(rename_all = "camelCase")]
15 pub struct Vote {
16   pub(crate) actor: ObjectId<ApubPerson>,
17   #[serde(deserialize_with = "crate::deserialize_one_or_many")]
18   pub(crate) to: Vec<Url>,
19   pub(crate) object: ObjectId<PostOrComment>,
20   #[serde(deserialize_with = "crate::deserialize_one_or_many")]
21   pub(crate) cc: Vec<Url>,
22   #[serde(rename = "type")]
23   pub(crate) kind: VoteType,
24   pub(crate) id: Url,
25
26   #[serde(flatten)]
27   pub(crate) unparsed: Unparsed,
28 }
29
30 #[derive(Clone, Debug, Display, Deserialize, Serialize, PartialEq)]
31 pub enum VoteType {
32   Like,
33   Dislike,
34 }
35
36 impl TryFrom<i16> for VoteType {
37   type Error = LemmyError;
38
39   fn try_from(value: i16) -> Result<Self, Self::Error> {
40     match value {
41       1 => Ok(VoteType::Like),
42       -1 => Ok(VoteType::Dislike),
43       _ => Err(LemmyError::from_message("invalid vote value")),
44     }
45   }
46 }
47
48 impl From<&VoteType> for i16 {
49   fn from(value: &VoteType) -> i16 {
50     match value {
51       VoteType::Like => 1,
52       VoteType::Dislike => -1,
53     }
54   }
55 }