Refactor SubscriptionNotificationView
authorRoman Chvanikov <chvanikoff@pm.me>
Tue, 17 Sep 2019 14:44:10 +0000 (17:44 +0300)
committerRoman Chvanikov <chvanikoff@pm.me>
Tue, 17 Sep 2019 14:44:10 +0000 (17:44 +0300)
lib/pleroma/web/pleroma_api/controllers/subscription_notification_controller.ex
lib/pleroma/web/pleroma_api/views/subscription_notification_view.ex

index fa8307668695c3e4bb1ea74b4d98db1305456cfb..37c2222defd5926cf48f0f7275377755f51b8311 100644 (file)
@@ -7,11 +7,16 @@ defmodule Pleroma.Web.PleromaAPI.SubscriptionNotificationController do
 
   import Pleroma.Web.ControllerHelper, only: [add_link_headers: 2]
 
+  alias Pleroma.Activity
   alias Pleroma.SubscriptionNotification
+  alias Pleroma.User
   alias Pleroma.Web.PleromaAPI.PleromaAPI
 
   def index(%{assigns: %{user: user}} = conn, params) do
-    notifications = PleromaAPI.get_subscription_notifications(user, params)
+    notifications =
+      user
+      |> PleromaAPI.get_subscription_notifications(params)
+      |> Enum.map(&build_notification_data/1)
 
     conn
     |> add_link_headers(notifications)
@@ -20,7 +25,10 @@ defmodule Pleroma.Web.PleromaAPI.SubscriptionNotificationController do
 
   def show(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do
     with {:ok, notification} <- SubscriptionNotification.get(user, id) do
-      render(conn, "show.json", %{subscription_notification: notification, for: user})
+      render(conn, "show.json", %{
+        subscription_notification: build_notification_data(notification),
+        for: user
+      })
     else
       {:error, reason} ->
         conn
@@ -52,4 +60,12 @@ defmodule Pleroma.Web.PleromaAPI.SubscriptionNotificationController do
     SubscriptionNotification.destroy_multiple(user, ids)
     json(conn, %{})
   end
+
+  defp build_notification_data(%{activity: %{data: data}} = notification) do
+    %{
+      notification: notification,
+      actor: User.get_cached_by_ap_id(data["actor"]),
+      parent_activity: Activity.get_create_by_object_ap_id(data["object"])
+    }
+  end
 end
index d7f7f4c5abc802aed3307519d369220d9be203a7..0eccbcbb96bff34ffaa823d76ad22fade562f65d 100644 (file)
@@ -6,7 +6,6 @@ defmodule Pleroma.Web.PleromaAPI.SubscriptionNotificationView do
   use Pleroma.Web, :view
 
   alias Pleroma.Activity
-  alias Pleroma.User
   alias Pleroma.Web.CommonAPI
   alias Pleroma.Web.MastodonAPI.AccountView
   alias Pleroma.Web.MastodonAPI.StatusView
@@ -17,11 +16,13 @@ defmodule Pleroma.Web.PleromaAPI.SubscriptionNotificationView do
   end
 
   def render("show.json", %{
-        subscription_notification: %{activity: activity} = notification,
+        subscription_notification: %{
+          notification: %{activity: activity} = notification,
+          actor: actor,
+          parent_activity: parent_activity
+        },
         for: user
       }) do
-    actor = User.get_cached_by_ap_id(activity.data["actor"])
-    parent_activity = Activity.get_create_by_object_ap_id(activity.data["object"])
     mastodon_type = Activity.mastodon_notification_type(activity)
 
     response = %{