[#2497] Specified SHELL in .gitlab-ci.yml as required for `exexec`.
[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, subset: :source_mutes)
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 with %{id: _} = account <-
88 AccountView.render(
89 "show.json",
90 %{user: actor, for: reading_user}
91 ) do
92 response = %{
93 id: to_string(notification.id),
94 type: mastodon_type,
95 created_at: CommonAPI.Utils.to_masto_date(notification.inserted_at),
96 account: account,
97 pleroma: %{
98 is_seen: notification.seen
99 }
100 }
101
102 case mastodon_type do
103 "mention" ->
104 put_status(response, activity, reading_user, status_render_opts)
105
106 "favourite" ->
107 put_status(response, parent_activity_fn.(), reading_user, status_render_opts)
108
109 "reblog" ->
110 put_status(response, parent_activity_fn.(), reading_user, status_render_opts)
111
112 "move" ->
113 put_target(response, activity, reading_user, %{})
114
115 "pleroma:emoji_reaction" ->
116 response
117 |> put_status(parent_activity_fn.(), reading_user, status_render_opts)
118 |> put_emoji(activity)
119
120 type when type in ["follow", "follow_request"] ->
121 response
122
123 _ ->
124 nil
125 end
126 else
127 _ -> nil
128 end
129 end
130
131 defp put_emoji(response, activity) do
132 Map.put(response, :emoji, activity.data["content"])
133 end
134
135 defp put_status(response, activity, reading_user, opts) do
136 status_render_opts = Map.merge(opts, %{activity: activity, for: reading_user})
137 status_render = StatusView.render("show.json", status_render_opts)
138
139 Map.put(response, :status, status_render)
140 end
141
142 defp put_target(response, activity, reading_user, opts) do
143 target_user = User.get_cached_by_ap_id(activity.data["target"])
144 target_render_opts = Map.merge(opts, %{user: target_user, for: reading_user})
145 target_render = AccountView.render("show.json", target_render_opts)
146
147 Map.put(response, :target, target_render)
148 end
149 end