Rewrite mention_to_address and parse_acct_uri, remove unnecessary regexp

This commit is contained in:
silverpill 2022-12-27 23:47:40 +00:00
parent 477839345c
commit de9bc7f35e
3 changed files with 17 additions and 22 deletions

View file

@ -214,6 +214,9 @@ impl Actor {
}
}
// See also: USERNAME_RE in models::profiles::validators
const ACTOR_ADDRESS_RE: &str = r"(?P<username>[\w\.-]+)@(?P<hostname>[\w\.-]+)";
pub struct ActorAddress {
pub username: String,
pub hostname: String,
@ -230,9 +233,6 @@ impl ActorAddress {
}
}
// See also: USERNAME_RE in models::profiles::validators
pub const ACTOR_ADDRESS_RE: &str = r"(?P<username>[\w\.-]+)@(?P<hostname>[\w\.-]+)";
impl FromStr for ActorAddress {
type Err = ValidationError;
@ -390,9 +390,9 @@ mod tests {
const INSTANCE_URL: &str = "https://example.com";
#[test]
fn test_actor_address_parsing() {
fn test_actor_address_parse_address() {
let value = "user_1@example.com";
let actor_address = value.parse::<ActorAddress>().unwrap();
let actor_address: ActorAddress = value.parse().unwrap();
assert_eq!(actor_address.username, "user_1");
assert_eq!(actor_address.hostname, "example.com");
assert_eq!(actor_address.to_string(), value);

View file

@ -11,7 +11,6 @@ use crate::models::profiles::types::DbActorProfile;
use super::links::is_inside_code_block;
// See also: ACTOR_ADDRESS_RE in activitypub::actors::types
const MENTION_RE: &str = r"@?(?P<username>[\w\.-]+)@(?P<hostname>.+)";
const MENTION_SEARCH_RE: &str = r"(?m)(?P<before>^|\s|>|[\(])@(?P<mention>[^\s<]+)";
const MENTION_SEARCH_SECONDARY_RE: &str = r"^(?P<username>[\w\.-]+)(@(?P<hostname>[\w\.-]+\w))?(?P<after>[\.,:?\)]?)$";
@ -106,13 +105,10 @@ pub fn replace_mentions(
pub fn mention_to_address(
mention: &str,
) -> Result<ActorAddress, ValidationError> {
let mention_re = Regex::new(MENTION_RE).unwrap();
let mention_caps = mention_re.captures(mention)
.ok_or(ValidationError("invalid mention tag"))?;
let actor_address = ActorAddress {
username: mention_caps["username"].to_string(),
hostname: mention_caps["hostname"].to_string(),
};
// @ prefix is optional
let actor_address = mention.strip_prefix('@')
.unwrap_or(mention)
.parse()?;
Ok(actor_address)
}
@ -209,6 +205,10 @@ mod tests {
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

@ -1,8 +1,7 @@
use actix_web::{get, web, HttpResponse};
use regex::Regex;
use tokio_postgres::GenericClient;
use crate::activitypub::actors::types::{ActorAddress, ACTOR_ADDRESS_RE};
use crate::activitypub::actors::types::ActorAddress;
use crate::activitypub::constants::AP_MEDIA_TYPE;
use crate::activitypub::identifiers::{
local_actor_id,
@ -21,13 +20,9 @@ use super::types::{
// https://datatracker.ietf.org/doc/html/rfc7565#section-7
fn parse_acct_uri(uri: &str) -> Result<ActorAddress, ValidationError> {
let uri_regexp = Regex::new(&format!("acct:{}", ACTOR_ADDRESS_RE)).unwrap();
let uri_caps = uri_regexp.captures(uri)
.ok_or(ValidationError("invalid query target"))?;
let actor_address = ActorAddress {
username: uri_caps["username"].to_string(),
hostname: uri_caps["hostname"].to_string(),
};
let actor_address = uri.strip_prefix("acct:")
.ok_or(ValidationError("invalid query target"))?
.parse()?;
Ok(actor_address)
}