2019-09-30 09:52:07 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-02 05:08:45 +00:00
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
2019-09-30 09:52:07 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.MastodonAPI.ConversationControllerTest do
|
|
|
|
use Pleroma.Web.ConnCase
|
|
|
|
|
2020-09-01 06:37:08 +00:00
|
|
|
alias Pleroma.Conversation.Participation
|
2019-09-30 09:52:07 +00:00
|
|
|
alias Pleroma.User
|
|
|
|
alias Pleroma.Web.CommonAPI
|
|
|
|
|
|
|
|
import Pleroma.Factory
|
|
|
|
|
2019-12-19 14:23:27 +00:00
|
|
|
setup do: oauth_access(["read:statuses"])
|
|
|
|
|
2020-05-30 10:02:37 +00:00
|
|
|
describe "returns a list of conversations" do
|
|
|
|
setup(%{user: user_one, conn: conn}) do
|
|
|
|
user_two = insert(:user)
|
|
|
|
user_three = insert(:user)
|
|
|
|
|
|
|
|
{:ok, user_two} = User.follow(user_two, user_one)
|
|
|
|
|
|
|
|
{:ok, %{user: user_one, user_two: user_two, user_three: user_three, conn: conn}}
|
|
|
|
end
|
|
|
|
|
|
|
|
test "returns correct conversations", %{
|
|
|
|
user: user_one,
|
|
|
|
user_two: user_two,
|
|
|
|
user_three: user_three,
|
|
|
|
conn: conn
|
|
|
|
} do
|
2020-09-01 06:37:08 +00:00
|
|
|
assert Participation.unread_count(user_two) == 0
|
2020-05-30 10:02:37 +00:00
|
|
|
{:ok, direct} = create_direct_message(user_one, [user_two, user_three])
|
|
|
|
|
2020-09-01 06:37:08 +00:00
|
|
|
assert Participation.unread_count(user_two) == 1
|
2020-05-30 10:02:37 +00:00
|
|
|
|
|
|
|
{:ok, _follower_only} =
|
|
|
|
CommonAPI.post(user_one, %{
|
|
|
|
status: "Hi @#{user_two.nickname}!",
|
|
|
|
visibility: "private"
|
|
|
|
})
|
|
|
|
|
|
|
|
res_conn = get(conn, "/api/v1/conversations")
|
|
|
|
|
|
|
|
assert response = json_response_and_validate_schema(res_conn, 200)
|
|
|
|
|
|
|
|
assert [
|
|
|
|
%{
|
|
|
|
"id" => res_id,
|
|
|
|
"accounts" => res_accounts,
|
|
|
|
"last_status" => res_last_status,
|
|
|
|
"unread" => unread
|
|
|
|
}
|
|
|
|
] = response
|
|
|
|
|
|
|
|
account_ids = Enum.map(res_accounts, & &1["id"])
|
2020-10-30 12:01:58 +00:00
|
|
|
assert length(res_accounts) == 2
|
|
|
|
assert user_one.id not in account_ids
|
2020-05-30 10:02:37 +00:00
|
|
|
assert user_two.id in account_ids
|
|
|
|
assert user_three.id in account_ids
|
|
|
|
assert is_binary(res_id)
|
|
|
|
assert unread == false
|
|
|
|
assert res_last_status["id"] == direct.id
|
2020-10-30 12:01:58 +00:00
|
|
|
assert res_last_status["account"]["id"] == user_one.id
|
2020-09-01 06:37:08 +00:00
|
|
|
assert Participation.unread_count(user_one) == 0
|
2020-05-30 10:02:37 +00:00
|
|
|
end
|
|
|
|
|
2020-10-30 12:01:58 +00:00
|
|
|
test "special behaviour when conversation have only one user", %{
|
|
|
|
user: user_one,
|
|
|
|
user_two: user_two,
|
|
|
|
conn: conn
|
|
|
|
} do
|
|
|
|
{:ok, direct} = create_direct_message(user_one, [])
|
|
|
|
|
|
|
|
res_conn = get(conn, "/api/v1/conversations")
|
|
|
|
|
|
|
|
assert response = json_response_and_validate_schema(res_conn, 200)
|
2020-10-30 12:10:19 +00:00
|
|
|
|
2020-10-30 12:01:58 +00:00
|
|
|
assert [
|
|
|
|
%{
|
|
|
|
"accounts" => res_accounts,
|
|
|
|
"last_status" => res_last_status
|
|
|
|
}
|
|
|
|
] = response
|
2020-10-30 12:10:19 +00:00
|
|
|
|
|
|
|
account_ids = Enum.map(res_accounts, & &1["id"])
|
2020-10-30 12:01:58 +00:00
|
|
|
assert length(res_accounts) == 1
|
2020-10-30 12:10:19 +00:00
|
|
|
assert user_one.id in account_ids
|
2020-10-30 12:01:58 +00:00
|
|
|
end
|
|
|
|
|
2020-05-30 10:02:37 +00:00
|
|
|
test "observes limit params", %{
|
|
|
|
user: user_one,
|
|
|
|
user_two: user_two,
|
|
|
|
user_three: user_three,
|
|
|
|
conn: conn
|
|
|
|
} do
|
|
|
|
{:ok, _} = create_direct_message(user_one, [user_two, user_three])
|
|
|
|
{:ok, _} = create_direct_message(user_two, [user_one, user_three])
|
|
|
|
{:ok, _} = create_direct_message(user_three, [user_two, user_one])
|
|
|
|
|
|
|
|
res_conn = get(conn, "/api/v1/conversations?limit=1")
|
|
|
|
|
|
|
|
assert response = json_response_and_validate_schema(res_conn, 200)
|
|
|
|
|
|
|
|
assert Enum.count(response) == 1
|
|
|
|
|
|
|
|
res_conn = get(conn, "/api/v1/conversations?limit=2")
|
|
|
|
|
|
|
|
assert response = json_response_and_validate_schema(res_conn, 200)
|
|
|
|
|
|
|
|
assert Enum.count(response) == 2
|
|
|
|
end
|
2019-10-01 21:37:08 +00:00
|
|
|
end
|
2019-10-01 21:37:08 +00:00
|
|
|
|
2019-12-19 14:23:27 +00:00
|
|
|
test "filters conversations by recipients", %{user: user_one, conn: conn} do
|
2019-11-07 00:00:21 +00:00
|
|
|
user_two = insert(:user)
|
|
|
|
user_three = insert(:user)
|
2020-05-30 10:02:37 +00:00
|
|
|
{:ok, direct1} = create_direct_message(user_one, [user_two])
|
|
|
|
{:ok, _direct2} = create_direct_message(user_one, [user_three])
|
|
|
|
{:ok, direct3} = create_direct_message(user_one, [user_two, user_three])
|
|
|
|
{:ok, _direct4} = create_direct_message(user_two, [user_three])
|
|
|
|
{:ok, direct5} = create_direct_message(user_two, [user_one])
|
2019-11-07 00:00:21 +00:00
|
|
|
|
2020-05-05 13:44:46 +00:00
|
|
|
assert [conversation1, conversation2] =
|
|
|
|
conn
|
|
|
|
|> get("/api/v1/conversations?recipients[]=#{user_two.id}")
|
|
|
|
|> json_response_and_validate_schema(200)
|
2019-11-07 00:00:21 +00:00
|
|
|
|
|
|
|
assert conversation1["last_status"]["id"] == direct5.id
|
|
|
|
assert conversation2["last_status"]["id"] == direct1.id
|
|
|
|
|
|
|
|
[conversation1] =
|
|
|
|
conn
|
2020-05-05 13:44:46 +00:00
|
|
|
|> get("/api/v1/conversations?recipients[]=#{user_two.id}&recipients[]=#{user_three.id}")
|
|
|
|
|> json_response_and_validate_schema(200)
|
2019-11-07 00:00:21 +00:00
|
|
|
|
|
|
|
assert conversation1["last_status"]["id"] == direct3.id
|
|
|
|
end
|
|
|
|
|
2019-12-19 14:23:27 +00:00
|
|
|
test "updates the last_status on reply", %{user: user_one, conn: conn} do
|
2019-10-01 21:37:08 +00:00
|
|
|
user_two = insert(:user)
|
2020-05-30 10:02:37 +00:00
|
|
|
{:ok, direct} = create_direct_message(user_one, [user_two])
|
2019-10-01 21:37:08 +00:00
|
|
|
|
|
|
|
{:ok, direct_reply} =
|
|
|
|
CommonAPI.post(user_two, %{
|
2020-05-12 19:59:26 +00:00
|
|
|
status: "reply",
|
|
|
|
visibility: "direct",
|
|
|
|
in_reply_to_status_id: direct.id
|
2019-10-01 21:37:08 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
[%{"last_status" => res_last_status}] =
|
2019-10-01 21:37:08 +00:00
|
|
|
conn
|
2019-10-01 21:37:08 +00:00
|
|
|
|> get("/api/v1/conversations")
|
2020-05-05 13:44:46 +00:00
|
|
|
|> json_response_and_validate_schema(200)
|
2019-10-01 21:37:08 +00:00
|
|
|
|
2019-10-01 21:37:08 +00:00
|
|
|
assert res_last_status["id"] == direct_reply.id
|
|
|
|
end
|
|
|
|
|
2019-12-19 14:23:27 +00:00
|
|
|
test "the user marks a conversation as read", %{user: user_one, conn: conn} do
|
2019-10-01 21:37:08 +00:00
|
|
|
user_two = insert(:user)
|
2020-05-30 10:02:37 +00:00
|
|
|
{:ok, direct} = create_direct_message(user_one, [user_two])
|
2019-10-01 21:37:08 +00:00
|
|
|
|
2020-09-01 06:37:08 +00:00
|
|
|
assert Participation.unread_count(user_one) == 0
|
|
|
|
assert Participation.unread_count(user_two) == 1
|
2019-10-17 12:25:15 +00:00
|
|
|
|
2019-12-19 14:23:27 +00:00
|
|
|
user_two_conn =
|
|
|
|
build_conn()
|
2019-10-17 12:25:15 +00:00
|
|
|
|> assign(:user, user_two)
|
2019-12-19 14:23:27 +00:00
|
|
|
|> assign(
|
|
|
|
:token,
|
|
|
|
insert(:oauth_token, user: user_two, scopes: ["read:statuses", "write:conversations"])
|
|
|
|
)
|
|
|
|
|
|
|
|
[%{"id" => direct_conversation_id, "unread" => true}] =
|
|
|
|
user_two_conn
|
2019-10-01 21:37:08 +00:00
|
|
|
|> get("/api/v1/conversations")
|
2020-05-05 13:44:46 +00:00
|
|
|
|> json_response_and_validate_schema(200)
|
2019-10-01 21:37:08 +00:00
|
|
|
|
|
|
|
%{"unread" => false} =
|
2019-12-19 14:23:27 +00:00
|
|
|
user_two_conn
|
2019-10-01 21:37:08 +00:00
|
|
|
|> post("/api/v1/conversations/#{direct_conversation_id}/read")
|
2020-05-05 13:44:46 +00:00
|
|
|
|> json_response_and_validate_schema(200)
|
2019-10-01 21:37:08 +00:00
|
|
|
|
2020-09-01 06:37:08 +00:00
|
|
|
assert Participation.unread_count(user_one) == 0
|
|
|
|
assert Participation.unread_count(user_two) == 0
|
2019-10-01 21:37:08 +00:00
|
|
|
|
|
|
|
# The conversation is marked as unread on reply
|
|
|
|
{:ok, _} =
|
|
|
|
CommonAPI.post(user_two, %{
|
2020-05-12 19:59:26 +00:00
|
|
|
status: "reply",
|
|
|
|
visibility: "direct",
|
|
|
|
in_reply_to_status_id: direct.id
|
2019-10-01 21:37:08 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
[%{"unread" => true}] =
|
|
|
|
conn
|
|
|
|
|> get("/api/v1/conversations")
|
2020-05-05 13:44:46 +00:00
|
|
|
|> json_response_and_validate_schema(200)
|
2019-10-01 21:37:08 +00:00
|
|
|
|
2020-09-01 06:37:08 +00:00
|
|
|
assert Participation.unread_count(user_one) == 1
|
|
|
|
assert Participation.unread_count(user_two) == 0
|
2019-10-01 21:37:08 +00:00
|
|
|
|
|
|
|
# A reply doesn't increment the user's unread_conversation_count if the conversation is unread
|
|
|
|
{:ok, _} =
|
|
|
|
CommonAPI.post(user_two, %{
|
2020-05-12 19:59:26 +00:00
|
|
|
status: "reply",
|
|
|
|
visibility: "direct",
|
|
|
|
in_reply_to_status_id: direct.id
|
2019-10-01 21:37:08 +00:00
|
|
|
})
|
|
|
|
|
2020-09-01 06:37:08 +00:00
|
|
|
assert Participation.unread_count(user_one) == 1
|
|
|
|
assert Participation.unread_count(user_two) == 0
|
2019-10-01 21:37:08 +00:00
|
|
|
end
|
|
|
|
|
2019-12-19 14:23:27 +00:00
|
|
|
test "(vanilla) Mastodon frontend behaviour", %{user: user_one, conn: conn} do
|
2019-10-01 21:37:08 +00:00
|
|
|
user_two = insert(:user)
|
2020-05-30 10:02:37 +00:00
|
|
|
{:ok, direct} = create_direct_message(user_one, [user_two])
|
2019-09-30 09:52:07 +00:00
|
|
|
|
2019-12-19 14:23:27 +00:00
|
|
|
res_conn = get(conn, "/api/v1/statuses/#{direct.id}/context")
|
2019-09-30 09:52:07 +00:00
|
|
|
|
|
|
|
assert %{"ancestors" => [], "descendants" => []} == json_response(res_conn, 200)
|
|
|
|
end
|
2020-05-30 10:02:37 +00:00
|
|
|
|
|
|
|
defp create_direct_message(sender, recips) do
|
|
|
|
hellos =
|
|
|
|
recips
|
|
|
|
|> Enum.map(fn s -> "@#{s.nickname}" end)
|
|
|
|
|> Enum.join(", ")
|
|
|
|
|
|
|
|
CommonAPI.post(sender, %{
|
|
|
|
status: "Hi #{hellos}!",
|
|
|
|
visibility: "direct"
|
|
|
|
})
|
|
|
|
end
|
2019-09-30 09:52:07 +00:00
|
|
|
end
|