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