e518bdedb09f6f2f26dfdd4557356e83a79eb456
[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.UserRelationship
12 alias Pleroma.Web.CommonAPI
13 alias Pleroma.Web.MastodonAPI.AccountView
14 alias Pleroma.Web.MastodonAPI.NotificationView
15 alias Pleroma.Web.MastodonAPI.StatusView
16
17 def render("index.json", %{notifications: notifications, for: reading_user} = opts) do
18 opts = Map.merge(%{skip_relationships: true}, opts)
19
20 activities = Enum.map(notifications, & &1.activity)
21
22 parent_activities =
23 activities
24 |> Enum.filter(
25 &(Activity.mastodon_notification_type(&1) in [
26 "favourite",
27 "reblog",
28 "pleroma:emoji_reaction"
29 ])
30 )
31 |> Enum.map(& &1.data["object"])
32 |> Activity.create_by_object_ap_id()
33 |> Activity.with_preloaded_object(:left)
34 |> Pleroma.Repo.all()
35
36 relationships_opt =
37 cond do
38 Map.has_key?(opts, :relationships) ->
39 opts[:relationships]
40
41 is_nil(reading_user) ->
42 UserRelationship.view_relationships_option(nil, [])
43
44 true ->
45 move_activities_targets =
46 activities
47 |> Enum.filter(&(Activity.mastodon_notification_type(&1) == "move"))
48 |> Enum.map(&User.get_cached_by_ap_id(&1.data["target"]))
49
50 actors =
51 activities
52 |> Enum.map(fn a -> User.get_cached_by_ap_id(a.data["actor"]) end)
53 |> Enum.filter(& &1)
54 |> Kernel.++(move_activities_targets)
55
56 UserRelationship.view_relationships_option(reading_user, actors,
57 source_mutes_only: opts[:skip_relationships]
58 )
59 end
60
61 opts =
62 opts
63 |> Map.put(:parent_activities, parent_activities)
64 |> Map.put(:relationships, relationships_opt)
65
66 safe_render_many(notifications, NotificationView, "show.json", opts)
67 end
68
69 def render(
70 "show.json",
71 %{
72 notification: %Notification{activity: activity} = notification,
73 for: reading_user
74 } = opts
75 ) do
76 opts = Map.merge(%{skip_relationships: true}, opts)
77
78 actor = User.get_cached_by_ap_id(activity.data["actor"])
79
80 parent_activity_fn = fn ->
81 if opts[:parent_activities] do
82 Activity.Queries.find_by_object_ap_id(opts[:parent_activities], activity.data["object"])
83 else
84 Activity.get_create_by_object_ap_id(activity.data["object"])
85 end
86 end
87
88 mastodon_type = Activity.mastodon_notification_type(activity)
89
90 render_opts = %{
91 relationships: opts[:relationships],
92 skip_relationships: opts[:skip_relationships]
93 }
94
95 with %{id: _} = account <-
96 AccountView.render(
97 "show.json",
98 Map.merge(render_opts, %{user: actor, for: reading_user})
99 ) do
100 response = %{
101 id: to_string(notification.id),
102 type: mastodon_type,
103 created_at: CommonAPI.Utils.to_masto_date(notification.inserted_at),
104 account: account,
105 pleroma: %{
106 is_seen: notification.seen
107 }
108 }
109
110 case mastodon_type do
111 "mention" ->
112 put_status(response, activity, reading_user, render_opts)
113
114 "favourite" ->
115 put_status(response, parent_activity_fn.(), reading_user, render_opts)
116
117 "reblog" ->
118 put_status(response, parent_activity_fn.(), reading_user, render_opts)
119
120 "move" ->
121 # Note: :skip_relationships option being applied to _account_ rendering (here)
122 put_target(response, activity, reading_user, render_opts)
123
124 "pleroma:emoji_reaction" ->
125 response
126 |> put_status(parent_activity_fn.(), reading_user, render_opts)
127 |> put_emoji(activity)
128
129 type when type in ["follow", "follow_request"] ->
130 response
131
132 _ ->
133 nil
134 end
135 else
136 _ -> nil
137 end
138 end
139
140 defp put_emoji(response, activity) do
141 Map.put(response, :emoji, activity.data["content"])
142 end
143
144 defp put_status(response, activity, reading_user, opts) do
145 status_render_opts = Map.merge(opts, %{activity: activity, for: reading_user})
146 status_render = StatusView.render("show.json", status_render_opts)
147
148 Map.put(response, :status, status_render)
149 end
150
151 defp put_target(response, activity, reading_user, opts) do
152 target_user = User.get_cached_by_ap_id(activity.data["target"])
153 target_render_opts = Map.merge(opts, %{user: target_user, for: reading_user})
154 target_render = AccountView.render("show.json", target_render_opts)
155
156 Map.put(response, :target, target_render)
157 end
158 end