2019-09-24 08:16:44 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-13 06:49:20 +00:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2019-09-24 08:16:44 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.MastodonAPI.NotificationControllerTest do
|
|
|
|
use Pleroma.Web.ConnCase
|
|
|
|
|
|
|
|
alias Pleroma.Notification
|
|
|
|
alias Pleroma.Repo
|
|
|
|
alias Pleroma.User
|
|
|
|
alias Pleroma.Web.CommonAPI
|
|
|
|
|
|
|
|
import Pleroma.Factory
|
|
|
|
|
2020-05-01 15:45:24 +00:00
|
|
|
test "does NOT render account/pleroma/relationship by default" do
|
2020-04-01 16:49:09 +00:00
|
|
|
%{user: user, conn: conn} = oauth_access(["read:notifications"])
|
|
|
|
other_user = insert(:user)
|
|
|
|
|
2020-05-12 19:59:26 +00:00
|
|
|
{:ok, activity} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
|
2020-04-01 16:49:09 +00:00
|
|
|
{:ok, [_notification]} = Notification.create_notifications(activity)
|
|
|
|
|
|
|
|
response =
|
|
|
|
conn
|
|
|
|
|> assign(:user, user)
|
|
|
|
|> get("/api/v1/notifications")
|
2020-04-28 17:27:54 +00:00
|
|
|
|> json_response_and_validate_schema(200)
|
2020-04-01 16:49:09 +00:00
|
|
|
|
|
|
|
assert Enum.all?(response, fn n ->
|
|
|
|
get_in(n, ["account", "pleroma", "relationship"]) == %{}
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2019-12-19 14:23:27 +00:00
|
|
|
test "list of notifications" do
|
|
|
|
%{user: user, conn: conn} = oauth_access(["read:notifications"])
|
2019-09-24 08:16:44 +00:00
|
|
|
other_user = insert(:user)
|
|
|
|
|
2020-05-12 19:59:26 +00:00
|
|
|
{:ok, activity} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
|
2019-09-24 08:16:44 +00:00
|
|
|
|
|
|
|
{:ok, [_notification]} = Notification.create_notifications(activity)
|
|
|
|
|
|
|
|
conn =
|
|
|
|
conn
|
|
|
|
|> assign(:user, user)
|
|
|
|
|> get("/api/v1/notifications")
|
|
|
|
|
|
|
|
expected_response =
|
2021-10-06 06:08:21 +00:00
|
|
|
"hi <span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{user.id}\" href=\"#{user.ap_id}\" rel=\"ugc\">@<span>#{user.nickname}</span></a></span>"
|
2019-09-24 08:16:44 +00:00
|
|
|
|
2020-04-28 17:27:54 +00:00
|
|
|
assert [%{"status" => %{"content" => response}} | _rest] =
|
|
|
|
json_response_and_validate_schema(conn, 200)
|
|
|
|
|
2019-09-24 08:16:44 +00:00
|
|
|
assert response == expected_response
|
|
|
|
end
|
|
|
|
|
2020-06-02 12:05:53 +00:00
|
|
|
test "by default, does not contain pleroma:chat_mention" do
|
|
|
|
%{user: user, conn: conn} = oauth_access(["read:notifications"])
|
|
|
|
other_user = insert(:user)
|
|
|
|
|
|
|
|
{:ok, _activity} = CommonAPI.post_chat_message(other_user, user, "hey")
|
|
|
|
|
|
|
|
result =
|
|
|
|
conn
|
|
|
|
|> get("/api/v1/notifications")
|
|
|
|
|> json_response_and_validate_schema(200)
|
|
|
|
|
|
|
|
assert [] == result
|
|
|
|
|
|
|
|
result =
|
|
|
|
conn
|
|
|
|
|> get("/api/v1/notifications?include_types[]=pleroma:chat_mention")
|
|
|
|
|> json_response_and_validate_schema(200)
|
|
|
|
|
|
|
|
assert [_] = result
|
|
|
|
end
|
|
|
|
|
2020-11-13 13:35:46 +00:00
|
|
|
test "by default, does not contain pleroma:report" do
|
|
|
|
%{user: user, conn: conn} = oauth_access(["read:notifications"])
|
|
|
|
other_user = insert(:user)
|
|
|
|
third_user = insert(:user)
|
|
|
|
|
|
|
|
user
|
|
|
|
|> User.admin_api_update(%{is_moderator: true})
|
|
|
|
|
|
|
|
{:ok, activity} = CommonAPI.post(other_user, %{status: "hey"})
|
|
|
|
|
|
|
|
{:ok, _report} =
|
|
|
|
CommonAPI.report(third_user, %{account_id: other_user.id, status_ids: [activity.id]})
|
|
|
|
|
|
|
|
result =
|
|
|
|
conn
|
|
|
|
|> get("/api/v1/notifications")
|
|
|
|
|> json_response_and_validate_schema(200)
|
|
|
|
|
|
|
|
assert [] == result
|
|
|
|
|
|
|
|
result =
|
|
|
|
conn
|
|
|
|
|> get("/api/v1/notifications?include_types[]=pleroma:report")
|
|
|
|
|> json_response_and_validate_schema(200)
|
|
|
|
|
|
|
|
assert [_] = result
|
|
|
|
end
|
|
|
|
|
2019-12-19 14:23:27 +00:00
|
|
|
test "getting a single notification" do
|
|
|
|
%{user: user, conn: conn} = oauth_access(["read:notifications"])
|
2019-09-24 08:16:44 +00:00
|
|
|
other_user = insert(:user)
|
|
|
|
|
2020-05-12 19:59:26 +00:00
|
|
|
{:ok, activity} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
|
2019-09-24 08:16:44 +00:00
|
|
|
|
|
|
|
{:ok, [notification]} = Notification.create_notifications(activity)
|
|
|
|
|
2019-12-19 14:23:27 +00:00
|
|
|
conn = get(conn, "/api/v1/notifications/#{notification.id}")
|
2019-09-24 08:16:44 +00:00
|
|
|
|
|
|
|
expected_response =
|
2021-10-06 06:08:21 +00:00
|
|
|
"hi <span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{user.id}\" href=\"#{user.ap_id}\" rel=\"ugc\">@<span>#{user.nickname}</span></a></span>"
|
2019-09-24 08:16:44 +00:00
|
|
|
|
2020-04-28 17:27:54 +00:00
|
|
|
assert %{"status" => %{"content" => response}} = json_response_and_validate_schema(conn, 200)
|
2019-09-24 08:16:44 +00:00
|
|
|
assert response == expected_response
|
|
|
|
end
|
|
|
|
|
2020-04-09 13:08:43 +00:00
|
|
|
test "dismissing a single notification (deprecated endpoint)" do
|
2019-12-19 14:23:27 +00:00
|
|
|
%{user: user, conn: conn} = oauth_access(["write:notifications"])
|
2019-09-24 08:16:44 +00:00
|
|
|
other_user = insert(:user)
|
|
|
|
|
2020-05-12 19:59:26 +00:00
|
|
|
{:ok, activity} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
|
2019-09-24 08:16:44 +00:00
|
|
|
|
|
|
|
{:ok, [notification]} = Notification.create_notifications(activity)
|
|
|
|
|
|
|
|
conn =
|
|
|
|
conn
|
|
|
|
|> assign(:user, user)
|
2020-04-28 17:27:54 +00:00
|
|
|
|> put_req_header("content-type", "application/json")
|
|
|
|
|> post("/api/v1/notifications/dismiss", %{"id" => to_string(notification.id)})
|
2019-09-24 08:16:44 +00:00
|
|
|
|
2020-04-28 17:27:54 +00:00
|
|
|
assert %{} = json_response_and_validate_schema(conn, 200)
|
2019-09-24 08:16:44 +00:00
|
|
|
end
|
|
|
|
|
2020-04-09 13:08:43 +00:00
|
|
|
test "dismissing a single notification" do
|
|
|
|
%{user: user, conn: conn} = oauth_access(["write:notifications"])
|
|
|
|
other_user = insert(:user)
|
|
|
|
|
2020-05-12 19:59:26 +00:00
|
|
|
{:ok, activity} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
|
2020-04-09 13:08:43 +00:00
|
|
|
|
|
|
|
{:ok, [notification]} = Notification.create_notifications(activity)
|
|
|
|
|
|
|
|
conn =
|
|
|
|
conn
|
|
|
|
|> assign(:user, user)
|
|
|
|
|> post("/api/v1/notifications/#{notification.id}/dismiss")
|
|
|
|
|
2020-04-28 17:27:54 +00:00
|
|
|
assert %{} = json_response_and_validate_schema(conn, 200)
|
2020-04-09 13:08:43 +00:00
|
|
|
end
|
|
|
|
|
2019-12-19 14:23:27 +00:00
|
|
|
test "clearing all notifications" do
|
|
|
|
%{user: user, conn: conn} = oauth_access(["write:notifications", "read:notifications"])
|
2019-09-24 08:16:44 +00:00
|
|
|
other_user = insert(:user)
|
|
|
|
|
2020-05-12 19:59:26 +00:00
|
|
|
{:ok, activity} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
|
2019-09-24 08:16:44 +00:00
|
|
|
|
|
|
|
{:ok, [_notification]} = Notification.create_notifications(activity)
|
|
|
|
|
2019-12-19 14:23:27 +00:00
|
|
|
ret_conn = post(conn, "/api/v1/notifications/clear")
|
2019-09-24 08:16:44 +00:00
|
|
|
|
2020-04-28 17:27:54 +00:00
|
|
|
assert %{} = json_response_and_validate_schema(ret_conn, 200)
|
2019-09-24 08:16:44 +00:00
|
|
|
|
2019-12-19 14:23:27 +00:00
|
|
|
ret_conn = get(conn, "/api/v1/notifications")
|
2019-09-24 08:16:44 +00:00
|
|
|
|
2020-04-28 17:27:54 +00:00
|
|
|
assert all = json_response_and_validate_schema(ret_conn, 200)
|
2019-09-24 08:16:44 +00:00
|
|
|
assert all == []
|
|
|
|
end
|
|
|
|
|
2019-12-19 14:23:27 +00:00
|
|
|
test "paginates notifications using min_id, since_id, max_id, and limit" do
|
|
|
|
%{user: user, conn: conn} = oauth_access(["read:notifications"])
|
2019-09-24 08:16:44 +00:00
|
|
|
other_user = insert(:user)
|
|
|
|
|
2020-05-12 19:59:26 +00:00
|
|
|
{:ok, activity1} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
|
|
|
|
{:ok, activity2} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
|
|
|
|
{:ok, activity3} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
|
|
|
|
{:ok, activity4} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
|
2019-09-24 08:16:44 +00:00
|
|
|
|
|
|
|
notification1_id = get_notification_id_by_activity(activity1)
|
|
|
|
notification2_id = get_notification_id_by_activity(activity2)
|
|
|
|
notification3_id = get_notification_id_by_activity(activity3)
|
|
|
|
notification4_id = get_notification_id_by_activity(activity4)
|
|
|
|
|
|
|
|
conn = assign(conn, :user, user)
|
|
|
|
|
|
|
|
# min_id
|
|
|
|
result =
|
|
|
|
conn
|
|
|
|
|> get("/api/v1/notifications?limit=2&min_id=#{notification1_id}")
|
2020-04-28 17:27:54 +00:00
|
|
|
|> json_response_and_validate_schema(:ok)
|
2019-09-24 08:16:44 +00:00
|
|
|
|
|
|
|
assert [%{"id" => ^notification3_id}, %{"id" => ^notification2_id}] = result
|
|
|
|
|
|
|
|
# since_id
|
|
|
|
result =
|
|
|
|
conn
|
|
|
|
|> get("/api/v1/notifications?limit=2&since_id=#{notification1_id}")
|
2020-04-28 17:27:54 +00:00
|
|
|
|> json_response_and_validate_schema(:ok)
|
2019-09-24 08:16:44 +00:00
|
|
|
|
|
|
|
assert [%{"id" => ^notification4_id}, %{"id" => ^notification3_id}] = result
|
|
|
|
|
|
|
|
# max_id
|
|
|
|
result =
|
|
|
|
conn
|
|
|
|
|> get("/api/v1/notifications?limit=2&max_id=#{notification4_id}")
|
2020-04-28 17:27:54 +00:00
|
|
|
|> json_response_and_validate_schema(:ok)
|
2019-09-24 08:16:44 +00:00
|
|
|
|
|
|
|
assert [%{"id" => ^notification3_id}, %{"id" => ^notification2_id}] = result
|
|
|
|
end
|
|
|
|
|
2019-11-09 10:56:27 +00:00
|
|
|
describe "exclude_visibilities" do
|
2019-12-19 14:23:27 +00:00
|
|
|
test "filters notifications for mentions" do
|
|
|
|
%{user: user, conn: conn} = oauth_access(["read:notifications"])
|
2019-11-09 10:56:27 +00:00
|
|
|
other_user = insert(:user)
|
|
|
|
|
|
|
|
{:ok, public_activity} =
|
2020-05-12 19:59:26 +00:00
|
|
|
CommonAPI.post(other_user, %{status: "@#{user.nickname}", visibility: "public"})
|
2019-11-09 10:56:27 +00:00
|
|
|
|
|
|
|
{:ok, direct_activity} =
|
2020-05-12 19:59:26 +00:00
|
|
|
CommonAPI.post(other_user, %{status: "@#{user.nickname}", visibility: "direct"})
|
2019-11-09 10:56:27 +00:00
|
|
|
|
|
|
|
{:ok, unlisted_activity} =
|
2020-05-12 19:59:26 +00:00
|
|
|
CommonAPI.post(other_user, %{status: "@#{user.nickname}", visibility: "unlisted"})
|
2019-11-09 10:56:27 +00:00
|
|
|
|
|
|
|
{:ok, private_activity} =
|
2020-05-12 19:59:26 +00:00
|
|
|
CommonAPI.post(other_user, %{status: "@#{user.nickname}", visibility: "private"})
|
2019-11-09 10:56:27 +00:00
|
|
|
|
2020-04-28 17:27:54 +00:00
|
|
|
query = params_to_query(%{exclude_visibilities: ["public", "unlisted", "private"]})
|
|
|
|
conn_res = get(conn, "/api/v1/notifications?" <> query)
|
2019-11-09 10:56:27 +00:00
|
|
|
|
2020-04-28 17:27:54 +00:00
|
|
|
assert [%{"status" => %{"id" => id}}] = json_response_and_validate_schema(conn_res, 200)
|
2019-11-09 10:56:27 +00:00
|
|
|
assert id == direct_activity.id
|
|
|
|
|
2020-04-28 17:27:54 +00:00
|
|
|
query = params_to_query(%{exclude_visibilities: ["public", "unlisted", "direct"]})
|
|
|
|
conn_res = get(conn, "/api/v1/notifications?" <> query)
|
2019-11-09 10:56:27 +00:00
|
|
|
|
2020-04-28 17:27:54 +00:00
|
|
|
assert [%{"status" => %{"id" => id}}] = json_response_and_validate_schema(conn_res, 200)
|
2019-11-09 10:56:27 +00:00
|
|
|
assert id == private_activity.id
|
|
|
|
|
2020-04-28 17:27:54 +00:00
|
|
|
query = params_to_query(%{exclude_visibilities: ["public", "private", "direct"]})
|
|
|
|
conn_res = get(conn, "/api/v1/notifications?" <> query)
|
2019-11-09 10:56:27 +00:00
|
|
|
|
2020-04-28 17:27:54 +00:00
|
|
|
assert [%{"status" => %{"id" => id}}] = json_response_and_validate_schema(conn_res, 200)
|
2019-11-09 10:56:27 +00:00
|
|
|
assert id == unlisted_activity.id
|
|
|
|
|
2020-04-28 17:27:54 +00:00
|
|
|
query = params_to_query(%{exclude_visibilities: ["unlisted", "private", "direct"]})
|
|
|
|
conn_res = get(conn, "/api/v1/notifications?" <> query)
|
2019-11-09 10:56:27 +00:00
|
|
|
|
2020-04-28 17:27:54 +00:00
|
|
|
assert [%{"status" => %{"id" => id}}] = json_response_and_validate_schema(conn_res, 200)
|
2019-11-09 10:56:27 +00:00
|
|
|
assert id == public_activity.id
|
|
|
|
end
|
|
|
|
|
2019-12-19 14:23:27 +00:00
|
|
|
test "filters notifications for Like activities" do
|
2019-11-09 10:56:27 +00:00
|
|
|
user = insert(:user)
|
2019-12-19 14:23:27 +00:00
|
|
|
%{user: other_user, conn: conn} = oauth_access(["read:notifications"])
|
2019-11-09 10:56:27 +00:00
|
|
|
|
2020-05-12 19:59:26 +00:00
|
|
|
{:ok, public_activity} = CommonAPI.post(other_user, %{status: ".", visibility: "public"})
|
2019-11-09 10:56:27 +00:00
|
|
|
|
|
|
|
{:ok, direct_activity} =
|
2020-05-12 19:59:26 +00:00
|
|
|
CommonAPI.post(other_user, %{status: "@#{user.nickname}", visibility: "direct"})
|
2019-11-09 10:56:27 +00:00
|
|
|
|
|
|
|
{:ok, unlisted_activity} =
|
2020-05-12 19:59:26 +00:00
|
|
|
CommonAPI.post(other_user, %{status: ".", visibility: "unlisted"})
|
2019-11-09 10:56:27 +00:00
|
|
|
|
2020-05-12 19:59:26 +00:00
|
|
|
{:ok, private_activity} = CommonAPI.post(other_user, %{status: ".", visibility: "private"})
|
2019-11-09 10:56:27 +00:00
|
|
|
|
2020-03-19 17:00:55 +00:00
|
|
|
{:ok, _} = CommonAPI.favorite(user, public_activity.id)
|
|
|
|
{:ok, _} = CommonAPI.favorite(user, direct_activity.id)
|
|
|
|
{:ok, _} = CommonAPI.favorite(user, unlisted_activity.id)
|
|
|
|
{:ok, _} = CommonAPI.favorite(user, private_activity.id)
|
2019-11-09 10:56:27 +00:00
|
|
|
|
|
|
|
activity_ids =
|
|
|
|
conn
|
2020-04-28 17:27:54 +00:00
|
|
|
|> get("/api/v1/notifications?exclude_visibilities[]=direct")
|
|
|
|
|> json_response_and_validate_schema(200)
|
2019-11-09 10:56:27 +00:00
|
|
|
|> Enum.map(& &1["status"]["id"])
|
|
|
|
|
|
|
|
assert public_activity.id in activity_ids
|
|
|
|
assert unlisted_activity.id in activity_ids
|
|
|
|
assert private_activity.id in activity_ids
|
|
|
|
refute direct_activity.id in activity_ids
|
|
|
|
|
|
|
|
activity_ids =
|
|
|
|
conn
|
2020-04-28 17:27:54 +00:00
|
|
|
|> get("/api/v1/notifications?exclude_visibilities[]=unlisted")
|
|
|
|
|> json_response_and_validate_schema(200)
|
2019-11-09 10:56:27 +00:00
|
|
|
|> Enum.map(& &1["status"]["id"])
|
|
|
|
|
|
|
|
assert public_activity.id in activity_ids
|
|
|
|
refute unlisted_activity.id in activity_ids
|
|
|
|
assert private_activity.id in activity_ids
|
|
|
|
assert direct_activity.id in activity_ids
|
|
|
|
|
|
|
|
activity_ids =
|
|
|
|
conn
|
2020-04-28 17:27:54 +00:00
|
|
|
|> get("/api/v1/notifications?exclude_visibilities[]=private")
|
|
|
|
|> json_response_and_validate_schema(200)
|
2019-11-09 10:56:27 +00:00
|
|
|
|> Enum.map(& &1["status"]["id"])
|
|
|
|
|
|
|
|
assert public_activity.id in activity_ids
|
|
|
|
assert unlisted_activity.id in activity_ids
|
|
|
|
refute private_activity.id in activity_ids
|
|
|
|
assert direct_activity.id in activity_ids
|
|
|
|
|
|
|
|
activity_ids =
|
|
|
|
conn
|
2020-04-28 17:27:54 +00:00
|
|
|
|> get("/api/v1/notifications?exclude_visibilities[]=public")
|
|
|
|
|> json_response_and_validate_schema(200)
|
2019-11-09 10:56:27 +00:00
|
|
|
|> Enum.map(& &1["status"]["id"])
|
|
|
|
|
|
|
|
refute public_activity.id in activity_ids
|
|
|
|
assert unlisted_activity.id in activity_ids
|
|
|
|
assert private_activity.id in activity_ids
|
|
|
|
assert direct_activity.id in activity_ids
|
|
|
|
end
|
|
|
|
|
2019-12-19 14:23:27 +00:00
|
|
|
test "filters notifications for Announce activities" do
|
2019-11-09 10:56:27 +00:00
|
|
|
user = insert(:user)
|
2019-12-19 14:23:27 +00:00
|
|
|
%{user: other_user, conn: conn} = oauth_access(["read:notifications"])
|
2019-11-09 10:56:27 +00:00
|
|
|
|
2020-05-12 19:59:26 +00:00
|
|
|
{:ok, public_activity} = CommonAPI.post(other_user, %{status: ".", visibility: "public"})
|
2019-11-09 10:56:27 +00:00
|
|
|
|
|
|
|
{:ok, unlisted_activity} =
|
2020-05-12 19:59:26 +00:00
|
|
|
CommonAPI.post(other_user, %{status: ".", visibility: "unlisted"})
|
2019-11-09 10:56:27 +00:00
|
|
|
|
2020-05-21 11:16:21 +00:00
|
|
|
{:ok, _} = CommonAPI.repeat(public_activity.id, user)
|
|
|
|
{:ok, _} = CommonAPI.repeat(unlisted_activity.id, user)
|
2019-11-09 10:56:27 +00:00
|
|
|
|
|
|
|
activity_ids =
|
|
|
|
conn
|
2020-04-28 17:27:54 +00:00
|
|
|
|> get("/api/v1/notifications?exclude_visibilities[]=unlisted")
|
|
|
|
|> json_response_and_validate_schema(200)
|
2019-11-09 10:56:27 +00:00
|
|
|
|> Enum.map(& &1["status"]["id"])
|
|
|
|
|
|
|
|
assert public_activity.id in activity_ids
|
|
|
|
refute unlisted_activity.id in activity_ids
|
|
|
|
end
|
2020-05-18 15:46:04 +00:00
|
|
|
|
|
|
|
test "doesn't return less than the requested amount of records when the user's reply is liked" do
|
|
|
|
user = insert(:user)
|
|
|
|
%{user: other_user, conn: conn} = oauth_access(["read:notifications"])
|
|
|
|
|
|
|
|
{:ok, mention} =
|
|
|
|
CommonAPI.post(user, %{status: "@#{other_user.nickname}", visibility: "public"})
|
|
|
|
|
|
|
|
{:ok, activity} = CommonAPI.post(user, %{status: ".", visibility: "public"})
|
|
|
|
|
|
|
|
{:ok, reply} =
|
|
|
|
CommonAPI.post(other_user, %{
|
|
|
|
status: ".",
|
|
|
|
visibility: "public",
|
|
|
|
in_reply_to_status_id: activity.id
|
|
|
|
})
|
|
|
|
|
|
|
|
{:ok, _favorite} = CommonAPI.favorite(user, reply.id)
|
|
|
|
|
|
|
|
activity_ids =
|
|
|
|
conn
|
|
|
|
|> get("/api/v1/notifications?exclude_visibilities[]=direct&limit=2")
|
|
|
|
|> json_response_and_validate_schema(200)
|
|
|
|
|> Enum.map(& &1["status"]["id"])
|
|
|
|
|
|
|
|
assert [reply.id, mention.id] == activity_ids
|
|
|
|
end
|
2019-10-08 20:05:57 +00:00
|
|
|
end
|
|
|
|
|
2019-12-19 14:23:27 +00:00
|
|
|
test "filters notifications using exclude_types" do
|
|
|
|
%{user: user, conn: conn} = oauth_access(["read:notifications"])
|
2019-09-24 08:16:44 +00:00
|
|
|
other_user = insert(:user)
|
|
|
|
|
2020-05-12 19:59:26 +00:00
|
|
|
{:ok, mention_activity} = CommonAPI.post(other_user, %{status: "hey @#{user.nickname}"})
|
|
|
|
{:ok, create_activity} = CommonAPI.post(user, %{status: "hey"})
|
2019-10-16 14:16:39 +00:00
|
|
|
{:ok, favorite_activity} = CommonAPI.favorite(other_user, create_activity.id)
|
2020-05-21 11:16:21 +00:00
|
|
|
{:ok, reblog_activity} = CommonAPI.repeat(create_activity.id, other_user)
|
2019-09-24 08:16:44 +00:00
|
|
|
{:ok, _, _, follow_activity} = CommonAPI.follow(other_user, user)
|
|
|
|
|
|
|
|
mention_notification_id = get_notification_id_by_activity(mention_activity)
|
|
|
|
favorite_notification_id = get_notification_id_by_activity(favorite_activity)
|
|
|
|
reblog_notification_id = get_notification_id_by_activity(reblog_activity)
|
|
|
|
follow_notification_id = get_notification_id_by_activity(follow_activity)
|
|
|
|
|
2020-04-28 17:27:54 +00:00
|
|
|
query = params_to_query(%{exclude_types: ["mention", "favourite", "reblog"]})
|
|
|
|
conn_res = get(conn, "/api/v1/notifications?" <> query)
|
2019-09-24 08:16:44 +00:00
|
|
|
|
2020-04-28 17:27:54 +00:00
|
|
|
assert [%{"id" => ^follow_notification_id}] = json_response_and_validate_schema(conn_res, 200)
|
2019-09-24 08:16:44 +00:00
|
|
|
|
2020-04-28 17:27:54 +00:00
|
|
|
query = params_to_query(%{exclude_types: ["favourite", "reblog", "follow"]})
|
|
|
|
conn_res = get(conn, "/api/v1/notifications?" <> query)
|
2019-09-24 08:16:44 +00:00
|
|
|
|
2020-04-28 17:27:54 +00:00
|
|
|
assert [%{"id" => ^mention_notification_id}] =
|
|
|
|
json_response_and_validate_schema(conn_res, 200)
|
2019-09-24 08:16:44 +00:00
|
|
|
|
2020-04-28 17:27:54 +00:00
|
|
|
query = params_to_query(%{exclude_types: ["reblog", "follow", "mention"]})
|
|
|
|
conn_res = get(conn, "/api/v1/notifications?" <> query)
|
2019-09-24 08:16:44 +00:00
|
|
|
|
2020-04-28 17:27:54 +00:00
|
|
|
assert [%{"id" => ^favorite_notification_id}] =
|
|
|
|
json_response_and_validate_schema(conn_res, 200)
|
2019-09-24 08:16:44 +00:00
|
|
|
|
2020-04-28 17:27:54 +00:00
|
|
|
query = params_to_query(%{exclude_types: ["follow", "mention", "favourite"]})
|
|
|
|
conn_res = get(conn, "/api/v1/notifications?" <> query)
|
2019-09-24 08:16:44 +00:00
|
|
|
|
2020-04-28 17:27:54 +00:00
|
|
|
assert [%{"id" => ^reblog_notification_id}] = json_response_and_validate_schema(conn_res, 200)
|
2019-09-24 08:16:44 +00:00
|
|
|
end
|
|
|
|
|
2020-03-18 14:37:54 +00:00
|
|
|
test "filters notifications using include_types" do
|
|
|
|
%{user: user, conn: conn} = oauth_access(["read:notifications"])
|
|
|
|
other_user = insert(:user)
|
|
|
|
|
2020-05-12 19:59:26 +00:00
|
|
|
{:ok, mention_activity} = CommonAPI.post(other_user, %{status: "hey @#{user.nickname}"})
|
|
|
|
{:ok, create_activity} = CommonAPI.post(user, %{status: "hey"})
|
2020-03-20 14:00:28 +00:00
|
|
|
{:ok, favorite_activity} = CommonAPI.favorite(other_user, create_activity.id)
|
2020-05-21 11:16:21 +00:00
|
|
|
{:ok, reblog_activity} = CommonAPI.repeat(create_activity.id, other_user)
|
2020-03-18 14:37:54 +00:00
|
|
|
{:ok, _, _, follow_activity} = CommonAPI.follow(other_user, user)
|
|
|
|
|
|
|
|
mention_notification_id = get_notification_id_by_activity(mention_activity)
|
|
|
|
favorite_notification_id = get_notification_id_by_activity(favorite_activity)
|
|
|
|
reblog_notification_id = get_notification_id_by_activity(reblog_activity)
|
|
|
|
follow_notification_id = get_notification_id_by_activity(follow_activity)
|
|
|
|
|
2020-04-28 17:27:54 +00:00
|
|
|
conn_res = get(conn, "/api/v1/notifications?include_types[]=follow")
|
2020-03-18 14:37:54 +00:00
|
|
|
|
2020-04-28 17:27:54 +00:00
|
|
|
assert [%{"id" => ^follow_notification_id}] = json_response_and_validate_schema(conn_res, 200)
|
2020-03-18 14:37:54 +00:00
|
|
|
|
2020-04-28 17:27:54 +00:00
|
|
|
conn_res = get(conn, "/api/v1/notifications?include_types[]=mention")
|
2020-03-18 14:37:54 +00:00
|
|
|
|
2020-04-28 17:27:54 +00:00
|
|
|
assert [%{"id" => ^mention_notification_id}] =
|
|
|
|
json_response_and_validate_schema(conn_res, 200)
|
2020-03-18 14:37:54 +00:00
|
|
|
|
2020-04-28 17:27:54 +00:00
|
|
|
conn_res = get(conn, "/api/v1/notifications?include_types[]=favourite")
|
2020-03-18 14:37:54 +00:00
|
|
|
|
2020-04-28 17:27:54 +00:00
|
|
|
assert [%{"id" => ^favorite_notification_id}] =
|
|
|
|
json_response_and_validate_schema(conn_res, 200)
|
2020-03-18 14:37:54 +00:00
|
|
|
|
2020-04-28 17:27:54 +00:00
|
|
|
conn_res = get(conn, "/api/v1/notifications?include_types[]=reblog")
|
2020-03-18 14:37:54 +00:00
|
|
|
|
2020-04-28 17:27:54 +00:00
|
|
|
assert [%{"id" => ^reblog_notification_id}] = json_response_and_validate_schema(conn_res, 200)
|
2020-03-18 14:37:54 +00:00
|
|
|
|
2020-04-28 17:27:54 +00:00
|
|
|
result = conn |> get("/api/v1/notifications") |> json_response_and_validate_schema(200)
|
2020-03-18 14:37:54 +00:00
|
|
|
|
|
|
|
assert length(result) == 4
|
|
|
|
|
2020-04-28 17:27:54 +00:00
|
|
|
query = params_to_query(%{include_types: ["follow", "mention", "favourite", "reblog"]})
|
|
|
|
|
2020-03-18 14:37:54 +00:00
|
|
|
result =
|
|
|
|
conn
|
2020-04-28 17:27:54 +00:00
|
|
|
|> get("/api/v1/notifications?" <> query)
|
|
|
|
|> json_response_and_validate_schema(200)
|
2020-03-18 14:37:54 +00:00
|
|
|
|
|
|
|
assert length(result) == 4
|
|
|
|
end
|
|
|
|
|
2019-12-19 14:23:27 +00:00
|
|
|
test "destroy multiple" do
|
|
|
|
%{user: user, conn: conn} = oauth_access(["read:notifications", "write:notifications"])
|
2019-09-24 08:16:44 +00:00
|
|
|
other_user = insert(:user)
|
|
|
|
|
2020-05-12 19:59:26 +00:00
|
|
|
{:ok, activity1} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
|
|
|
|
{:ok, activity2} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
|
|
|
|
{:ok, activity3} = CommonAPI.post(user, %{status: "hi @#{other_user.nickname}"})
|
|
|
|
{:ok, activity4} = CommonAPI.post(user, %{status: "hi @#{other_user.nickname}"})
|
2019-09-24 08:16:44 +00:00
|
|
|
|
|
|
|
notification1_id = get_notification_id_by_activity(activity1)
|
|
|
|
notification2_id = get_notification_id_by_activity(activity2)
|
|
|
|
notification3_id = get_notification_id_by_activity(activity3)
|
|
|
|
notification4_id = get_notification_id_by_activity(activity4)
|
|
|
|
|
|
|
|
result =
|
|
|
|
conn
|
|
|
|
|> get("/api/v1/notifications")
|
2020-04-28 17:27:54 +00:00
|
|
|
|> json_response_and_validate_schema(:ok)
|
2019-09-24 08:16:44 +00:00
|
|
|
|
|
|
|
assert [%{"id" => ^notification2_id}, %{"id" => ^notification1_id}] = result
|
|
|
|
|
|
|
|
conn2 =
|
|
|
|
conn
|
|
|
|
|> assign(:user, other_user)
|
2019-12-19 14:23:27 +00:00
|
|
|
|> assign(:token, insert(:oauth_token, user: other_user, scopes: ["read:notifications"]))
|
2019-09-24 08:16:44 +00:00
|
|
|
|
|
|
|
result =
|
|
|
|
conn2
|
|
|
|
|> get("/api/v1/notifications")
|
2020-04-28 17:27:54 +00:00
|
|
|
|> json_response_and_validate_schema(:ok)
|
2019-09-24 08:16:44 +00:00
|
|
|
|
|
|
|
assert [%{"id" => ^notification4_id}, %{"id" => ^notification3_id}] = result
|
|
|
|
|
2020-04-28 17:27:54 +00:00
|
|
|
query = params_to_query(%{ids: [notification1_id, notification2_id]})
|
|
|
|
conn_destroy = delete(conn, "/api/v1/notifications/destroy_multiple?" <> query)
|
2019-09-24 08:16:44 +00:00
|
|
|
|
2020-04-28 17:27:54 +00:00
|
|
|
assert json_response_and_validate_schema(conn_destroy, 200) == %{}
|
2019-09-24 08:16:44 +00:00
|
|
|
|
|
|
|
result =
|
|
|
|
conn2
|
|
|
|
|> get("/api/v1/notifications")
|
2020-04-28 17:27:54 +00:00
|
|
|
|> json_response_and_validate_schema(:ok)
|
2019-09-24 08:16:44 +00:00
|
|
|
|
|
|
|
assert [%{"id" => ^notification4_id}, %{"id" => ^notification3_id}] = result
|
|
|
|
end
|
|
|
|
|
2019-12-19 14:23:27 +00:00
|
|
|
test "doesn't see notifications after muting user with notifications" do
|
|
|
|
%{user: user, conn: conn} = oauth_access(["read:notifications"])
|
2019-09-24 08:16:44 +00:00
|
|
|
user2 = insert(:user)
|
|
|
|
|
|
|
|
{:ok, _, _, _} = CommonAPI.follow(user, user2)
|
2020-05-12 19:59:26 +00:00
|
|
|
{:ok, _} = CommonAPI.post(user2, %{status: "hey @#{user.nickname}"})
|
2019-09-24 08:16:44 +00:00
|
|
|
|
2019-12-19 14:23:27 +00:00
|
|
|
ret_conn = get(conn, "/api/v1/notifications")
|
2019-09-24 08:16:44 +00:00
|
|
|
|
2020-04-28 17:27:54 +00:00
|
|
|
assert length(json_response_and_validate_schema(ret_conn, 200)) == 1
|
2019-09-24 08:16:44 +00:00
|
|
|
|
2019-11-19 20:22:10 +00:00
|
|
|
{:ok, _user_relationships} = User.mute(user, user2)
|
2019-09-24 08:16:44 +00:00
|
|
|
|
|
|
|
conn = get(conn, "/api/v1/notifications")
|
|
|
|
|
2020-04-28 17:27:54 +00:00
|
|
|
assert json_response_and_validate_schema(conn, 200) == []
|
2019-09-24 08:16:44 +00:00
|
|
|
end
|
|
|
|
|
2019-12-19 14:23:27 +00:00
|
|
|
test "see notifications after muting user without notifications" do
|
|
|
|
%{user: user, conn: conn} = oauth_access(["read:notifications"])
|
2019-09-24 08:16:44 +00:00
|
|
|
user2 = insert(:user)
|
|
|
|
|
|
|
|
{:ok, _, _, _} = CommonAPI.follow(user, user2)
|
2020-05-12 19:59:26 +00:00
|
|
|
{:ok, _} = CommonAPI.post(user2, %{status: "hey @#{user.nickname}"})
|
2019-09-24 08:16:44 +00:00
|
|
|
|
2019-12-19 14:23:27 +00:00
|
|
|
ret_conn = get(conn, "/api/v1/notifications")
|
2019-09-24 08:16:44 +00:00
|
|
|
|
2020-04-28 17:27:54 +00:00
|
|
|
assert length(json_response_and_validate_schema(ret_conn, 200)) == 1
|
2019-09-24 08:16:44 +00:00
|
|
|
|
2020-09-08 10:26:44 +00:00
|
|
|
{:ok, _user_relationships} = User.mute(user, user2, %{notifications: false})
|
2019-09-24 08:16:44 +00:00
|
|
|
|
|
|
|
conn = get(conn, "/api/v1/notifications")
|
|
|
|
|
2020-04-28 17:27:54 +00:00
|
|
|
assert length(json_response_and_validate_schema(conn, 200)) == 1
|
2019-09-24 08:16:44 +00:00
|
|
|
end
|
|
|
|
|
2019-12-19 14:23:27 +00:00
|
|
|
test "see notifications after muting user with notifications and with_muted parameter" do
|
|
|
|
%{user: user, conn: conn} = oauth_access(["read:notifications"])
|
2019-09-24 08:16:44 +00:00
|
|
|
user2 = insert(:user)
|
|
|
|
|
|
|
|
{:ok, _, _, _} = CommonAPI.follow(user, user2)
|
2020-05-12 19:59:26 +00:00
|
|
|
{:ok, _} = CommonAPI.post(user2, %{status: "hey @#{user.nickname}"})
|
2019-09-24 08:16:44 +00:00
|
|
|
|
2019-12-19 14:23:27 +00:00
|
|
|
ret_conn = get(conn, "/api/v1/notifications")
|
2019-09-24 08:16:44 +00:00
|
|
|
|
2020-04-28 17:27:54 +00:00
|
|
|
assert length(json_response_and_validate_schema(ret_conn, 200)) == 1
|
2019-09-24 08:16:44 +00:00
|
|
|
|
2019-11-19 20:22:10 +00:00
|
|
|
{:ok, _user_relationships} = User.mute(user, user2)
|
2019-09-24 08:16:44 +00:00
|
|
|
|
2020-04-28 17:27:54 +00:00
|
|
|
conn = get(conn, "/api/v1/notifications?with_muted=true")
|
2019-09-24 08:16:44 +00:00
|
|
|
|
2020-04-28 17:27:54 +00:00
|
|
|
assert length(json_response_and_validate_schema(conn, 200)) == 1
|
2019-09-24 08:16:44 +00:00
|
|
|
end
|
|
|
|
|
2020-03-16 17:05:21 +00:00
|
|
|
test "see move notifications" do
|
2019-12-03 15:13:38 +00:00
|
|
|
old_user = insert(:user)
|
|
|
|
new_user = insert(:user, also_known_as: [old_user.ap_id])
|
2019-12-19 14:23:27 +00:00
|
|
|
%{user: follower, conn: conn} = oauth_access(["read:notifications"])
|
2019-12-03 15:13:38 +00:00
|
|
|
|
|
|
|
User.follow(follower, old_user)
|
|
|
|
Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
|
|
|
|
Pleroma.Tests.ObanHelpers.perform_all()
|
|
|
|
|
2020-03-16 17:05:21 +00:00
|
|
|
conn = get(conn, "/api/v1/notifications")
|
2019-12-03 15:13:38 +00:00
|
|
|
|
2020-04-28 17:27:54 +00:00
|
|
|
assert length(json_response_and_validate_schema(conn, 200)) == 1
|
2019-12-03 15:13:38 +00:00
|
|
|
end
|
|
|
|
|
2019-12-19 14:23:27 +00:00
|
|
|
describe "link headers" do
|
|
|
|
test "preserves parameters in link headers" do
|
|
|
|
%{user: user, conn: conn} = oauth_access(["read:notifications"])
|
|
|
|
other_user = insert(:user)
|
|
|
|
|
|
|
|
{:ok, activity1} =
|
|
|
|
CommonAPI.post(other_user, %{
|
2020-05-12 19:59:26 +00:00
|
|
|
status: "hi @#{user.nickname}",
|
|
|
|
visibility: "public"
|
2019-12-19 14:23:27 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
{:ok, activity2} =
|
|
|
|
CommonAPI.post(other_user, %{
|
2020-05-12 19:59:26 +00:00
|
|
|
status: "hi @#{user.nickname}",
|
|
|
|
visibility: "public"
|
2019-12-19 14:23:27 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
notification1 = Repo.get_by(Notification, activity_id: activity1.id)
|
|
|
|
notification2 = Repo.get_by(Notification, activity_id: activity2.id)
|
|
|
|
|
|
|
|
conn =
|
|
|
|
conn
|
|
|
|
|> assign(:user, user)
|
2020-04-28 17:27:54 +00:00
|
|
|
|> get("/api/v1/notifications?limit=5")
|
2019-12-19 14:23:27 +00:00
|
|
|
|
|
|
|
assert [link_header] = get_resp_header(conn, "link")
|
2020-04-28 17:27:54 +00:00
|
|
|
assert link_header =~ ~r/limit=5/
|
2019-12-19 14:23:27 +00:00
|
|
|
assert link_header =~ ~r/min_id=#{notification2.id}/
|
|
|
|
assert link_header =~ ~r/max_id=#{notification1.id}/
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-17 10:00:46 +00:00
|
|
|
describe "from specified user" do
|
2020-01-15 12:51:09 +00:00
|
|
|
test "account_id" do
|
|
|
|
%{user: user, conn: conn} = oauth_access(["read:notifications"])
|
|
|
|
|
2019-12-17 10:00:46 +00:00
|
|
|
%{id: account_id} = other_user1 = insert(:user)
|
|
|
|
other_user2 = insert(:user)
|
|
|
|
|
2020-05-12 19:59:26 +00:00
|
|
|
{:ok, _activity} = CommonAPI.post(other_user1, %{status: "hi @#{user.nickname}"})
|
|
|
|
{:ok, _activity} = CommonAPI.post(other_user2, %{status: "bye @#{user.nickname}"})
|
2019-12-17 10:00:46 +00:00
|
|
|
|
|
|
|
assert [%{"account" => %{"id" => ^account_id}}] =
|
|
|
|
conn
|
|
|
|
|> assign(:user, user)
|
2020-04-28 17:27:54 +00:00
|
|
|
|> get("/api/v1/notifications?account_id=#{account_id}")
|
|
|
|
|> json_response_and_validate_schema(200)
|
2019-12-17 10:00:46 +00:00
|
|
|
|
2019-12-19 13:45:44 +00:00
|
|
|
assert %{"error" => "Account is not found"} =
|
2019-12-17 10:00:46 +00:00
|
|
|
conn
|
|
|
|
|> assign(:user, user)
|
2020-04-28 17:27:54 +00:00
|
|
|
|> get("/api/v1/notifications?account_id=cofe")
|
|
|
|
|> json_response_and_validate_schema(404)
|
2019-12-17 10:00:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-24 08:16:44 +00:00
|
|
|
defp get_notification_id_by_activity(%{id: id}) do
|
|
|
|
Notification
|
|
|
|
|> Repo.get_by(activity_id: id)
|
|
|
|
|> Map.get(:id)
|
|
|
|
|> to_string()
|
|
|
|
end
|
2020-04-28 17:27:54 +00:00
|
|
|
|
|
|
|
defp params_to_query(%{} = params) do
|
|
|
|
Enum.map_join(params, "&", fn
|
|
|
|
{k, v} when is_list(v) -> Enum.map_join(v, "&", &"#{k}[]=#{&1}")
|
|
|
|
{k, v} -> k <> "=" <> v
|
|
|
|
end)
|
|
|
|
end
|
2019-09-24 08:16:44 +00:00
|
|
|
end
|