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.ChatMessageReference
10 alias Pleroma.Notification
13 alias Pleroma.UserRelationship
14 alias Pleroma.Web.CommonAPI
15 alias Pleroma.Web.MastodonAPI.AccountView
16 alias Pleroma.Web.MastodonAPI.NotificationView
17 alias Pleroma.Web.MastodonAPI.StatusView
18 alias Pleroma.Web.PleromaAPI.ChatMessageReferenceView
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"]))
52 |> Enum.map(fn a -> User.get_cached_by_ap_id(a.data["actor"]) end)
54 |> Kernel.++(move_activities_targets)
56 UserRelationship.view_relationships_option(reading_user, actors, subset: :source_mutes)
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 # 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),
94 type: notification.type,
95 created_at: CommonAPI.Utils.to_masto_date(notification.inserted_at),
98 is_seen: notification.seen
102 case notification.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 "pleroma:chat_mention" ->
121 put_chat_message(response, activity, reading_user, status_render_opts)
123 type when type in ["follow", "follow_request"] ->
134 defp put_emoji(response, activity) do
135 Map.put(response, :emoji, activity.data["content"])
138 defp put_chat_message(response, activity, reading_user, opts) do
139 object = Object.normalize(activity)
140 author = User.get_cached_by_ap_id(object.data["actor"])
141 chat = Pleroma.Chat.get(reading_user.id, author.ap_id)
142 cm_ref = ChatMessageReference.for_chat_and_object(chat, object)
143 render_opts = Map.merge(opts, %{for: reading_user, chat_message_reference: cm_ref})
144 chat_message_render = ChatMessageReferenceView.render("show.json", render_opts)
146 Map.put(response, :chat_message, chat_message_render)
149 defp put_status(response, activity, reading_user, opts) do
150 status_render_opts = Map.merge(opts, %{activity: activity, for: reading_user})
151 status_render = StatusView.render("show.json", status_render_opts)
153 Map.put(response, :status, status_render)
156 defp put_target(response, activity, reading_user, opts) do
157 target_user = User.get_cached_by_ap_id(activity.data["target"])
158 target_render_opts = Map.merge(opts, %{user: target_user, for: reading_user})
159 target_render = AccountView.render("show.json", target_render_opts)
161 Map.put(response, :target, target_render)