Rename posts::tags module to posts::hashtags

This commit is contained in:
silverpill 2022-08-21 21:33:44 +00:00
parent ee0095ce53
commit fcf7db97cb
4 changed files with 17 additions and 17 deletions

View file

@ -21,13 +21,13 @@ use crate::activitypub::{
use crate::config::Instance;
use crate::errors::{ConversionError, DatabaseError, ValidationError};
use crate::models::attachments::queries::create_attachment;
use crate::models::posts::hashtags::normalize_hashtag;
use crate::models::posts::mentions::mention_to_address;
use crate::models::posts::queries::{
create_post,
get_post_by_id,
get_post_by_object_id,
};
use crate::models::posts::tags::normalize_tag;
use crate::models::posts::types::{Post, PostCreateData, Visibility};
use crate::models::profiles::queries::get_profile_by_acct;
use crate::models::profiles::types::DbActorProfile;
@ -180,7 +180,7 @@ pub async fn handle_note(
if tag.tag_type == HASHTAG {
if let Some(tag_name) = tag.name {
// Ignore invalid tags
if let Ok(tag_name) = normalize_tag(&tag_name) {
if let Ok(tag_name) = normalize_hashtag(&tag_name) {
if !tags.contains(&tag_name) {
tags.push(tag_name);
};

View file

@ -21,9 +21,9 @@ use crate::ipfs::store as ipfs_store;
use crate::ipfs::posts::PostMetadata;
use crate::ipfs::utils::get_ipfs_url;
use crate::mastodon_api::oauth::auth::get_current_user;
use crate::models::posts::hashtags::{find_hashtags, replace_hashtags};
use crate::models::posts::helpers::can_view_post;
use crate::models::posts::mentions::{find_mentioned_profiles, replace_mentions};
use crate::models::posts::tags::{find_tags, replace_tags};
use crate::models::posts::queries::{
create_post,
get_post_by_id,
@ -72,9 +72,9 @@ async fn create_status(
.map(|profile| profile.id));
post_data.mentions.sort();
post_data.mentions.dedup();
// Tags
post_data.tags = find_tags(&post_data.content);
post_data.content = replace_tags(
// Hashtags
post_data.tags = find_hashtags(&post_data.content);
post_data.content = replace_hashtags(
&instance.url(),
&post_data.content,
&post_data.tags,

View file

@ -8,7 +8,7 @@ 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_tags(text: &str) -> Vec<String> {
pub fn find_hashtags(text: &str) -> Vec<String> {
let hashtag_re = Regex::new(HASHTAG_RE).unwrap();
let hashtag_secondary_re = Regex::new(HASHTAG_SECONDARY_RE).unwrap();
let mut tags = vec![];
@ -24,7 +24,7 @@ pub fn find_tags(text: &str) -> Vec<String> {
}
/// Replaces hashtags with links
pub fn replace_tags(instance_url: &str, text: &str, tags: &[String]) -> String {
pub fn replace_hashtags(instance_url: &str, text: &str, tags: &[String]) -> String {
let hashtag_re = Regex::new(HASHTAG_RE).unwrap();
let hashtag_secondary_re = Regex::new(HASHTAG_SECONDARY_RE).unwrap();
let result = hashtag_re.replace_all(text, |caps: &Captures| {
@ -49,7 +49,7 @@ pub fn replace_tags(instance_url: &str, text: &str, tags: &[String]) -> String {
result.to_string()
}
pub fn normalize_tag(tag: &str) -> Result<String, ValidationError> {
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) {
@ -71,8 +71,8 @@ mod tests {
);
#[test]
fn test_find_tags() {
let tags = find_tags(TEXT_WITH_TAGS);
fn test_find_hashtags() {
let tags = find_hashtags(TEXT_WITH_TAGS);
assert_eq!(tags, vec![
"testtag",
@ -85,9 +85,9 @@ mod tests {
}
#[test]
fn test_replace_tags() {
let tags = find_tags(TEXT_WITH_TAGS);
let output = replace_tags(INSTANCE_URL, TEXT_WITH_TAGS, &tags);
fn test_replace_hashtags() {
let tags = find_hashtags(TEXT_WITH_TAGS);
let output = replace_hashtags(INSTANCE_URL, TEXT_WITH_TAGS, &tags);
let expected_output = concat!(
r#"@user1@server1 some text <a class="hashtag" href="https://example.com/tag/testtag">#TestTag</a>."#, "\n",
@ -102,9 +102,9 @@ mod tests {
}
#[test]
fn test_normalize_tag() {
fn test_normalize_hashtag() {
let tag = "#ActivityPub";
let output = normalize_tag(tag).unwrap();
let output = normalize_hashtag(tag).unwrap();
assert_eq!(output, "activitypub");
}

View file

@ -1,5 +1,5 @@
pub mod hashtags;
pub mod helpers;
pub mod mentions;
pub mod queries;
pub mod tags;
pub mod types;