1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MastodonAPI.NotificationControllerTest do
6 use Pleroma.Web.ConnCase
8 alias Pleroma.Notification
11 alias Pleroma.Web.CommonAPI
13 import Pleroma.Factory
15 test "does NOT render account/pleroma/relationship if this is disabled by default" do
16 clear_config([:extensions, :output_relationships_in_statuses_by_default], false)
18 %{user: user, conn: conn} = oauth_access(["read:notifications"])
19 other_user = insert(:user)
21 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
22 {:ok, [_notification]} = Notification.create_notifications(activity)
26 |> assign(:user, user)
27 |> get("/api/v1/notifications")
30 assert Enum.all?(response, fn n ->
31 get_in(n, ["account", "pleroma", "relationship"]) == %{}
35 test "list of notifications" do
36 %{user: user, conn: conn} = oauth_access(["read:notifications"])
37 other_user = insert(:user)
39 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
41 {:ok, [_notification]} = Notification.create_notifications(activity)
45 |> assign(:user, user)
46 |> get("/api/v1/notifications")
49 "hi <span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{user.id}\" href=\"#{
51 }\" rel=\"ugc\">@<span>#{user.nickname}</span></a></span>"
53 assert [%{"status" => %{"content" => response}} | _rest] = json_response(conn, 200)
54 assert response == expected_response
57 test "getting a single notification" do
58 %{user: user, conn: conn} = oauth_access(["read:notifications"])
59 other_user = insert(:user)
61 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
63 {:ok, [notification]} = Notification.create_notifications(activity)
65 conn = get(conn, "/api/v1/notifications/#{notification.id}")
68 "hi <span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{user.id}\" href=\"#{
70 }\" rel=\"ugc\">@<span>#{user.nickname}</span></a></span>"
72 assert %{"status" => %{"content" => response}} = json_response(conn, 200)
73 assert response == expected_response
76 test "dismissing a single notification (deprecated endpoint)" do
77 %{user: user, conn: conn} = oauth_access(["write:notifications"])
78 other_user = insert(:user)
80 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
82 {:ok, [notification]} = Notification.create_notifications(activity)
86 |> assign(:user, user)
87 |> post("/api/v1/notifications/dismiss", %{"id" => notification.id})
89 assert %{} = json_response(conn, 200)
92 test "dismissing a single notification" do
93 %{user: user, conn: conn} = oauth_access(["write:notifications"])
94 other_user = insert(:user)
96 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
98 {:ok, [notification]} = Notification.create_notifications(activity)
102 |> assign(:user, user)
103 |> post("/api/v1/notifications/#{notification.id}/dismiss")
105 assert %{} = json_response(conn, 200)
108 test "clearing all notifications" do
109 %{user: user, conn: conn} = oauth_access(["write:notifications", "read:notifications"])
110 other_user = insert(:user)
112 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
114 {:ok, [_notification]} = Notification.create_notifications(activity)
116 ret_conn = post(conn, "/api/v1/notifications/clear")
118 assert %{} = json_response(ret_conn, 200)
120 ret_conn = get(conn, "/api/v1/notifications")
122 assert all = json_response(ret_conn, 200)
126 test "paginates notifications using min_id, since_id, max_id, and limit" do
127 %{user: user, conn: conn} = oauth_access(["read:notifications"])
128 other_user = insert(:user)
130 {:ok, activity1} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
131 {:ok, activity2} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
132 {:ok, activity3} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
133 {:ok, activity4} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
135 notification1_id = get_notification_id_by_activity(activity1)
136 notification2_id = get_notification_id_by_activity(activity2)
137 notification3_id = get_notification_id_by_activity(activity3)
138 notification4_id = get_notification_id_by_activity(activity4)
140 conn = assign(conn, :user, user)
145 |> get("/api/v1/notifications?limit=2&min_id=#{notification1_id}")
146 |> json_response(:ok)
148 assert [%{"id" => ^notification3_id}, %{"id" => ^notification2_id}] = result
153 |> get("/api/v1/notifications?limit=2&since_id=#{notification1_id}")
154 |> json_response(:ok)
156 assert [%{"id" => ^notification4_id}, %{"id" => ^notification3_id}] = result
161 |> get("/api/v1/notifications?limit=2&max_id=#{notification4_id}")
162 |> json_response(:ok)
164 assert [%{"id" => ^notification3_id}, %{"id" => ^notification2_id}] = result
167 describe "exclude_visibilities" do
168 test "filters notifications for mentions" do
169 %{user: user, conn: conn} = oauth_access(["read:notifications"])
170 other_user = insert(:user)
172 {:ok, public_activity} =
173 CommonAPI.post(other_user, %{"status" => "@#{user.nickname}", "visibility" => "public"})
175 {:ok, direct_activity} =
176 CommonAPI.post(other_user, %{"status" => "@#{user.nickname}", "visibility" => "direct"})
178 {:ok, unlisted_activity} =
179 CommonAPI.post(other_user, %{"status" => "@#{user.nickname}", "visibility" => "unlisted"})
181 {:ok, private_activity} =
182 CommonAPI.post(other_user, %{"status" => "@#{user.nickname}", "visibility" => "private"})
185 get(conn, "/api/v1/notifications", %{
186 exclude_visibilities: ["public", "unlisted", "private"]
189 assert [%{"status" => %{"id" => id}}] = json_response(conn_res, 200)
190 assert id == direct_activity.id
193 get(conn, "/api/v1/notifications", %{
194 exclude_visibilities: ["public", "unlisted", "direct"]
197 assert [%{"status" => %{"id" => id}}] = json_response(conn_res, 200)
198 assert id == private_activity.id
201 get(conn, "/api/v1/notifications", %{
202 exclude_visibilities: ["public", "private", "direct"]
205 assert [%{"status" => %{"id" => id}}] = json_response(conn_res, 200)
206 assert id == unlisted_activity.id
209 get(conn, "/api/v1/notifications", %{
210 exclude_visibilities: ["unlisted", "private", "direct"]
213 assert [%{"status" => %{"id" => id}}] = json_response(conn_res, 200)
214 assert id == public_activity.id
217 test "filters notifications for Like activities" do
219 %{user: other_user, conn: conn} = oauth_access(["read:notifications"])
221 {:ok, public_activity} =
222 CommonAPI.post(other_user, %{"status" => ".", "visibility" => "public"})
224 {:ok, direct_activity} =
225 CommonAPI.post(other_user, %{"status" => "@#{user.nickname}", "visibility" => "direct"})
227 {:ok, unlisted_activity} =
228 CommonAPI.post(other_user, %{"status" => ".", "visibility" => "unlisted"})
230 {:ok, private_activity} =
231 CommonAPI.post(other_user, %{"status" => ".", "visibility" => "private"})
233 {:ok, _} = CommonAPI.favorite(user, public_activity.id)
234 {:ok, _} = CommonAPI.favorite(user, direct_activity.id)
235 {:ok, _} = CommonAPI.favorite(user, unlisted_activity.id)
236 {:ok, _} = CommonAPI.favorite(user, private_activity.id)
240 |> get("/api/v1/notifications", %{exclude_visibilities: ["direct"]})
241 |> json_response(200)
242 |> Enum.map(& &1["status"]["id"])
244 assert public_activity.id in activity_ids
245 assert unlisted_activity.id in activity_ids
246 assert private_activity.id in activity_ids
247 refute direct_activity.id in activity_ids
251 |> get("/api/v1/notifications", %{exclude_visibilities: ["unlisted"]})
252 |> json_response(200)
253 |> Enum.map(& &1["status"]["id"])
255 assert public_activity.id in activity_ids
256 refute unlisted_activity.id in activity_ids
257 assert private_activity.id in activity_ids
258 assert direct_activity.id in activity_ids
262 |> get("/api/v1/notifications", %{exclude_visibilities: ["private"]})
263 |> json_response(200)
264 |> Enum.map(& &1["status"]["id"])
266 assert public_activity.id in activity_ids
267 assert unlisted_activity.id in activity_ids
268 refute private_activity.id in activity_ids
269 assert direct_activity.id in activity_ids
273 |> get("/api/v1/notifications", %{exclude_visibilities: ["public"]})
274 |> json_response(200)
275 |> Enum.map(& &1["status"]["id"])
277 refute public_activity.id in activity_ids
278 assert unlisted_activity.id in activity_ids
279 assert private_activity.id in activity_ids
280 assert direct_activity.id in activity_ids
283 test "filters notifications for Announce activities" do
285 %{user: other_user, conn: conn} = oauth_access(["read:notifications"])
287 {:ok, public_activity} =
288 CommonAPI.post(other_user, %{"status" => ".", "visibility" => "public"})
290 {:ok, unlisted_activity} =
291 CommonAPI.post(other_user, %{"status" => ".", "visibility" => "unlisted"})
293 {:ok, _, _} = CommonAPI.repeat(public_activity.id, user)
294 {:ok, _, _} = CommonAPI.repeat(unlisted_activity.id, user)
298 |> get("/api/v1/notifications", %{exclude_visibilities: ["unlisted"]})
299 |> json_response(200)
300 |> Enum.map(& &1["status"]["id"])
302 assert public_activity.id in activity_ids
303 refute unlisted_activity.id in activity_ids
307 test "filters notifications using exclude_types" do
308 %{user: user, conn: conn} = oauth_access(["read:notifications"])
309 other_user = insert(:user)
311 {:ok, mention_activity} = CommonAPI.post(other_user, %{"status" => "hey @#{user.nickname}"})
312 {:ok, create_activity} = CommonAPI.post(user, %{"status" => "hey"})
313 {:ok, favorite_activity} = CommonAPI.favorite(other_user, create_activity.id)
314 {:ok, reblog_activity, _} = CommonAPI.repeat(create_activity.id, other_user)
315 {:ok, _, _, follow_activity} = CommonAPI.follow(other_user, user)
317 mention_notification_id = get_notification_id_by_activity(mention_activity)
318 favorite_notification_id = get_notification_id_by_activity(favorite_activity)
319 reblog_notification_id = get_notification_id_by_activity(reblog_activity)
320 follow_notification_id = get_notification_id_by_activity(follow_activity)
323 get(conn, "/api/v1/notifications", %{exclude_types: ["mention", "favourite", "reblog"]})
325 assert [%{"id" => ^follow_notification_id}] = json_response(conn_res, 200)
328 get(conn, "/api/v1/notifications", %{exclude_types: ["favourite", "reblog", "follow"]})
330 assert [%{"id" => ^mention_notification_id}] = json_response(conn_res, 200)
333 get(conn, "/api/v1/notifications", %{exclude_types: ["reblog", "follow", "mention"]})
335 assert [%{"id" => ^favorite_notification_id}] = json_response(conn_res, 200)
338 get(conn, "/api/v1/notifications", %{exclude_types: ["follow", "mention", "favourite"]})
340 assert [%{"id" => ^reblog_notification_id}] = json_response(conn_res, 200)
343 test "filters notifications using include_types" do
344 %{user: user, conn: conn} = oauth_access(["read:notifications"])
345 other_user = insert(:user)
347 {:ok, mention_activity} = CommonAPI.post(other_user, %{"status" => "hey @#{user.nickname}"})
348 {:ok, create_activity} = CommonAPI.post(user, %{"status" => "hey"})
349 {:ok, favorite_activity} = CommonAPI.favorite(other_user, create_activity.id)
350 {:ok, reblog_activity, _} = CommonAPI.repeat(create_activity.id, other_user)
351 {:ok, _, _, follow_activity} = CommonAPI.follow(other_user, user)
353 mention_notification_id = get_notification_id_by_activity(mention_activity)
354 favorite_notification_id = get_notification_id_by_activity(favorite_activity)
355 reblog_notification_id = get_notification_id_by_activity(reblog_activity)
356 follow_notification_id = get_notification_id_by_activity(follow_activity)
358 conn_res = get(conn, "/api/v1/notifications", %{include_types: ["follow"]})
360 assert [%{"id" => ^follow_notification_id}] = json_response(conn_res, 200)
362 conn_res = get(conn, "/api/v1/notifications", %{include_types: ["mention"]})
364 assert [%{"id" => ^mention_notification_id}] = json_response(conn_res, 200)
366 conn_res = get(conn, "/api/v1/notifications", %{include_types: ["favourite"]})
368 assert [%{"id" => ^favorite_notification_id}] = json_response(conn_res, 200)
370 conn_res = get(conn, "/api/v1/notifications", %{include_types: ["reblog"]})
372 assert [%{"id" => ^reblog_notification_id}] = json_response(conn_res, 200)
374 result = conn |> get("/api/v1/notifications") |> json_response(200)
376 assert length(result) == 4
380 |> get("/api/v1/notifications", %{
381 include_types: ["follow", "mention", "favourite", "reblog"]
383 |> json_response(200)
385 assert length(result) == 4
388 test "destroy multiple" do
389 %{user: user, conn: conn} = oauth_access(["read:notifications", "write:notifications"])
390 other_user = insert(:user)
392 {:ok, activity1} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
393 {:ok, activity2} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
394 {:ok, activity3} = CommonAPI.post(user, %{"status" => "hi @#{other_user.nickname}"})
395 {:ok, activity4} = CommonAPI.post(user, %{"status" => "hi @#{other_user.nickname}"})
397 notification1_id = get_notification_id_by_activity(activity1)
398 notification2_id = get_notification_id_by_activity(activity2)
399 notification3_id = get_notification_id_by_activity(activity3)
400 notification4_id = get_notification_id_by_activity(activity4)
404 |> get("/api/v1/notifications")
405 |> json_response(:ok)
407 assert [%{"id" => ^notification2_id}, %{"id" => ^notification1_id}] = result
411 |> assign(:user, other_user)
412 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["read:notifications"]))
416 |> get("/api/v1/notifications")
417 |> json_response(:ok)
419 assert [%{"id" => ^notification4_id}, %{"id" => ^notification3_id}] = result
423 |> delete("/api/v1/notifications/destroy_multiple", %{
424 "ids" => [notification1_id, notification2_id]
427 assert json_response(conn_destroy, 200) == %{}
431 |> get("/api/v1/notifications")
432 |> json_response(:ok)
434 assert [%{"id" => ^notification4_id}, %{"id" => ^notification3_id}] = result
437 test "doesn't see notifications after muting user with notifications" do
438 %{user: user, conn: conn} = oauth_access(["read:notifications"])
439 user2 = insert(:user)
441 {:ok, _, _, _} = CommonAPI.follow(user, user2)
442 {:ok, _} = CommonAPI.post(user2, %{"status" => "hey @#{user.nickname}"})
444 ret_conn = get(conn, "/api/v1/notifications")
446 assert length(json_response(ret_conn, 200)) == 1
448 {:ok, _user_relationships} = User.mute(user, user2)
450 conn = get(conn, "/api/v1/notifications")
452 assert json_response(conn, 200) == []
455 test "see notifications after muting user without notifications" do
456 %{user: user, conn: conn} = oauth_access(["read:notifications"])
457 user2 = insert(:user)
459 {:ok, _, _, _} = CommonAPI.follow(user, user2)
460 {:ok, _} = CommonAPI.post(user2, %{"status" => "hey @#{user.nickname}"})
462 ret_conn = get(conn, "/api/v1/notifications")
464 assert length(json_response(ret_conn, 200)) == 1
466 {:ok, _user_relationships} = User.mute(user, user2, false)
468 conn = get(conn, "/api/v1/notifications")
470 assert length(json_response(conn, 200)) == 1
473 test "see notifications after muting user with notifications and with_muted parameter" do
474 %{user: user, conn: conn} = oauth_access(["read:notifications"])
475 user2 = insert(:user)
477 {:ok, _, _, _} = CommonAPI.follow(user, user2)
478 {:ok, _} = CommonAPI.post(user2, %{"status" => "hey @#{user.nickname}"})
480 ret_conn = get(conn, "/api/v1/notifications")
482 assert length(json_response(ret_conn, 200)) == 1
484 {:ok, _user_relationships} = User.mute(user, user2)
486 conn = get(conn, "/api/v1/notifications", %{"with_muted" => "true"})
488 assert length(json_response(conn, 200)) == 1
491 @tag capture_log: true
492 test "see move notifications" do
493 old_user = insert(:user)
494 new_user = insert(:user, also_known_as: [old_user.ap_id])
495 %{user: follower, conn: conn} = oauth_access(["read:notifications"])
497 old_user_url = old_user.ap_id
500 File.read!("test/fixtures/users_mock/localhost.json")
501 |> String.replace("{{nickname}}", old_user.nickname)
505 %{method: :get, url: ^old_user_url} ->
506 %Tesla.Env{status: 200, body: body}
509 User.follow(follower, old_user)
510 Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
511 Pleroma.Tests.ObanHelpers.perform_all()
513 conn = get(conn, "/api/v1/notifications")
515 assert length(json_response(conn, 200)) == 1
518 describe "link headers" do
519 test "preserves parameters in link headers" do
520 %{user: user, conn: conn} = oauth_access(["read:notifications"])
521 other_user = insert(:user)
524 CommonAPI.post(other_user, %{
525 "status" => "hi @#{user.nickname}",
526 "visibility" => "public"
530 CommonAPI.post(other_user, %{
531 "status" => "hi @#{user.nickname}",
532 "visibility" => "public"
535 notification1 = Repo.get_by(Notification, activity_id: activity1.id)
536 notification2 = Repo.get_by(Notification, activity_id: activity2.id)
540 |> assign(:user, user)
541 |> get("/api/v1/notifications", %{media_only: true})
543 assert [link_header] = get_resp_header(conn, "link")
544 assert link_header =~ ~r/media_only=true/
545 assert link_header =~ ~r/min_id=#{notification2.id}/
546 assert link_header =~ ~r/max_id=#{notification1.id}/
550 describe "from specified user" do
552 %{user: user, conn: conn} = oauth_access(["read:notifications"])
554 %{id: account_id} = other_user1 = insert(:user)
555 other_user2 = insert(:user)
557 {:ok, _activity} = CommonAPI.post(other_user1, %{"status" => "hi @#{user.nickname}"})
558 {:ok, _activity} = CommonAPI.post(other_user2, %{"status" => "bye @#{user.nickname}"})
560 assert [%{"account" => %{"id" => ^account_id}}] =
562 |> assign(:user, user)
563 |> get("/api/v1/notifications", %{account_id: account_id})
564 |> json_response(200)
566 assert %{"error" => "Account is not found"} =
568 |> assign(:user, user)
569 |> get("/api/v1/notifications", %{account_id: "cofe"})
570 |> json_response(404)
574 defp get_notification_id_by_activity(%{id: id}) do
576 |> Repo.get_by(activity_id: id)