From 4f040da3fa7ef4cb4bf71cb6c5bcf112b2692a3e Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Wed, 10 Feb 2021 20:01:05 -0500 Subject: [PATCH] expand oembed to a proper view --- .../controllers/api/json/oembed_controller.ex | 30 +++-------------- .../views/api/json/oembed_view.ex | 32 +++++++++++++++++++ 2 files changed, 36 insertions(+), 26 deletions(-) create mode 100644 lib/philomena_web/views/api/json/oembed_view.ex diff --git a/lib/philomena_web/controllers/api/json/oembed_controller.ex b/lib/philomena_web/controllers/api/json/oembed_controller.ex index 03269388..88486410 100644 --- a/lib/philomena_web/controllers/api/json/oembed_controller.ex +++ b/lib/philomena_web/controllers/api/json/oembed_controller.ex @@ -27,7 +27,8 @@ defmodule PhilomenaWeb.Api.Json.OembedController do true -> nil end - load_image(image_id) + image_id + |> load_image() |> oembed_image(conn) end @@ -41,34 +42,11 @@ defmodule PhilomenaWeb.Api.Json.OembedController do end defp oembed_image(nil, conn), do: oembed_error(conn) - defp oembed_image(image, conn), do: json(conn, oembed_json(image)) + defp oembed_image(image, conn), do: render(conn, "show.json", image: image) defp oembed_error(conn) do conn |> Plug.Conn.put_status(:not_found) - |> json(%{error: "couldn't find an image"}) - end - - defp oembed_json(image) do - %{ - version: "1.0", - type: "photo", - title: "##{image.id} - #{image.tag_list_cache} - Derpibooru", - author_url: image.source_url || "", - author_name: artist_tags(image.tags), - provider_name: "Derpibooru", - provider_url: PhilomenaWeb.Endpoint.url(), - cache_age: 7200, - derpibooru_id: image.id, - derpibooru_score: image.score, - derpibooru_comments: image.comments_count, - derpibooru_tags: Enum.map(image.tags, & &1.name) - } - end - - defp artist_tags(tags) do - tags - |> Enum.filter(&(&1.namespace == "artist")) - |> Enum.map_join(", ", & &1.name_in_namespace) + |> render("error.json") end end diff --git a/lib/philomena_web/views/api/json/oembed_view.ex b/lib/philomena_web/views/api/json/oembed_view.ex new file mode 100644 index 00000000..3aa4ea0f --- /dev/null +++ b/lib/philomena_web/views/api/json/oembed_view.ex @@ -0,0 +1,32 @@ +defmodule PhilomenaWeb.Api.Json.OembedView do + use PhilomenaWeb, :view + + def render("error.json", _assigns) do + %{ + error: "Couldn't find an image" + } + end + + def render("show.json", %{image: image}) do + %{ + version: "1.0", + type: "photo", + title: "##{image.id} - #{image.tag_list_cache} - Derpibooru", + author_url: image.source_url || "", + author_name: artist_tags(image.tags), + provider_name: "Derpibooru", + provider_url: PhilomenaWeb.Endpoint.url(), + cache_age: 7200, + derpibooru_id: image.id, + derpibooru_score: image.score, + derpibooru_comments: image.comments_count, + derpibooru_tags: Enum.map(image.tags, & &1.name) + } + end + + defp artist_tags(tags) do + tags + |> Enum.filter(& &1.namespace == "artist") + |> Enum.map_join(", ", & &1.name_in_namespace) + end +end