Skip to content

Commit

Permalink
refactor: 💡 clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
ThaddeusJiang committed Oct 23, 2024
1 parent 6015163 commit 4b0239c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 27 deletions.
36 changes: 19 additions & 17 deletions lib/save_it/bot.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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/(?<bot_id>\d+)/(?<file_id>.+)", photo_url)
Expand Down
10 changes: 0 additions & 10 deletions lib/save_it/typesense_photo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 4b0239c

Please sign in to comment.