From 5c0e6b0b0c48fb143caf51331fd2c7984e7aa1cd Mon Sep 17 00:00:00 2001 From: silverpill Date: Mon, 2 May 2022 22:50:07 +0000 Subject: [PATCH] Rewrite fetch_profile to accept ActorAddress object --- src/activitypub/actor.rs | 8 +++++++- src/activitypub/fetcher/fetchers.rs | 13 +++++++------ src/activitypub/fetcher/helpers.rs | 3 +-- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/activitypub/actor.rs b/src/activitypub/actor.rs index c7ceacb..e5ab9fd 100644 --- a/src/activitypub/actor.rs +++ b/src/activitypub/actor.rs @@ -208,13 +208,19 @@ pub struct ActorAddress { pub is_local: bool, } +impl ToString for ActorAddress { + fn to_string(&self) -> String { + format!("{}@{}", self.username, self.instance) + } +} + impl ActorAddress { /// Returns acct string, as used in Mastodon pub fn acct(&self) -> String { if self.is_local { self.username.clone() } else { - format!("{}@{}", self.username, self.instance) + self.to_string() } } } diff --git a/src/activitypub/fetcher/fetchers.rs b/src/activitypub/fetcher/fetchers.rs index 082b88b..a20802a 100644 --- a/src/activitypub/fetcher/fetchers.rs +++ b/src/activitypub/fetcher/fetchers.rs @@ -4,7 +4,7 @@ use reqwest::Method; use serde_json::Value; use crate::activitypub::activity::Object; -use crate::activitypub::actor::Actor; +use crate::activitypub::actor::{Actor, ActorAddress}; use crate::activitypub::constants::ACTIVITY_CONTENT_TYPE; use crate::config::Instance; use crate::http_signatures::create::{create_http_signature, SignatureError}; @@ -112,14 +112,15 @@ pub async fn fetch_avatar_and_banner( pub async fn fetch_profile( instance: &Instance, - username: &str, - actor_host: &str, + actor_address: &ActorAddress, media_dir: &Path, ) -> Result { - let actor_address = format!("{}@{}", &username, &actor_host); - let webfinger_account_uri = format!("acct:{}", actor_address); + let webfinger_account_uri = format!("acct:{}", actor_address.to_string()); // TOOD: support http - let webfinger_url = format!("https://{}/.well-known/webfinger", actor_host); + let webfinger_url = format!( + "https://{}/.well-known/webfinger", + actor_address.instance, + ); let client = reqwest::Client::new(); let mut request_builder = client.get(&webfinger_url); if !instance.is_private { diff --git a/src/activitypub/fetcher/helpers.rs b/src/activitypub/fetcher/helpers.rs index 3672f64..02fe1ee 100644 --- a/src/activitypub/fetcher/helpers.rs +++ b/src/activitypub/fetcher/helpers.rs @@ -94,8 +94,7 @@ pub async fn import_profile_by_actor_address( }; let mut profile_data = fetch_profile( instance, - &actor_address.username, - &actor_address.instance, + actor_address, media_dir, ).await?; if profile_data.acct != actor_address.acct() {