mirror of
https://github.com/philomena-dev/philomena.git
synced 2025-01-19 14:17:59 +01:00
expand oembed to a proper view
This commit is contained in:
parent
19c126a7a6
commit
4f040da3fa
2 changed files with 36 additions and 26 deletions
|
@ -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
|
||||
|
|
32
lib/philomena_web/views/api/json/oembed_view.ex
Normal file
32
lib/philomena_web/views/api/json/oembed_view.ex
Normal file
|
@ -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
|
Loading…
Reference in a new issue