diff --git a/assets/js/booru.js b/assets/js/booru.js index 6daf469d..b52e1592 100644 --- a/assets/js/booru.js +++ b/assets/js/booru.js @@ -43,9 +43,9 @@ function fetchAndPersistTags(tagIds) { for (let i = 0; i < tagIds.length; i += chunk) { const ids = tagIds.slice(i, i + chunk); - /*fetch(`${window.booru.apiEndpoint}tags/fetch_many.json?ids[]=${ids.join('&ids[]=')}`) + fetch(`/tags/fetch?ids[]=${ids.join('&ids[]=')}`) .then(response => response.json()) - .then(data => data.tags.forEach(tag => persistTag(tag)));*/ + .then(data => data.tags.forEach(tag => persistTag(tag))); } } diff --git a/lib/philomena_web/controllers/tag/fetch_controller.ex b/lib/philomena_web/controllers/tag/fetch_controller.ex new file mode 100644 index 00000000..fc7b8d27 --- /dev/null +++ b/lib/philomena_web/controllers/tag/fetch_controller.ex @@ -0,0 +1,39 @@ +defmodule PhilomenaWeb.Tag.FetchController do + use PhilomenaWeb, :controller + + alias Philomena.Tags.Tag + alias Philomena.Repo + import Ecto.Query + + def index(conn, %{"ids" => ids}) when is_list(ids) do + # limit amount to 50 + ids = Enum.take(ids, 50) + + tags = + Tag + |> where([t], t.id in ^ids) + |> Repo.all() + |> Enum.map(&tag_json/1) + + conn + |> json(%{tags: tags}) + end + + defp tag_json(tag) do + %{ + id: tag.id, + name: tag.name, + images: tag.images_count, + spoiler_image_uri: tag_image(tag) + } + end + + defp tag_image(%{image: image}) when image not in [nil, ""], + do: tag_url_root() <> "/" <> image + defp tag_image(_other), + do: nil + + defp tag_url_root do + Application.get_env(:philomena, :tag_url_root) + end +end \ No newline at end of file diff --git a/lib/philomena_web/router.ex b/lib/philomena_web/router.ex index 95664e59..a6635301 100644 --- a/lib/philomena_web/router.ex +++ b/lib/philomena_web/router.ex @@ -123,6 +123,7 @@ defmodule PhilomenaWeb.Router do end scope "/tags", Tag, as: :tag do resources "/autocomplete", AutocompleteController, only: [:show], singleton: true + resources "/fetch", FetchController, only: [:index] end resources "/tags", TagController, only: [:index, :show] scope "/search", Search, as: :search do