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