From 9fa2586abd915342095574f05358642412db0f04 Mon Sep 17 00:00:00 2001 From: Roman Chvanikov Date: Tue, 17 Sep 2019 17:44:10 +0300 Subject: [PATCH] Refactor SubscriptionNotificationView --- .../subscription_notification_controller.ex | 20 +++++++++++++++++-- .../views/subscription_notification_view.ex | 9 +++++---- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/lib/pleroma/web/pleroma_api/controllers/subscription_notification_controller.ex b/lib/pleroma/web/pleroma_api/controllers/subscription_notification_controller.ex index fa8307668..37c2222de 100644 --- a/lib/pleroma/web/pleroma_api/controllers/subscription_notification_controller.ex +++ b/lib/pleroma/web/pleroma_api/controllers/subscription_notification_controller.ex @@ -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 diff --git a/lib/pleroma/web/pleroma_api/views/subscription_notification_view.ex b/lib/pleroma/web/pleroma_api/views/subscription_notification_view.ex index d7f7f4c5a..0eccbcbb9 100644 --- a/lib/pleroma/web/pleroma_api/views/subscription_notification_view.ex +++ b/lib/pleroma/web/pleroma_api/views/subscription_notification_view.ex @@ -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 = %{