Twitter Representers: Handle Mastodon attachments.
authorlain <lain@soykaf.club>
Sun, 11 Feb 2018 16:21:06 +0000 (17:21 +0100)
committerlain <lain@soykaf.club>
Sun, 11 Feb 2018 16:21:06 +0000 (17:21 +0100)
lib/pleroma/web/twitter_api/representers/object_representer.ex
test/web/twitter_api/representers/object_representer_test.exs

index 69eaeb36c3d9c60aeb790e5547de70ef558df155..e2d653ba85ee285b041ccd50f882ac7a0bb6937b 100644 (file)
@@ -2,9 +2,8 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ObjectRepresenter do
   use Pleroma.Web.TwitterAPI.Representers.BaseRepresenter
   alias Pleroma.Object
 
-  def to_map(%Object{} = object, _opts) do
+  def to_map(%Object{data: %{"url" => [url | _]}} = object, _opts) do
     data = object.data
-    url = List.first(data["url"])
     %{
       url: url["href"] |> Pleroma.Web.MediaProxy.url(),
       mimetype: url["mediaType"],
@@ -13,6 +12,19 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ObjectRepresenter do
     }
   end
 
+  def to_map(%Object{data: %{"url" => url} = data}, _opts) when is_binary(url) do
+    %{
+      url: url |> Pleroma.Web.MediaProxy.url(),
+      mimetype: data["mediaType"],
+      id: data["uuid"],
+      oembed: false
+    }
+  end
+
+  def to_map(%Object{}, _opts) do
+    %{}
+  end
+
   # If we only get the naked data, wrap in an object
   def to_map(%{} = data, opts) do
     to_map(%Object{data: data}, opts)
index 791b30237b3032cdc033a71e2739d37818ee6a1e..ac8184407ecfe4ba8577c7e5318e9d736785d429 100644 (file)
@@ -28,4 +28,24 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ObjectReprenterTest do
 
     assert expected_object == ObjectRepresenter.to_map(object)
   end
+
+  test "represents mastodon-style attachments" do
+    object = %Object{
+      id: nil,
+      data: %{
+        "mediaType" => "image/png",
+        "name" => "blabla", "type" => "Document",
+        "url" => "http://mastodon.example.org/system/media_attachments/files/000/000/001/original/8619f31c6edec470.png"
+      }
+    }
+
+    expected_object = %{
+      url: "http://mastodon.example.org/system/media_attachments/files/000/000/001/original/8619f31c6edec470.png",
+      mimetype: "image/png",
+      oembed: false,
+      id: nil
+    }
+
+    assert expected_object == ObjectRepresenter.to_map(object)
+  end
 end