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