Ignore object link if referenced post doesn't exist

Made it behave similarly to mentions.
This commit is contained in:
silverpill 2022-12-19 21:24:19 +00:00
parent f2df270a82
commit 04e74a6e05
3 changed files with 24 additions and 26 deletions

View file

@ -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());
};

View file

@ -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<url>[^\s\|]+)(\|(?P<text>.+?))?\]\]";
@ -45,13 +45,26 @@ pub async fn find_linked_posts(
let links = find_object_links(text);
let mut link_map: HashMap<String, Post> = 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)
}

View file

@ -51,6 +51,7 @@ pub async fn find_mentioned_profiles(
text: &str,
) -> Result<HashMap<String, DbActorProfile>, 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<String, DbActorProfile> = HashMap::new();
for profile in profiles {