Merge remote-tracking branch 'remotes/origin/develop' into relations-preloading-for...
[akkoma] / lib / pleroma / web / mastodon_api / views / notification_view.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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: reading_user}) do
17 activities = Enum.map(notifications, & &1.activity)
18
19 parent_activities =
20 activities
21 |> Enum.filter(
22 &(Activity.mastodon_notification_type(&1) in [
23 "favourite",
24 "reblog",
25 "pleroma:emoji_reaction"
26 ])
27 )
28 |> Enum.map(& &1.data["object"])
29 |> Activity.create_by_object_ap_id()
30 |> Activity.with_preloaded_object(:left)
31 |> Pleroma.Repo.all()
32
33 move_activities_targets =
34 activities
35 |> Enum.filter(&(Activity.mastodon_notification_type(&1) == "move"))
36 |> Enum.map(&User.get_cached_by_ap_id(&1.data["target"]))
37
38 actors =
39 activities
40 |> Enum.map(fn a -> User.get_cached_by_ap_id(a.data["actor"]) end)
41 |> Enum.filter(& &1)
42 |> Kernel.++(move_activities_targets)
43
44 opts = %{
45 for: reading_user,
46 parent_activities: parent_activities,
47 relationships: StatusView.relationships_opts(reading_user, actors)
48 }
49
50 safe_render_many(notifications, NotificationView, "show.json", opts)
51 end
52
53 def render(
54 "show.json",
55 %{
56 notification: %Notification{activity: activity} = notification,
57 for: reading_user
58 } = opts
59 ) do
60 actor = User.get_cached_by_ap_id(activity.data["actor"])
61
62 parent_activity_fn = fn ->
63 if opts[:parent_activities] do
64 Activity.Queries.find_by_object_ap_id(opts[:parent_activities], activity.data["object"])
65 else
66 Activity.get_create_by_object_ap_id(activity.data["object"])
67 end
68 end
69
70 mastodon_type = Activity.mastodon_notification_type(activity)
71
72 with %{id: _} = account <-
73 AccountView.render("show.json", %{
74 user: actor,
75 for: reading_user,
76 relationships: opts[:relationships]
77 }) do
78 response = %{
79 id: to_string(notification.id),
80 type: mastodon_type,
81 created_at: CommonAPI.Utils.to_masto_date(notification.inserted_at),
82 account: account,
83 pleroma: %{
84 is_seen: notification.seen
85 }
86 }
87
88 relationships_opts = %{relationships: opts[:relationships]}
89
90 case mastodon_type do
91 "mention" ->
92 put_status(response, activity, reading_user, relationships_opts)
93
94 "favourite" ->
95 put_status(response, parent_activity_fn.(), reading_user, relationships_opts)
96
97 "reblog" ->
98 put_status(response, parent_activity_fn.(), reading_user, relationships_opts)
99
100 "move" ->
101 put_target(response, activity, reading_user, relationships_opts)
102
103 "follow" ->
104 response
105
106 "pleroma:emoji_reaction" ->
107 response
108 |> put_status(parent_activity_fn.(), reading_user, relationships_opts)
109 |> put_emoji(activity)
110
111 _ ->
112 nil
113 end
114 else
115 _ -> nil
116 end
117 end
118
119 defp put_emoji(response, activity) do
120 Map.put(response, :emoji, activity.data["content"])
121 end
122
123 defp put_status(response, activity, reading_user, opts) do
124 status_render_opts = Map.merge(opts, %{activity: activity, for: reading_user})
125 status_render = StatusView.render("show.json", status_render_opts)
126
127 Map.put(response, :status, status_render)
128 end
129
130 defp put_target(response, activity, reading_user, opts) do
131 target_user = User.get_cached_by_ap_id(activity.data["target"])
132 target_render_opts = Map.merge(opts, %{user: target_user, for: reading_user})
133 target_render = AccountView.render("show.json", target_render_opts)
134
135 Map.put(response, :target, target_render)
136 end
137 end