typo fix
[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(opts[:for]) ->
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)
55 end
56
57 opts = %{
58 for: reading_user,
59 parent_activities: parent_activities,
60 relationships: relationships_opt
61 }
62
63 safe_render_many(notifications, NotificationView, "show.json", opts)
64 end
65
66 def render(
67 "show.json",
68 %{
69 notification: %Notification{activity: activity} = notification,
70 for: reading_user
71 } = opts
72 ) do
73 actor = User.get_cached_by_ap_id(activity.data["actor"])
74
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"])
78 else
79 Activity.get_create_by_object_ap_id(activity.data["object"])
80 end
81 end
82
83 mastodon_type = Activity.mastodon_notification_type(activity)
84
85 with %{id: _} = account <-
86 AccountView.render("show.json", %{
87 user: actor,
88 for: reading_user,
89 relationships: opts[:relationships]
90 }) do
91 response = %{
92 id: to_string(notification.id),
93 type: mastodon_type,
94 created_at: CommonAPI.Utils.to_masto_date(notification.inserted_at),
95 account: account,
96 pleroma: %{
97 is_seen: notification.seen
98 }
99 }
100
101 render_opts = %{relationships: opts[:relationships]}
102
103 case mastodon_type do
104 "mention" ->
105 put_status(response, activity, reading_user, render_opts)
106
107 "favourite" ->
108 put_status(response, parent_activity_fn.(), reading_user, render_opts)
109
110 "reblog" ->
111 put_status(response, parent_activity_fn.(), reading_user, render_opts)
112
113 "move" ->
114 put_target(response, activity, reading_user, render_opts)
115
116 "follow" ->
117 response
118
119 "pleroma:emoji_reaction" ->
120 response
121 |> put_status(parent_activity_fn.(), reading_user, render_opts)
122 |> put_emoji(activity)
123
124 _ ->
125 nil
126 end
127 else
128 _ -> nil
129 end
130 end
131
132 defp put_emoji(response, activity) do
133 Map.put(response, :emoji, activity.data["content"])
134 end
135
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)
139
140 Map.put(response, :status, status_render)
141 end
142
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)
147
148 Map.put(response, :target, target_render)
149 end
150 end