Cleanup PleromaAPIController
[akkoma] / lib / pleroma / web / pleroma_api / views / subscription_notification_view.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.PleromaAPI.SubscriptionNotificationView do
6 use Pleroma.Web, :view
7
8 alias Pleroma.Activity
9 alias Pleroma.User
10 alias Pleroma.Web.CommonAPI
11 alias Pleroma.Web.MastodonAPI.AccountView
12 alias Pleroma.Web.MastodonAPI.StatusView
13 alias Pleroma.Web.PleromaAPI.SubscriptionNotificationView
14
15 def render("index.json", %{notifications: notifications, for: user}) do
16 safe_render_many(notifications, SubscriptionNotificationView, "show.json", %{for: user})
17 end
18
19 def render("show.json", %{
20 subscription_notification: %{activity: activity} = notification,
21 for: user
22 }) do
23 actor = User.get_cached_by_ap_id(activity.data["actor"])
24 parent_activity = Activity.get_create_by_object_ap_id(activity.data["object"])
25 mastodon_type = Activity.mastodon_notification_type(activity)
26
27 response = %{
28 id: to_string(notification.id),
29 type: mastodon_type,
30 created_at: CommonAPI.Utils.to_masto_date(notification.inserted_at),
31 account: AccountView.render("account.json", %{user: actor, for: user})
32 }
33
34 case mastodon_type do
35 "mention" ->
36 response
37 |> Map.merge(%{
38 status: StatusView.render("status.json", %{activity: activity, for: user})
39 })
40
41 "favourite" ->
42 response
43 |> Map.merge(%{
44 status: StatusView.render("status.json", %{activity: parent_activity, for: user})
45 })
46
47 "reblog" ->
48 response
49 |> Map.merge(%{
50 status: StatusView.render("status.json", %{activity: parent_activity, for: user})
51 })
52
53 "follow" ->
54 response
55
56 _ ->
57 nil
58 end
59 end
60 end