]> Untitled Git - lemmy.git/blob - crates/apub/src/protocol/activities/voting/undo_vote.rs
0973c76a86059d2472b31869c7f7f4e071b171d8
[lemmy.git] / crates / apub / src / protocol / activities / voting / undo_vote.rs
1 use crate::{
2   activities::verify_community_matches,
3   local_instance,
4   objects::{community::ApubCommunity, person::ApubPerson},
5   protocol::{activities::voting::vote::Vote, InCommunity},
6 };
7 use activitypub_federation::core::object_id::ObjectId;
8 use activitystreams_kinds::activity::UndoType;
9 use lemmy_api_common::context::LemmyContext;
10 use lemmy_utils::error::LemmyError;
11 use serde::{Deserialize, Serialize};
12 use url::Url;
13
14 #[derive(Clone, Debug, Deserialize, Serialize)]
15 #[serde(rename_all = "camelCase")]
16 pub struct UndoVote {
17   pub(crate) actor: ObjectId<ApubPerson>,
18   pub(crate) object: Vote,
19   #[serde(rename = "type")]
20   pub(crate) kind: UndoType,
21   pub(crate) id: Url,
22   pub(crate) audience: Option<ObjectId<ApubCommunity>>,
23 }
24
25 #[async_trait::async_trait(?Send)]
26 impl InCommunity for UndoVote {
27   async fn community(
28     &self,
29     context: &LemmyContext,
30     request_counter: &mut i32,
31   ) -> Result<ApubCommunity, LemmyError> {
32     let local_instance = local_instance(context).await;
33     let object_community = self.object.community(context, request_counter).await?;
34     if let Some(audience) = &self.audience {
35       let audience = audience
36         .dereference(context, local_instance, request_counter)
37         .await?;
38       verify_community_matches(&audience, object_community.id)?;
39       Ok(audience)
40     } else {
41       Ok(object_community)
42     }
43   }
44 }