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

View file

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

View file

@ -4,7 +4,6 @@ use regex::{Captures, Regex};
use crate::activitypub::identifiers::profile_actor_url; use crate::activitypub::identifiers::profile_actor_url;
use crate::database::{DatabaseClient, DatabaseError}; use crate::database::{DatabaseClient, DatabaseError};
use crate::errors::ValidationError;
use crate::models::{ use crate::models::{
profiles::queries::get_profiles_by_accts, profiles::queries::get_profiles_by_accts,
profiles::types::DbActorProfile, profiles::types::DbActorProfile,
@ -104,16 +103,6 @@ pub fn replace_mentions(
result.to_string() 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)] #[cfg(test)]
mod tests { mod tests {
use crate::activitypub::actors::types::Actor; use crate::activitypub::actors::types::Actor;
@ -197,22 +186,4 @@ mod tests {
); );
assert_eq!(result, expected_result); 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 { 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( pub fn from_profile(
local_hostname: &str, local_hostname: &str,
profile: &DbActorProfile, profile: &DbActorProfile,
@ -163,4 +173,22 @@ mod tests {
let result = value.parse::<ActorAddress>(); let result = value.parse::<ActorAddress>();
assert_eq!(result.is_err(), true); 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);
}
} }