Move Scrobble views to ScrobbleView
authorEgor Kislitsyn <egor@kislitsyn.com>
Tue, 19 May 2020 12:11:59 +0000 (16:11 +0400)
committerEgor Kislitsyn <egor@kislitsyn.com>
Tue, 19 May 2020 12:26:06 +0000 (16:26 +0400)
lib/pleroma/web/mastodon_api/views/status_view.ex
lib/pleroma/web/pleroma_api/controllers/scrobble_controller.ex
lib/pleroma/web/pleroma_api/views/scrobble_view.ex [new file with mode: 0644]
test/web/mastodon_api/views/status_view_test.exs
test/web/pleroma_api/views/scrobble_view_test.exs [new file with mode: 0644]

index 05a26017a801c593b3bb2db86e783f57b0f008ad..8e37150931a9c4c3a0ab7c1ff510e226c5091e49 100644 (file)
@@ -436,27 +436,6 @@ 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("show.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("listens.json", opts) do
-    safe_render_many(opts.activities, StatusView, "listen.json", opts)
-  end
-
   def render("context.json", %{activity: activity, activities: activities, user: user}) do
     %{ancestors: ancestors, descendants: descendants} =
       activities
index 35a37f69e2b799613220bddec9b97d2631f52b12..8665ca56ca667b5525c6cf288478d65d862d9c97 100644 (file)
@@ -11,7 +11,6 @@ defmodule Pleroma.Web.PleromaAPI.ScrobbleController do
   alias Pleroma.User
   alias Pleroma.Web.ActivityPub.ActivityPub
   alias Pleroma.Web.CommonAPI
-  alias Pleroma.Web.MastodonAPI.StatusView
 
   plug(Pleroma.Web.ApiSpec.CastAndValidate)
 
@@ -26,9 +25,7 @@ defmodule Pleroma.Web.PleromaAPI.ScrobbleController do
 
   def create(%{assigns: %{user: user}, body_params: params} = conn, _) do
     with {:ok, activity} <- CommonAPI.listen(user, params) do
-      conn
-      |> put_view(StatusView)
-      |> render("listen.json", %{activity: activity, for: user})
+      render(conn, "show.json", activity: activity, for: user)
     else
       {:error, message} ->
         conn
@@ -48,8 +45,7 @@ defmodule Pleroma.Web.PleromaAPI.ScrobbleController do
 
       conn
       |> add_link_headers(activities)
-      |> put_view(StatusView)
-      |> render("listens.json", %{
+      |> render("index.json", %{
         activities: activities,
         for: reading_user,
         as: :activity
diff --git a/lib/pleroma/web/pleroma_api/views/scrobble_view.ex b/lib/pleroma/web/pleroma_api/views/scrobble_view.ex
new file mode 100644 (file)
index 0000000..bbff93a
--- /dev/null
@@ -0,0 +1,37 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Web.PleromaAPI.ScrobbleView do
+  use Pleroma.Web, :view
+
+  require Pleroma.Constants
+
+  alias Pleroma.Activity
+  alias Pleroma.HTML
+  alias Pleroma.Object
+  alias Pleroma.Web.CommonAPI.Utils
+  alias Pleroma.Web.MastodonAPI.AccountView
+  alias Pleroma.Web.MastodonAPI.StatusView
+
+  def render("show.json", %{activity: %Activity{data: %{"type" => "Listen"}} = activity} = opts) do
+    object = Object.normalize(activity)
+
+    user = StatusView.get_user(activity.data["actor"])
+    created_at = Utils.to_masto_date(activity.data["published"])
+
+    %{
+      id: activity.id,
+      account: AccountView.render("show.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("index.json", opts) do
+    safe_render_many(opts.activities, __MODULE__, "show.json", opts)
+  end
+end
index 5d7adbe290d9772e99588987d97f5a0cdbe0764c..43e3bdca12163c639827421b5052741fed886474 100644 (file)
@@ -620,14 +620,4 @@ 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"]
-    assert_schema(status, "Status", Pleroma.Web.ApiSpec.spec())
-  end
 end
diff --git a/test/web/pleroma_api/views/scrobble_view_test.exs b/test/web/pleroma_api/views/scrobble_view_test.exs
new file mode 100644 (file)
index 0000000..6bdb565
--- /dev/null
@@ -0,0 +1,20 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Web.PleromaAPI.StatusViewTest do
+  use Pleroma.DataCase
+
+  alias Pleroma.Web.PleromaAPI.ScrobbleView
+
+  import Pleroma.Factory
+
+  test "successfully renders a Listen activity (pleroma extension)" do
+    listen_activity = insert(:listen)
+
+    status = ScrobbleView.render("show.json", activity: listen_activity)
+
+    assert status.length == listen_activity.data["object"]["length"]
+    assert status.title == listen_activity.data["object"]["title"]
+  end
+end