1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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.Web.CommonAPI
12 alias Pleroma.Web.MastodonAPI.AccountView
13 alias Pleroma.Web.MastodonAPI.NotificationView
14 alias Pleroma.Web.MastodonAPI.StatusView
16 def render("index.json", %{notifications: notifications, for: reading_user}) do
17 activities = Enum.map(notifications, & &1.activity)
22 &(Activity.mastodon_notification_type(&1) in [
25 "pleroma:emoji_reaction"
28 |> Enum.map(& &1.data["object"])
29 |> Activity.create_by_object_ap_id()
30 |> Activity.with_preloaded_object(:left)
33 move_activities_targets =
35 |> Enum.filter(&(Activity.mastodon_notification_type(&1) == "move"))
36 |> Enum.map(&User.get_cached_by_ap_id(&1.data["target"]))
40 |> Enum.map(fn a -> User.get_cached_by_ap_id(a.data["actor"]) end)
42 |> Kernel.++(move_activities_targets)
46 parent_activities: parent_activities,
47 relationships: StatusView.relationships_opts(reading_user, actors)
50 safe_render_many(notifications, NotificationView, "show.json", opts)
56 notification: %Notification{activity: activity} = notification,
60 actor = User.get_cached_by_ap_id(activity.data["actor"])
62 parent_activity_fn = fn ->
63 if opts[:parent_activities] do
64 Activity.Queries.find_by_object_ap_id(opts[:parent_activities], activity.data["object"])
66 Activity.get_create_by_object_ap_id(activity.data["object"])
70 mastodon_type = Activity.mastodon_notification_type(activity)
72 with %{id: _} = account <-
73 AccountView.render("show.json", %{
76 relationships: opts[:relationships]
79 id: to_string(notification.id),
81 created_at: CommonAPI.Utils.to_masto_date(notification.inserted_at),
84 is_seen: notification.seen
88 relationships_opts = %{relationships: opts[:relationships]}
92 put_status(response, activity, reading_user, relationships_opts)
95 put_status(response, parent_activity_fn.(), reading_user, relationships_opts)
98 put_status(response, parent_activity_fn.(), reading_user, relationships_opts)
101 put_target(response, activity, reading_user, relationships_opts)
106 "pleroma:emoji_reaction" ->
108 |> put_status(parent_activity_fn.(), reading_user, relationships_opts)
109 |> put_emoji(activity)
119 defp put_emoji(response, activity) do
120 Map.put(response, :emoji, activity.data["content"])
123 defp put_status(response, activity, reading_user, opts) do
124 status_render_opts = Map.merge(opts, %{activity: activity, for: reading_user})
125 status_render = StatusView.render("show.json", status_render_opts)
127 Map.put(response, :status, status_render)
130 defp put_target(response, activity, reading_user, opts) do
131 target_user = User.get_cached_by_ap_id(activity.data["target"])
132 target_render_opts = Map.merge(opts, %{user: target_user, for: reading_user})
133 target_render = AccountView.render("show.json", target_render_opts)
135 Map.put(response, :target, target_render)