]> Untitled Git - lemmy.git/blob - crates/apub/src/extensions/person_extension.rs
Federate Matrix ID (fixes #1438)
[lemmy.git] / crates / apub / src / extensions / person_extension.rs
1 use activitystreams::unparsed::UnparsedMutExt;
2 use activitystreams_ext::UnparsedExtension;
3 use lemmy_utils::LemmyError;
4 use serde::{Deserialize, Serialize};
5
6 /// Activitystreams extension to allow (de)serializing additional Person field
7 /// `also_known_as` (used for Matrix profile link).
8 #[derive(Clone, Debug, Default, Deserialize, Serialize)]
9 #[serde(rename_all = "camelCase")]
10 pub struct PersonExtension {
11   pub matrix_user_id: Option<String>,
12 }
13
14 impl PersonExtension {
15   pub fn new(matrix_user_id: Option<String>) -> Result<PersonExtension, LemmyError> {
16     Ok(PersonExtension { matrix_user_id })
17   }
18 }
19
20 impl<U> UnparsedExtension<U> for PersonExtension
21 where
22   U: UnparsedMutExt,
23 {
24   type Error = serde_json::Error;
25
26   fn try_from_unparsed(unparsed_mut: &mut U) -> Result<Self, Self::Error> {
27     Ok(PersonExtension {
28       matrix_user_id: unparsed_mut.remove("matrix_user_id")?,
29     })
30   }
31
32   fn try_into_unparsed(self, unparsed_mut: &mut U) -> Result<(), Self::Error> {
33     unparsed_mut.insert("matrix_user_id", self.matrix_user_id)?;
34     Ok(())
35   }
36 }