Refactor SubscriptionNotificationView
[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.Web.CommonAPI
10 alias Pleroma.Web.MastodonAPI.AccountView
11 alias Pleroma.Web.MastodonAPI.StatusView
12 alias Pleroma.Web.PleromaAPI.SubscriptionNotificationView
13
14 def render("index.json", %{notifications: notifications, for: user}) do
15 safe_render_many(notifications, SubscriptionNotificationView, "show.json", %{for: user})
16 end
17
18 def render("show.json", %{
19 subscription_notification: %{
20 notification: %{activity: activity} = notification,
21 actor: actor,
22 parent_activity: parent_activity
23 },
24 for: user
25 }) do
26 mastodon_type = Activity.mastodon_notification_type(activity)
27
28 response = %{
29 id: to_string(notification.id),
30 type: mastodon_type,
31 created_at: CommonAPI.Utils.to_masto_date(notification.inserted_at),
32 account: AccountView.render("account.json", %{user: actor, for: user})
33 }
34
35 case mastodon_type do
36 "mention" ->
37 response
38 |> Map.merge(%{
39 status: StatusView.render("status.json", %{activity: activity, for: user})
40 })
41
42 "favourite" ->
43 response
44 |> Map.merge(%{
45 status: StatusView.render("status.json", %{activity: parent_activity, for: user})
46 })
47
48 "reblog" ->
49 response
50 |> Map.merge(%{
51 status: StatusView.render("status.json", %{activity: parent_activity, for: user})
52 })
53
54 "follow" ->
55 response
56
57 _ ->
58 nil
59 end
60 end
61 end