Merge branch 'bugfix/repeating-announce' into 'develop'
[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,
55 source_mutes_only: opts[:skip_relationships]
56 )
57 end
58
59 opts =
60 opts
61 |> Map.put(:parent_activities, parent_activities)
62 |> Map.put(:relationships, relationships_opt)
63
64 safe_render_many(notifications, NotificationView, "show.json", opts)
65 end
66
67 def render(
68 "show.json",
69 %{
70 notification: %Notification{activity: activity} = notification,
71 for: reading_user
72 } = opts
73 ) do
74 actor = User.get_cached_by_ap_id(activity.data["actor"])
75
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"])
79 else
80 Activity.get_create_by_object_ap_id(activity.data["object"])
81 end
82 end
83
84 mastodon_type = Activity.mastodon_notification_type(activity)
85
86 render_opts = %{
87 relationships: opts[:relationships],
88 skip_relationships: opts[:skip_relationships]
89 }
90
91 with %{id: _} = account <-
92 AccountView.render(
93 "show.json",
94 Map.merge(render_opts, %{user: actor, for: reading_user})
95 ) do
96 response = %{
97 id: to_string(notification.id),
98 type: mastodon_type,
99 created_at: CommonAPI.Utils.to_masto_date(notification.inserted_at),
100 account: account,
101 pleroma: %{
102 is_seen: notification.seen
103 }
104 }
105
106 case mastodon_type do
107 "mention" ->
108 put_status(response, activity, reading_user, render_opts)
109
110 "favourite" ->
111 put_status(response, parent_activity_fn.(), reading_user, render_opts)
112
113 "reblog" ->
114 put_status(response, parent_activity_fn.(), reading_user, render_opts)
115
116 "move" ->
117 # Note: :skip_relationships option being applied to _account_ rendering (here)
118 put_target(response, activity, reading_user, render_opts)
119
120 "pleroma:emoji_reaction" ->
121 response
122 |> put_status(parent_activity_fn.(), reading_user, render_opts)
123 |> put_emoji(activity)
124
125 type when type in ["follow", "follow_request"] ->
126 response
127
128 _ ->
129 nil
130 end
131 else
132 _ -> nil
133 end
134 end
135
136 defp put_emoji(response, activity) do
137 Map.put(response, :emoji, activity.data["content"])
138 end
139
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)
143
144 Map.put(response, :status, status_render)
145 end
146
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)
151
152 Map.put(response, :target, target_render)
153 end
154 end