2019-09-24 02:16:44 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-01 22:08:45 -07:00
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
2019-09-24 02:16:44 -06: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 09:45:24 -06:00
|
|
|
test "does NOT render account/pleroma/relationship by default" do
|
2020-04-01 10:49:09 -06:00
|
|
|
%{user: user, conn: conn} = oauth_access(["read:notifications"])
|
|
|
|
other_user = insert(:user)
|
|
|
|
|
2020-05-12 13:59:26 -06:00
|
|
|
{:ok, activity} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
|
2020-04-01 10:49:09 -06:00
|
|
|
{:ok, [_notification]} = Notification.create_notifications(activity)
|
|
|
|
|
|
|
|
response =
|
|
|
|
conn
|
|
|
|
|> assign(:user, user)
|
|
|
|
|> get("/api/v1/notifications")
|
2020-04-28 11:27:54 -06:00
|
|
|
|> json_response_and_validate_schema(200)
|
2020-04-01 10:49:09 -06:00
|
|
|
|
|
|
|
assert Enum.all?(response, fn n ->
|
|
|
|
get_in(n, ["account", "pleroma", "relationship"]) == %{}
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2019-12-19 07:23:27 -07:00
|
|
|
test "list of notifications" do
|
|
|
|
%{user: user, conn: conn} = oauth_access(["read:notifications"])
|
2019-09-24 02:16:44 -06:00
|
|
|
other_user = insert(:user)
|
|
|
|
|
2020-05-12 13:59:26 -06:00
|
|
|
{:ok, activity} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
|
2019-09-24 02:16:44 -06:00
|
|
|
|
|
|
|
{:ok, [_notification]} = Notification.create_notifications(activity)
|
|
|
|
|
|
|
|
conn =
|
|
|
|
conn
|
|
|
|
|> assign(:user, user)
|
|
|
|
|> get("/api/v1/notifications")
|
|
|
|
|
|
|
|
expected_response =
|
2020-04-08 13:58:31 -06:00
|
|
|
"hi <span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{user.id}\" href=\"#{
|
2019-09-24 02:16:44 -06:00
|
|
|
user.ap_id
|
|
|
|
}\" rel=\"ugc\">@<span>#{user.nickname}</span></a></span>"
|
|
|
|
|
2020-04-28 11:27:54 -06:00
|
|
|
assert [%{"status" => %{"content" => response}} | _rest] =
|
|
|
|
json_response_and_validate_schema(conn, 200)
|
|
|
|
|
2019-09-24 02:16:44 -06:00
|
|
|
assert response == expected_response
|
|
|
|
end
|
|
|
|
|
2020-06-02 06:05:53 -06: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
|
|
|
|
|
2019-12-19 07:23:27 -07:00
|
|
|
test "getting a single notification" do
|
|
|
|
%{user: user, conn: conn} = oauth_access(["read:notifications"])
|
2019-09-24 02:16:44 -06:00
|
|
|
other_user = insert(:user)
|
|
|
|
|
2020-05-12 13:59:26 -06:00
|
|
|
{:ok, activity} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
|
2019-09-24 02:16:44 -06:00
|
|
|
|
|
|
|
{:ok, [notification]} = Notification.create_notifications(activity)
|
|
|
|
|
2019-12-19 07:23:27 -07:00
|
|
|
conn = get(conn, "/api/v1/notifications/#{notification.id}")
|
2019-09-24 02:16:44 -06:00
|
|
|
|
|
|
|
expected_response =
|
2020-04-08 13:58:31 -06:00
|
|
|
"hi <span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{user.id}\" href=\"#{
|
2019-09-24 02:16:44 -06:00
|
|
|
user.ap_id
|
|
|
|
}\" rel=\"ugc\">@<span>#{user.nickname}</span></a></span>"
|
|
|
|
|
2020-04-28 11:27:54 -06:00
|
|
|
assert %{"status" => %{"content" => response}} = json_response_and_validate_schema(conn, 200)
|
2019-09-24 02:16:44 -06:00
|
|
|
assert response == expected_response
|
|
|
|
end
|
|
|
|
|
2020-04-09 07:08:43 -06:00
|
|
|
test "dismissing a single notification (deprecated endpoint)" do
|
2019-12-19 07:23:27 -07:00
|
|
|
%{user: user, conn: conn} = oauth_access(["write:notifications"])
|
2019-09-24 02:16:44 -06:00
|
|
|
other_user = insert(:user)
|
|
|
|
|
2020-05-12 13:59:26 -06:00
|
|
|
{:ok, activity} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
|
2019-09-24 02:16:44 -06:00
|
|
|
|
|
|
|
{:ok, [notification]} = Notification.create_notifications(activity)
|
|
|
|
|
|
|
|
conn =
|
|
|
|
conn
|
|
|
|
|> assign(:user, user)
|
2020-04-28 11:27:54 -06:00
|
|
|
|> put_req_header("content-type", "application/json")
|
|
|
|
|> post("/api/v1/notifications/dismiss", %{"id" => to_string(notification.id)})
|
2019-09-24 02:16:44 -06:00
|
|
|
|
2020-04-28 11:27:54 -06:00
|
|
|
assert %{} = json_response_and_validate_schema(conn, 200)
|
2019-09-24 02:16:44 -06:00
|
|
|
end
|
|
|
|
|
2020-04-09 07:08:43 -06:00
|
|
|
test "dismissing a single notification" do
|
|
|
|
%{user: user, conn: conn} = oauth_access(["write:notifications"])
|
|
|
|
other_user = insert(:user)
|
|
|
|
|
2020-05-12 13:59:26 -06:00
|
|
|
{:ok, activity} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
|
2020-04-09 07:08:43 -06:00
|
|
|
|
|
|
|
{:ok, [notification]} = Notification.create_notifications(activity)
|
|
|
|
|
|
|
|
conn =
|
|
|
|
conn
|
|
|
|
|> assign(:user, user)
|
|
|
|
|> post("/api/v1/notifications/#{notification.id}/dismiss")
|
|
|
|
|
2020-04-28 11:27:54 -06:00
|
|
|
assert %{} = json_response_and_validate_schema(conn, 200)
|
2020-04-09 07:08:43 -06:00
|
|
|
end
|
|
|
|
|
2019-12-19 07:23:27 -07:00
|
|
|
test "clearing all notifications" do
|
|
|
|
%{user: user, conn: conn} = oauth_access(["write:notifications", "read:notifications"])
|
2019-09-24 02:16:44 -06:00
|
|
|
other_user = insert(:user)
|
|
|
|
|
2020-05-12 13:59:26 -06:00
|
|
|
{:ok, activity} = CommonAPI.post(other_user, %{status: "hi @#{user.nickname}"})
|
2019-09-24 02:16:44 -06:00
|
|
|
|
|
|
|
{:ok, [_notification]} = Notification.create_notifications(activity)
|
|
|
|
|
2019-12-19 07:23:27 -07:00
|
|
|
ret_conn = post(conn, "/api/v1/notifications/clear")
|
2019-09-24 02:16:44 -06:00
|
|
|
|
2020-04-28 11:27:54 -06:00
|
|
|
assert %{} = json_response_and_validate_schema(ret_conn, 200)
|
2019-09-24 02:16:44 -06:00
|
|
|
|
2019-12-19 07:23:27 -07:00
|
|
|
ret_conn = get(conn, "/api/v1/notifications")
|
2019-09-24 02:16:44 -06:00
|
|
|
|
2020-04-28 11:27:54 -06:00
|
|
|
assert all = json_response_and_validate_schema(ret_conn, 200)
|
2019-09-24 02:16:44 -06:00
|
|
|
assert all == []
|
|
|
|
end
|
|
|
|
|
2019-12-19 07:23:27 -07: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 02:16:44 -06:00
|
|
|
other_user = insert(:user)
|
|
|
|
|
2020-05-12 13:59:26 -06: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 02:16:44 -06: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 11:27:54 -06:00
|
|
|
|> json_response_and_validate_schema(:ok)
|
2019-09-24 02:16:44 -06: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 11:27:54 -06:00
|
|
|
|> json_response_and_validate_schema(:ok)
|
2019-09-24 02:16:44 -06: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 11:27:54 -06:00
|
|
|
|> json_response_and_validate_schema(:ok)
|
2019-09-24 02:16:44 -06:00
|
|
|
|
|
|
|
assert [%{"id" => ^notification3_id}, %{"id" => ^notification2_id}] = result
|
|
|
|
end
|
|
|
|
|
2019-11-09 03:56:27 -07:00
|
|
|
describe "exclude_visibilities" do
|
2019-12-19 07:23:27 -07:00
|
|
|
test "filters notifications for mentions" do
|
|
|
|
%{user: user, conn: conn} = oauth_access(["read:notifications"])
|
2019-11-09 03:56:27 -07:00
|
|
|
other_user = insert(:user)
|
|
|
|
|
|
|
|
{:ok, public_activity} =
|
2020-05-12 13:59:26 -06:00
|
|
|
CommonAPI.post(other_user, %{status: "@#{user.nickname}", visibility: "public"})
|
2019-11-09 03:56:27 -07:00
|
|
|
|
|
|
|
{:ok, direct_activity} =
|
2020-05-12 13:59:26 -06:00
|
|
|
CommonAPI.post(other_user, %{status: "@#{user.nickname}", visibility: "direct"})
|
2019-11-09 03:56:27 -07:00
|
|
|
|
|
|
|
{:ok, unlisted_activity} =
|
2020-05-12 13:59:26 -06:00
|
|
|
CommonAPI.post(other_user, %{status: "@#{user.nickname}", visibility: "unlisted"})
|
2019-11-09 03:56:27 -07:00
|
|
|
|
|
|
|
{:ok, private_activity} =
|
2020-05-12 13:59:26 -06:00
|
|
|
CommonAPI.post(other_user, %{status: "@#{user.nickname}", visibility: "private"})
|
2019-11-09 03:56:27 -07:00
|
|
|
|
2020-04-28 11:27:54 -06:00
|
|
|
query = params_to_query(%{exclude_visibilities: ["public", "unlisted", "private"]})
|
|
|
|
conn_res = get(conn, "/api/v1/notifications?" <> query)
|
2019-11-09 03:56:27 -07:00
|
|
|
|
2020-04-28 11:27:54 -06:00
|
|
|
assert [%{"status" => %{"id" => id}}] = json_response_and_validate_schema(conn_res, 200)
|
2019-11-09 03:56:27 -07:00
|
|
|
assert id == direct_activity.id
|
|
|
|
|
2020-04-28 11:27:54 -06:00
|
|
|
query = params_to_query(%{exclude_visibilities: ["public", "unlisted", "direct"]})
|
|
|
|
conn_res = get(conn, "/api/v1/notifications?" <> query)
|
2019-11-09 03:56:27 -07:00
|
|
|
|
2020-04-28 11:27:54 -06:00
|
|
|
assert [%{"status" => %{"id" => id}}] = json_response_and_validate_schema(conn_res, 200)
|
2019-11-09 03:56:27 -07:00
|
|
|
assert id == private_activity.id
|
|
|
|
|
2020-04-28 11:27:54 -06:00
|
|
|
query = params_to_query(%{exclude_visibilities: ["public", "private", "direct"]})
|
|
|
|
conn_res = get(conn, "/api/v1/notifications?" <> query)
|
2019-11-09 03:56:27 -07:00
|
|
|
|
2020-04-28 11:27:54 -06:00
|
|
|
assert [%{"status" => %{"id" => id}}] = json_response_and_validate_schema(conn_res, 200)
|
2019-11-09 03:56:27 -07:00
|
|
|
assert id == unlisted_activity.id
|
|
|
|
|
2020-04-28 11:27:54 -06:00
|
|
|
query = params_to_query(%{exclude_visibilities: ["unlisted", "private", "direct"]})
|
|
|
|
conn_res = get(conn, "/api/v1/notifications?" <> query)
|
2019-11-09 03:56:27 -07:00
|
|
|
|
2020-04-28 11:27:54 -06:00
|
|
|
assert [%{"status" => %{"id" => id}}] = json_response_and_validate_schema(conn_res, 200)
|
2019-11-09 03:56:27 -07:00
|
|
|
assert id == public_activity.id
|
|
|
|
end
|
|
|
|
|
2019-12-19 07:23:27 -07:00
|
|
|
test "filters notifications for Like activities" do
|
2019-11-09 03:56:27 -07:00
|
|
|
user = insert(:user)
|
2019-12-19 07:23:27 -07:00
|
|
|
%{user: other_user, conn: conn} = oauth_access(["read:notifications"])
|
2019-11-09 03:56:27 -07:00
|
|
|
|
2020-05-12 13:59:26 -06:00
|
|
|
{:ok, public_activity} = CommonAPI.post(other_user, %{status: ".", visibility: "public"})
|
2019-11-09 03:56:27 -07:00
|
|
|
|
|
|
|
{:ok, direct_activity} =
|
2020-05-12 13:59:26 -06:00
|
|
|
CommonAPI.post(other_user, %{status: "@#{user.nickname}", visibility: "direct"})
|
2019-11-09 03:56:27 -07:00
|
|
|
|
|
|
|
{:ok, unlisted_activity} =
|
2020-05-12 13:59:26 -06:00
|
|
|
CommonAPI.post(other_user, %{status: ".", visibility: "unlisted"})
|
2019-11-09 03:56:27 -07:00
|
|
|
|
2020-05-12 13:59:26 -06:00
|
|
|
{:ok, private_activity} = CommonAPI.post(other_user, %{status: ".", visibility: "private"})
|
2019-11-09 03:56:27 -07:00
|
|
|
|
2020-03-19 11:00:55 -06: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 03:56:27 -07:00
|
|
|
|
|
|
|
activity_ids =
|
|
|
|
conn
|
2020-04-28 11:27:54 -06:00
|
|
|
|> get("/api/v1/notifications?exclude_visibilities[]=direct")
|
|
|
|
|> json_response_and_validate_schema(200)
|
2019-11-09 03:56:27 -07: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 11:27:54 -06:00
|
|
|
|> get("/api/v1/notifications?exclude_visibilities[]=unlisted")
|
|
|
|
|> json_response_and_validate_schema(200)
|
2019-11-09 03:56:27 -07: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 11:27:54 -06:00
|
|
|
|> get("/api/v1/notifications?exclude_visibilities[]=private")
|
|
|
|
|> json_response_and_validate_schema(200)
|
2019-11-09 03:56:27 -07: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 11:27:54 -06:00
|
|
|
|> get("/api/v1/notifications?exclude_visibilities[]=public")
|
|
|
|
|> json_response_and_validate_schema(200)
|
2019-11-09 03:56:27 -07: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 07:23:27 -07:00
|
|
|
test "filters notifications for Announce activities" do
|
2019-11-09 03:56:27 -07:00
|
|
|
user = insert(:user)
|
2019-12-19 07:23:27 -07:00
|
|
|
%{user: other_user, conn: conn} = oauth_access(["read:notifications"])
|
2019-11-09 03:56:27 -07:00
|
|
|
|
2020-05-12 13:59:26 -06:00
|
|
|
{:ok, public_activity} = CommonAPI.post(other_user, %{status: ".", visibility: "public"})
|
2019-11-09 03:56:27 -07:00
|
|
|
|
|
|
|
{:ok, unlisted_activity} =
|
2020-05-12 13:59:26 -06:00
|
|
|
CommonAPI.post(other_user, %{status: ".", visibility: "unlisted"})
|
2019-11-09 03:56:27 -07:00
|
|
|
|
2020-05-21 05:16:21 -06:00
|
|
|
{:ok, _} = CommonAPI.repeat(public_activity.id, user)
|
|
|
|
{:ok, _} = CommonAPI.repeat(unlisted_activity.id, user)
|
2019-11-09 03:56:27 -07:00
|
|
|
|
|
|
|
activity_ids =
|
|
|
|
conn
|
2020-04-28 11:27:54 -06:00
|
|
|
|> get("/api/v1/notifications?exclude_visibilities[]=unlisted")
|
|
|
|
|> json_response_and_validate_schema(200)
|
2019-11-09 03:56:27 -07:00
|
|
|
|> Enum.map(& &1["status"]["id"])
|
|
|
|
|
|
|
|
assert public_activity.id in activity_ids
|
|
|
|
refute unlisted_activity.id in activity_ids
|
|
|
|
end
|
2020-05-18 09:46:04 -06: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 14:05:57 -06:00
|
|
|
end
|
|
|
|
|
2019-12-19 07:23:27 -07:00
|
|
|
test "filters notifications using exclude_types" do
|
|
|
|
%{user: user, conn: conn} = oauth_access(["read:notifications"])
|
2019-09-24 02:16:44 -06:00
|
|
|
other_user = insert(:user)
|
|
|
|
|
2020-05-12 13:59:26 -06:00
|
|
|
{:ok, mention_activity} = CommonAPI.post(other_user, %{status: "hey @#{user.nickname}"})
|
|
|
|
{:ok, create_activity} = CommonAPI.post(user, %{status: "hey"})
|
2019-10-16 08:16:39 -06:00
|
|
|
{:ok, favorite_activity} = CommonAPI.favorite(other_user, create_activity.id)
|
2020-05-21 05:16:21 -06:00
|
|
|
{:ok, reblog_activity} = CommonAPI.repeat(create_activity.id, other_user)
|
2019-09-24 02:16:44 -06: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 11:27:54 -06:00
|
|
|
query = params_to_query(%{exclude_types: ["mention", "favourite", "reblog"]})
|
|
|
|
conn_res = get(conn, "/api/v1/notifications?" <> query)
|
2019-09-24 02:16:44 -06:00
|
|
|
|
2020-04-28 11:27:54 -06:00
|
|
|
assert [%{"id" => ^follow_notification_id}] = json_response_and_validate_schema(conn_res, 200)
|
2019-09-24 02:16:44 -06:00
|
|
|
|
2020-04-28 11:27:54 -06:00
|
|
|
query = params_to_query(%{exclude_types: ["favourite", "reblog", "follow"]})
|
|
|
|
conn_res = get(conn, "/api/v1/notifications?" <> query)
|
2019-09-24 02:16:44 -06:00
|
|
|
|
2020-04-28 11:27:54 -06:00
|
|
|
assert [%{"id" => ^mention_notification_id}] =
|
|
|
|
json_response_and_validate_schema(conn_res, 200)
|
2019-09-24 02:16:44 -06:00
|
|
|
|
2020-04-28 11:27:54 -06:00
|
|
|
query = params_to_query(%{exclude_types: ["reblog", "follow", "mention"]})
|
|
|
|
conn_res = get(conn, "/api/v1/notifications?" <> query)
|
2019-09-24 02:16:44 -06:00
|
|
|
|
2020-04-28 11:27:54 -06:00
|
|
|
assert [%{"id" => ^favorite_notification_id}] =
|
|
|
|
json_response_and_validate_schema(conn_res, 200)
|
2019-09-24 02:16:44 -06:00
|
|
|
|
2020-04-28 11:27:54 -06:00
|
|
|
query = params_to_query(%{exclude_types: ["follow", "mention", "favourite"]})
|
|
|
|
conn_res = get(conn, "/api/v1/notifications?" <> query)
|
2019-09-24 02:16:44 -06:00
|
|
|
|
2020-04-28 11:27:54 -06:00
|
|
|
assert [%{"id" => ^reblog_notification_id}] = json_response_and_validate_schema(conn_res, 200)
|
2019-09-24 02:16:44 -06:00
|
|
|
end
|
|
|
|
|
2020-03-18 08:37:54 -06:00
|
|
|
test "filters notifications using include_types" do
|
|
|
|
%{user: user, conn: conn} = oauth_access(["read:notifications"])
|
|
|
|
other_user = insert(:user)
|
|
|
|
|
2020-05-12 13:59:26 -06:00
|
|
|
{:ok, mention_activity} = CommonAPI.post(other_user, %{status: "hey @#{user.nickname}"})
|
|
|
|
{:ok, create_activity} = CommonAPI.post(user, %{status: "hey"})
|
2020-03-20 08:00:28 -06:00
|
|
|
{:ok, favorite_activity} = CommonAPI.favorite(other_user, create_activity.id)
|
2020-05-21 05:16:21 -06:00
|
|
|
{:ok, reblog_activity} = CommonAPI.repeat(create_activity.id, other_user)
|
2020-03-18 08:37:54 -06: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 11:27:54 -06:00
|
|
|
conn_res = get(conn, "/api/v1/notifications?include_types[]=follow")
|
2020-03-18 08:37:54 -06:00
|
|
|
|
2020-04-28 11:27:54 -06:00
|
|
|
assert [%{"id" => ^follow_notification_id}] = json_response_and_validate_schema(conn_res, 200)
|
2020-03-18 08:37:54 -06:00
|
|
|
|
2020-04-28 11:27:54 -06:00
|
|
|
conn_res = get(conn, "/api/v1/notifications?include_types[]=mention")
|
2020-03-18 08:37:54 -06:00
|
|
|
|
2020-04-28 11:27:54 -06:00
|
|
|
assert [%{"id" => ^mention_notification_id}] =
|
|
|
|
json_response_and_validate_schema(conn_res, 200)
|
2020-03-18 08:37:54 -06:00
|
|
|
|
2020-04-28 11:27:54 -06:00
|
|
|
conn_res = get(conn, "/api/v1/notifications?include_types[]=favourite")
|
2020-03-18 08:37:54 -06:00
|
|
|
|
2020-04-28 11:27:54 -06:00
|
|
|
assert [%{"id" => ^favorite_notification_id}] =
|
|
|
|
json_response_and_validate_schema(conn_res, 200)
|
2020-03-18 08:37:54 -06:00
|
|
|
|
2020-04-28 11:27:54 -06:00
|
|
|
conn_res = get(conn, "/api/v1/notifications?include_types[]=reblog")
|
2020-03-18 08:37:54 -06:00
|
|
|
|
2020-04-28 11:27:54 -06:00
|
|
|
assert [%{"id" => ^reblog_notification_id}] = json_response_and_validate_schema(conn_res, 200)
|
2020-03-18 08:37:54 -06:00
|
|
|
|
2020-04-28 11:27:54 -06:00
|
|
|
result = conn |> get("/api/v1/notifications") |> json_response_and_validate_schema(200)
|
2020-03-18 08:37:54 -06:00
|
|
|
|
|
|
|
assert length(result) == 4
|
|
|
|
|
2020-04-28 11:27:54 -06:00
|
|
|
query = params_to_query(%{include_types: ["follow", "mention", "favourite", "reblog"]})
|
|
|
|
|
2020-03-18 08:37:54 -06:00
|
|
|
result =
|
|
|
|
conn
|
2020-04-28 11:27:54 -06:00
|
|
|
|> get("/api/v1/notifications?" <> query)
|
|
|
|
|> json_response_and_validate_schema(200)
|
2020-03-18 08:37:54 -06:00
|
|
|
|
|
|
|
assert length(result) == 4
|
|
|
|
end
|
|
|
|
|
2019-12-19 07:23:27 -07:00
|
|
|
test "destroy multiple" do
|
|
|
|
%{user: user, conn: conn} = oauth_access(["read:notifications", "write:notifications"])
|
2019-09-24 02:16:44 -06:00
|
|
|
other_user = insert(:user)
|
|
|
|
|
2020-05-12 13:59:26 -06: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 02:16:44 -06: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 11:27:54 -06:00
|
|
|
|> json_response_and_validate_schema(:ok)
|
2019-09-24 02:16:44 -06:00
|
|
|
|
|
|
|
assert [%{"id" => ^notification2_id}, %{"id" => ^notification1_id}] = result
|
|
|
|
|
|
|
|
conn2 =
|
|
|
|
conn
|
|
|
|
|> assign(:user, other_user)
|
2019-12-19 07:23:27 -07:00
|
|
|
|> assign(:token, insert(:oauth_token, user: other_user, scopes: ["read:notifications"]))
|
2019-09-24 02:16:44 -06:00
|
|
|
|
|
|
|
result =
|
|
|
|
conn2
|
|
|
|
|> get("/api/v1/notifications")
|
2020-04-28 11:27:54 -06:00
|
|
|
|> json_response_and_validate_schema(:ok)
|
2019-09-24 02:16:44 -06:00
|
|
|
|
|
|
|
assert [%{"id" => ^notification4_id}, %{"id" => ^notification3_id}] = result
|
|
|
|
|
2020-04-28 11:27:54 -06:00
|
|
|
query = params_to_query(%{ids: [notification1_id, notification2_id]})
|
|
|
|
conn_destroy = delete(conn, "/api/v1/notifications/destroy_multiple?" <> query)
|
2019-09-24 02:16:44 -06:00
|
|
|
|
2020-04-28 11:27:54 -06:00
|
|
|
assert json_response_and_validate_schema(conn_destroy, 200) == %{}
|
2019-09-24 02:16:44 -06:00
|
|
|
|
|
|
|
result =
|
|
|
|
conn2
|
|
|
|
|> get("/api/v1/notifications")
|
2020-04-28 11:27:54 -06:00
|
|
|
|> json_response_and_validate_schema(:ok)
|
2019-09-24 02:16:44 -06:00
|
|
|
|
|
|
|
assert [%{"id" => ^notification4_id}, %{"id" => ^notification3_id}] = result
|
|
|
|
end
|
|
|
|
|
2019-12-19 07:23:27 -07:00
|
|
|
test "doesn't see notifications after muting user with notifications" do
|
|
|
|
%{user: user, conn: conn} = oauth_access(["read:notifications"])
|
2019-09-24 02:16:44 -06:00
|
|
|
user2 = insert(:user)
|
|
|
|
|
|
|
|
{:ok, _, _, _} = CommonAPI.follow(user, user2)
|
2020-05-12 13:59:26 -06:00
|
|
|
{:ok, _} = CommonAPI.post(user2, %{status: "hey @#{user.nickname}"})
|
2019-09-24 02:16:44 -06:00
|
|
|
|
2019-12-19 07:23:27 -07:00
|
|
|
ret_conn = get(conn, "/api/v1/notifications")
|
2019-09-24 02:16:44 -06:00
|
|
|
|
2020-04-28 11:27:54 -06:00
|
|
|
assert length(json_response_and_validate_schema(ret_conn, 200)) == 1
|
2019-09-24 02:16:44 -06:00
|
|
|
|
2019-11-19 13:22:10 -07:00
|
|
|
{:ok, _user_relationships} = User.mute(user, user2)
|
2019-09-24 02:16:44 -06:00
|
|
|
|
|
|
|
conn = get(conn, "/api/v1/notifications")
|
|
|
|
|
2020-04-28 11:27:54 -06:00
|
|
|
assert json_response_and_validate_schema(conn, 200) == []
|
2019-09-24 02:16:44 -06:00
|
|
|
end
|
|
|
|
|
2019-12-19 07:23:27 -07:00
|
|
|
test "see notifications after muting user without notifications" do
|
|
|
|
%{user: user, conn: conn} = oauth_access(["read:notifications"])
|
2019-09-24 02:16:44 -06:00
|
|
|
user2 = insert(:user)
|
|
|
|
|
|
|
|
{:ok, _, _, _} = CommonAPI.follow(user, user2)
|
2020-05-12 13:59:26 -06:00
|
|
|
{:ok, _} = CommonAPI.post(user2, %{status: "hey @#{user.nickname}"})
|
2019-09-24 02:16:44 -06:00
|
|
|
|
2019-12-19 07:23:27 -07:00
|
|
|
ret_conn = get(conn, "/api/v1/notifications")
|
2019-09-24 02:16:44 -06:00
|
|
|
|
2020-04-28 11:27:54 -06:00
|
|
|
assert length(json_response_and_validate_schema(ret_conn, 200)) == 1
|
2019-09-24 02:16:44 -06:00
|
|
|
|
2019-11-19 13:22:10 -07:00
|
|
|
{:ok, _user_relationships} = User.mute(user, user2, false)
|
2019-09-24 02:16:44 -06:00
|
|
|
|
|
|
|
conn = get(conn, "/api/v1/notifications")
|
|
|
|
|
2020-04-28 11:27:54 -06:00
|
|
|
assert length(json_response_and_validate_schema(conn, 200)) == 1
|
2019-09-24 02:16:44 -06:00
|
|
|
end
|
|
|
|
|
2019-12-19 07:23:27 -07: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 02:16:44 -06:00
|
|
|
user2 = insert(:user)
|
|
|
|
|
|
|
|
{:ok, _, _, _} = CommonAPI.follow(user, user2)
|
2020-05-12 13:59:26 -06:00
|
|
|
{:ok, _} = CommonAPI.post(user2, %{status: "hey @#{user.nickname}"})
|
2019-09-24 02:16:44 -06:00
|
|
|
|
2019-12-19 07:23:27 -07:00
|
|
|
ret_conn = get(conn, "/api/v1/notifications")
|
2019-09-24 02:16:44 -06:00
|
|
|
|
2020-04-28 11:27:54 -06:00
|
|
|
assert length(json_response_and_validate_schema(ret_conn, 200)) == 1
|
2019-09-24 02:16:44 -06:00
|
|
|
|
2019-11-19 13:22:10 -07:00
|
|
|
{:ok, _user_relationships} = User.mute(user, user2)
|
2019-09-24 02:16:44 -06:00
|
|
|
|
2020-04-28 11:27:54 -06:00
|
|
|
conn = get(conn, "/api/v1/notifications?with_muted=true")
|
2019-09-24 02:16:44 -06:00
|
|
|
|
2020-04-28 11:27:54 -06:00
|
|
|
assert length(json_response_and_validate_schema(conn, 200)) == 1
|
2019-09-24 02:16:44 -06:00
|
|
|
end
|
|
|
|
|
2020-03-05 07:31:06 -07:00
|
|
|
@tag capture_log: true
|
2020-03-16 11:05:21 -06:00
|
|
|
test "see move notifications" do
|
2019-12-03 08:13:38 -07:00
|
|
|
old_user = insert(:user)
|
|
|
|
new_user = insert(:user, also_known_as: [old_user.ap_id])
|
2019-12-19 07:23:27 -07:00
|
|
|
%{user: follower, conn: conn} = oauth_access(["read:notifications"])
|
2019-12-03 08:13:38 -07:00
|
|
|
|
2020-03-05 07:31:06 -07:00
|
|
|
old_user_url = old_user.ap_id
|
|
|
|
|
|
|
|
body =
|
|
|
|
File.read!("test/fixtures/users_mock/localhost.json")
|
|
|
|
|> String.replace("{{nickname}}", old_user.nickname)
|
|
|
|
|> Jason.encode!()
|
|
|
|
|
|
|
|
Tesla.Mock.mock(fn
|
|
|
|
%{method: :get, url: ^old_user_url} ->
|
|
|
|
%Tesla.Env{status: 200, body: body}
|
|
|
|
end)
|
|
|
|
|
2019-12-03 08:13:38 -07:00
|
|
|
User.follow(follower, old_user)
|
|
|
|
Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
|
|
|
|
Pleroma.Tests.ObanHelpers.perform_all()
|
|
|
|
|
2020-03-16 11:05:21 -06:00
|
|
|
conn = get(conn, "/api/v1/notifications")
|
2019-12-03 08:13:38 -07:00
|
|
|
|
2020-04-28 11:27:54 -06:00
|
|
|
assert length(json_response_and_validate_schema(conn, 200)) == 1
|
2019-12-03 08:13:38 -07:00
|
|
|
end
|
|
|
|
|
2019-12-19 07:23:27 -07: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 13:59:26 -06:00
|
|
|
status: "hi @#{user.nickname}",
|
|
|
|
visibility: "public"
|
2019-12-19 07:23:27 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
{:ok, activity2} =
|
|
|
|
CommonAPI.post(other_user, %{
|
2020-05-12 13:59:26 -06:00
|
|
|
status: "hi @#{user.nickname}",
|
|
|
|
visibility: "public"
|
2019-12-19 07:23:27 -07: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 11:27:54 -06:00
|
|
|
|> get("/api/v1/notifications?limit=5")
|
2019-12-19 07:23:27 -07:00
|
|
|
|
|
|
|
assert [link_header] = get_resp_header(conn, "link")
|
2020-04-28 11:27:54 -06:00
|
|
|
assert link_header =~ ~r/limit=5/
|
2019-12-19 07:23:27 -07:00
|
|
|
assert link_header =~ ~r/min_id=#{notification2.id}/
|
|
|
|
assert link_header =~ ~r/max_id=#{notification1.id}/
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-17 03:00:46 -07:00
|
|
|
describe "from specified user" do
|
2020-01-15 05:51:09 -07:00
|
|
|
test "account_id" do
|
|
|
|
%{user: user, conn: conn} = oauth_access(["read:notifications"])
|
|
|
|
|
2019-12-17 03:00:46 -07:00
|
|
|
%{id: account_id} = other_user1 = insert(:user)
|
|
|
|
other_user2 = insert(:user)
|
|
|
|
|
2020-05-12 13:59:26 -06:00
|
|
|
{:ok, _activity} = CommonAPI.post(other_user1, %{status: "hi @#{user.nickname}"})
|
|
|
|
{:ok, _activity} = CommonAPI.post(other_user2, %{status: "bye @#{user.nickname}"})
|
2019-12-17 03:00:46 -07:00
|
|
|
|
|
|
|
assert [%{"account" => %{"id" => ^account_id}}] =
|
|
|
|
conn
|
|
|
|
|> assign(:user, user)
|
2020-04-28 11:27:54 -06:00
|
|
|
|> get("/api/v1/notifications?account_id=#{account_id}")
|
|
|
|
|> json_response_and_validate_schema(200)
|
2019-12-17 03:00:46 -07:00
|
|
|
|
2019-12-19 06:45:44 -07:00
|
|
|
assert %{"error" => "Account is not found"} =
|
2019-12-17 03:00:46 -07:00
|
|
|
conn
|
|
|
|
|> assign(:user, user)
|
2020-04-28 11:27:54 -06:00
|
|
|
|> get("/api/v1/notifications?account_id=cofe")
|
|
|
|
|> json_response_and_validate_schema(404)
|
2019-12-17 03:00:46 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-24 02:16:44 -06: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 11:27:54 -06: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 02:16:44 -06:00
|
|
|
end
|