From 2232bf814c337c307147935b30c962440de2ef4d Mon Sep 17 00:00:00 2001 From: silverpill Date: Mon, 19 Dec 2022 21:40:28 +0000 Subject: [PATCH] Limit the number of database queries in find_linked_posts() --- src/mastodon_api/statuses/views.rs | 3 --- src/models/posts/links.rs | 7 +++++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/mastodon_api/statuses/views.rs b/src/mastodon_api/statuses/views.rs index ca443ce..aa2d25e 100644 --- a/src/mastodon_api/statuses/views.rs +++ b/src/mastodon_api/statuses/views.rs @@ -102,9 +102,6 @@ async fn create_status( if post_data.links.len() > 0 && post_data.visibility != Visibility::Public { 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 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 { diff --git a/src/models/posts/links.rs b/src/models/posts/links.rs index 7ec5e57..c07a1cd 100644 --- a/src/models/posts/links.rs +++ b/src/models/posts/links.rs @@ -44,7 +44,13 @@ pub async fn find_linked_posts( ) -> Result, DatabaseError> { let links = find_object_links(text); let mut link_map: HashMap = HashMap::new(); + let mut counter = 0; for url in links { + if counter > 10 { + // Limit the number of queries + break; + // TODO: single database query + }; match get_post_by_object_id( db_client, instance_url, @@ -65,6 +71,7 @@ pub async fn find_linked_posts( Err(DatabaseError::NotFound(_)) => continue, Err(other_error) => return Err(other_error), }; + counter += 1; }; Ok(link_map) }