2019-09-30 03:52:07 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
2023-01-02 13:38:50 -07:00
|
|
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
2019-09-30 03:52:07 -06:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.MastodonAPI.ConversationControllerTest do
|
2020-12-21 04:21:40 -07:00
|
|
|
use Pleroma.Web.ConnCase, async: true
|
2019-09-30 03:52:07 -06:00
|
|
|
|
2020-09-01 00:37:08 -06:00
|
|
|
alias Pleroma.Conversation.Participation
|
2019-09-30 03:52:07 -06:00
|
|
|
alias Pleroma.User
|
|
|
|
alias Pleroma.Web.CommonAPI
|
|
|
|
|
|
|
|
import Pleroma.Factory
|
|
|
|
|
2019-12-19 07:23:27 -07:00
|
|
|
setup do: oauth_access(["read:statuses"])
|
|
|
|
|
2020-05-30 04:02:37 -06:00
|
|
|
describe "returns a list of conversations" do
|
|
|
|
setup(%{user: user_one, conn: conn}) do
|
|
|
|
user_two = insert(:user)
|
|
|
|
user_three = insert(:user)
|
|
|
|
|
2020-12-01 13:17:52 -07:00
|
|
|
{:ok, user_two, user_one} = User.follow(user_two, user_one)
|
2020-05-30 04:02:37 -06:00
|
|
|
|
|
|
|
{: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 00:37:08 -06:00
|
|
|
assert Participation.unread_count(user_two) == 0
|
2020-05-30 04:02:37 -06:00
|
|
|
{:ok, direct} = create_direct_message(user_one, [user_two, user_three])
|
|
|
|
|
2020-09-01 00:37:08 -06:00
|
|
|
assert Participation.unread_count(user_two) == 1
|
2020-05-30 04:02:37 -06: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 06:01:58 -06:00
|
|
|
assert length(res_accounts) == 2
|
|
|
|
assert user_one.id not in account_ids
|
2020-05-30 04:02:37 -06: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 06:01:58 -06:00
|
|
|
assert res_last_status["account"]["id"] == user_one.id
|
2020-09-01 00:37:08 -06:00
|
|
|
assert Participation.unread_count(user_one) == 0
|
2020-05-30 04:02:37 -06:00
|
|
|
end
|
|
|
|
|
2020-11-03 05:56:12 -07:00
|
|
|
test "includes the user if the user is the only participant", %{
|
2020-10-30 06:01:58 -06:00
|
|
|
user: user_one,
|
|
|
|
conn: conn
|
|
|
|
} do
|
2020-11-03 05:56:12 -07:00
|
|
|
{:ok, _direct} = create_direct_message(user_one, [])
|
2020-10-30 06:01:58 -06:00
|
|
|
|
|
|
|
res_conn = get(conn, "/api/v1/conversations")
|
|
|
|
|
|
|
|
assert response = json_response_and_validate_schema(res_conn, 200)
|
2020-10-30 06:10:19 -06:00
|
|
|
|
2020-10-30 06:01:58 -06:00
|
|
|
assert [
|
|
|
|
%{
|
2020-11-03 05:56:12 -07:00
|
|
|
"accounts" => [account]
|
2020-10-30 06:01:58 -06:00
|
|
|
}
|
|
|
|
] = response
|
2020-10-30 06:10:19 -06:00
|
|
|
|
2020-11-03 05:56:12 -07:00
|
|
|
assert user_one.id == account["id"]
|
2020-10-30 06:01:58 -06:00
|
|
|
end
|
|
|
|
|
2020-05-30 04:02:37 -06: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 15:37:08 -06:00
|
|
|
end
|
2019-10-01 15:37:08 -06:00
|
|
|
|
2019-12-19 07:23:27 -07:00
|
|
|
test "filters conversations by recipients", %{user: user_one, conn: conn} do
|
2019-11-06 17:00:21 -07:00
|
|
|
user_two = insert(:user)
|
|
|
|
user_three = insert(:user)
|
2020-05-30 04:02:37 -06: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-06 17:00:21 -07:00
|
|
|
|
2020-05-05 07:44:46 -06:00
|
|
|
assert [conversation1, conversation2] =
|
|
|
|
conn
|
|
|
|
|> get("/api/v1/conversations?recipients[]=#{user_two.id}")
|
|
|
|
|> json_response_and_validate_schema(200)
|
2019-11-06 17:00:21 -07:00
|
|
|
|
|
|
|
assert conversation1["last_status"]["id"] == direct5.id
|
|
|
|
assert conversation2["last_status"]["id"] == direct1.id
|
|
|
|
|
|
|
|
[conversation1] =
|
|
|
|
conn
|
2020-05-05 07:44:46 -06:00
|
|
|
|> get("/api/v1/conversations?recipients[]=#{user_two.id}&recipients[]=#{user_three.id}")
|
|
|
|
|> json_response_and_validate_schema(200)
|
2019-11-06 17:00:21 -07:00
|
|
|
|
|
|
|
assert conversation1["last_status"]["id"] == direct3.id
|
|
|
|
end
|
|
|
|
|
2019-12-19 07:23:27 -07:00
|
|
|
test "updates the last_status on reply", %{user: user_one, conn: conn} do
|
2019-10-01 15:37:08 -06:00
|
|
|
user_two = insert(:user)
|
2020-05-30 04:02:37 -06:00
|
|
|
{:ok, direct} = create_direct_message(user_one, [user_two])
|
2019-10-01 15:37:08 -06:00
|
|
|
|
|
|
|
{:ok, direct_reply} =
|
|
|
|
CommonAPI.post(user_two, %{
|
2020-05-12 13:59:26 -06:00
|
|
|
status: "reply",
|
|
|
|
visibility: "direct",
|
|
|
|
in_reply_to_status_id: direct.id
|
2019-10-01 15:37:08 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
[%{"last_status" => res_last_status}] =
|
2019-10-01 15:37:08 -06:00
|
|
|
conn
|
2019-10-01 15:37:08 -06:00
|
|
|
|> get("/api/v1/conversations")
|
2020-05-05 07:44:46 -06:00
|
|
|
|> json_response_and_validate_schema(200)
|
2019-10-01 15:37:08 -06:00
|
|
|
|
2019-10-01 15:37:08 -06:00
|
|
|
assert res_last_status["id"] == direct_reply.id
|
|
|
|
end
|
|
|
|
|
2019-12-19 07:23:27 -07:00
|
|
|
test "the user marks a conversation as read", %{user: user_one, conn: conn} do
|
2019-10-01 15:37:08 -06:00
|
|
|
user_two = insert(:user)
|
2020-05-30 04:02:37 -06:00
|
|
|
{:ok, direct} = create_direct_message(user_one, [user_two])
|
2019-10-01 15:37:08 -06:00
|
|
|
|
2020-09-01 00:37:08 -06:00
|
|
|
assert Participation.unread_count(user_one) == 0
|
|
|
|
assert Participation.unread_count(user_two) == 1
|
2019-10-17 06:25:15 -06:00
|
|
|
|
2019-12-19 07:23:27 -07:00
|
|
|
user_two_conn =
|
|
|
|
build_conn()
|
2019-10-17 06:25:15 -06:00
|
|
|
|> assign(:user, user_two)
|
2019-12-19 07:23:27 -07: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 15:37:08 -06:00
|
|
|
|> get("/api/v1/conversations")
|
2020-05-05 07:44:46 -06:00
|
|
|
|> json_response_and_validate_schema(200)
|
2019-10-01 15:37:08 -06:00
|
|
|
|
|
|
|
%{"unread" => false} =
|
2019-12-19 07:23:27 -07:00
|
|
|
user_two_conn
|
2019-10-01 15:37:08 -06:00
|
|
|
|> post("/api/v1/conversations/#{direct_conversation_id}/read")
|
2020-05-05 07:44:46 -06:00
|
|
|
|> json_response_and_validate_schema(200)
|
2019-10-01 15:37:08 -06:00
|
|
|
|
2020-09-01 00:37:08 -06:00
|
|
|
assert Participation.unread_count(user_one) == 0
|
|
|
|
assert Participation.unread_count(user_two) == 0
|
2019-10-01 15:37:08 -06:00
|
|
|
|
|
|
|
# The conversation is marked as unread on reply
|
|
|
|
{:ok, _} =
|
|
|
|
CommonAPI.post(user_two, %{
|
2020-05-12 13:59:26 -06:00
|
|
|
status: "reply",
|
|
|
|
visibility: "direct",
|
|
|
|
in_reply_to_status_id: direct.id
|
2019-10-01 15:37:08 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
[%{"unread" => true}] =
|
|
|
|
conn
|
|
|
|
|> get("/api/v1/conversations")
|
2020-05-05 07:44:46 -06:00
|
|
|
|> json_response_and_validate_schema(200)
|
2019-10-01 15:37:08 -06:00
|
|
|
|
2020-09-01 00:37:08 -06:00
|
|
|
assert Participation.unread_count(user_one) == 1
|
|
|
|
assert Participation.unread_count(user_two) == 0
|
2019-10-01 15:37:08 -06: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 13:59:26 -06:00
|
|
|
status: "reply",
|
|
|
|
visibility: "direct",
|
|
|
|
in_reply_to_status_id: direct.id
|
2019-10-01 15:37:08 -06:00
|
|
|
})
|
|
|
|
|
2020-09-01 00:37:08 -06:00
|
|
|
assert Participation.unread_count(user_one) == 1
|
|
|
|
assert Participation.unread_count(user_two) == 0
|
2019-10-01 15:37:08 -06:00
|
|
|
end
|
|
|
|
|
2019-12-19 07:23:27 -07:00
|
|
|
test "(vanilla) Mastodon frontend behaviour", %{user: user_one, conn: conn} do
|
2019-10-01 15:37:08 -06:00
|
|
|
user_two = insert(:user)
|
2020-05-30 04:02:37 -06:00
|
|
|
{:ok, direct} = create_direct_message(user_one, [user_two])
|
2019-09-30 03:52:07 -06:00
|
|
|
|
2019-12-19 07:23:27 -07:00
|
|
|
res_conn = get(conn, "/api/v1/statuses/#{direct.id}/context")
|
2019-09-30 03:52:07 -06:00
|
|
|
|
2021-02-16 15:39:07 -07:00
|
|
|
assert %{"ancestors" => [], "descendants" => []} ==
|
|
|
|
json_response_and_validate_schema(res_conn, 200)
|
2019-09-30 03:52:07 -06:00
|
|
|
end
|
2020-05-30 04:02:37 -06:00
|
|
|
|
2021-02-15 10:48:13 -07:00
|
|
|
test "Removes a conversation", %{user: user_one, conn: conn} do
|
|
|
|
user_two = insert(:user)
|
|
|
|
token = insert(:oauth_token, user: user_one, scopes: ["read:statuses", "write:conversations"])
|
|
|
|
|
|
|
|
{:ok, _direct} = create_direct_message(user_one, [user_two])
|
|
|
|
{:ok, _direct} = create_direct_message(user_one, [user_two])
|
|
|
|
|
|
|
|
assert [%{"id" => conv1_id}, %{"id" => conv2_id}] =
|
|
|
|
conn
|
|
|
|
|> assign(:token, token)
|
|
|
|
|> get("/api/v1/conversations")
|
|
|
|
|> json_response_and_validate_schema(200)
|
|
|
|
|
|
|
|
assert %{} =
|
|
|
|
conn
|
|
|
|
|> assign(:token, token)
|
|
|
|
|> delete("/api/v1/conversations/#{conv1_id}")
|
|
|
|
|> json_response_and_validate_schema(200)
|
|
|
|
|
|
|
|
assert [%{"id" => ^conv2_id}] =
|
|
|
|
conn
|
|
|
|
|> assign(:token, token)
|
|
|
|
|> get("/api/v1/conversations")
|
|
|
|
|> json_response_and_validate_schema(200)
|
|
|
|
end
|
|
|
|
|
2020-05-30 04:02:37 -06: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 03:52:07 -06:00
|
|
|
end
|