Rewrite DbActorProfile.actor_address() to return ActorAddress object

This commit is contained in:
silverpill 2022-10-04 18:34:00 +00:00
parent a6c525f35d
commit 5c56f831db
4 changed files with 26 additions and 16 deletions

View file

@ -1,3 +1,4 @@
use std::fmt;
use std::str::FromStr;
use regex::Regex;
@ -214,9 +215,9 @@ impl FromStr for ActorAddress {
}
}
impl ToString for ActorAddress {
fn to_string(&self) -> String {
format!("{}@{}", self.username, self.instance)
impl fmt::Display for ActorAddress {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "{}@{}", self.username, self.instance)
}
}

View file

@ -291,6 +291,7 @@ mod tests {
let subscriber_id = "https://test.com/users/3";
let subscriber = DbActorProfile {
username: "subscriber".to_string(),
hostname: Some("test.com".to_string()),
actor_json: Some(Actor {
id: subscriber_id.to_string(),
..Default::default()
@ -317,6 +318,7 @@ mod tests {
let mentioned_id = "https://test.com/users/3";
let mentioned = DbActorProfile {
username: "mention".to_string(),
hostname: Some("test.com".to_string()),
actor_json: Some(Actor {
id: mentioned_id.to_string(),
..Default::default()
@ -361,6 +363,8 @@ mod tests {
let parent_author_actor_id = "https://test.net/user/test";
let parent_author_actor_url = "https://test.net/@test";
let parent_author = DbActorProfile {
username: "test".to_string(),
hostname: Some("test.net".to_string()),
acct: parent_author_acct.to_string(),
actor_json: Some(Actor {
id: parent_author_actor_id.to_string(),

View file

@ -104,7 +104,7 @@ pub async fn perform_webfinger_query(
instance: &Instance,
actor_address: &ActorAddress,
) -> Result<String, FetchError> {
let webfinger_account_uri = format!("acct:{}", actor_address.to_string());
let webfinger_account_uri = format!("acct:{}", actor_address);
// TOOD: support http
let webfinger_url = format!(
"https://{}/.well-known/webfinger",

View file

@ -222,7 +222,6 @@ pub struct DbActorProfile {
pub id: Uuid,
pub username: String,
pub hostname: Option<String>,
pub acct: String,
pub display_name: Option<String>,
pub bio: Option<String>, // html
pub bio_source: Option<String>, // plaintext or markdown
@ -235,11 +234,12 @@ pub struct DbActorProfile {
pub following_count: i32,
pub subscriber_count: i32,
pub post_count: i32,
pub actor_json: Option<Actor>,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
pub actor_json: Option<Actor>,
// auto-generated database fields
pub acct: String,
pub actor_id: Option<String>,
}
@ -256,7 +256,6 @@ impl DbActorProfile {
}
pub fn actor_id(&self, instance_url: &str) -> String {
// TODO: use actor_id field
match self.actor_json {
Some(ref actor) => actor.id.clone(),
None => local_actor_id(instance_url, &self.username),
@ -273,11 +272,13 @@ impl DbActorProfile {
self.actor_id(instance_url)
}
pub fn actor_address(&self, instance_host: &str) -> String {
if self.is_local() {
format!("{}@{}", self.acct, instance_host)
} else {
self.acct.clone()
pub fn actor_address(&self, instance_host: &str) -> ActorAddress {
assert_eq!(self.hostname.is_none(), self.is_local());
ActorAddress {
username: self.username.clone(),
instance: self.hostname.as_deref()
.unwrap_or(instance_host)
.to_string(),
}
}
@ -311,10 +312,10 @@ impl Default for DbActorProfile {
following_count: 0,
subscriber_count: 0,
post_count: 0,
created_at: now,
updated_at: now,
actor_json: None,
actor_id: None,
created_at: now,
updated_at: now,
}
}
}
@ -465,12 +466,14 @@ mod tests {
#[test]
fn test_local_actor_address() {
let local_profile = DbActorProfile {
username: "user".to_string(),
hostname: None,
acct: "user".to_string(),
actor_json: None,
..Default::default()
};
assert_eq!(
local_profile.actor_address(INSTANCE_HOST),
local_profile.actor_address(INSTANCE_HOST).to_string(),
"user@example.com",
);
}
@ -478,6 +481,8 @@ mod tests {
#[test]
fn test_remote_actor_address() {
let remote_profile = DbActorProfile {
username: "test".to_string(),
hostname: Some("remote.com".to_string()),
acct: "test@remote.com".to_string(),
actor_json: Some(Actor {
id: "https://test".to_string(),
@ -486,7 +491,7 @@ mod tests {
..Default::default()
};
assert_eq!(
remote_profile.actor_address(INSTANCE_HOST),
remote_profile.actor_address(INSTANCE_HOST).to_string(),
remote_profile.acct,
);
}