Move mention_to_address function to webfinger::types module

This commit is contained in:
silverpill 2023-03-23 19:19:16 +00:00
parent 3b5c8a4131
commit 521c2cbe41
4 changed files with 31 additions and 33 deletions

View file

@ -42,7 +42,6 @@ use crate::models::{
},
posts::{
hashtags::normalize_hashtag,
mentions::mention_to_address,
queries::create_post,
types::{Post, PostCreateData, Visibility},
validators::{
@ -55,6 +54,7 @@ use crate::models::{
profiles::types::DbActorProfile,
users::queries::get_user_by_name,
};
use crate::webfinger::types::ActorAddress;
use super::HandlerResult;
fn get_object_attributed_to(object: &Object)
@ -401,7 +401,7 @@ pub async fn get_object_tags(
continue;
},
};
if let Ok(actor_address) = mention_to_address(&tag_name) {
if let Ok(actor_address) = ActorAddress::from_mention(&tag_name) {
let profile = match get_or_import_profile_by_actor_address(
db_client,
&instance,

View file

@ -19,7 +19,6 @@ use crate::database::{
use crate::errors::ValidationError;
use crate::mastodon_api::accounts::helpers::follow_or_create_request;
use crate::models::{
posts::mentions::mention_to_address,
profiles::types::DbActorProfile,
relationships::queries::{
follow,
@ -72,7 +71,7 @@ pub fn parse_address_list(csv: &str)
let mut addresses: Vec<_> = csv.lines()
.map(|line| line.trim().to_string())
.filter(|line| !line.is_empty())
.map(|line| mention_to_address(&line))
.map(|line| ActorAddress::from_mention(&line))
.collect::<Result<_, _>>()?;
addresses.sort();
addresses.dedup();

View file

@ -4,7 +4,6 @@ use regex::{Captures, Regex};
use crate::activitypub::identifiers::profile_actor_url;
use crate::database::{DatabaseClient, DatabaseError};
use crate::errors::ValidationError;
use crate::models::{
profiles::queries::get_profiles_by_accts,
profiles::types::DbActorProfile,
@ -104,16 +103,6 @@ pub fn replace_mentions(
result.to_string()
}
pub fn mention_to_address(
mention: &str,
) -> Result<ActorAddress, ValidationError> {
// @ prefix is optional
let actor_address = mention.strip_prefix('@')
.unwrap_or(mention)
.parse()?;
Ok(actor_address)
}
#[cfg(test)]
mod tests {
use crate::activitypub::actors::types::Actor;
@ -197,22 +186,4 @@ mod tests {
);
assert_eq!(result, expected_result);
}
#[test]
fn test_mention_to_address() {
let mention = "@user@example.com";
let address_1 = mention_to_address(mention).unwrap();
assert_eq!(address_1.acct("example.com"), "user");
let address_2 = mention_to_address(mention).unwrap();
assert_eq!(address_2.acct("server.info"), "user@example.com");
let mention_without_prefix = "user@test.com";
let address_3 = mention_to_address(mention_without_prefix).unwrap();
assert_eq!(address_3.to_string(), mention_without_prefix);
let short_mention = "@user";
let result = mention_to_address(short_mention);
assert_eq!(result.is_err(), true);
}
}

View file

@ -28,6 +28,16 @@ pub struct ActorAddress {
}
impl ActorAddress {
pub fn from_mention(
mention: &str,
) -> Result<Self, ValidationError> {
// @ prefix is optional
let actor_address = mention.strip_prefix('@')
.unwrap_or(mention)
.parse()?;
Ok(actor_address)
}
pub fn from_profile(
local_hostname: &str,
profile: &DbActorProfile,
@ -163,4 +173,22 @@ mod tests {
let result = value.parse::<ActorAddress>();
assert_eq!(result.is_err(), true);
}
#[test]
fn test_actor_address_from_mention() {
let mention = "@user@example.com";
let address_1 = ActorAddress::from_mention(mention).unwrap();
assert_eq!(address_1.acct("example.com"), "user");
let address_2 = ActorAddress::from_mention(mention).unwrap();
assert_eq!(address_2.acct("server.info"), "user@example.com");
let mention_without_prefix = "user@test.com";
let address_3 = ActorAddress::from_mention(mention_without_prefix).unwrap();
assert_eq!(address_3.to_string(), mention_without_prefix);
let short_mention = "@user";
let result = ActorAddress::from_mention(short_mention);
assert_eq!(result.is_err(), true);
}
}