1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MastodonAPI.NotificationView do
9 alias Pleroma.Chat.MessageReference
10 alias Pleroma.Notification
13 alias Pleroma.UserRelationship
14 alias Pleroma.Web.AdminAPI.Report
15 alias Pleroma.Web.AdminAPI.ReportView
16 alias Pleroma.Web.CommonAPI
17 alias Pleroma.Web.MastodonAPI.AccountView
18 alias Pleroma.Web.MastodonAPI.NotificationView
19 alias Pleroma.Web.MastodonAPI.StatusView
20 alias Pleroma.Web.PleromaAPI.Chat.MessageReferenceView
22 @parent_types ~w{Like Announce EmojiReact}
24 def render("index.json", %{notifications: notifications, for: reading_user} = opts) do
25 activities = Enum.map(notifications, & &1.activity)
30 %{data: %{"type" => type}} ->
33 |> Enum.map(& &1.data["object"])
34 |> Activity.create_by_object_ap_id()
35 |> Activity.with_preloaded_object(:left)
40 Map.has_key?(opts, :relationships) ->
43 is_nil(reading_user) ->
44 UserRelationship.view_relationships_option(nil, [])
47 move_activities_targets =
49 |> Enum.filter(&(&1.data["type"] == "Move"))
50 |> Enum.map(&User.get_cached_by_ap_id(&1.data["target"]))
55 |> Enum.map(fn a -> User.get_cached_by_ap_id(a.data["actor"]) end)
57 |> Kernel.++(move_activities_targets)
59 UserRelationship.view_relationships_option(reading_user, actors, subset: :source_mutes)
64 |> Map.put(:parent_activities, parent_activities)
65 |> Map.put(:relationships, relationships_opt)
67 safe_render_many(notifications, NotificationView, "show.json", opts)
73 notification: %Notification{activity: activity} = notification,
77 actor = User.get_cached_by_ap_id(activity.data["actor"])
79 parent_activity_fn = fn ->
80 if opts[:parent_activities] do
81 Activity.Queries.find_by_object_ap_id(opts[:parent_activities], activity.data["object"])
83 Activity.get_create_by_object_ap_id(activity.data["object"])
87 # Note: :relationships contain user mutes (needed for :muted flag in :status)
88 status_render_opts = %{relationships: opts[:relationships]}
89 account = AccountView.render("show.json", %{user: actor, for: reading_user})
92 id: to_string(notification.id),
93 type: notification.type,
94 created_at: CommonAPI.Utils.to_masto_date(notification.inserted_at),
97 is_muted: User.mutes?(reading_user, actor),
98 is_seen: notification.seen
102 case notification.type do
104 put_status(response, activity, reading_user, status_render_opts)
107 put_status(response, parent_activity_fn.(), reading_user, status_render_opts)
110 put_status(response, parent_activity_fn.(), reading_user, status_render_opts)
113 put_target(response, activity, reading_user, %{})
115 "pleroma:emoji_reaction" ->
117 |> put_status(parent_activity_fn.(), reading_user, status_render_opts)
118 |> put_emoji(activity)
120 "pleroma:chat_mention" ->
121 put_chat_message(response, activity, reading_user, status_render_opts)
124 put_report(response, activity)
126 type when type in ["follow", "follow_request"] ->
131 defp put_report(response, activity) do
132 report_render = ReportView.render("show.json", Report.extract_report_info(activity))
134 Map.put(response, :report, report_render)
137 defp put_emoji(response, activity) do
138 Map.put(response, :emoji, activity.data["content"])
141 defp put_chat_message(response, activity, reading_user, opts) do
142 object = Object.normalize(activity, fetch: false)
143 author = User.get_cached_by_ap_id(object.data["actor"])
144 chat = Pleroma.Chat.get(reading_user.id, author.ap_id)
145 cm_ref = MessageReference.for_chat_and_object(chat, object)
146 render_opts = Map.merge(opts, %{for: reading_user, chat_message_reference: cm_ref})
147 chat_message_render = MessageReferenceView.render("show.json", render_opts)
149 Map.put(response, :chat_message, chat_message_render)
152 defp put_status(response, activity, reading_user, opts) do
153 status_render_opts = Map.merge(opts, %{activity: activity, for: reading_user})
154 status_render = StatusView.render("show.json", status_render_opts)
156 Map.put(response, :status, status_render)
159 defp put_target(response, activity, reading_user, opts) do
160 target_user = User.get_cached_by_ap_id(activity.data["target"])
161 target_render_opts = Map.merge(opts, %{user: target_user, for: reading_user})
162 target_render = AccountView.render("show.json", target_render_opts)
164 Map.put(response, :target, target_render)