diff --git a/lib/migration/typesense.ex b/lib/migration/typesense.ex index 13eb509..8bea87e 100644 --- a/lib/migration/typesense.ex +++ b/lib/migration/typesense.ex @@ -1,11 +1,4 @@ defmodule Migration.Typesense do - def list_collections() do - req = build_request("/collections") - {:ok, res} = Req.get(req) - - res.body - end - def create_collection!(schema) do req = build_request("/collections") {:ok, res} = Req.post(req, json: schema) @@ -20,6 +13,13 @@ defmodule Migration.Typesense do res.body end + def list_collections() do + req = build_request("/collections") + {:ok, res} = Req.get(req) + + res.body + end + defp get_env() do url = Application.fetch_env!(:save_it, :typesense_url) api_key = Application.fetch_env!(:save_it, :typesense_api_key) diff --git a/lib/save_it/bot.ex b/lib/save_it/bot.ex index df73336..60120a5 100644 --- a/lib/save_it/bot.ex +++ b/lib/save_it/bot.ex @@ -5,7 +5,7 @@ defmodule SaveIt.Bot do alias SaveIt.GoogleDrive alias SaveIt.GoogleOAuth2DeviceFlow - alias SaveIt.TypesensePhoto + alias SaveIt.PhotoService alias SaveIt.NoteService @@ -131,7 +131,7 @@ defmodule SaveIt.Bot do send_message(chat.id, "What do you want to search? animal, food, etc.") _ -> - photos = TypesensePhoto.search_photos!(q, belongs_to_id: chat.id) + photos = PhotoService.search_photos!(q, belongs_to_id: chat.id) answer_photos(chat.id, photos) end @@ -142,7 +142,7 @@ defmodule SaveIt.Bot do # end def handle( - {:command, :note, %{chat: chat, text: text, reply_to_message: reply_to_message}} = msg, + {:command, :note, %{chat: chat, text: text, reply_to_message: reply_to_message}}, _context ) when is_binary(text) do @@ -161,7 +161,7 @@ defmodule SaveIt.Bot do note_content -> # photo_url = photo_url(chat.id, file_id) - # TypesensePhoto.update_photo!(photo_url, %{"note" => text}) + # PhotoService.update_photo!(photo_url, %{"note" => text}) note = NoteService.create_note!(%{ @@ -197,7 +197,7 @@ defmodule SaveIt.Bot do chat_id = chat.id typesense_photo = - TypesensePhoto.create_photo!(%{ + PhotoService.create_photo!(%{ image: Base.encode64(photo_file_content), caption: "", url: photo_url(bot_id, file.file_id), @@ -205,7 +205,7 @@ defmodule SaveIt.Bot do }) photos = - TypesensePhoto.search_similar_photos!( + PhotoService.search_similar_photos!( typesense_photo["id"], distance_threshold: 0.1, belongs_to_id: chat_id @@ -235,7 +235,7 @@ defmodule SaveIt.Bot do end typesense_photo = - TypesensePhoto.create_photo!(%{ + PhotoService.create_photo!(%{ image: Base.encode64(photo_file_content), caption: caption, url: photo_url(bot_id, file.file_id), @@ -245,7 +245,7 @@ defmodule SaveIt.Bot do case caption do "" -> photos = - TypesensePhoto.search_similar_photos!( + PhotoService.search_similar_photos!( typesense_photo["id"], distance_threshold: 0.4, belongs_to_id: chat_id @@ -255,7 +255,7 @@ defmodule SaveIt.Bot do _ -> photos = - TypesensePhoto.search_similar_photos!( + PhotoService.search_similar_photos!( typesense_photo["id"], distance_threshold: 0.1, belongs_to_id: chat_id @@ -483,7 +483,7 @@ defmodule SaveIt.Bot do {:file_content, file_content, _file_name} -> Base.encode64(file_content) end - TypesensePhoto.create_photo!(%{ + PhotoService.create_photo!(%{ image: image_base64, caption: file_name, url: photo_url(bot_id, file_id), diff --git a/lib/save_it/typesense_photo.ex b/lib/save_it/photo_service.ex similarity index 98% rename from lib/save_it/typesense_photo.ex rename to lib/save_it/photo_service.ex index 4a75db5..b7aa2d8 100644 --- a/lib/save_it/typesense_photo.ex +++ b/lib/save_it/photo_service.ex @@ -1,4 +1,4 @@ -defmodule SaveIt.TypesensePhoto do +defmodule SaveIt.PhotoService do require Logger alias SmallSdk.Typesense diff --git a/lib/small_sdk/typesense_admin.ex b/lib/small_sdk/typesense_admin.ex deleted file mode 100644 index a123240..0000000 --- a/lib/small_sdk/typesense_admin.ex +++ /dev/null @@ -1,35 +0,0 @@ -defmodule SmallSdk.TypesenseAdmin do - def create_collection!(schema) do - req = build_request("/collections") - {:ok, res} = Req.post(req, json: schema) - - res.body - end - - def delete_collection!(collection_name) do - req = build_request("/collections/#{collection_name}") - {:ok, res} = Req.delete(req) - - res.body - end - - defp get_env() do - url = Application.fetch_env!(:save_it, :typesense_url) - api_key = Application.fetch_env!(:save_it, :typesense_api_key) - - {url, api_key} - end - - defp build_request(path) do - {url, api_key} = get_env() - - Req.new( - base_url: url, - url: path, - headers: [ - {"Content-Type", "application/json"}, - {"X-TYPESENSE-API-KEY", api_key} - ] - ) - end -end