0349bcc839cc88ced670baac868124c21e321c0e
[akkoma] / lib / pleroma / web / mastodon_api / views / notification_view.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MastodonAPI.NotificationView do
6 use Pleroma.Web, :view
7
8 alias Pleroma.Activity
9 alias Pleroma.Notification
10 alias Pleroma.User
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
16
17 def render("index.json", %{notifications: notifications, for: reading_user} = opts) do
18 activities = Enum.map(notifications, & &1.activity)
19
20 parent_activities =
21 activities
22 |> Enum.filter(
23 &(Activity.mastodon_notification_type(&1) in [
24 "favourite",
25 "reblog",
26 "pleroma:emoji_reaction"
27 ])
28 )
29 |> Enum.map(& &1.data["object"])
30 |> Activity.create_by_object_ap_id()
31 |> Activity.with_preloaded_object(:left)
32 |> Pleroma.Repo.all()
33
34 relationships_opt =
35 cond do
36 Map.has_key?(opts, :relationships) ->
37 opts[:relationships]
38
39 is_nil(reading_user) ->
40 UserRelationship.view_relationships_option(nil, [])
41
42 true ->
43 move_activities_targets =
44 activities
45 |> Enum.filter(&(Activity.mastodon_notification_type(&1) == "move"))
46 |> Enum.map(&User.get_cached_by_ap_id(&1.data["target"]))
47
48 actors =
49 activities
50 |> Enum.map(fn a -> User.get_cached_by_ap_id(a.data["actor"]) end)
51 |> Enum.filter(& &1)
52 |> Kernel.++(move_activities_targets)
53
54 UserRelationship.view_relationships_option(reading_user, actors, source_mutes_only: true)
55 end
56
57 opts =
58 opts
59 |> Map.put(:parent_activities, parent_activities)
60 |> Map.put(:relationships, relationships_opt)
61
62 safe_render_many(notifications, NotificationView, "show.json", opts)
63 end
64
65 def render(
66 "show.json",
67 %{
68 notification: %Notification{activity: activity} = notification,
69 for: reading_user
70 } = opts
71 ) do
72 actor = User.get_cached_by_ap_id(activity.data["actor"])
73
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"])
77 else
78 Activity.get_create_by_object_ap_id(activity.data["object"])
79 end
80 end
81
82 mastodon_type = Activity.mastodon_notification_type(activity)
83
84 # Note: :relationships contain user mutes (needed for :muted flag in :status)
85 status_render_opts = %{relationships: opts[:relationships]}
86
87 account_render_opts = %{skip_relationships: true}
88
89 with %{id: _} = account <-
90 AccountView.render(
91 "show.json",
92 Map.merge(account_render_opts, %{user: actor, for: reading_user})
93 ) do
94 response = %{
95 id: to_string(notification.id),
96 type: mastodon_type,
97 created_at: CommonAPI.Utils.to_masto_date(notification.inserted_at),
98 account: account,
99 pleroma: %{
100 is_seen: notification.seen
101 }
102 }
103
104 case mastodon_type do
105 "mention" ->
106 put_status(response, activity, reading_user, status_render_opts)
107
108 "favourite" ->
109 put_status(response, parent_activity_fn.(), reading_user, status_render_opts)
110
111 "reblog" ->
112 put_status(response, parent_activity_fn.(), reading_user, status_render_opts)
113
114 "move" ->
115 put_target(response, activity, reading_user, account_render_opts)
116
117 "pleroma:emoji_reaction" ->
118 response
119 |> put_status(parent_activity_fn.(), reading_user, status_render_opts)
120 |> put_emoji(activity)
121
122 type when type in ["follow", "follow_request"] ->
123 response
124
125 _ ->
126 nil
127 end
128 else
129 _ -> nil
130 end
131 end
132
133 defp put_emoji(response, activity) do
134 Map.put(response, :emoji, activity.data["content"])
135 end
136
137 defp put_status(response, activity, reading_user, opts) do
138 status_render_opts = Map.merge(opts, %{activity: activity, for: reading_user})
139 status_render = StatusView.render("show.json", status_render_opts)
140
141 Map.put(response, :status, status_render)
142 end
143
144 defp put_target(response, activity, reading_user, opts) do
145 target_user = User.get_cached_by_ap_id(activity.data["target"])
146 target_render_opts = Map.merge(opts, %{user: target_user, for: reading_user})
147 target_render = AccountView.render("show.json", target_render_opts)
148
149 Map.put(response, :target, target_render)
150 end
151 end