]> Untitled Git - lemmy.git/blobdiff - crates/apub/src/lib.rs
Adding shortname fetching for users and communities. Fixes #1662 (#1663)
[lemmy.git] / crates / apub / src / lib.rs
index bbc6d515ad68e0204a9c4f78675b15ff0f4a2986..c79d14bd7ada56462773b4620f79310fa9e80fc3 100644 (file)
@@ -279,10 +279,11 @@ pub enum EndpointType {
   PrivateMessage,
 }
 
-/// Generates the ActivityPub ID for a given object type and ID.
-pub fn generate_apub_endpoint(
+/// Generates an apub endpoint for a given domain, IE xyz.tld
+pub fn generate_apub_endpoint_for_domain(
   endpoint_type: EndpointType,
   name: &str,
+  domain: &str,
 ) -> Result<DbUrl, ParseError> {
   let point = match endpoint_type {
     EndpointType::Community => "c",
@@ -292,14 +293,18 @@ pub fn generate_apub_endpoint(
     EndpointType::PrivateMessage => "private_message",
   };
 
-  Ok(
-    Url::parse(&format!(
-      "{}/{}/{}",
-      Settings::get().get_protocol_and_hostname(),
-      point,
-      name
-    ))?
-    .into(),
+  Ok(Url::parse(&format!("{}/{}/{}", domain, point, name))?.into())
+}
+
+/// Generates the ActivityPub ID for a given object type and ID.
+pub fn generate_apub_endpoint(
+  endpoint_type: EndpointType,
+  name: &str,
+) -> Result<DbUrl, ParseError> {
+  generate_apub_endpoint_for_domain(
+    endpoint_type,
+    name,
+    &Settings::get().get_protocol_and_hostname(),
   )
 }
 
@@ -330,6 +335,26 @@ pub fn generate_moderators_url(community_id: &DbUrl) -> Result<DbUrl, LemmyError
   Ok(Url::parse(&format!("{}/moderators", community_id))?.into())
 }
 
+/// Takes in a shortname of the type dessalines@xyz.tld or dessalines (assumed to be local), and outputs the actor id.
+/// Used in the API for communities and users.
+pub fn build_actor_id_from_shortname(
+  endpoint_type: EndpointType,
+  short_name: &str,
+) -> Result<DbUrl, ParseError> {
+  let split = short_name.split('@').collect::<Vec<&str>>();
+
+  let name = split[0];
+
+  // If there's no @, its local
+  let domain = if split.len() == 1 {
+    Settings::get().get_protocol_and_hostname()
+  } else {
+    format!("https://{}", split[1])
+  };
+
+  generate_apub_endpoint_for_domain(endpoint_type, name, &domain)
+}
+
 /// Store a sent or received activity in the database, for logging purposes. These records are not
 /// persistent.
 pub async fn insert_activity<T>(