07d55a3e9c18fd6046e7a6824f0f031c7330e0b2
[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, subset: :source_mutes)
57 end
58
59 opts =
60 opts
61 |> Map.put(:parent_activities, parent_activities)
62 |> Map.put(:relationships, relationships_opt)
63
64 safe_render_many(notifications, NotificationView, "show.json", opts)
65 end
66
67 def render(
68 "show.json",
69 %{
70 notification: %Notification{activity: activity} = notification,
71 for: reading_user
72 } = opts
73 ) do
74 actor = User.get_cached_by_ap_id(activity.data["actor"])
75
76 parent_activity_fn = fn ->
77 if opts[:parent_activities] do
78 Activity.Queries.find_by_object_ap_id(opts[:parent_activities], activity.data["object"])
79 else
80 Activity.get_create_by_object_ap_id(activity.data["object"])
81 end
82 end
83
84 # This returns the notification type by activity, but both chats and statuses
85 # are in "Create" activities.
86 mastodon_type =
87 case Activity.mastodon_notification_type(activity) do
88 "mention" ->
89 object = Object.normalize(activity)
90
91 case object do
92 %{data: %{"type" => "ChatMessage"}} -> "pleroma:chat_mention"
93 _ -> "mention"
94 end
95
96 type ->
97 type
98 end
99
100 # Note: :relationships contain user mutes (needed for :muted flag in :status)
101 status_render_opts = %{relationships: opts[:relationships]}
102
103 with %{id: _} = account <-
104 AccountView.render(
105 "show.json",
106 %{user: actor, for: reading_user}
107 ) do
108 response = %{
109 id: to_string(notification.id),
110 type: mastodon_type,
111 created_at: CommonAPI.Utils.to_masto_date(notification.inserted_at),
112 account: account,
113 pleroma: %{
114 is_seen: notification.seen
115 }
116 }
117
118 case mastodon_type do
119 "mention" ->
120 put_status(response, activity, reading_user, status_render_opts)
121
122 "favourite" ->
123 put_status(response, parent_activity_fn.(), reading_user, status_render_opts)
124
125 "reblog" ->
126 put_status(response, parent_activity_fn.(), reading_user, status_render_opts)
127
128 "move" ->
129 put_target(response, activity, reading_user, %{})
130
131 "pleroma:emoji_reaction" ->
132 response
133 |> put_status(parent_activity_fn.(), reading_user, status_render_opts)
134 |> put_emoji(activity)
135
136 "pleroma:chat_mention" ->
137 put_chat_message(response, activity, reading_user, status_render_opts)
138
139 type when type in ["follow", "follow_request"] ->
140 response
141
142 _ ->
143 nil
144 end
145 else
146 _ -> nil
147 end
148 end
149
150 defp put_emoji(response, activity) do
151 Map.put(response, :emoji, activity.data["content"])
152 end
153
154 defp put_chat_message(response, activity, reading_user, opts) do
155 object = Object.normalize(activity)
156 author = User.get_cached_by_ap_id(object.data["actor"])
157 chat = Pleroma.Chat.get(reading_user.id, author.ap_id)
158 render_opts = Map.merge(opts, %{object: object, for: reading_user, chat: chat})
159 chat_message_render = ChatMessageView.render("show.json", render_opts)
160
161 Map.put(response, :chat_message, chat_message_render)
162 end
163
164 defp put_status(response, activity, reading_user, opts) do
165 status_render_opts = Map.merge(opts, %{activity: activity, for: reading_user})
166 status_render = StatusView.render("show.json", status_render_opts)
167
168 Map.put(response, :status, status_render)
169 end
170
171 defp put_target(response, activity, reading_user, opts) do
172 target_user = User.get_cached_by_ap_id(activity.data["target"])
173 target_render_opts = Map.merge(opts, %{user: target_user, for: reading_user})
174 target_render = AccountView.render("show.json", target_render_opts)
175
176 Map.put(response, :target, target_render)
177 end
178 end