mastodon api: factor out status card fetching, move status card rendering to statusvi...
authorWilliam Pitcock <nenolod@dereferenced.org>
Sun, 27 Jan 2019 09:37:11 +0000 (09:37 +0000)
committerWilliam Pitcock <nenolod@dereferenced.org>
Mon, 28 Jan 2019 05:53:17 +0000 (05:53 +0000)
lib/pleroma/web/mastodon_api/mastodon_api.ex
lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
lib/pleroma/web/mastodon_api/views/status_view.ex
test/web/mastodon_api/mastodon_api_controller_test.exs

index 8b137891791fe96927ad78e64b0aad7bded08bdc..9d1fb22ea467a2f38ce43e13846e23e40ae244ce 100644 (file)
@@ -1 +1,20 @@
+# Pleroma: A lightweight social networking server
+# Copyright _ 2017-2019 Pleroma Authors <https://pleroma.social/>
+# 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
index a366a149f4eb8e2751274e6a96b899d51f51e56a..1cdb96989daca49f522ec9da856b000f61b4b30d 100644 (file)
@@ -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 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController 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)
index 0f26794441e78a03ece52daadf840d1b97ca6ff0..a9e1e03ba15cdac7fdc2470ac59e1c94563e43b1 100644 (file)
@@ -176,6 +176,29 @@ defmodule Pleroma.Web.MastodonAPI.StatusView 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"
index b8f901e6c31efc21109ff4bfe33ba316452d10ae..02a0eb228572912f3eda9a80dbf31d44eeaef1ad 100644 (file)
@@ -1663,9 +1663,19 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest 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