Refactor SubscriptionNotificationView

This commit is contained in:
Roman Chvanikov 2019-09-17 17:44:10 +03:00
parent e9f69a3eb7
commit 9fa2586abd
2 changed files with 23 additions and 6 deletions

View file

@ -7,11 +7,16 @@ defmodule Pleroma.Web.PleromaAPI.SubscriptionNotificationController do
import Pleroma.Web.ControllerHelper, only: [add_link_headers: 2] import Pleroma.Web.ControllerHelper, only: [add_link_headers: 2]
alias Pleroma.Activity
alias Pleroma.SubscriptionNotification alias Pleroma.SubscriptionNotification
alias Pleroma.User
alias Pleroma.Web.PleromaAPI.PleromaAPI alias Pleroma.Web.PleromaAPI.PleromaAPI
def index(%{assigns: %{user: user}} = conn, params) do 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 conn
|> add_link_headers(notifications) |> add_link_headers(notifications)
@ -20,7 +25,10 @@ def index(%{assigns: %{user: user}} = conn, params) do
def show(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do def show(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do
with {:ok, notification} <- SubscriptionNotification.get(user, id) 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 else
{:error, reason} -> {:error, reason} ->
conn conn
@ -52,4 +60,12 @@ def destroy_multiple(
SubscriptionNotification.destroy_multiple(user, ids) SubscriptionNotification.destroy_multiple(user, ids)
json(conn, %{}) json(conn, %{})
end 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 end

View file

@ -6,7 +6,6 @@ defmodule Pleroma.Web.PleromaAPI.SubscriptionNotificationView do
use Pleroma.Web, :view use Pleroma.Web, :view
alias Pleroma.Activity alias Pleroma.Activity
alias Pleroma.User
alias Pleroma.Web.CommonAPI alias Pleroma.Web.CommonAPI
alias Pleroma.Web.MastodonAPI.AccountView alias Pleroma.Web.MastodonAPI.AccountView
alias Pleroma.Web.MastodonAPI.StatusView alias Pleroma.Web.MastodonAPI.StatusView
@ -17,11 +16,13 @@ def render("index.json", %{notifications: notifications, for: user}) do
end end
def render("show.json", %{ def render("show.json", %{
subscription_notification: %{activity: activity} = notification, subscription_notification: %{
notification: %{activity: activity} = notification,
actor: actor,
parent_activity: parent_activity
},
for: user for: user
}) do }) 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) mastodon_type = Activity.mastodon_notification_type(activity)
response = %{ response = %{