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)
59 parent_activities: parent_activities,
60 relationships: relationships_opt
63 safe_render_many(notifications, NotificationView, "show.json", opts)
69 notification: %Notification{activity: activity} = notification,
73 actor = User.get_cached_by_ap_id(activity.data["actor"])
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"])
79 Activity.get_create_by_object_ap_id(activity.data["object"])
83 mastodon_type = Activity.mastodon_notification_type(activity)
85 with %{id: _} = account <-
86 AccountView.render("show.json", %{
89 relationships: opts[:relationships]
92 id: to_string(notification.id),
94 created_at: CommonAPI.Utils.to_masto_date(notification.inserted_at),
97 is_seen: notification.seen
101 render_opts = %{relationships: opts[:relationships]}
103 case mastodon_type do
105 put_status(response, activity, reading_user, render_opts)
108 put_status(response, parent_activity_fn.(), reading_user, render_opts)
111 put_status(response, parent_activity_fn.(), reading_user, render_opts)
114 put_target(response, activity, reading_user, render_opts)
116 "pleroma:emoji_reaction" ->
118 |> put_status(parent_activity_fn.(), reading_user, render_opts)
119 |> put_emoji(activity)
121 type when type in ["follow", "follow_request"] ->
132 defp put_emoji(response, activity) do
133 Map.put(response, :emoji, activity.data["content"])
136 defp put_status(response, activity, reading_user, opts) do
137 status_render_opts = Map.merge(opts, %{activity: activity, for: reading_user})
138 status_render = StatusView.render("show.json", status_render_opts)
140 Map.put(response, :status, status_render)
143 defp put_target(response, activity, reading_user, opts) do
144 target_user = User.get_cached_by_ap_id(activity.data["target"])
145 target_render_opts = Map.merge(opts, %{user: target_user, for: reading_user})
146 target_render = AccountView.render("show.json", target_render_opts)
148 Map.put(response, :target, target_render)