5d231f0c40b84439e822c73bd3e5d336ee447843
[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.Object
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 are in "Create" activities.
87 mastodon_type =
88 case Activity.mastodon_notification_type(activity) do
89 "mention" ->
90 object = Object.normalize(activity)
91
92 case object do
93 %{data: %{"type" => "ChatMessage"}} -> "pleroma:chat_mention"
94 _ -> "mention"
95 end
96
97 type ->
98 type
99 end
100
101 render_opts = %{
102 relationships: opts[:relationships],
103 skip_relationships: opts[:skip_relationships]
104 }
105
106 with %{id: _} = account <-
107 AccountView.render(
108 "show.json",
109 Map.merge(render_opts, %{user: actor, for: reading_user})
110 ) do
111 response = %{
112 id: to_string(notification.id),
113 type: mastodon_type,
114 created_at: CommonAPI.Utils.to_masto_date(notification.inserted_at),
115 account: account,
116 pleroma: %{
117 is_seen: notification.seen
118 }
119 }
120
121 case mastodon_type do
122 "mention" ->
123 put_status(response, activity, reading_user, render_opts)
124
125 "favourite" ->
126 put_status(response, parent_activity_fn.(), reading_user, render_opts)
127
128 "reblog" ->
129 put_status(response, parent_activity_fn.(), reading_user, render_opts)
130
131 "move" ->
132 # Note: :skip_relationships option being applied to _account_ rendering (here)
133 put_target(response, activity, reading_user, render_opts)
134
135 "follow" ->
136 response
137
138 "pleroma:emoji_reaction" ->
139 response
140 |> put_status(parent_activity_fn.(), reading_user, render_opts)
141 |> put_emoji(activity)
142
143 "pleroma:chat_mention" ->
144 put_chat_message(response, activity, reading_user, render_opts)
145
146 _ ->
147 nil
148 end
149 else
150 _ -> nil
151 end
152 end
153
154 defp put_emoji(response, activity) do
155 Map.put(response, :emoji, activity.data["content"])
156 end
157
158 defp put_chat_message(response, activity, reading_user, opts) do
159 object = Object.normalize(activity)
160 author = User.get_cached_by_ap_id(object.data["actor"])
161 chat = Pleroma.Chat.get(reading_user.id, author.ap_id)
162 render_opts = Map.merge(opts, %{object: object, for: reading_user, chat: chat})
163 chat_message_render = ChatMessageView.render("show.json", render_opts)
164
165 Map.put(response, :chat_message, chat_message_render)
166 end
167
168 defp put_status(response, activity, reading_user, opts) do
169 status_render_opts = Map.merge(opts, %{activity: activity, for: reading_user})
170 status_render = StatusView.render("show.json", status_render_opts)
171
172 Map.put(response, :status, status_render)
173 end
174
175 defp put_target(response, activity, reading_user, opts) do
176 target_user = User.get_cached_by_ap_id(activity.data["target"])
177 target_render_opts = Map.merge(opts, %{user: target_user, for: reading_user})
178 target_render = AccountView.render("show.json", target_render_opts)
179
180 Map.put(response, :target, target_render)
181 end
182 end