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
12 alias Pleroma.UserRelationship
13 alias Pleroma.Web.CommonAPI
14 alias Pleroma.Web.MastodonAPI.AccountView
15 alias Pleroma.Web.MastodonAPI.NotificationView
16 alias Pleroma.Web.MastodonAPI.StatusView
17 alias Pleroma.Web.PleromaAPI.ChatMessageView
19 def render("index.json", %{notifications: notifications, for: reading_user} = opts) do
20 activities = Enum.map(notifications, & &1.activity)
25 &(Activity.mastodon_notification_type(&1) in [
28 "pleroma:emoji_reaction"
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(&(Activity.mastodon_notification_type(&1) == "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,
57 source_mutes_only: opts[:skip_relationships]
63 |> Map.put(:parent_activities, parent_activities)
64 |> Map.put(:relationships, relationships_opt)
66 safe_render_many(notifications, NotificationView, "show.json", opts)
72 notification: %Notification{activity: activity} = notification,
76 actor = User.get_cached_by_ap_id(activity.data["actor"])
78 parent_activity_fn = fn ->
79 if opts[:parent_activities] do
80 Activity.Queries.find_by_object_ap_id(opts[:parent_activities], activity.data["object"])
82 Activity.get_create_by_object_ap_id(activity.data["object"])
86 # This returns the notification type by activity, but both chats and statuses are in "Create" activities.
88 case Activity.mastodon_notification_type(activity) do
90 object = Object.normalize(activity)
93 %{data: %{"type" => "ChatMessage"}} -> "pleroma:chat_mention"
102 relationships: opts[:relationships],
103 skip_relationships: opts[:skip_relationships]
106 with %{id: _} = account <-
109 Map.merge(render_opts, %{user: actor, for: reading_user})
112 id: to_string(notification.id),
114 created_at: CommonAPI.Utils.to_masto_date(notification.inserted_at),
117 is_seen: notification.seen
121 case mastodon_type do
123 put_status(response, activity, reading_user, render_opts)
126 put_status(response, parent_activity_fn.(), reading_user, render_opts)
129 put_status(response, parent_activity_fn.(), reading_user, render_opts)
132 # Note: :skip_relationships option being applied to _account_ rendering (here)
133 put_target(response, activity, reading_user, render_opts)
138 "pleroma:emoji_reaction" ->
140 |> put_status(parent_activity_fn.(), reading_user, render_opts)
141 |> put_emoji(activity)
143 "pleroma:chat_mention" ->
144 put_chat_message(response, activity, reading_user, render_opts)
154 defp put_emoji(response, activity) do
155 Map.put(response, :emoji, activity.data["content"])
158 defp put_chat_message(response, activity, reading_user, opts) do
159 object = Object.normalize(activity)
160 author = User.get_cached_by_ap_id(object.data["actor"])
161 chat = Pleroma.Chat.get(reading_user.id, author.ap_id)
162 render_opts = Map.merge(opts, %{object: object, for: reading_user, chat: chat})
163 chat_message_render = ChatMessageView.render("show.json", render_opts)
165 Map.put(response, :chat_message, chat_message_render)
168 defp put_status(response, activity, reading_user, opts) do
169 status_render_opts = Map.merge(opts, %{activity: activity, for: reading_user})
170 status_render = StatusView.render("show.json", status_render_opts)
172 Map.put(response, :status, status_render)
175 defp put_target(response, activity, reading_user, opts) do
176 target_user = User.get_cached_by_ap_id(activity.data["target"])
177 target_render_opts = Map.merge(opts, %{user: target_user, for: reading_user})
178 target_render = AccountView.render("show.json", target_render_opts)
180 Map.put(response, :target, target_render)