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.UserRelationship
12 alias Pleroma.Web.CommonAPI
13 alias Pleroma.Web.MastodonAPI.AccountView
14 alias Pleroma.Web.MastodonAPI.NotificationView
15 alias Pleroma.Web.MastodonAPI.StatusView
17 def render("index.json", %{notifications: notifications, for: reading_user} = opts) do
18 activities = Enum.map(notifications, & &1.activity)
23 &(Activity.mastodon_notification_type(&1) in [
26 "pleroma:emoji_reaction"
29 |> Enum.map(& &1.data["object"])
30 |> Activity.create_by_object_ap_id()
31 |> Activity.with_preloaded_object(:left)
36 Map.has_key?(opts, :relationships) ->
39 is_nil(reading_user) ->
40 UserRelationship.view_relationships_option(nil, [])
43 move_activities_targets =
45 |> Enum.filter(&(Activity.mastodon_notification_type(&1) == "move"))
46 |> Enum.map(&User.get_cached_by_ap_id(&1.data["target"]))
50 |> Enum.map(fn a -> User.get_cached_by_ap_id(a.data["actor"]) end)
52 |> Kernel.++(move_activities_targets)
54 UserRelationship.view_relationships_option(reading_user, actors, subset: :source_mutes)
59 |> Map.put(:parent_activities, parent_activities)
60 |> Map.put(:relationships, relationships_opt)
62 safe_render_many(notifications, NotificationView, "show.json", opts)
68 notification: %Notification{activity: activity} = notification,
72 actor = User.get_cached_by_ap_id(activity.data["actor"])
74 parent_activity_fn = fn ->
75 if opts[:parent_activities] do
76 Activity.Queries.find_by_object_ap_id(opts[:parent_activities], activity.data["object"])
78 Activity.get_create_by_object_ap_id(activity.data["object"])
82 mastodon_type = Activity.mastodon_notification_type(activity)
84 # Note: :relationships contain user mutes (needed for :muted flag in :status)
85 status_render_opts = %{relationships: opts[:relationships]}
87 with %{id: _} = account <-
90 %{user: actor, for: reading_user}
93 id: to_string(notification.id),
95 created_at: CommonAPI.Utils.to_masto_date(notification.inserted_at),
98 is_seen: notification.seen
102 case mastodon_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 type when type in ["follow", "follow_request"] ->
131 defp put_emoji(response, activity) do
132 Map.put(response, :emoji, activity.data["content"])
135 defp put_status(response, activity, reading_user, opts) do
136 status_render_opts = Map.merge(opts, %{activity: activity, for: reading_user})
137 status_render = StatusView.render("show.json", status_render_opts)
139 Map.put(response, :status, status_render)
142 defp put_target(response, activity, reading_user, opts) do
143 target_user = User.get_cached_by_ap_id(activity.data["target"])
144 target_render_opts = Map.merge(opts, %{user: target_user, for: reading_user})
145 target_render = AccountView.render("show.json", target_render_opts)
147 Map.put(response, :target, target_render)