Limit the number of database queries in find_linked_posts()

This commit is contained in:
silverpill 2022-12-19 21:40:28 +00:00
parent 04e74a6e05
commit 2232bf814c
2 changed files with 7 additions and 3 deletions

View file

@ -102,9 +102,6 @@ async fn create_status(
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());
}; };
if post_data.links.len() > 3 {
return Err(ValidationError("too many links").into());
};
// Reply validation // Reply validation
let maybe_in_reply_to = if let Some(in_reply_to_id) = post_data.in_reply_to_id.as_ref() { let maybe_in_reply_to = if let Some(in_reply_to_id) = post_data.in_reply_to_id.as_ref() {
let in_reply_to = match get_post_by_id(db_client, in_reply_to_id).await { let in_reply_to = match get_post_by_id(db_client, in_reply_to_id).await {

View file

@ -44,7 +44,13 @@ pub async fn find_linked_posts(
) -> Result<HashMap<String, Post>, DatabaseError> { ) -> Result<HashMap<String, Post>, DatabaseError> {
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();
let mut counter = 0;
for url in links { for url in links {
if counter > 10 {
// Limit the number of queries
break;
// TODO: single database query
};
match get_post_by_object_id( match get_post_by_object_id(
db_client, db_client,
instance_url, instance_url,
@ -65,6 +71,7 @@ pub async fn find_linked_posts(
Err(DatabaseError::NotFound(_)) => continue, Err(DatabaseError::NotFound(_)) => continue,
Err(other_error) => return Err(other_error), Err(other_error) => return Err(other_error),
}; };
counter += 1;
}; };
Ok(link_map) Ok(link_map)
} }