Move normalize_hashtag function to activitypub::handlers::create
This commit is contained in:
parent
f5dd0a17c9
commit
73b576c643
4 changed files with 30 additions and 20 deletions
|
@ -36,7 +36,6 @@ use crate::models::{
|
|||
},
|
||||
emojis::types::{DbEmoji, EmojiImage},
|
||||
posts::{
|
||||
hashtags::normalize_hashtag,
|
||||
queries::create_post,
|
||||
types::{Post, PostCreateData, Visibility},
|
||||
},
|
||||
|
@ -55,6 +54,7 @@ use crate::validators::{
|
|||
CONTENT_MAX_SIZE,
|
||||
EMOJIS_MAX_NUM,
|
||||
},
|
||||
tags::validate_hashtag,
|
||||
};
|
||||
use crate::webfinger::types::ActorAddress;
|
||||
use super::HandlerResult;
|
||||
|
@ -200,6 +200,12 @@ pub async fn get_object_attachments(
|
|||
Ok((attachments, unprocessed))
|
||||
}
|
||||
|
||||
fn normalize_hashtag(tag: &str) -> Result<String, ValidationError> {
|
||||
let tag_name = tag.trim_start_matches('#');
|
||||
validate_hashtag(tag_name)?;
|
||||
Ok(tag_name.to_lowercase())
|
||||
}
|
||||
|
||||
pub fn get_object_links(
|
||||
object: &Object,
|
||||
) -> Vec<String> {
|
||||
|
@ -707,6 +713,14 @@ mod tests {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_normalize_hashtag() {
|
||||
let tag = "#ActivityPub";
|
||||
let output = normalize_hashtag(tag).unwrap();
|
||||
|
||||
assert_eq!(output, "activitypub");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_object_visibility_public() {
|
||||
let author = DbActorProfile::default();
|
||||
|
|
|
@ -1,12 +1,11 @@
|
|||
use regex::{Captures, Regex};
|
||||
|
||||
use crate::activitypub::identifiers::local_tag_collection;
|
||||
use crate::errors::ValidationError;
|
||||
use super::links::is_inside_code_block;
|
||||
|
||||
// See also: HASHTAG_NAME_RE in validators::tags
|
||||
const HASHTAG_RE: &str = r"(?m)(?P<before>^|\s|>|[\(])#(?P<tag>[^\s<]+)";
|
||||
const HASHTAG_SECONDARY_RE: &str = r"^(?P<tag>[0-9A-Za-z]+)(?P<after>[\.,:?!\)]?)$";
|
||||
const HASHTAG_NAME_RE: &str = r"^\w+$";
|
||||
|
||||
/// Finds anything that looks like a hashtag
|
||||
pub fn find_hashtags(text: &str) -> Vec<String> {
|
||||
|
@ -60,15 +59,6 @@ pub fn replace_hashtags(instance_url: &str, text: &str, tags: &[String]) -> Stri
|
|||
result.to_string()
|
||||
}
|
||||
|
||||
pub fn normalize_hashtag(tag: &str) -> Result<String, ValidationError> {
|
||||
let hashtag_name_re = Regex::new(HASHTAG_NAME_RE).unwrap();
|
||||
let tag_name = tag.trim_start_matches('#');
|
||||
if !hashtag_name_re.is_match(tag_name) {
|
||||
return Err(ValidationError("invalid tag name"));
|
||||
};
|
||||
Ok(tag_name.to_lowercase())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
@ -112,12 +102,4 @@ mod tests {
|
|||
);
|
||||
assert_eq!(output, expected_output);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_normalize_hashtag() {
|
||||
let tag = "#ActivityPub";
|
||||
let output = normalize_hashtag(tag).unwrap();
|
||||
|
||||
assert_eq!(output, "activitypub");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
pub mod emojis;
|
||||
pub mod posts;
|
||||
pub mod profiles;
|
||||
pub mod tags;
|
||||
pub mod users;
|
||||
|
|
13
src/validators/tags.rs
Normal file
13
src/validators/tags.rs
Normal file
|
@ -0,0 +1,13 @@
|
|||
use regex::Regex;
|
||||
|
||||
use crate::errors::ValidationError;
|
||||
|
||||
const HASHTAG_NAME_RE: &str = r"^\w+$";
|
||||
|
||||
pub fn validate_hashtag(tag_name: &str) -> Result<(), ValidationError> {
|
||||
let hashtag_name_re = Regex::new(HASHTAG_NAME_RE).unwrap();
|
||||
if !hashtag_name_re.is_match(tag_name) {
|
||||
return Err(ValidationError("invalid tag name"));
|
||||
};
|
||||
Ok(())
|
||||
}
|
Loading…
Reference in a new issue