Merge branch 'develop' of git.pleroma.social:pleroma/pleroma into remake-remodel-dms
[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.Object
11 alias Pleroma.User
12 alias Pleroma.UserRelationship
13 alias Pleroma.Web.CommonAPI
14 alias Pleroma.Web.MastodonAPI.AccountView
15 alias Pleroma.Web.MastodonAPI.NotificationView
16 alias Pleroma.Web.MastodonAPI.StatusView
17 alias Pleroma.Web.PleromaAPI.ChatMessageView
18
19 def render("index.json", %{notifications: notifications, for: reading_user} = opts) do
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 actor = User.get_cached_by_ap_id(activity.data["actor"])
77
78 parent_activity_fn = fn ->
79 if opts[:parent_activities] do
80 Activity.Queries.find_by_object_ap_id(opts[:parent_activities], activity.data["object"])
81 else
82 Activity.get_create_by_object_ap_id(activity.data["object"])
83 end
84 end
85
86 # This returns the notification type by activity, but both chats and statuses
87 # are in "Create" activities.
88 mastodon_type =
89 case Activity.mastodon_notification_type(activity) do
90 "mention" ->
91 object = Object.normalize(activity)
92
93 case object do
94 %{data: %{"type" => "ChatMessage"}} -> "pleroma:chat_mention"
95 _ -> "mention"
96 end
97
98 type ->
99 type
100 end
101
102 render_opts = %{
103 relationships: opts[:relationships],
104 skip_relationships: opts[:skip_relationships]
105 }
106
107 with %{id: _} = account <-
108 AccountView.render(
109 "show.json",
110 Map.merge(render_opts, %{user: actor, for: reading_user})
111 ) do
112 response = %{
113 id: to_string(notification.id),
114 type: mastodon_type,
115 created_at: CommonAPI.Utils.to_masto_date(notification.inserted_at),
116 account: account,
117 pleroma: %{
118 is_seen: notification.seen
119 }
120 }
121
122 case mastodon_type do
123 "mention" ->
124 put_status(response, activity, reading_user, render_opts)
125
126 "favourite" ->
127 put_status(response, parent_activity_fn.(), reading_user, render_opts)
128
129 "reblog" ->
130 put_status(response, parent_activity_fn.(), reading_user, render_opts)
131
132 "move" ->
133 # Note: :skip_relationships option being applied to _account_ rendering (here)
134 put_target(response, activity, reading_user, render_opts)
135
136 "pleroma:emoji_reaction" ->
137 response
138 |> put_status(parent_activity_fn.(), reading_user, render_opts)
139 |> put_emoji(activity)
140
141 "pleroma:chat_mention" ->
142 put_chat_message(response, activity, reading_user, render_opts)
143
144 type when type in ["follow", "follow_request"] ->
145 response
146
147 _ ->
148 nil
149 end
150 else
151 _ -> nil
152 end
153 end
154
155 defp put_emoji(response, activity) do
156 Map.put(response, :emoji, activity.data["content"])
157 end
158
159 defp put_chat_message(response, activity, reading_user, opts) do
160 object = Object.normalize(activity)
161 author = User.get_cached_by_ap_id(object.data["actor"])
162 chat = Pleroma.Chat.get(reading_user.id, author.ap_id)
163 render_opts = Map.merge(opts, %{object: object, for: reading_user, chat: chat})
164 chat_message_render = ChatMessageView.render("show.json", render_opts)
165
166 Map.put(response, :chat_message, chat_message_render)
167 end
168
169 defp put_status(response, activity, reading_user, opts) do
170 status_render_opts = Map.merge(opts, %{activity: activity, for: reading_user})
171 status_render = StatusView.render("show.json", status_render_opts)
172
173 Map.put(response, :status, status_render)
174 end
175
176 defp put_target(response, activity, reading_user, opts) do
177 target_user = User.get_cached_by_ap_id(activity.data["target"])
178 target_render_opts = Map.merge(opts, %{user: target_user, for: reading_user})
179 target_render = AccountView.render("show.json", target_render_opts)
180
181 Map.put(response, :target, target_render)
182 end
183 end