diff --git a/src/mastodon_api/statuses/views.rs b/src/mastodon_api/statuses/views.rs index 9d2678f..ca443ce 100644 --- a/src/mastodon_api/statuses/views.rs +++ b/src/mastodon_api/statuses/views.rs @@ -87,34 +87,18 @@ async fn create_status( &post_data.tags, ); // Links - let mut linked = vec![]; - let link_map = match find_linked_posts( + let link_map = find_linked_posts( db_client, &instance.url(), &post_data.content, - ).await { - Ok(link_map) => link_map, - Err(DatabaseError::NotFound(_)) => { - return Err(ValidationError("referenced post does't exist").into()); - }, - Err(other_error) => return Err(other_error.into()), - }; + ).await?; post_data.content = replace_object_links( &link_map, &post_data.content, ); - for post in link_map.into_values() { - if !post_data.links.contains(&post.id) { - if post.repost_of_id.is_some() { - return Err(ValidationError("can't reference repost").into()); - }; - if post.visibility != Visibility::Public { - return Err(ValidationError("can't reference non-public post").into()); - }; - post_data.links.push(post.id); - linked.push(post); - }; - }; + post_data.links.extend(link_map.values().map(|post| post.id)); + let linked = link_map.into_values().collect(); + if post_data.links.len() > 0 && post_data.visibility != Visibility::Public { return Err(ValidationError("can't add links to non-public posts").into()); }; diff --git a/src/models/posts/links.rs b/src/models/posts/links.rs index d204c67..7ec5e57 100644 --- a/src/models/posts/links.rs +++ b/src/models/posts/links.rs @@ -5,7 +5,7 @@ use tokio_postgres::GenericClient; use crate::database::DatabaseError; use super::helpers::get_post_by_object_id; -use super::types::Post; +use super::types::{Post, Visibility}; // MediaWiki-like syntax: [[url|text]] const OBJECT_LINK_SEARCH_RE: &str = r"(?m)\[\[(?P[^\s\|]+)(\|(?P.+?))?\]\]"; @@ -45,13 +45,26 @@ pub async fn find_linked_posts( let links = find_object_links(text); let mut link_map: HashMap = HashMap::new(); for url in links { - // Return error if post doesn't exist - let post = get_post_by_object_id( + match get_post_by_object_id( db_client, instance_url, &url, - ).await?; - link_map.insert(url, post); + ).await { + Ok(post) => { + if post.repost_of_id.is_some() { + // Can't reference reposts + continue; + }; + if post.visibility != Visibility::Public { + // Can't reference non-public posts + continue; + }; + link_map.insert(url, post); + }, + // If post doesn't exist in database, link is ignored + Err(DatabaseError::NotFound(_)) => continue, + Err(other_error) => return Err(other_error), + }; }; Ok(link_map) } diff --git a/src/models/posts/mentions.rs b/src/models/posts/mentions.rs index ac4c42a..0643341 100644 --- a/src/models/posts/mentions.rs +++ b/src/models/posts/mentions.rs @@ -51,6 +51,7 @@ pub async fn find_mentioned_profiles( text: &str, ) -> Result, DatabaseError> { let mentions = find_mentions(instance_hostname, text); + // If acct doesn't exist in database, mention is ignored let profiles = get_profiles_by_accts(db_client, mentions).await?; let mut mention_map: HashMap = HashMap::new(); for profile in profiles {