Ignore object link if referenced post doesn't exist
Made it behave similarly to mentions.
This commit is contained in:
parent
f2df270a82
commit
04e74a6e05
|
@ -87,34 +87,18 @@ async fn create_status(
|
||||||
&post_data.tags,
|
&post_data.tags,
|
||||||
);
|
);
|
||||||
// Links
|
// Links
|
||||||
let mut linked = vec![];
|
let link_map = find_linked_posts(
|
||||||
let link_map = match find_linked_posts(
|
|
||||||
db_client,
|
db_client,
|
||||||
&instance.url(),
|
&instance.url(),
|
||||||
&post_data.content,
|
&post_data.content,
|
||||||
).await {
|
).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()),
|
|
||||||
};
|
|
||||||
post_data.content = replace_object_links(
|
post_data.content = replace_object_links(
|
||||||
&link_map,
|
&link_map,
|
||||||
&post_data.content,
|
&post_data.content,
|
||||||
);
|
);
|
||||||
for post in link_map.into_values() {
|
post_data.links.extend(link_map.values().map(|post| post.id));
|
||||||
if !post_data.links.contains(&post.id) {
|
let linked = link_map.into_values().collect();
|
||||||
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);
|
|
||||||
};
|
|
||||||
};
|
|
||||||
if post_data.links.len() > 0 && post_data.visibility != Visibility::Public {
|
if post_data.links.len() > 0 && post_data.visibility != Visibility::Public {
|
||||||
return Err(ValidationError("can't add links to non-public posts").into());
|
return Err(ValidationError("can't add links to non-public posts").into());
|
||||||
};
|
};
|
||||||
|
|
|
@ -5,7 +5,7 @@ use tokio_postgres::GenericClient;
|
||||||
|
|
||||||
use crate::database::DatabaseError;
|
use crate::database::DatabaseError;
|
||||||
use super::helpers::get_post_by_object_id;
|
use super::helpers::get_post_by_object_id;
|
||||||
use super::types::Post;
|
use super::types::{Post, Visibility};
|
||||||
|
|
||||||
// MediaWiki-like syntax: [[url|text]]
|
// MediaWiki-like syntax: [[url|text]]
|
||||||
const OBJECT_LINK_SEARCH_RE: &str = r"(?m)\[\[(?P<url>[^\s\|]+)(\|(?P<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 links = find_object_links(text);
|
||||||
let mut link_map: HashMap<String, Post> = HashMap::new();
|
let mut link_map: HashMap<String, Post> = HashMap::new();
|
||||||
for url in links {
|
for url in links {
|
||||||
// Return error if post doesn't exist
|
match get_post_by_object_id(
|
||||||
let post = get_post_by_object_id(
|
|
||||||
db_client,
|
db_client,
|
||||||
instance_url,
|
instance_url,
|
||||||
&url,
|
&url,
|
||||||
).await?;
|
).await {
|
||||||
link_map.insert(url, post);
|
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)
|
Ok(link_map)
|
||||||
}
|
}
|
||||||
|
|
|
@ -51,6 +51,7 @@ pub async fn find_mentioned_profiles(
|
||||||
text: &str,
|
text: &str,
|
||||||
) -> Result<HashMap<String, DbActorProfile>, DatabaseError> {
|
) -> Result<HashMap<String, DbActorProfile>, DatabaseError> {
|
||||||
let mentions = find_mentions(instance_hostname, text);
|
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 profiles = get_profiles_by_accts(db_client, mentions).await?;
|
||||||
let mut mention_map: HashMap<String, DbActorProfile> = HashMap::new();
|
let mut mention_map: HashMap<String, DbActorProfile> = HashMap::new();
|
||||||
for profile in profiles {
|
for profile in profiles {
|
||||||
|
|
Loading…
Reference in a new issue