]> Untitled Git - lemmy.git/commitdiff
Federate Matrix ID (fixes #1438)
authorFelix Ableitner <me@nutomic.com>
Wed, 24 Mar 2021 16:43:18 +0000 (17:43 +0100)
committerFelix Ableitner <me@nutomic.com>
Wed, 24 Mar 2021 16:56:13 +0000 (17:56 +0100)
crates/apub/src/extensions/context.rs
crates/apub/src/extensions/group_extension.rs [moved from crates/apub/src/extensions/group_extensions.rs with 100% similarity]
crates/apub/src/extensions/mod.rs
crates/apub/src/extensions/person_extension.rs [new file with mode: 0644]
crates/apub/src/lib.rs
crates/apub/src/objects/community.rs
crates/apub/src/objects/person.rs

index a36b848991dad7dc2e5dbd7570f44754a058d029..041ca4bf0a271bb109a09d8ef5531b2beecf432b 100644 (file)
@@ -14,7 +14,11 @@ pub(crate) fn lemmy_context() -> Result<Vec<AnyBase>, LemmyError> {
       "type": "sc:Boolean",
       "id": "pt:commentsEnabled"
     },
-    "moderators": "as:moderators"
+    "moderators": "as:moderators",
+    "matrixUserId": {
+      "type": "sc:Text",
+      "id": "as:alsoKnownAs"
+    },
   }))?;
   Ok(vec![
     AnyBase::from(context()),
index 4b4c08664fa59ce6a09cd86e3832308edfea431a..2f7df8211a3d619e3619bef909d4f2174f68b775 100644 (file)
@@ -1,4 +1,5 @@
 pub(crate) mod context;
-pub(crate) mod group_extensions;
+pub(crate) mod group_extension;
 pub(crate) mod page_extension;
+pub(crate) mod person_extension;
 pub(crate) mod signatures;
diff --git a/crates/apub/src/extensions/person_extension.rs b/crates/apub/src/extensions/person_extension.rs
new file mode 100644 (file)
index 0000000..248adcc
--- /dev/null
@@ -0,0 +1,36 @@
+use activitystreams::unparsed::UnparsedMutExt;
+use activitystreams_ext::UnparsedExtension;
+use lemmy_utils::LemmyError;
+use serde::{Deserialize, Serialize};
+
+/// Activitystreams extension to allow (de)serializing additional Person field
+/// `also_known_as` (used for Matrix profile link).
+#[derive(Clone, Debug, Default, Deserialize, Serialize)]
+#[serde(rename_all = "camelCase")]
+pub struct PersonExtension {
+  pub matrix_user_id: Option<String>,
+}
+
+impl PersonExtension {
+  pub fn new(matrix_user_id: Option<String>) -> Result<PersonExtension, LemmyError> {
+    Ok(PersonExtension { matrix_user_id })
+  }
+}
+
+impl<U> UnparsedExtension<U> for PersonExtension
+where
+  U: UnparsedMutExt,
+{
+  type Error = serde_json::Error;
+
+  fn try_from_unparsed(unparsed_mut: &mut U) -> Result<Self, Self::Error> {
+    Ok(PersonExtension {
+      matrix_user_id: unparsed_mut.remove("matrix_user_id")?,
+    })
+  }
+
+  fn try_into_unparsed(self, unparsed_mut: &mut U) -> Result<(), Self::Error> {
+    unparsed_mut.insert("matrix_user_id", self.matrix_user_id)?;
+    Ok(())
+  }
+}
index 74d4cbef306bbda7833dd7de78c2c01d064f9b5d..0e2c826addabe4ad4ba0e94346f7fde4ff5d17dd 100644 (file)
@@ -11,8 +11,9 @@ pub mod objects;
 pub mod routes;
 
 use crate::extensions::{
-  group_extensions::GroupExtension,
+  group_extension::GroupExtension,
   page_extension::PageExtension,
+  person_extension::PersonExtension,
   signatures::{PublicKey, PublicKeyExtension},
 };
 use activitystreams::{
@@ -46,7 +47,7 @@ use url::{ParseError, Url};
 /// Activitystreams type for community
 type GroupExt = Ext2<actor::ApActor<ApObject<actor::Group>>, GroupExtension, PublicKeyExtension>;
 /// Activitystreams type for person
-type PersonExt = Ext1<actor::ApActor<ApObject<actor::Person>>, PublicKeyExtension>;
+type PersonExt = Ext2<actor::ApActor<ApObject<actor::Person>>, PersonExtension, PublicKeyExtension>;
 /// Activitystreams type for post
 type PageExt = Ext1<ApObject<Page>, PageExtension>;
 type NoteExt = ApObject<Note>;
index a73febceb20508a97c3fd38860f4a1bd39168ce1..9667031c7059838ebf7683db0ddc08aaffabaaa1 100644 (file)
@@ -1,5 +1,5 @@
 use crate::{
-  extensions::{context::lemmy_context, group_extensions::GroupExtension},
+  extensions::{context::lemmy_context, group_extension::GroupExtension},
   fetcher::{community::fetch_community_mods, person::get_or_fetch_and_upsert_person},
   generate_moderators_url,
   objects::{
index cc45fd7ed80df14d3c5741ea1561b8e2b75db983..662871dc14ea0a59672ab6a89e37ee3ea5cbdbfa 100644 (file)
@@ -1,5 +1,5 @@
 use crate::{
-  extensions::context::lemmy_context,
+  extensions::{context::lemmy_context, person_extension::PersonExtension},
   objects::{
     check_object_domain,
     get_source_markdown_value,
@@ -16,7 +16,7 @@ use activitystreams::{
   object::{ApObject, Image, Tombstone},
   prelude::*,
 };
-use activitystreams_ext::Ext1;
+use activitystreams_ext::Ext2;
 use anyhow::Context;
 use lemmy_api_structs::blocking;
 use lemmy_db_queries::{ApubObject, DbPool};
@@ -77,7 +77,8 @@ impl ToApub for DbPerson {
         ..Default::default()
       });
 
-    Ok(Ext1::new(ap_actor, self.get_public_key_ext()?))
+    let person_ext = PersonExtension::new(self.matrix_user_id.to_owned())?;
+    Ok(Ext2::new(ap_actor, person_ext, self.get_public_key_ext()?))
   }
   fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
     unimplemented!()
@@ -192,11 +193,11 @@ impl FromApubToForm<PersonExt> for PersonForm {
       local: Some(false),
       admin: Some(false),
       private_key: None,
-      public_key: Some(Some(person.ext_one.public_key.to_owned().public_key_pem)),
+      public_key: Some(Some(person.ext_two.public_key.to_owned().public_key_pem)),
       last_refreshed_at: Some(naive_now()),
       inbox_url: Some(person.inner.inbox()?.to_owned().into()),
       shared_inbox_url: Some(shared_inbox),
-      matrix_user_id: None, // TODO
+      matrix_user_id: Some(person.ext_one.matrix_user_id.to_owned()),
     })
   }
 }