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