mastodon api: implement rendering of listen activities
authorAriadne Conill <ariadne@dereferenced.org>
Fri, 27 Sep 2019 11:40:40 +0000 (11:40 +0000)
committerAriadne Conill <ariadne@dereferenced.org>
Mon, 30 Sep 2019 10:39:17 +0000 (10:39 +0000)
lib/pleroma/web/mastodon_api/views/status_view.ex
test/web/mastodon_api/views/status_view_test.exs

index 2321d0de29b13adffdc8f68eb65bee235562e32b..cf024a83c771931680105f15d8235ccf6d2a86d1 100644 (file)
@@ -368,6 +368,23 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
     }
   end
 
+  def render("listen.json", %{activity: %Activity{data: %{"type" => "Listen"}} = activity} = opts) do
+    object = Object.normalize(activity)
+
+    user = get_user(activity.data["actor"])
+    created_at = Utils.to_masto_date(activity.data["published"])
+
+    %{
+      id: activity.id,
+      account: AccountView.render("account.json", %{user: user, for: opts[:for]}),
+      created_at: created_at,
+      title: object.data["title"] |> HTML.strip_tags(),
+      artist: object.data["artist"] |> HTML.strip_tags(),
+      album: object.data["album"] |> HTML.strip_tags(),
+      length: object.data["length"]
+    }
+  end
+
   def render("poll.json", %{object: object} = opts) do
     {multiple, options} =
       case object.data do
index c17d0ef958c92a57e535331244721b770d620392..683132f8df5e55e0447072308648cc54e4232d8e 100644 (file)
@@ -608,4 +608,13 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do
 
     assert status.visibility == "list"
   end
+
+  test "successfully renders a Listen activity (pleroma extension)" do
+    listen_activity = insert(:listen)
+
+    status = StatusView.render("listen.json", activity: listen_activity)
+
+    assert status.length == listen_activity.data["object"]["length"]
+    assert status.title == listen_activity.data["object"]["title"]
+  end
 end