Merge branch 'patch-2' into 'develop'
[akkoma] / lib / pleroma / web / mastodon_api / views / 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.MastodonAPI.NotificationView do
6 use Pleroma.Web, :view
7
8 alias Pleroma.Activity
9 alias Pleroma.Notification
10 alias Pleroma.User
11 alias Pleroma.Web.CommonAPI
12 alias Pleroma.Web.MastodonAPI.AccountView
13 alias Pleroma.Web.MastodonAPI.NotificationView
14 alias Pleroma.Web.MastodonAPI.StatusView
15
16 def render("index.json", %{notifications: notifications, for: user}) do
17 safe_render_many(notifications, NotificationView, "show.json", %{for: user})
18 end
19
20 def render("show.json", %{
21 notification: %Notification{activity: activity} = notification,
22 for: user
23 }) do
24 actor = User.get_cached_by_ap_id(activity.data["actor"])
25 parent_activity = Activity.get_create_by_object_ap_id(activity.data["object"])
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 pleroma: %{
34 is_seen: notification.seen
35 }
36 }
37
38 case mastodon_type do
39 "mention" ->
40 response
41 |> Map.merge(%{
42 status: StatusView.render("status.json", %{activity: activity, for: user})
43 })
44
45 "favourite" ->
46 response
47 |> Map.merge(%{
48 status: StatusView.render("status.json", %{activity: parent_activity, for: user})
49 })
50
51 "reblog" ->
52 response
53 |> Map.merge(%{
54 status: StatusView.render("status.json", %{activity: parent_activity, for: user})
55 })
56
57 "follow" ->
58 response
59
60 _ ->
61 nil
62 end
63 end
64 end