]> Untitled Git - lemmy.git/blob - crates/apub/src/protocol/activities/voting/vote.rs
Remove ActivityFields trait, deserialize into another struct instead
[lemmy.git] / crates / apub / src / protocol / activities / voting / vote.rs
1 use crate::{
2   fetcher::{object_id::ObjectId, post_or_comment::PostOrComment},
3   objects::person::ApubPerson,
4 };
5 use activitystreams::unparsed::Unparsed;
6 use anyhow::anyhow;
7 use lemmy_utils::LemmyError;
8 use serde::{Deserialize, Serialize};
9 use std::convert::TryFrom;
10 use strum_macros::ToString;
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) to: Vec<Url>,
18   pub(crate) object: ObjectId<PostOrComment>,
19   pub(crate) cc: Vec<Url>,
20   #[serde(rename = "type")]
21   pub(crate) kind: VoteType,
22   pub(crate) id: Url,
23   #[serde(flatten)]
24   pub(crate) unparsed: Unparsed,
25 }
26
27 #[derive(Clone, Debug, ToString, Deserialize, Serialize)]
28 pub enum VoteType {
29   Like,
30   Dislike,
31 }
32
33 impl TryFrom<i16> for VoteType {
34   type Error = LemmyError;
35
36   fn try_from(value: i16) -> Result<Self, Self::Error> {
37     match value {
38       1 => Ok(VoteType::Like),
39       -1 => Ok(VoteType::Dislike),
40       _ => Err(anyhow!("invalid vote value").into()),
41     }
42   }
43 }
44
45 impl From<&VoteType> for i16 {
46   fn from(value: &VoteType) -> i16 {
47     match value {
48       VoteType::Like => 1,
49       VoteType::Dislike => -1,
50     }
51   }
52 }