From 4b0239c03f0712593e08eac49e961b49dce67234 Mon Sep 17 00:00:00 2001 From: TJ Date: Wed, 23 Oct 2024 13:44:31 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=F0=9F=92=A1=20clean=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/save_it/bot.ex | 36 ++++++++++++++++++---------------- lib/save_it/typesense_photo.ex | 10 ---------- 2 files changed, 19 insertions(+), 27 deletions(-) diff --git a/lib/save_it/bot.ex b/lib/save_it/bot.ex index d6dfcec..5befcb2 100644 --- a/lib/save_it/bot.ex +++ b/lib/save_it/bot.ex @@ -156,7 +156,12 @@ defmodule SaveIt.Bot do belongs_to_id: chat_id }) - photos = find_same_photos(typesense_photo["id"]) + photos = + TypesensePhoto.search_similar_photos!( + typesense_photo["id"], + distance_threshold: 0.1, + belongs_to_id: chat_id + ) case photos do [] -> nil @@ -191,11 +196,22 @@ defmodule SaveIt.Bot do case caption do "" -> - photos = search_similar_photos(typesense_photo["id"]) + photos = + TypesensePhoto.search_similar_photos!( + typesense_photo["id"], + distance_threshold: 0.4, + belongs_to_id: chat_id + ) + answer_photos(chat.id, photos) _ -> - photos = find_same_photos(typesense_photo["id"]) + photos = + TypesensePhoto.search_similar_photos!( + typesense_photo["id"], + distance_threshold: 0.1, + belongs_to_id: chat_id + ) case photos do [] -> nil @@ -318,20 +334,6 @@ defmodule SaveIt.Bot do {:ok, nil} end - defp find_same_photos(photo_id) do - TypesensePhoto.search_similar_photos!( - photo_id, - distance_threshold: 0.1 - ) - end - - defp search_similar_photos(photo_id) do - TypesensePhoto.search_similar_photos!( - photo_id, - distance_threshold: 0.4 - ) - end - defp pick_file_id_from_photo_url(photo_url) do captures = Regex.named_captures(~r"/files/(?\d+)/(?.+)", photo_url) diff --git a/lib/save_it/typesense_photo.ex b/lib/save_it/typesense_photo.ex index fe9641e..a0da8c2 100644 --- a/lib/save_it/typesense_photo.ex +++ b/lib/save_it/typesense_photo.ex @@ -47,16 +47,6 @@ defmodule SaveIt.TypesensePhoto do res.body["results"] |> typesense_results_to_documents() end - def search_photos!(id, opts \\ []) do - belongs_to_id = Keyword.get(opts, :belongs_to_id) - distance_threshold = Keyword.get(opts, :distance_threshold, 0.4) - - search_similar_photos!(id, - distance_threshold: distance_threshold, - belongs_to_id: belongs_to_id - ) - end - def search_similar_photos!(photo_id, opts \\ []) when is_binary(photo_id) do belongs_to_id = Keyword.get(opts, :belongs_to_id) distance_threshold = Keyword.get(opts, :distance_threshold, 0.4)