Add address() method to Actor type

Returns ActorAddress object.
This commit is contained in:
silverpill 2022-07-10 19:41:27 +00:00
parent e0a1141552
commit d831eb79e4
2 changed files with 32 additions and 16 deletions

View file

@ -162,6 +162,22 @@ fn parse_extra_field(
}
impl Actor {
pub fn address(
&self,
this_instance_host: &str,
) -> Result<ActorAddress, url::ParseError> {
let actor_host = url::Url::parse(&self.id)?
.host_str()
.ok_or(url::ParseError::EmptyHost)?
.to_owned();
let is_local = actor_host == this_instance_host;
let actor_address = ActorAddress {
username: self.preferred_username.clone(),
instance: actor_host,
is_local,
};
Ok(actor_address)
}
pub fn parse_attachments(&self) -> (Vec<IdentityProof>, Vec<ExtraField>) {
let mut identity_proofs = vec![];
@ -365,8 +381,21 @@ mod tests {
};
use super::*;
const INSTANCE_HOST: &str = "example.com";
const INSTANCE_URL: &str = "https://example.com";
#[test]
fn test_get_actor_address() {
let actor = Actor {
id: "https://test.org/users/1".to_string(),
preferred_username: "test".to_string(),
..Default::default()
};
let actor_address = actor.address(INSTANCE_HOST).unwrap();
assert_eq!(actor_address.is_local, false);
assert_eq!(actor_address.acct(), "test@test.org");
}
#[test]
fn test_local_actor() {
let private_key = generate_weak_private_key().unwrap();

View file

@ -56,35 +56,22 @@ impl From<ImportError> for HttpError {
}
}
fn get_actor_host(actor_id: &str) -> Result<String, url::ParseError> {
let actor_host = url::Url::parse(actor_id)?
.host_str()
.ok_or(url::ParseError::EmptyHost)?
.to_owned();
Ok(actor_host)
}
async fn prepare_remote_profile_data(
instance: &Instance,
media_dir: &Path,
actor: Actor,
) -> Result<ProfileCreateData, ImportError> {
let actor_host = get_actor_host(&actor.id)
let actor_address = actor.address(&instance.host())
.map_err(|_| ValidationError("invalid actor ID"))?;
if actor_host == instance.host() {
if actor_address.is_local {
return Err(ImportError::LocalObject);
};
let actor_address = format!(
"{}@{}",
actor.preferred_username,
actor_host,
);
let (avatar, banner) = fetch_avatar_and_banner(&actor, media_dir).await?;
let (identity_proofs, extra_fields) = actor.parse_attachments();
let profile_data = ProfileCreateData {
username: actor.preferred_username.clone(),
display_name: actor.name.clone(),
acct: actor_address,
acct: actor_address.acct(),
bio: actor.summary.clone(),
avatar,
banner,