]> Untitled Git - lemmy.git/blob - crates/apub/src/protocol/activities/voting/vote.rs
When announcing incoming activities, keep extra fields (#2550)
[lemmy.git] / crates / apub / src / protocol / activities / voting / vote.rs
1 use crate::{fetcher::post_or_comment::PostOrComment, objects::person::ApubPerson};
2 use activitypub_federation::core::object_id::ObjectId;
3 use lemmy_utils::error::LemmyError;
4 use serde::{Deserialize, Serialize};
5 use std::convert::TryFrom;
6 use strum_macros::Display;
7 use url::Url;
8
9 #[derive(Clone, Debug, Deserialize, Serialize)]
10 #[serde(rename_all = "camelCase")]
11 pub struct Vote {
12   pub(crate) actor: ObjectId<ApubPerson>,
13   pub(crate) object: ObjectId<PostOrComment>,
14   #[serde(rename = "type")]
15   pub(crate) kind: VoteType,
16   pub(crate) id: Url,
17 }
18
19 #[derive(Clone, Debug, Display, Deserialize, Serialize, PartialEq, Eq)]
20 pub enum VoteType {
21   Like,
22   Dislike,
23 }
24
25 impl TryFrom<i16> for VoteType {
26   type Error = LemmyError;
27
28   fn try_from(value: i16) -> Result<Self, Self::Error> {
29     match value {
30       1 => Ok(VoteType::Like),
31       -1 => Ok(VoteType::Dislike),
32       _ => Err(LemmyError::from_message("invalid vote value")),
33     }
34   }
35 }
36
37 impl From<&VoteType> for i16 {
38   fn from(value: &VoteType) -> i16 {
39     match value {
40       VoteType::Like => 1,
41       VoteType::Dislike => -1,
42     }
43   }
44 }