From 132d815f1f749cec4b54083fc46046c45e7d0d04 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Sun, 27 Jan 2019 09:37:11 +0000 Subject: [PATCH] mastodon api: factor out status card fetching, move status card rendering to statusview, add opengraph extended data --- lib/pleroma/web/mastodon_api/mastodon_api.ex | 19 ++++++++++++++ .../mastodon_api/mastodon_api_controller.ex | 25 +++---------------- .../web/mastodon_api/views/status_view.ex | 23 +++++++++++++++++ .../mastodon_api_controller_test.exs | 12 ++++++++- 4 files changed, 57 insertions(+), 22 deletions(-) diff --git a/lib/pleroma/web/mastodon_api/mastodon_api.ex b/lib/pleroma/web/mastodon_api/mastodon_api.ex index 8b1378917..9d1fb22ea 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api.ex @@ -1 +1,20 @@ +# Pleroma: A lightweight social networking server +# Copyright _ 2017-2019 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only +defmodule Pleroma.Web.MastodonAPI do + alias Pleroma.{Repo, Activity, Object, HTML} + alias Pleroma.Web.ActivityPub.ActivityPub + + def get_status_card(status_id) do + with %Activity{} = activity <- Repo.get(Activity, status_id), + true <- ActivityPub.is_public?(activity), + %Object{} = object <- Object.normalize(activity.data["object"]), + page_url <- HTML.extract_first_external_url(object, object.data["content"]), + {:ok, rich_media} <- Pleroma.Web.RichMedia.Parser.parse(page_url) do + %{page_url: page_url, rich_media: rich_media} + else + _ -> %{} + end + end +end diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index a366a149f..1cdb96989 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -6,8 +6,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do use Pleroma.Web, :controller alias Pleroma.{Repo, Object, Activity, User, Notification, Stats} alias Pleroma.Web - alias Pleroma.HTML + alias Pleroma.Web.MastodonAPI alias Pleroma.Web.MastodonAPI.{ StatusView, AccountView, @@ -1342,27 +1342,10 @@ def suggestions(%{assigns: %{user: user}} = conn, _) do end end - def get_status_card(status_id) do - with %Activity{} = activity <- Repo.get(Activity, status_id), - true <- ActivityPub.is_public?(activity), - %Object{} = object <- Object.normalize(activity.data["object"]), - page_url <- HTML.extract_first_external_url(object, object.data["content"]), - {:ok, rich_media} <- Pleroma.Web.RichMedia.Parser.parse(page_url) do - page_url = rich_media[:url] || page_url - site_name = rich_media[:site_name] || URI.parse(page_url).host - - rich_media - |> Map.take([:image, :title, :description]) - |> Map.put(:type, "link") - |> Map.put(:provider_name, site_name) - |> Map.put(:url, page_url) - else - _ -> %{} - end - end - def status_card(conn, %{"id" => status_id}) do - json(conn, get_status_card(status_id)) + data = StatusView.render("card.json", MastodonAPI.get_status_card(status_id)) + + json(conn, data) end def try_render(conn, target, params) diff --git a/lib/pleroma/web/mastodon_api/views/status_view.ex b/lib/pleroma/web/mastodon_api/views/status_view.ex index 0f2679444..a9e1e03ba 100644 --- a/lib/pleroma/web/mastodon_api/views/status_view.ex +++ b/lib/pleroma/web/mastodon_api/views/status_view.ex @@ -176,6 +176,29 @@ def render("status.json", _) do nil end + def render("card.json", %{rich_media: rich_media, page_url: page_url}) do + page_url = rich_media[:url] || page_url + page_url_data = URI.parse(page_url) + site_name = rich_media[:site_name] || page_url_data.host + + %{ + type: "link", + provider_name: site_name, + provider_url: page_url_data.scheme <> "://" <> page_url_data.host, + url: page_url, + image: rich_media[:image], + title: rich_media[:title], + description: rich_media[:description], + pleroma: %{ + opengraph: rich_media + } + } + end + + def render("card.json", _) do + nil + end + def render("attachment.json", %{attachment: attachment}) do [attachment_url | _] = attachment["url"] media_type = attachment_url["mediaType"] || attachment_url["mimeType"] || "image" diff --git a/test/web/mastodon_api/mastodon_api_controller_test.exs b/test/web/mastodon_api/mastodon_api_controller_test.exs index b8f901e6c..02a0eb228 100644 --- a/test/web/mastodon_api/mastodon_api_controller_test.exs +++ b/test/web/mastodon_api/mastodon_api_controller_test.exs @@ -1663,9 +1663,19 @@ test "Status rich-media Card", %{conn: conn, user: user} do assert response == %{ "image" => "http://ia.media-imdb.com/images/rock.jpg", "provider_name" => "www.imdb.com", + "provider_url" => "http://www.imdb.com", "title" => "The Rock", "type" => "link", - "url" => "http://www.imdb.com/title/tt0117500/" + "url" => "http://www.imdb.com/title/tt0117500/", + "description" => nil, + "pleroma" => %{ + "opengraph" => %{ + "image" => "http://ia.media-imdb.com/images/rock.jpg", + "title" => "The Rock", + "type" => "video.movie", + "url" => "http://www.imdb.com/title/tt0117500/" + } + } } end end