Use mediaproxy for emoji notifications if enabled
[akkoma] / test / pleroma / web / mastodon_api / views / notification_view_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MastodonAPI.NotificationViewTest do
6 use Pleroma.DataCase
7
8 alias Pleroma.Activity
9 alias Pleroma.Chat
10 alias Pleroma.Chat.MessageReference
11 alias Pleroma.Notification
12 alias Pleroma.Object
13 alias Pleroma.Repo
14 alias Pleroma.User
15 alias Pleroma.Web.ActivityPub.Builder
16 alias Pleroma.Web.ActivityPub.Pipeline
17 alias Pleroma.Web.AdminAPI.Report
18 alias Pleroma.Web.AdminAPI.ReportView
19 alias Pleroma.Web.CommonAPI
20 alias Pleroma.Web.CommonAPI.Utils
21 alias Pleroma.Web.MastodonAPI.AccountView
22 alias Pleroma.Web.MastodonAPI.NotificationView
23 alias Pleroma.Web.MastodonAPI.StatusView
24 alias Pleroma.Web.MediaProxy
25 alias Pleroma.Web.PleromaAPI.Chat.MessageReferenceView
26 import Pleroma.Factory
27
28 defp test_notifications_rendering(notifications, user, expected_result) do
29 result = NotificationView.render("index.json", %{notifications: notifications, for: user})
30
31 assert expected_result == result
32
33 result =
34 NotificationView.render("index.json", %{
35 notifications: notifications,
36 for: user,
37 relationships: nil
38 })
39
40 assert expected_result == result
41 end
42
43 test "ChatMessage notification" do
44 user = insert(:user)
45 recipient = insert(:user)
46 {:ok, activity} = CommonAPI.post_chat_message(user, recipient, "what's up my dude")
47
48 {:ok, [notification]} = Notification.create_notifications(activity)
49
50 object = Object.normalize(activity, fetch: false)
51 chat = Chat.get(recipient.id, user.ap_id)
52
53 cm_ref = MessageReference.for_chat_and_object(chat, object)
54
55 expected = %{
56 id: to_string(notification.id),
57 pleroma: %{is_seen: false, is_muted: false},
58 type: "pleroma:chat_mention",
59 account: AccountView.render("show.json", %{user: user, for: recipient}),
60 chat_message: MessageReferenceView.render("show.json", %{chat_message_reference: cm_ref}),
61 created_at: Utils.to_masto_date(notification.inserted_at)
62 }
63
64 test_notifications_rendering([notification], recipient, [expected])
65 end
66
67 test "Mention notification" do
68 user = insert(:user)
69 mentioned_user = insert(:user)
70 {:ok, activity} = CommonAPI.post(user, %{status: "hey @#{mentioned_user.nickname}"})
71 {:ok, [notification]} = Notification.create_notifications(activity)
72 user = User.get_cached_by_id(user.id)
73
74 expected = %{
75 id: to_string(notification.id),
76 pleroma: %{is_seen: false, is_muted: false},
77 type: "mention",
78 account:
79 AccountView.render("show.json", %{
80 user: user,
81 for: mentioned_user
82 }),
83 status: StatusView.render("show.json", %{activity: activity, for: mentioned_user}),
84 created_at: Utils.to_masto_date(notification.inserted_at)
85 }
86
87 test_notifications_rendering([notification], mentioned_user, [expected])
88 end
89
90 test "Favourite notification" do
91 user = insert(:user)
92 another_user = insert(:user)
93 {:ok, create_activity} = CommonAPI.post(user, %{status: "hey"})
94 {:ok, favorite_activity} = CommonAPI.favorite(another_user, create_activity.id)
95 {:ok, [notification]} = Notification.create_notifications(favorite_activity)
96 create_activity = Activity.get_by_id(create_activity.id)
97
98 expected = %{
99 id: to_string(notification.id),
100 pleroma: %{is_seen: false, is_muted: false},
101 type: "favourite",
102 account: AccountView.render("show.json", %{user: another_user, for: user}),
103 status: StatusView.render("show.json", %{activity: create_activity, for: user}),
104 created_at: Utils.to_masto_date(notification.inserted_at)
105 }
106
107 test_notifications_rendering([notification], user, [expected])
108 end
109
110 test "Reblog notification" do
111 user = insert(:user)
112 another_user = insert(:user)
113 {:ok, create_activity} = CommonAPI.post(user, %{status: "hey"})
114 {:ok, reblog_activity} = CommonAPI.repeat(create_activity.id, another_user)
115 {:ok, [notification]} = Notification.create_notifications(reblog_activity)
116 reblog_activity = Activity.get_by_id(create_activity.id)
117
118 expected = %{
119 id: to_string(notification.id),
120 pleroma: %{is_seen: false, is_muted: false},
121 type: "reblog",
122 account: AccountView.render("show.json", %{user: another_user, for: user}),
123 status: StatusView.render("show.json", %{activity: reblog_activity, for: user}),
124 created_at: Utils.to_masto_date(notification.inserted_at)
125 }
126
127 test_notifications_rendering([notification], user, [expected])
128 end
129
130 test "Follow notification" do
131 follower = insert(:user)
132 followed = insert(:user)
133 {:ok, follower, followed, _activity} = CommonAPI.follow(follower, followed)
134 notification = Notification |> Repo.one() |> Repo.preload(:activity)
135
136 expected = %{
137 id: to_string(notification.id),
138 pleroma: %{is_seen: false, is_muted: false},
139 type: "follow",
140 account: AccountView.render("show.json", %{user: follower, for: followed}),
141 created_at: Utils.to_masto_date(notification.inserted_at)
142 }
143
144 test_notifications_rendering([notification], followed, [expected])
145
146 User.perform(:delete, follower)
147 refute Repo.one(Notification)
148 end
149
150 test "Move notification" do
151 old_user = insert(:user)
152 new_user = insert(:user, also_known_as: [old_user.ap_id])
153 follower = insert(:user)
154
155 User.follow(follower, old_user)
156 Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
157 Pleroma.Tests.ObanHelpers.perform_all()
158
159 old_user = refresh_record(old_user)
160 new_user = refresh_record(new_user)
161
162 [notification] = Notification.for_user(follower)
163
164 expected = %{
165 id: to_string(notification.id),
166 pleroma: %{is_seen: false, is_muted: false},
167 type: "move",
168 account: AccountView.render("show.json", %{user: old_user, for: follower}),
169 target: AccountView.render("show.json", %{user: new_user, for: follower}),
170 created_at: Utils.to_masto_date(notification.inserted_at)
171 }
172
173 test_notifications_rendering([notification], follower, [expected])
174 end
175
176 test "EmojiReact notification" do
177 user = insert(:user)
178 other_user = insert(:user)
179
180 {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
181 {:ok, _activity} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
182
183 activity = Repo.get(Activity, activity.id)
184
185 [notification] = Notification.for_user(user)
186
187 assert notification
188
189 expected = %{
190 id: to_string(notification.id),
191 pleroma: %{is_seen: false, is_muted: false},
192 type: "pleroma:emoji_reaction",
193 emoji: "☕",
194 emoji_url: nil,
195 account: AccountView.render("show.json", %{user: other_user, for: user}),
196 status: StatusView.render("show.json", %{activity: activity, for: user}),
197 created_at: Utils.to_masto_date(notification.inserted_at)
198 }
199
200 test_notifications_rendering([notification], user, [expected])
201 end
202
203 test "EmojiReact notification with custom emoji" do
204 user = insert(:user)
205 other_user = insert(:user)
206
207 {:ok, activity} = CommonAPI.post(user, %{status: "#morb"})
208 {:ok, _activity} = CommonAPI.react_with_emoji(activity.id, other_user, ":100a:")
209
210 activity = Repo.get(Activity, activity.id)
211
212 [notification] = Notification.for_user(user)
213
214 assert notification
215
216 expected = %{
217 id: to_string(notification.id),
218 pleroma: %{is_seen: false, is_muted: false},
219 type: "pleroma:emoji_reaction",
220 emoji: ":100a:",
221 emoji_url: "http://localhost:4001/emoji/100a.png",
222 account: AccountView.render("show.json", %{user: other_user, for: user}),
223 status: StatusView.render("show.json", %{activity: activity, for: user}),
224 created_at: Utils.to_masto_date(notification.inserted_at)
225 }
226
227 test_notifications_rendering([notification], user, [expected])
228 end
229
230 test "EmojiReact notification with remote custom emoji" do
231 proxyBaseUrl = "https://cache.pleroma.social"
232 clear_config([:media_proxy, :base_url], proxyBaseUrl)
233
234 for testProxy <- [true, false] do
235 clear_config([:media_proxy, :enabled], testProxy)
236
237 user = insert(:user)
238 other_user = insert(:user, local: false)
239
240 {:ok, activity} = CommonAPI.post(user, %{status: "#morb"})
241 {:ok, emoji_react, _} = Builder.emoji_react(other_user, Object.normalize(activity, fetch: false), ":100a:")
242
243 remoteUrl = "http://evil.website/emoji/100a.png"
244 [tag] = emoji_react["tag"]
245 tag = put_in(tag["id"], remoteUrl)
246 tag = put_in(tag["icon"]["url"], remoteUrl)
247 emoji_react = put_in(emoji_react["tag"], [tag])
248
249 {:ok, _activity, _} = Pipeline.common_pipeline(emoji_react, local: false)
250
251 activity = Repo.get(Activity, activity.id)
252
253 [notification] = Notification.for_user(user)
254
255 assert notification
256
257 expected = %{
258 id: to_string(notification.id),
259 pleroma: %{is_seen: false, is_muted: false},
260 type: "pleroma:emoji_reaction",
261 emoji: ":100a:",
262 emoji_url: (if testProxy, do: MediaProxy.encode_url(remoteUrl), else: remoteUrl),
263 account: AccountView.render("show.json", %{user: other_user, for: user}),
264 status: StatusView.render("show.json", %{activity: activity, for: user}),
265 created_at: Utils.to_masto_date(notification.inserted_at)
266 }
267 test_notifications_rendering([notification], user, [expected])
268 end
269 end
270
271 test "Poll notification" do
272 user = insert(:user)
273 activity = insert(:question_activity, user: user)
274 {:ok, [notification]} = Notification.create_poll_notifications(activity)
275
276 expected = %{
277 id: to_string(notification.id),
278 pleroma: %{is_seen: false, is_muted: false},
279 type: "poll",
280 account:
281 AccountView.render("show.json", %{
282 user: user,
283 for: user
284 }),
285 status: StatusView.render("show.json", %{activity: activity, for: user}),
286 created_at: Utils.to_masto_date(notification.inserted_at)
287 }
288
289 test_notifications_rendering([notification], user, [expected])
290 end
291
292 test "Report notification" do
293 reporting_user = insert(:user)
294 reported_user = insert(:user)
295 {:ok, moderator_user} = insert(:user) |> User.admin_api_update(%{is_moderator: true})
296
297 {:ok, activity} = CommonAPI.report(reporting_user, %{account_id: reported_user.id})
298 {:ok, [notification]} = Notification.create_notifications(activity)
299
300 expected = %{
301 id: to_string(notification.id),
302 pleroma: %{is_seen: false, is_muted: false},
303 type: "pleroma:report",
304 account: AccountView.render("show.json", %{user: reporting_user, for: moderator_user}),
305 created_at: Utils.to_masto_date(notification.inserted_at),
306 report: ReportView.render("show.json", Report.extract_report_info(activity))
307 }
308
309 test_notifications_rendering([notification], moderator_user, [expected])
310 end
311
312 test "muted notification" do
313 user = insert(:user)
314 another_user = insert(:user)
315
316 {:ok, _} = Pleroma.UserRelationship.create_mute(user, another_user)
317 {:ok, create_activity} = CommonAPI.post(user, %{status: "hey"})
318 {:ok, favorite_activity} = CommonAPI.favorite(another_user, create_activity.id)
319 {:ok, [notification]} = Notification.create_notifications(favorite_activity)
320 create_activity = Activity.get_by_id(create_activity.id)
321
322 expected = %{
323 id: to_string(notification.id),
324 pleroma: %{is_seen: true, is_muted: true},
325 type: "favourite",
326 account: AccountView.render("show.json", %{user: another_user, for: user}),
327 status: StatusView.render("show.json", %{activity: create_activity, for: user}),
328 created_at: Utils.to_masto_date(notification.inserted_at)
329 }
330
331 test_notifications_rendering([notification], user, [expected])
332 end
333 end