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 data-user=\"#{user.id}\" class=\"u-url mention\" 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 data-user=\"#{user.id}\" class=\"u-url mention\" 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" 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 "clearing all notifications" do
93 %{user: user, conn: conn} = oauth_access(["write:notifications", "read:notifications"])
94 other_user = insert(:user)
96 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
98 {:ok, [_notification]} = Notification.create_notifications(activity)
100 ret_conn = post(conn, "/api/v1/notifications/clear")
102 assert %{} = json_response(ret_conn, 200)
104 ret_conn = get(conn, "/api/v1/notifications")
106 assert all = json_response(ret_conn, 200)
110 test "paginates notifications using min_id, since_id, max_id, and limit" do
111 %{user: user, conn: conn} = oauth_access(["read:notifications"])
112 other_user = insert(:user)
114 {:ok, activity1} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
115 {:ok, activity2} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
116 {:ok, activity3} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
117 {:ok, activity4} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
119 notification1_id = get_notification_id_by_activity(activity1)
120 notification2_id = get_notification_id_by_activity(activity2)
121 notification3_id = get_notification_id_by_activity(activity3)
122 notification4_id = get_notification_id_by_activity(activity4)
124 conn = assign(conn, :user, user)
129 |> get("/api/v1/notifications?limit=2&min_id=#{notification1_id}")
130 |> json_response(:ok)
132 assert [%{"id" => ^notification3_id}, %{"id" => ^notification2_id}] = result
137 |> get("/api/v1/notifications?limit=2&since_id=#{notification1_id}")
138 |> json_response(:ok)
140 assert [%{"id" => ^notification4_id}, %{"id" => ^notification3_id}] = result
145 |> get("/api/v1/notifications?limit=2&max_id=#{notification4_id}")
146 |> json_response(:ok)
148 assert [%{"id" => ^notification3_id}, %{"id" => ^notification2_id}] = result
151 describe "exclude_visibilities" do
152 test "filters notifications for mentions" do
153 %{user: user, conn: conn} = oauth_access(["read:notifications"])
154 other_user = insert(:user)
156 {:ok, public_activity} =
157 CommonAPI.post(other_user, %{"status" => "@#{user.nickname}", "visibility" => "public"})
159 {:ok, direct_activity} =
160 CommonAPI.post(other_user, %{"status" => "@#{user.nickname}", "visibility" => "direct"})
162 {:ok, unlisted_activity} =
163 CommonAPI.post(other_user, %{"status" => "@#{user.nickname}", "visibility" => "unlisted"})
165 {:ok, private_activity} =
166 CommonAPI.post(other_user, %{"status" => "@#{user.nickname}", "visibility" => "private"})
169 get(conn, "/api/v1/notifications", %{
170 exclude_visibilities: ["public", "unlisted", "private"]
173 assert [%{"status" => %{"id" => id}}] = json_response(conn_res, 200)
174 assert id == direct_activity.id
177 get(conn, "/api/v1/notifications", %{
178 exclude_visibilities: ["public", "unlisted", "direct"]
181 assert [%{"status" => %{"id" => id}}] = json_response(conn_res, 200)
182 assert id == private_activity.id
185 get(conn, "/api/v1/notifications", %{
186 exclude_visibilities: ["public", "private", "direct"]
189 assert [%{"status" => %{"id" => id}}] = json_response(conn_res, 200)
190 assert id == unlisted_activity.id
193 get(conn, "/api/v1/notifications", %{
194 exclude_visibilities: ["unlisted", "private", "direct"]
197 assert [%{"status" => %{"id" => id}}] = json_response(conn_res, 200)
198 assert id == public_activity.id
201 test "filters notifications for Like activities" do
203 %{user: other_user, conn: conn} = oauth_access(["read:notifications"])
205 {:ok, public_activity} =
206 CommonAPI.post(other_user, %{"status" => ".", "visibility" => "public"})
208 {:ok, direct_activity} =
209 CommonAPI.post(other_user, %{"status" => "@#{user.nickname}", "visibility" => "direct"})
211 {:ok, unlisted_activity} =
212 CommonAPI.post(other_user, %{"status" => ".", "visibility" => "unlisted"})
214 {:ok, private_activity} =
215 CommonAPI.post(other_user, %{"status" => ".", "visibility" => "private"})
217 {:ok, _, _} = CommonAPI.favorite(public_activity.id, user)
218 {:ok, _, _} = CommonAPI.favorite(direct_activity.id, user)
219 {:ok, _, _} = CommonAPI.favorite(unlisted_activity.id, user)
220 {:ok, _, _} = CommonAPI.favorite(private_activity.id, user)
224 |> get("/api/v1/notifications", %{exclude_visibilities: ["direct"]})
225 |> json_response(200)
226 |> Enum.map(& &1["status"]["id"])
228 assert public_activity.id in activity_ids
229 assert unlisted_activity.id in activity_ids
230 assert private_activity.id in activity_ids
231 refute direct_activity.id in activity_ids
235 |> get("/api/v1/notifications", %{exclude_visibilities: ["unlisted"]})
236 |> json_response(200)
237 |> Enum.map(& &1["status"]["id"])
239 assert public_activity.id in activity_ids
240 refute unlisted_activity.id in activity_ids
241 assert private_activity.id in activity_ids
242 assert direct_activity.id in activity_ids
246 |> get("/api/v1/notifications", %{exclude_visibilities: ["private"]})
247 |> json_response(200)
248 |> Enum.map(& &1["status"]["id"])
250 assert public_activity.id in activity_ids
251 assert unlisted_activity.id in activity_ids
252 refute private_activity.id in activity_ids
253 assert direct_activity.id in activity_ids
257 |> get("/api/v1/notifications", %{exclude_visibilities: ["public"]})
258 |> json_response(200)
259 |> Enum.map(& &1["status"]["id"])
261 refute public_activity.id in activity_ids
262 assert unlisted_activity.id in activity_ids
263 assert private_activity.id in activity_ids
264 assert direct_activity.id in activity_ids
267 test "filters notifications for Announce activities" do
269 %{user: other_user, conn: conn} = oauth_access(["read:notifications"])
271 {:ok, public_activity} =
272 CommonAPI.post(other_user, %{"status" => ".", "visibility" => "public"})
274 {:ok, unlisted_activity} =
275 CommonAPI.post(other_user, %{"status" => ".", "visibility" => "unlisted"})
277 {:ok, _, _} = CommonAPI.repeat(public_activity.id, user)
278 {:ok, _, _} = CommonAPI.repeat(unlisted_activity.id, user)
282 |> get("/api/v1/notifications", %{exclude_visibilities: ["unlisted"]})
283 |> json_response(200)
284 |> Enum.map(& &1["status"]["id"])
286 assert public_activity.id in activity_ids
287 refute unlisted_activity.id in activity_ids
291 test "filters notifications using exclude_types" do
292 %{user: user, conn: conn} = oauth_access(["read:notifications"])
293 other_user = insert(:user)
295 {:ok, mention_activity} = CommonAPI.post(other_user, %{"status" => "hey @#{user.nickname}"})
296 {:ok, create_activity} = CommonAPI.post(user, %{"status" => "hey"})
297 {:ok, favorite_activity, _} = CommonAPI.favorite(create_activity.id, other_user)
298 {:ok, reblog_activity, _} = CommonAPI.repeat(create_activity.id, other_user)
299 {:ok, _, _, follow_activity} = CommonAPI.follow(other_user, user)
301 mention_notification_id = get_notification_id_by_activity(mention_activity)
302 favorite_notification_id = get_notification_id_by_activity(favorite_activity)
303 reblog_notification_id = get_notification_id_by_activity(reblog_activity)
304 follow_notification_id = get_notification_id_by_activity(follow_activity)
307 get(conn, "/api/v1/notifications", %{exclude_types: ["mention", "favourite", "reblog"]})
309 assert [%{"id" => ^follow_notification_id}] = json_response(conn_res, 200)
312 get(conn, "/api/v1/notifications", %{exclude_types: ["favourite", "reblog", "follow"]})
314 assert [%{"id" => ^mention_notification_id}] = json_response(conn_res, 200)
317 get(conn, "/api/v1/notifications", %{exclude_types: ["reblog", "follow", "mention"]})
319 assert [%{"id" => ^favorite_notification_id}] = json_response(conn_res, 200)
322 get(conn, "/api/v1/notifications", %{exclude_types: ["follow", "mention", "favourite"]})
324 assert [%{"id" => ^reblog_notification_id}] = json_response(conn_res, 200)
327 test "filters notifications using include_types" do
328 %{user: user, conn: conn} = oauth_access(["read:notifications"])
329 other_user = insert(:user)
331 {:ok, mention_activity} = CommonAPI.post(other_user, %{"status" => "hey @#{user.nickname}"})
332 {:ok, create_activity} = CommonAPI.post(user, %{"status" => "hey"})
333 {:ok, favorite_activity, _} = CommonAPI.favorite(create_activity.id, other_user)
334 {:ok, reblog_activity, _} = CommonAPI.repeat(create_activity.id, other_user)
335 {:ok, _, _, follow_activity} = CommonAPI.follow(other_user, user)
337 mention_notification_id = get_notification_id_by_activity(mention_activity)
338 favorite_notification_id = get_notification_id_by_activity(favorite_activity)
339 reblog_notification_id = get_notification_id_by_activity(reblog_activity)
340 follow_notification_id = get_notification_id_by_activity(follow_activity)
342 conn_res = get(conn, "/api/v1/notifications", %{include_types: ["follow"]})
344 assert [%{"id" => ^follow_notification_id}] = json_response(conn_res, 200)
346 conn_res = get(conn, "/api/v1/notifications", %{include_types: ["mention"]})
348 assert [%{"id" => ^mention_notification_id}] = json_response(conn_res, 200)
350 conn_res = get(conn, "/api/v1/notifications", %{include_types: ["favourite"]})
352 assert [%{"id" => ^favorite_notification_id}] = json_response(conn_res, 200)
354 conn_res = get(conn, "/api/v1/notifications", %{include_types: ["reblog"]})
356 assert [%{"id" => ^reblog_notification_id}] = json_response(conn_res, 200)
358 result = conn |> get("/api/v1/notifications") |> json_response(200)
360 assert length(result) == 4
364 |> get("/api/v1/notifications", %{
365 include_types: ["follow", "mention", "favourite", "reblog"]
367 |> json_response(200)
369 assert length(result) == 4
372 test "destroy multiple" do
373 %{user: user, conn: conn} = oauth_access(["read:notifications", "write:notifications"])
374 other_user = insert(:user)
376 {:ok, activity1} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
377 {:ok, activity2} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
378 {:ok, activity3} = CommonAPI.post(user, %{"status" => "hi @#{other_user.nickname}"})
379 {:ok, activity4} = CommonAPI.post(user, %{"status" => "hi @#{other_user.nickname}"})
381 notification1_id = get_notification_id_by_activity(activity1)
382 notification2_id = get_notification_id_by_activity(activity2)
383 notification3_id = get_notification_id_by_activity(activity3)
384 notification4_id = get_notification_id_by_activity(activity4)
388 |> get("/api/v1/notifications")
389 |> json_response(:ok)
391 assert [%{"id" => ^notification2_id}, %{"id" => ^notification1_id}] = result
395 |> assign(:user, other_user)
396 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["read:notifications"]))
400 |> get("/api/v1/notifications")
401 |> json_response(:ok)
403 assert [%{"id" => ^notification4_id}, %{"id" => ^notification3_id}] = result
407 |> delete("/api/v1/notifications/destroy_multiple", %{
408 "ids" => [notification1_id, notification2_id]
411 assert json_response(conn_destroy, 200) == %{}
415 |> get("/api/v1/notifications")
416 |> json_response(:ok)
418 assert [%{"id" => ^notification4_id}, %{"id" => ^notification3_id}] = result
421 test "doesn't see notifications after muting user with notifications" do
422 %{user: user, conn: conn} = oauth_access(["read:notifications"])
423 user2 = insert(:user)
425 {:ok, _, _, _} = CommonAPI.follow(user, user2)
426 {:ok, _} = CommonAPI.post(user2, %{"status" => "hey @#{user.nickname}"})
428 ret_conn = get(conn, "/api/v1/notifications")
430 assert length(json_response(ret_conn, 200)) == 1
432 {:ok, _user_relationships} = User.mute(user, user2)
434 conn = get(conn, "/api/v1/notifications")
436 assert json_response(conn, 200) == []
439 test "see notifications after muting user without notifications" do
440 %{user: user, conn: conn} = oauth_access(["read:notifications"])
441 user2 = insert(:user)
443 {:ok, _, _, _} = CommonAPI.follow(user, user2)
444 {:ok, _} = CommonAPI.post(user2, %{"status" => "hey @#{user.nickname}"})
446 ret_conn = get(conn, "/api/v1/notifications")
448 assert length(json_response(ret_conn, 200)) == 1
450 {:ok, _user_relationships} = User.mute(user, user2, false)
452 conn = get(conn, "/api/v1/notifications")
454 assert length(json_response(conn, 200)) == 1
457 test "see notifications after muting user with notifications and with_muted parameter" do
458 %{user: user, conn: conn} = oauth_access(["read:notifications"])
459 user2 = insert(:user)
461 {:ok, _, _, _} = CommonAPI.follow(user, user2)
462 {:ok, _} = CommonAPI.post(user2, %{"status" => "hey @#{user.nickname}"})
464 ret_conn = get(conn, "/api/v1/notifications")
466 assert length(json_response(ret_conn, 200)) == 1
468 {:ok, _user_relationships} = User.mute(user, user2)
470 conn = get(conn, "/api/v1/notifications", %{"with_muted" => "true"})
472 assert length(json_response(conn, 200)) == 1
475 test "see move notifications" do
476 old_user = insert(:user)
477 new_user = insert(:user, also_known_as: [old_user.ap_id])
478 %{user: follower, conn: conn} = oauth_access(["read:notifications"])
480 User.follow(follower, old_user)
481 Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
482 Pleroma.Tests.ObanHelpers.perform_all()
484 conn = get(conn, "/api/v1/notifications")
486 assert length(json_response(conn, 200)) == 1
489 describe "link headers" do
490 test "preserves parameters in link headers" do
491 %{user: user, conn: conn} = oauth_access(["read:notifications"])
492 other_user = insert(:user)
495 CommonAPI.post(other_user, %{
496 "status" => "hi @#{user.nickname}",
497 "visibility" => "public"
501 CommonAPI.post(other_user, %{
502 "status" => "hi @#{user.nickname}",
503 "visibility" => "public"
506 notification1 = Repo.get_by(Notification, activity_id: activity1.id)
507 notification2 = Repo.get_by(Notification, activity_id: activity2.id)
511 |> assign(:user, user)
512 |> get("/api/v1/notifications", %{media_only: true})
514 assert [link_header] = get_resp_header(conn, "link")
515 assert link_header =~ ~r/media_only=true/
516 assert link_header =~ ~r/min_id=#{notification2.id}/
517 assert link_header =~ ~r/max_id=#{notification1.id}/
521 describe "from specified user" do
523 %{user: user, conn: conn} = oauth_access(["read:notifications"])
525 %{id: account_id} = other_user1 = insert(:user)
526 other_user2 = insert(:user)
528 {:ok, _activity} = CommonAPI.post(other_user1, %{"status" => "hi @#{user.nickname}"})
529 {:ok, _activity} = CommonAPI.post(other_user2, %{"status" => "bye @#{user.nickname}"})
531 assert [%{"account" => %{"id" => ^account_id}}] =
533 |> assign(:user, user)
534 |> get("/api/v1/notifications", %{account_id: account_id})
535 |> json_response(200)
537 assert %{"error" => "Account is not found"} =
539 |> assign(:user, user)
540 |> get("/api/v1/notifications", %{account_id: "cofe"})
541 |> json_response(404)
545 defp get_notification_id_by_activity(%{id: id}) do
547 |> Repo.get_by(activity_id: id)