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,
55 source_mutes_only: opts[:skip_relationships]
61 |> Map.put(:parent_activities, parent_activities)
62 |> Map.put(:relationships, relationships_opt)
64 safe_render_many(notifications, NotificationView, "show.json", opts)
70 notification: %Notification{activity: activity} = notification,
74 actor = User.get_cached_by_ap_id(activity.data["actor"])
76 parent_activity_fn = fn ->
77 if opts[:parent_activities] do
78 Activity.Queries.find_by_object_ap_id(opts[:parent_activities], activity.data["object"])
80 Activity.get_create_by_object_ap_id(activity.data["object"])
84 mastodon_type = Activity.mastodon_notification_type(activity)
87 relationships: opts[:relationships],
88 skip_relationships: opts[:skip_relationships]
91 with %{id: _} = account <-
94 Map.merge(render_opts, %{user: actor, for: reading_user})
97 id: to_string(notification.id),
99 created_at: CommonAPI.Utils.to_masto_date(notification.inserted_at),
102 is_seen: notification.seen
106 case mastodon_type do
108 put_status(response, activity, reading_user, render_opts)
111 put_status(response, parent_activity_fn.(), reading_user, render_opts)
114 put_status(response, parent_activity_fn.(), reading_user, render_opts)
117 # Note: :skip_relationships option being applied to _account_ rendering (here)
118 put_target(response, activity, reading_user, render_opts)
120 "pleroma:emoji_reaction" ->
122 |> put_status(parent_activity_fn.(), reading_user, render_opts)
123 |> put_emoji(activity)
125 type when type in ["follow", "follow_request"] ->
136 defp put_emoji(response, activity) do
137 Map.put(response, :emoji, activity.data["content"])
140 defp put_status(response, activity, reading_user, opts) do
141 status_render_opts = Map.merge(opts, %{activity: activity, for: reading_user})
142 status_render = StatusView.render("show.json", status_render_opts)
144 Map.put(response, :status, status_render)
147 defp put_target(response, activity, reading_user, opts) do
148 target_user = User.get_cached_by_ap_id(activity.data["target"])
149 target_render_opts = Map.merge(opts, %{user: target_user, for: reading_user})
150 target_render = AccountView.render("show.json", target_render_opts)
152 Map.put(response, :target, target_render)