a22c9df1d1a5b377b504bb7b37cc7f5fc9515c7a
[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
242 {:ok, emoji_react, _} =
243 Builder.emoji_react(other_user, Object.normalize(activity, fetch: false), ":100a:")
244
245 remoteUrl = "http://evil.website/emoji/100a.png"
246 [tag] = emoji_react["tag"]
247 tag = put_in(tag["id"], remoteUrl)
248 tag = put_in(tag["icon"]["url"], remoteUrl)
249 emoji_react = put_in(emoji_react["tag"], [tag])
250
251 {:ok, _activity, _} = Pipeline.common_pipeline(emoji_react, local: false)
252
253 activity = Repo.get(Activity, activity.id)
254
255 [notification] = Notification.for_user(user)
256
257 assert notification
258
259 expected = %{
260 id: to_string(notification.id),
261 pleroma: %{is_seen: false, is_muted: false},
262 type: "pleroma:emoji_reaction",
263 emoji: ":100a:",
264 emoji_url: if(testProxy, do: MediaProxy.encode_url(remoteUrl), else: remoteUrl),
265 account: AccountView.render("show.json", %{user: other_user, for: user}),
266 status: StatusView.render("show.json", %{activity: activity, for: user}),
267 created_at: Utils.to_masto_date(notification.inserted_at)
268 }
269
270 test_notifications_rendering([notification], user, [expected])
271 end
272 end
273
274 test "Poll notification" do
275 user = insert(:user)
276 activity = insert(:question_activity, user: user)
277 {:ok, [notification]} = Notification.create_poll_notifications(activity)
278
279 expected = %{
280 id: to_string(notification.id),
281 pleroma: %{is_seen: false, is_muted: false},
282 type: "poll",
283 account:
284 AccountView.render("show.json", %{
285 user: user,
286 for: user
287 }),
288 status: StatusView.render("show.json", %{activity: activity, for: user}),
289 created_at: Utils.to_masto_date(notification.inserted_at)
290 }
291
292 test_notifications_rendering([notification], user, [expected])
293 end
294
295 test "Report notification" do
296 reporting_user = insert(:user)
297 reported_user = insert(:user)
298 {:ok, moderator_user} = insert(:user) |> User.admin_api_update(%{is_moderator: true})
299
300 {:ok, activity} = CommonAPI.report(reporting_user, %{account_id: reported_user.id})
301 {:ok, [notification]} = Notification.create_notifications(activity)
302
303 expected = %{
304 id: to_string(notification.id),
305 pleroma: %{is_seen: false, is_muted: false},
306 type: "pleroma:report",
307 account: AccountView.render("show.json", %{user: reporting_user, for: moderator_user}),
308 created_at: Utils.to_masto_date(notification.inserted_at),
309 report: ReportView.render("show.json", Report.extract_report_info(activity))
310 }
311
312 test_notifications_rendering([notification], moderator_user, [expected])
313 end
314
315 test "muted notification" do
316 user = insert(:user)
317 another_user = insert(:user)
318
319 {:ok, _} = Pleroma.UserRelationship.create_mute(user, another_user)
320 {:ok, create_activity} = CommonAPI.post(user, %{status: "hey"})
321 {:ok, favorite_activity} = CommonAPI.favorite(another_user, create_activity.id)
322 {:ok, [notification]} = Notification.create_notifications(favorite_activity)
323 create_activity = Activity.get_by_id(create_activity.id)
324
325 expected = %{
326 id: to_string(notification.id),
327 pleroma: %{is_seen: true, is_muted: true},
328 type: "favourite",
329 account: AccountView.render("show.json", %{user: another_user, for: user}),
330 status: StatusView.render("show.json", %{activity: create_activity, for: user}),
331 created_at: Utils.to_masto_date(notification.inserted_at)
332 }
333
334 test_notifications_rendering([notification], user, [expected])
335 end
336 end