From 8f3f2c981bd8131d289f56baf16ed6ce5f8347b2 Mon Sep 17 00:00:00 2001 From: mdashlw Date: Fri, 19 Jul 2024 02:01:27 -0700 Subject: [PATCH] feat: support limit param in reverse search --- lib/philomena/duplicate_reports.ex | 4 ++-- .../api/json/search/reverse_controller.ex | 1 + lib/philomena_web/image_reverse.ex | 16 +++++++++++++++- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/philomena/duplicate_reports.ex b/lib/philomena/duplicate_reports.ex index 3e07151e..d802f364 100644 --- a/lib/philomena/duplicate_reports.ex +++ b/lib/philomena/duplicate_reports.ex @@ -25,7 +25,7 @@ defmodule Philomena.DuplicateReports do end) end - def duplicates_of(intensities, aspect_ratio, dist \\ 0.25, aspect_dist \\ 0.05) do + def duplicates_of(intensities, aspect_ratio, dist \\ 0.25, aspect_dist \\ 0.05, limit \\ 10) do # for each color channel dist = dist * 3 @@ -39,7 +39,7 @@ defmodule Philomena.DuplicateReports do where: i.image_aspect_ratio >= ^(aspect_ratio - aspect_dist) and i.image_aspect_ratio <= ^(aspect_ratio + aspect_dist), - limit: 10 + limit: ^limit end @doc """ diff --git a/lib/philomena_web/controllers/api/json/search/reverse_controller.ex b/lib/philomena_web/controllers/api/json/search/reverse_controller.ex index e1cf4d7c..ba94b753 100644 --- a/lib/philomena_web/controllers/api/json/search/reverse_controller.ex +++ b/lib/philomena_web/controllers/api/json/search/reverse_controller.ex @@ -13,6 +13,7 @@ defmodule PhilomenaWeb.Api.Json.Search.ReverseController do images = image_params |> Map.put("distance", conn.params["distance"]) + |> Map.put("limit", conn.params["limit"]) |> ImageReverse.images() interactions = Interactions.user_interactions(images, user) diff --git a/lib/philomena_web/image_reverse.ex b/lib/philomena_web/image_reverse.ex index 161ebad3..2ef5e427 100644 --- a/lib/philomena_web/image_reverse.ex +++ b/lib/philomena_web/image_reverse.ex @@ -18,8 +18,9 @@ defmodule PhilomenaWeb.ImageReverse do {width, height} = analysis.dimensions aspect = width / height dist = normalize_dist(image_params) + limit = parse_limit(image_params) - DuplicateReports.duplicates_of(intensities, aspect, dist, dist) + DuplicateReports.duplicates_of(intensities, aspect, dist, dist, limit) |> preload([:user, :intensity, [:sources, tags: :aliases]]) |> Repo.all() end @@ -60,4 +61,17 @@ defmodule PhilomenaWeb.ImageReverse do 0.0 end end + + defp parse_limit(%{"limit" => limit}) do + limit + |> Integer.parse() + |> case do + {limit, _rest} -> limit + _ -> 10 + end + |> max(1) + |> min(50) + end + + defp parse_limit(_), do: 10 end