]> Untitled Git - lemmy.git/blob - crates/apub/src/protocol/activities/voting/vote.rs
6d98862ec80279af5869c502b03e51aae6536a35
[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 activitypub_federation::core::object_id::ObjectId;
7 use lemmy_utils::error::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   pub(crate) object: ObjectId<PostOrComment>,
18   #[serde(rename = "type")]
19   pub(crate) kind: VoteType,
20   pub(crate) id: Url,
21
22   #[serde(flatten)]
23   pub(crate) unparsed: Unparsed,
24 }
25
26 #[derive(Clone, Debug, Display, Deserialize, Serialize, PartialEq, Eq)]
27 pub enum VoteType {
28   Like,
29   Dislike,
30 }
31
32 impl TryFrom<i16> for VoteType {
33   type Error = LemmyError;
34
35   fn try_from(value: i16) -> Result<Self, Self::Error> {
36     match value {
37       1 => Ok(VoteType::Like),
38       -1 => Ok(VoteType::Dislike),
39       _ => Err(LemmyError::from_message("invalid vote value")),
40     }
41   }
42 }
43
44 impl From<&VoteType> for i16 {
45   fn from(value: &VoteType) -> i16 {
46     match value {
47       VoteType::Like => 1,
48       VoteType::Dislike => -1,
49     }
50   }
51 }