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.Notification
11 alias Pleroma.UserRelationship
12 alias Pleroma.Web.AdminAPI.Report
13 alias Pleroma.Web.AdminAPI.ReportView
14 alias Pleroma.Web.CommonAPI
15 alias Pleroma.Web.MediaProxy
16 alias Pleroma.Web.MastodonAPI.AccountView
17 alias Pleroma.Web.MastodonAPI.NotificationView
18 alias Pleroma.Web.MastodonAPI.StatusView
20 @parent_types ~w{Like Announce EmojiReact}
22 def render("index.json", %{notifications: notifications, for: reading_user} = opts) do
23 activities = Enum.map(notifications, & &1.activity)
28 %{data: %{"type" => type}} ->
31 |> Enum.map(& &1.data["object"])
32 |> Activity.create_by_object_ap_id()
33 |> Activity.with_preloaded_object(:left)
38 Map.has_key?(opts, :relationships) ->
41 is_nil(reading_user) ->
42 UserRelationship.view_relationships_option(nil, [])
45 move_activities_targets =
47 |> Enum.filter(&(&1.data["type"] == "Move"))
48 |> Enum.map(&User.get_cached_by_ap_id(&1.data["target"]))
53 |> Enum.map(fn a -> User.get_cached_by_ap_id(a.data["actor"]) end)
55 |> Kernel.++(move_activities_targets)
57 UserRelationship.view_relationships_option(reading_user, actors, subset: :source_mutes)
62 |> Map.put(:parent_activities, parent_activities)
63 |> Map.put(:relationships, relationships_opt)
65 safe_render_many(notifications, NotificationView, "show.json", opts)
71 notification: %Notification{activity: activity} = notification,
75 actor = User.get_cached_by_ap_id(activity.data["actor"])
77 parent_activity_fn = fn ->
78 if opts[:parent_activities] do
79 Activity.Queries.find_by_object_ap_id(opts[:parent_activities], activity.data["object"])
81 Activity.get_create_by_object_ap_id(activity.data["object"])
85 # Note: :relationships contain user mutes (needed for :muted flag in :status)
86 status_render_opts = %{relationships: opts[:relationships]}
87 account = AccountView.render("show.json", %{user: actor, for: reading_user})
90 id: to_string(notification.id),
91 type: notification.type,
92 created_at: CommonAPI.Utils.to_masto_date(notification.inserted_at),
95 is_muted: User.mutes?(reading_user, actor),
96 is_seen: notification.seen
100 case notification.type do
102 put_status(response, activity, reading_user, status_render_opts)
105 put_status(response, parent_activity_fn.(), reading_user, status_render_opts)
108 put_status(response, parent_activity_fn.(), reading_user, status_render_opts)
111 put_target(response, activity, reading_user, %{})
114 put_status(response, activity, reading_user, status_render_opts)
116 "pleroma:emoji_reaction" ->
118 |> put_status(parent_activity_fn.(), reading_user, status_render_opts)
119 |> put_emoji(activity)
122 put_report(response, activity)
124 type when type in ["follow", "follow_request"] ->
129 defp put_report(response, activity) do
130 report_render = ReportView.render("show.json", Report.extract_report_info(activity))
132 Map.put(response, :report, report_render)
135 defp put_emoji(response, activity) do
137 |> Map.put(:emoji, activity.data["content"])
138 |> Map.put(:emoji_url, MediaProxy.url(Pleroma.Emoji.emoji_url(activity.data)))
141 defp put_status(response, activity, reading_user, opts) do
142 status_render_opts = Map.merge(opts, %{activity: activity, for: reading_user})
143 status_render = StatusView.render("show.json", status_render_opts)
145 Map.put(response, :status, status_render)
148 defp put_target(response, activity, reading_user, opts) do
149 target_user = User.get_cached_by_ap_id(activity.data["target"])
150 target_render_opts = Map.merge(opts, %{user: target_user, for: reading_user})
151 target_render = AccountView.render("show.json", target_render_opts)
153 Map.put(response, :target, target_render)