Add attachments to mastoapi statuses.
authorRoger Braun <roger@rogerbraun.net>
Sun, 10 Sep 2017 09:51:01 +0000 (11:51 +0200)
committerRoger Braun <roger@rogerbraun.net>
Sun, 10 Sep 2017 09:51:01 +0000 (11:51 +0200)
lib/pleroma/web/mastodon_api/views/status_view.ex
test/web/mastodon_api/status_view_test.exs

index 7b798506a08937ab66bbabde10e0e755b3d1dd42..686ffd29de26e6d4db5f2c6c554095ff163f591d 100644 (file)
@@ -24,6 +24,8 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
     repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
     favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
 
+    attachments = render_many(object["attachment"] || [], StatusView, "attachment.json", as: :attachment)
+
     %{
       id: activity.id,
       uri: object["id"],
@@ -42,11 +44,29 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
       sensitive: sensitive,
       spoiler_text: "",
       visibility: "public",
-      media_attachments: [], # fix
+      media_attachments: attachments,
       mentions: mentions,
       tags: [], # fix,
       application: nil,
       language: nil
     }
   end
+
+  def render("attachment.json", %{attachment: attachment}) do
+    [%{"mediaType" => media_type, "href" => href} | _] = attachment["url"]
+
+    type = cond do
+      String.contains?(media_type, "image") -> "image"
+      String.contains?(media_type, "video") -> "video"
+      true -> "unknown"
+    end
+
+    %{
+      id: attachment["uuid"],
+      url: href,
+      remote_url: href,
+      preview_url: href,
+      type: type
+    }
+  end
 end
index b8a96f71a7ed87218291db58ac65a822546042ca..a12fc8244db58e4082de7e9a24d7dbf3d99c6678 100644 (file)
@@ -2,7 +2,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do
   use Pleroma.DataCase
 
   alias Pleroma.Web.MastodonAPI.{StatusView, AccountView}
-  alias Pleroma.User
+  alias Pleroma.{User, Object}
   alias Pleroma.Web.OStatus
   import Pleroma.Factory
 
@@ -50,4 +50,28 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do
 
     assert status.mentions == [AccountView.render("mention.json", %{user: user})]
   end
+
+  test "attachments" do
+    incoming = File.read!("test/fixtures/incoming_reply_mastodon.xml")
+    object = %{
+      "type" => "Image",
+      "url" => [
+        %{
+          "mediaType" => "image/png",
+          "href" => "someurl"
+        }
+      ],
+      "uuid" => 6
+    }
+
+    expected = %{
+      id: 6,
+      type: "image",
+      url: "someurl",
+      remote_url: "someurl",
+      preview_url: "someurl"
+    }
+
+    assert expected == StatusView.render("attachment.json", %{attachment: object})
+  end
 end