1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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 "list of notifications", %{conn: conn} do
17 other_user = insert(:user)
19 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
21 {:ok, [_notification]} = Notification.create_notifications(activity)
25 |> assign(:user, user)
26 |> get("/api/v1/notifications")
29 "hi <span class=\"h-card\"><a data-user=\"#{user.id}\" class=\"u-url mention\" href=\"#{
31 }\" rel=\"ugc\">@<span>#{user.nickname}</span></a></span>"
33 assert [%{"status" => %{"content" => response}} | _rest] = json_response(conn, 200)
34 assert response == expected_response
37 test "getting a single notification", %{conn: conn} do
39 other_user = insert(:user)
41 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
43 {:ok, [notification]} = Notification.create_notifications(activity)
47 |> assign(:user, user)
48 |> get("/api/v1/notifications/#{notification.id}")
51 "hi <span class=\"h-card\"><a data-user=\"#{user.id}\" class=\"u-url mention\" href=\"#{
53 }\" rel=\"ugc\">@<span>#{user.nickname}</span></a></span>"
55 assert %{"status" => %{"content" => response}} = json_response(conn, 200)
56 assert response == expected_response
59 test "dismissing a single notification", %{conn: conn} do
61 other_user = insert(:user)
63 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
65 {:ok, [notification]} = Notification.create_notifications(activity)
69 |> assign(:user, user)
70 |> post("/api/v1/notifications/dismiss", %{"id" => notification.id})
72 assert %{} = json_response(conn, 200)
75 test "clearing all notifications", %{conn: conn} do
77 other_user = insert(:user)
79 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
81 {:ok, [_notification]} = Notification.create_notifications(activity)
85 |> assign(:user, user)
86 |> post("/api/v1/notifications/clear")
88 assert %{} = json_response(conn, 200)
92 |> assign(:user, user)
93 |> get("/api/v1/notifications")
95 assert all = json_response(conn, 200)
99 test "paginates notifications using min_id, since_id, max_id, and limit", %{conn: conn} do
101 other_user = insert(:user)
103 {:ok, activity1} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
104 {:ok, activity2} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
105 {:ok, activity3} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
106 {:ok, activity4} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
108 notification1_id = get_notification_id_by_activity(activity1)
109 notification2_id = get_notification_id_by_activity(activity2)
110 notification3_id = get_notification_id_by_activity(activity3)
111 notification4_id = get_notification_id_by_activity(activity4)
113 conn = assign(conn, :user, user)
118 |> get("/api/v1/notifications?limit=2&min_id=#{notification1_id}")
119 |> json_response(:ok)
121 assert [%{"id" => ^notification3_id}, %{"id" => ^notification2_id}] = result
126 |> get("/api/v1/notifications?limit=2&since_id=#{notification1_id}")
127 |> json_response(:ok)
129 assert [%{"id" => ^notification4_id}, %{"id" => ^notification3_id}] = result
134 |> get("/api/v1/notifications?limit=2&max_id=#{notification4_id}")
135 |> json_response(:ok)
137 assert [%{"id" => ^notification3_id}, %{"id" => ^notification2_id}] = result
140 test "filters notifications using exclude_visibilities", %{conn: conn} do
142 other_user = insert(:user)
144 {:ok, public_activity} =
145 CommonAPI.post(other_user, %{"status" => "@#{user.nickname}", "visibility" => "public"})
147 {:ok, direct_activity} =
148 CommonAPI.post(other_user, %{"status" => "@#{user.nickname}", "visibility" => "direct"})
150 {:ok, unlisted_activity} =
151 CommonAPI.post(other_user, %{"status" => "@#{user.nickname}", "visibility" => "unlisted"})
153 {:ok, private_activity} =
154 CommonAPI.post(other_user, %{"status" => "@#{user.nickname}", "visibility" => "private"})
156 conn = assign(conn, :user, user)
159 get(conn, "/api/v1/notifications", %{
160 exclude_visibilities: ["public", "unlisted", "private"]
163 assert [%{"status" => %{"id" => id}}] = json_response(conn_res, 200)
164 assert id == direct_activity.id
167 get(conn, "/api/v1/notifications", %{
168 exclude_visibilities: ["public", "unlisted", "direct"]
171 assert [%{"status" => %{"id" => id}}] = json_response(conn_res, 200)
172 assert id == private_activity.id
175 get(conn, "/api/v1/notifications", %{
176 exclude_visibilities: ["public", "private", "direct"]
179 assert [%{"status" => %{"id" => id}}] = json_response(conn_res, 200)
180 assert id == unlisted_activity.id
183 get(conn, "/api/v1/notifications", %{
184 exclude_visibilities: ["unlisted", "private", "direct"]
187 assert [%{"status" => %{"id" => id}}] = json_response(conn_res, 200)
188 assert id == public_activity.id
191 test "filters notifications using exclude_types", %{conn: conn} do
193 other_user = insert(:user)
195 {:ok, mention_activity} = CommonAPI.post(other_user, %{"status" => "hey @#{user.nickname}"})
196 {:ok, create_activity} = CommonAPI.post(user, %{"status" => "hey"})
197 {:ok, favorite_activity, _} = CommonAPI.favorite(create_activity.id, other_user)
198 {:ok, reblog_activity, _} = CommonAPI.repeat(create_activity.id, other_user)
199 {:ok, _, _, follow_activity} = CommonAPI.follow(other_user, user)
201 mention_notification_id = get_notification_id_by_activity(mention_activity)
202 favorite_notification_id = get_notification_id_by_activity(favorite_activity)
203 reblog_notification_id = get_notification_id_by_activity(reblog_activity)
204 follow_notification_id = get_notification_id_by_activity(follow_activity)
206 conn = assign(conn, :user, user)
209 get(conn, "/api/v1/notifications", %{exclude_types: ["mention", "favourite", "reblog"]})
211 assert [%{"id" => ^follow_notification_id}] = json_response(conn_res, 200)
214 get(conn, "/api/v1/notifications", %{exclude_types: ["favourite", "reblog", "follow"]})
216 assert [%{"id" => ^mention_notification_id}] = json_response(conn_res, 200)
219 get(conn, "/api/v1/notifications", %{exclude_types: ["reblog", "follow", "mention"]})
221 assert [%{"id" => ^favorite_notification_id}] = json_response(conn_res, 200)
224 get(conn, "/api/v1/notifications", %{exclude_types: ["follow", "mention", "favourite"]})
226 assert [%{"id" => ^reblog_notification_id}] = json_response(conn_res, 200)
229 test "destroy multiple", %{conn: conn} do
231 other_user = insert(:user)
233 {:ok, activity1} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
234 {:ok, activity2} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"})
235 {:ok, activity3} = CommonAPI.post(user, %{"status" => "hi @#{other_user.nickname}"})
236 {:ok, activity4} = CommonAPI.post(user, %{"status" => "hi @#{other_user.nickname}"})
238 notification1_id = get_notification_id_by_activity(activity1)
239 notification2_id = get_notification_id_by_activity(activity2)
240 notification3_id = get_notification_id_by_activity(activity3)
241 notification4_id = get_notification_id_by_activity(activity4)
243 conn = assign(conn, :user, user)
247 |> get("/api/v1/notifications")
248 |> json_response(:ok)
250 assert [%{"id" => ^notification2_id}, %{"id" => ^notification1_id}] = result
254 |> assign(:user, other_user)
258 |> get("/api/v1/notifications")
259 |> json_response(:ok)
261 assert [%{"id" => ^notification4_id}, %{"id" => ^notification3_id}] = result
265 |> delete("/api/v1/notifications/destroy_multiple", %{
266 "ids" => [notification1_id, notification2_id]
269 assert json_response(conn_destroy, 200) == %{}
273 |> get("/api/v1/notifications")
274 |> json_response(:ok)
276 assert [%{"id" => ^notification4_id}, %{"id" => ^notification3_id}] = result
279 test "doesn't see notifications after muting user with notifications", %{conn: conn} do
281 user2 = insert(:user)
283 {:ok, _, _, _} = CommonAPI.follow(user, user2)
284 {:ok, _} = CommonAPI.post(user2, %{"status" => "hey @#{user.nickname}"})
286 conn = assign(conn, :user, user)
288 conn = get(conn, "/api/v1/notifications")
290 assert length(json_response(conn, 200)) == 1
292 {:ok, user} = User.mute(user, user2)
294 conn = assign(build_conn(), :user, user)
295 conn = get(conn, "/api/v1/notifications")
297 assert json_response(conn, 200) == []
300 test "see notifications after muting user without notifications", %{conn: conn} do
302 user2 = insert(:user)
304 {:ok, _, _, _} = CommonAPI.follow(user, user2)
305 {:ok, _} = CommonAPI.post(user2, %{"status" => "hey @#{user.nickname}"})
307 conn = assign(conn, :user, user)
309 conn = get(conn, "/api/v1/notifications")
311 assert length(json_response(conn, 200)) == 1
313 {:ok, user} = User.mute(user, user2, false)
315 conn = assign(build_conn(), :user, user)
316 conn = get(conn, "/api/v1/notifications")
318 assert length(json_response(conn, 200)) == 1
321 test "see notifications after muting user with notifications and with_muted parameter", %{
325 user2 = insert(:user)
327 {:ok, _, _, _} = CommonAPI.follow(user, user2)
328 {:ok, _} = CommonAPI.post(user2, %{"status" => "hey @#{user.nickname}"})
330 conn = assign(conn, :user, user)
332 conn = get(conn, "/api/v1/notifications")
334 assert length(json_response(conn, 200)) == 1
336 {:ok, user} = User.mute(user, user2)
338 conn = assign(build_conn(), :user, user)
339 conn = get(conn, "/api/v1/notifications", %{"with_muted" => "true"})
341 assert length(json_response(conn, 200)) == 1
344 defp get_notification_id_by_activity(%{id: id}) do
346 |> Repo.get_by(activity_id: id)