Rename instance.host() to instance.hostname()

This commit is contained in:
silverpill 2022-11-24 01:51:43 +00:00
parent 473147ed04
commit 262b910638
17 changed files with 68 additions and 71 deletions

View file

@ -217,13 +217,9 @@ pub struct ActorAddress {
}
impl ActorAddress {
pub fn is_local(&self, instance_host: &str) -> bool {
self.hostname == instance_host
}
/// Returns acct string, as used in Mastodon
pub fn acct(&self, instance_host: &str) -> String {
if self.is_local(instance_host) {
pub fn acct(&self, local_hostname: &str) -> String {
if self.hostname == local_hostname {
self.username.clone()
} else {
self.to_string()
@ -357,8 +353,8 @@ pub fn get_instance_actor(
])),
id: actor_id,
object_type: SERVICE.to_string(),
name: Some(instance.host()),
preferred_username: instance.host(),
name: Some(instance.hostname()),
preferred_username: instance.hostname(),
inbox: actor_inbox,
outbox: actor_outbox,
followers: None,
@ -385,7 +381,7 @@ mod tests {
};
use super::*;
const INSTANCE_HOST: &str = "example.com";
const INSTANCE_HOSTNAME: &str = "example.com";
const INSTANCE_URL: &str = "https://example.com";
#[test]
@ -405,8 +401,7 @@ mod tests {
..Default::default()
};
let actor_address = actor.address().unwrap();
assert_eq!(actor_address.is_local(INSTANCE_HOST), false);
assert_eq!(actor_address.acct(INSTANCE_HOST), "test@test.org");
assert_eq!(actor_address.acct(INSTANCE_HOSTNAME), "test@test.org");
}
#[test]

View file

@ -58,7 +58,7 @@ pub struct Note {
}
pub fn build_note(
instance_host: &str,
instance_hostname: &str,
instance_url: &str,
post: &Post,
) -> Note {
@ -97,7 +97,7 @@ pub fn build_note(
let mut tags = vec![];
for profile in &post.mentions {
let tag_name = format!("@{}", profile.actor_address(instance_host));
let tag_name = format!("@{}", profile.actor_address(instance_hostname));
let actor_id = profile.actor_id(instance_url);
if !primary_audience.contains(&actor_id) {
primary_audience.push(actor_id.clone());
@ -164,11 +164,11 @@ pub fn build_note(
}
pub fn build_create_note(
instance_host: &str,
instance_hostname: &str,
instance_url: &str,
post: &Post,
) -> Activity {
let object = build_note(instance_host, instance_url, post);
let object = build_note(instance_hostname, instance_url, post);
let primary_audience = object.to.clone();
let secondary_audience = object.cc.clone();
let activity_id = format!("{}/create", object.id);
@ -225,7 +225,7 @@ pub async fn prepare_create_note(
) -> Result<OutgoingActivity<Activity>, DatabaseError> {
assert_eq!(author.id, post.author.id);
let activity = build_create_note(
&instance.host(),
&instance.hostname(),
&instance.url(),
post,
);
@ -244,7 +244,7 @@ mod tests {
use crate::models::profiles::types::DbActorProfile;
use super::*;
const INSTANCE_HOST: &str = "example.com";
const INSTANCE_HOSTNAME: &str = "example.com";
const INSTANCE_URL: &str = "https://example.com";
#[test]
@ -254,7 +254,7 @@ mod tests {
..Default::default()
};
let post = Post { author, ..Default::default() };
let note = build_note(INSTANCE_HOST, INSTANCE_URL, &post);
let note = build_note(INSTANCE_HOSTNAME, INSTANCE_URL, &post);
assert_eq!(
note.id,
@ -279,7 +279,7 @@ mod tests {
visibility: Visibility::Followers,
..Default::default()
};
let note = build_note(INSTANCE_HOST, INSTANCE_URL, &post);
let note = build_note(INSTANCE_HOSTNAME, INSTANCE_URL, &post);
assert_eq!(note.to, vec![
local_actor_followers(INSTANCE_URL, &post.author.username),
@ -305,7 +305,7 @@ mod tests {
mentions: vec![subscriber],
..Default::default()
};
let note = build_note(INSTANCE_HOST, INSTANCE_URL, &post);
let note = build_note(INSTANCE_HOSTNAME, INSTANCE_URL, &post);
assert_eq!(note.to, vec![
local_actor_subscribers(INSTANCE_URL, &post.author.username),
@ -332,7 +332,7 @@ mod tests {
mentions: vec![mentioned],
..Default::default()
};
let note = build_note(INSTANCE_HOST, INSTANCE_URL, &post);
let note = build_note(INSTANCE_HOSTNAME, INSTANCE_URL, &post);
assert_eq!(note.to, vec![mentioned_id]);
assert_eq!(note.cc.is_empty(), true);
@ -346,7 +346,7 @@ mod tests {
in_reply_to: Some(Box::new(parent.clone())),
..Default::default()
};
let note = build_note(INSTANCE_HOST, INSTANCE_URL, &post);
let note = build_note(INSTANCE_HOSTNAME, INSTANCE_URL, &post);
assert_eq!(
note.in_reply_to.unwrap(),
@ -386,7 +386,7 @@ mod tests {
mentions: vec![parent_author],
..Default::default()
};
let note = build_note(INSTANCE_HOST, INSTANCE_URL, &post);
let note = build_note(INSTANCE_HOSTNAME, INSTANCE_URL, &post);
assert_eq!(
note.in_reply_to.unwrap(),
@ -408,7 +408,7 @@ mod tests {
};
let post = Post { author, ..Default::default() };
let activity = build_create_note(
INSTANCE_HOST,
INSTANCE_HOSTNAME,
INSTANCE_URL,
&post,
);

View file

@ -17,7 +17,7 @@ use super::create_note::{
};
fn build_delete_note(
instance_host: &str,
instance_hostname: &str,
instance_url: &str,
post: &Post,
) -> Activity {
@ -31,7 +31,7 @@ fn build_delete_note(
};
let activity_id = format!("{}/delete", object.id);
let Note { to, cc, .. } = build_note(
instance_host,
instance_hostname,
instance_url,
post,
);
@ -57,7 +57,7 @@ pub async fn prepare_delete_note(
let mut post = post.clone();
add_related_posts(db_client, vec![&mut post]).await?;
let activity = build_delete_note(
&instance.host(),
&instance.hostname(),
&instance.url(),
&post,
);
@ -80,7 +80,7 @@ mod tests {
use crate::models::profiles::types::DbActorProfile;
use super::*;
const INSTANCE_HOST: &str = "example.com";
const INSTANCE_HOSTNAME: &str = "example.com";
const INSTANCE_URL: &str = "https://example.com";
#[test]
@ -91,7 +91,7 @@ mod tests {
};
let post = Post { author, ..Default::default() };
let activity = build_delete_note(
INSTANCE_HOST,
INSTANCE_HOSTNAME,
INSTANCE_URL,
&post,
);

View file

@ -38,7 +38,7 @@ async fn create_remote_profile(
actor: Actor,
) -> Result<DbActorProfile, HandlerError> {
let actor_address = actor.address()?;
if actor_address.is_local(&instance.host()) {
if actor_address.hostname == instance.hostname() {
return Err(HandlerError::LocalObject);
};
let (maybe_avatar, maybe_banner) = fetch_actor_images(
@ -110,7 +110,7 @@ pub async fn get_or_import_profile_by_actor_id(
Err(DatabaseError::NotFound(_)) => {
let actor = fetch_actor(instance, actor_id).await?;
let actor_address = actor.address()?;
let acct = actor_address.acct(&instance.host());
let acct = actor_address.acct(&instance.hostname());
match get_profile_by_acct(db_client, &acct).await {
Ok(profile) => {
// WARNING: Possible actor ID change
@ -149,13 +149,13 @@ pub async fn import_profile_by_actor_address(
media_dir: &Path,
actor_address: &ActorAddress,
) -> Result<DbActorProfile, HandlerError> {
if actor_address.hostname == instance.host() {
if actor_address.hostname == instance.hostname() {
return Err(HandlerError::LocalObject);
};
let actor_id = perform_webfinger_query(instance, actor_address).await?;
let actor = fetch_actor(instance, &actor_id).await?;
let profile_acct = actor.address()?.acct(&instance.host());
if profile_acct != actor_address.acct(&instance.host()) {
let profile_acct = actor.address()?.acct(&instance.hostname());
if profile_acct != actor_address.acct(&instance.hostname()) {
// Redirected to different server
match get_profile_by_acct(db_client, &profile_acct).await {
Ok(profile) => return Ok(profile),

View file

@ -260,7 +260,7 @@ pub async fn handle_note(
},
};
if let Ok(actor_address) = mention_to_address(&tag_name) {
let acct = actor_address.acct(&instance.host());
let acct = actor_address.acct(&instance.hostname());
let profile = match get_profile_by_acct(
db_client,
&acct,

View file

@ -147,7 +147,7 @@ async fn outbox(
return None;
};
let activity = build_create_note(
&instance.host(),
&instance.hostname(),
&instance.url(),
post,
);
@ -316,7 +316,7 @@ pub async fn object_view(
};
add_related_posts(db_client, vec![&mut post]).await?;
let object = build_note(
&config.instance().host(),
&config.instance().hostname(),
&config.instance().url(),
&post,
);

View file

@ -52,7 +52,7 @@ pub fn make_feed(
let actor_url = local_actor_id(&instance.url(), &profile.username);
let actor_name = profile.display_name.as_ref()
.unwrap_or(&profile.username);
let actor_address = profile.actor_address(&instance.host());
let actor_address = profile.actor_address(&instance.hostname());
let feed_title = format!("{} (@{})", actor_name, actor_address);
let mut entries = vec![];
let mut feed_updated_at = get_min_datetime();

View file

@ -185,7 +185,7 @@ impl Instance {
self._url.origin().ascii_serialization()
}
pub fn host(&self) -> String {
pub fn hostname(&self) -> String {
self._url.host_str().unwrap().to_string()
}
@ -300,7 +300,7 @@ mod tests {
};
assert_eq!(instance.url(), "https://example.com");
assert_eq!(instance.host(), "example.com");
assert_eq!(instance.hostname(), "example.com");
assert_eq!(instance.agent(), "Mitra 1.0.0; https://example.com");
}
@ -317,6 +317,6 @@ mod tests {
};
assert_eq!(instance.url(), "http://1.2.3.4:3777");
assert_eq!(instance.host(), "1.2.3.4");
assert_eq!(instance.hostname(), "1.2.3.4");
}
}

View file

@ -10,15 +10,15 @@ use super::utils::address_to_string;
pub fn verify_eip4361_signature(
message: &str,
signature: &str,
instance_host: &str,
instance_hostname: &str,
login_message: &str,
) -> Result<String, ValidationError> {
let message: Message = message.parse()
.map_err(|_| ValidationError("invalid EIP-4361 message"))?;
let signature_bytes = <[u8; 65]>::from_hex(signature.trim_start_matches("0x"))
.map_err(|_| ValidationError("invalid signature string"))?;
if message.domain != instance_host {
return Err(ValidationError("domain doesn't match instance host"));
if message.domain != instance_hostname {
return Err(ValidationError("domain doesn't match instance hostname"));
};
let statement = message.statement.as_ref()
.ok_or(ValidationError("statement is missing"))?;
@ -42,7 +42,7 @@ pub fn verify_eip4361_signature(
mod tests {
use super::*;
const INSTANCE_HOST: &str = "example.com";
const INSTANCE_HOSTNAME: &str = "example.com";
const LOGIN_MESSAGE: &str = "test";
#[test]
@ -60,7 +60,8 @@ Issued At: 2022-02-14T22:27:35.500Z";
let signature = "0x9059c9a69c31e87d887262a574abcc33f320d5b778bea8a35c6fbdea94a17e9652b99f7cdd146ed67fa8e4bb02462774b958a129c421fe8d743a43bf67dcbcd61c";
let wallet_address = verify_eip4361_signature(
message, signature,
INSTANCE_HOST, LOGIN_MESSAGE,
INSTANCE_HOSTNAME,
LOGIN_MESSAGE,
).unwrap();
assert_eq!(wallet_address, "0x70997970c51812dc3a010c7d01b50e0d17dc79c8");
}
@ -71,7 +72,8 @@ Issued At: 2022-02-14T22:27:35.500Z";
let signature = "xyz";
let error = verify_eip4361_signature(
message, signature,
INSTANCE_HOST, LOGIN_MESSAGE,
INSTANCE_HOSTNAME,
LOGIN_MESSAGE,
).unwrap_err();
assert_eq!(error.to_string(), "invalid EIP-4361 message");
}

View file

@ -135,7 +135,7 @@ pub async fn create_account(
let wallet_address = verify_eip4361_signature(
message,
signature,
&config.instance().host(),
&config.instance().hostname(),
&config.login_message,
)?;
Some(wallet_address)

View file

@ -102,7 +102,7 @@ impl InstanceInfo {
None => (),
};
Self {
uri: config.instance().host(),
uri: config.instance().hostname(),
title: config.instance_title.clone(),
short_description: config.instance_short_description.clone(),
description: config.instance_description.clone(),

View file

@ -52,7 +52,7 @@ async fn token_view(
let wallet_address = verify_eip4361_signature(
message,
signature,
&config.instance().host(),
&config.instance().hostname(),
&config.login_message,
)?;
get_user_by_login_address(db_client, &wallet_address).await?

View file

@ -105,7 +105,7 @@ async fn search_profiles_or_import(
limit: u16,
) -> Result<Vec<DbActorProfile>, DatabaseError> {
if let Some(ref hostname) = maybe_hostname {
if hostname == &config.instance().host() {
if hostname == &config.instance().hostname() {
// This is a local profile
maybe_hostname = None;
};

View file

@ -61,12 +61,12 @@ async fn create_status(
// Mentions
let mention_map = find_mentioned_profiles(
db_client,
&instance.host(),
&instance.hostname(),
&post_data.content,
).await?;
post_data.content = replace_mentions(
&mention_map,
&instance.host(),
&instance.hostname(),
&instance.url(),
&post_data.content,
);

View file

@ -15,7 +15,7 @@ const MENTION_SEARCH_SECONDARY_RE: &str = r"^(?P<username>[\w\.-]+)(@(?P<hostnam
/// Finds everything that looks like a mention
fn find_mentions(
instance_host: &str,
instance_hostname: &str,
text: &str,
) -> Vec<String> {
let mention_re = Regex::new(MENTION_SEARCH_RE).unwrap();
@ -26,10 +26,10 @@ fn find_mentions(
let username = secondary_caps["username"].to_string();
let hostname = secondary_caps.name("hostname")
.map(|match_| match_.as_str())
.unwrap_or(instance_host)
.unwrap_or(instance_hostname)
.to_string();
let actor_address = ActorAddress { username, hostname };
let acct = actor_address.acct(instance_host);
let acct = actor_address.acct(instance_hostname);
if !mentions.contains(&acct) {
mentions.push(acct);
};
@ -40,10 +40,10 @@ fn find_mentions(
pub async fn find_mentioned_profiles(
db_client: &impl GenericClient,
instance_host: &str,
instance_hostname: &str,
text: &str,
) -> Result<HashMap<String, DbActorProfile>, DatabaseError> {
let mentions = find_mentions(instance_host, text);
let mentions = find_mentions(instance_hostname, text);
let profiles = get_profiles_by_accts(db_client, mentions).await?;
let mut mention_map: HashMap<String, DbActorProfile> = HashMap::new();
for profile in profiles {
@ -54,7 +54,7 @@ pub async fn find_mentioned_profiles(
pub fn replace_mentions(
mention_map: &HashMap<String, DbActorProfile>,
instance_host: &str,
instance_hostname: &str,
instance_url: &str,
text: &str,
) -> String {
@ -65,10 +65,10 @@ pub fn replace_mentions(
let username = secondary_caps["username"].to_string();
let hostname = secondary_caps.name("hostname")
.map(|match_| match_.as_str())
.unwrap_or(instance_host)
.unwrap_or(instance_hostname)
.to_string();
let actor_address = ActorAddress { username, hostname };
let acct = actor_address.acct(instance_host);
let acct = actor_address.acct(instance_hostname);
if let Some(profile) = mention_map.get(&acct) {
// Replace with a link to profile.
// Actor URL may differ from actor ID.
@ -108,7 +108,7 @@ mod tests {
use crate::activitypub::actors::types::Actor;
use super::*;
const INSTANCE_HOST: &str = "server1.com";
const INSTANCE_HOSTNAME: &str = "server1.com";
const INSTANCE_URL: &str = "https://server1.com";
const TEXT_WITH_MENTIONS: &str = concat!(
"@user1 ",
@ -124,7 +124,7 @@ mod tests {
#[test]
fn test_find_mentions() {
let results = find_mentions(INSTANCE_HOST, TEXT_WITH_MENTIONS);
let results = find_mentions(INSTANCE_HOSTNAME, TEXT_WITH_MENTIONS);
assert_eq!(results, vec![
"user1",
"user_x",
@ -171,7 +171,7 @@ mod tests {
]);
let result = replace_mentions(
&mention_map,
INSTANCE_HOST,
INSTANCE_HOSTNAME,
INSTANCE_URL,
TEXT_WITH_MENTIONS,
);

View file

@ -330,12 +330,12 @@ impl DbActorProfile {
self.actor_id(instance_url)
}
pub fn actor_address(&self, instance_host: &str) -> ActorAddress {
pub fn actor_address(&self, local_hostname: &str) -> ActorAddress {
assert_eq!(self.hostname.is_none(), self.is_local());
ActorAddress {
username: self.username.clone(),
hostname: self.hostname.as_deref()
.unwrap_or(instance_host)
.unwrap_or(local_hostname)
.to_string(),
}
}
@ -475,7 +475,7 @@ mod tests {
use crate::activitypub::actors::types::Actor;
use super::*;
const INSTANCE_HOST: &str = "example.com";
const INSTANCE_HOSTNAME: &str = "example.com";
#[test]
fn test_identity_proof_serialization() {
@ -527,7 +527,7 @@ mod tests {
..Default::default()
};
assert_eq!(
local_profile.actor_address(INSTANCE_HOST).to_string(),
local_profile.actor_address(INSTANCE_HOSTNAME).to_string(),
"user@example.com",
);
}
@ -545,7 +545,7 @@ mod tests {
..Default::default()
};
assert_eq!(
remote_profile.actor_address(INSTANCE_HOST).to_string(),
remote_profile.actor_address(INSTANCE_HOSTNAME).to_string(),
remote_profile.acct,
);
}

View file

@ -37,11 +37,11 @@ async fn get_user_info(
query_params: WebfingerQueryParams,
) -> Result<JsonResourceDescriptor, HttpError> {
let actor_address = parse_acct_uri(&query_params.resource)?;
if !actor_address.is_local(&instance.host()) {
if actor_address.hostname != instance.hostname() {
// Wrong instance
return Err(HttpError::NotFoundError("user"));
};
let actor_url = if actor_address.username == instance.host() {
let actor_url = if actor_address.username == instance.hostname() {
local_instance_actor_id(&instance.url())
} else {
if !is_registered_user(db_client, &actor_address.username).await? {