2019-09-30 05:52:07 -04:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-02 00:08:45 -05:00
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
2019-09-30 05:52:07 -04:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.MastodonAPI.ConversationControllerTest do
|
|
|
|
use Pleroma.Web.ConnCase
|
|
|
|
|
|
|
|
alias Pleroma.User
|
|
|
|
alias Pleroma.Web.CommonAPI
|
|
|
|
|
|
|
|
import Pleroma.Factory
|
|
|
|
|
2019-12-19 09:23:27 -05:00
|
|
|
setup do: oauth_access(["read:statuses"])
|
|
|
|
|
2020-05-30 06:02:37 -04: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
|
|
|
|
assert User.get_cached_by_id(user_two.id).unread_conversation_count == 0
|
|
|
|
{:ok, direct} = create_direct_message(user_one, [user_two, user_three])
|
|
|
|
|
|
|
|
assert User.get_cached_by_id(user_two.id).unread_conversation_count == 1
|
|
|
|
|
|
|
|
{: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"])
|
|
|
|
assert length(res_accounts) == 2
|
|
|
|
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
|
|
|
|
assert User.get_cached_by_id(user_one.id).unread_conversation_count == 0
|
|
|
|
end
|
|
|
|
|
|
|
|
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 17:37:08 -04:00
|
|
|
end
|
2019-10-01 17:37:08 -04:00
|
|
|
|
2019-12-19 09:23:27 -05:00
|
|
|
test "filters conversations by recipients", %{user: user_one, conn: conn} do
|
2019-11-06 19:00:21 -05:00
|
|
|
user_two = insert(:user)
|
|
|
|
user_three = insert(:user)
|
2020-05-30 06:02:37 -04: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 19:00:21 -05:00
|
|
|
|
2020-05-05 09:44:46 -04:00
|
|
|
assert [conversation1, conversation2] =
|
|
|
|
conn
|
|
|
|
|> get("/api/v1/conversations?recipients[]=#{user_two.id}")
|
|
|
|
|> json_response_and_validate_schema(200)
|
2019-11-06 19:00:21 -05:00
|
|
|
|
|
|
|
assert conversation1["last_status"]["id"] == direct5.id
|
|
|
|
assert conversation2["last_status"]["id"] == direct1.id
|
|
|
|
|
|
|
|
[conversation1] =
|
|
|
|
conn
|
2020-05-05 09:44:46 -04:00
|
|
|
|> get("/api/v1/conversations?recipients[]=#{user_two.id}&recipients[]=#{user_three.id}")
|
|
|
|
|> json_response_and_validate_schema(200)
|
2019-11-06 19:00:21 -05:00
|
|
|
|
|
|
|
assert conversation1["last_status"]["id"] == direct3.id
|
|
|
|
end
|
|
|
|
|
2019-12-19 09:23:27 -05:00
|
|
|
test "updates the last_status on reply", %{user: user_one, conn: conn} do
|
2019-10-01 17:37:08 -04:00
|
|
|
user_two = insert(:user)
|
2020-05-30 06:02:37 -04:00
|
|
|
{:ok, direct} = create_direct_message(user_one, [user_two])
|
2019-10-01 17:37:08 -04:00
|
|
|
|
|
|
|
{:ok, direct_reply} =
|
|
|
|
CommonAPI.post(user_two, %{
|
2020-05-12 15:59:26 -04:00
|
|
|
status: "reply",
|
|
|
|
visibility: "direct",
|
|
|
|
in_reply_to_status_id: direct.id
|
2019-10-01 17:37:08 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
[%{"last_status" => res_last_status}] =
|
2019-10-01 17:37:08 -04:00
|
|
|
conn
|
2019-10-01 17:37:08 -04:00
|
|
|
|> get("/api/v1/conversations")
|
2020-05-05 09:44:46 -04:00
|
|
|
|> json_response_and_validate_schema(200)
|
2019-10-01 17:37:08 -04:00
|
|
|
|
2019-10-01 17:37:08 -04:00
|
|
|
assert res_last_status["id"] == direct_reply.id
|
|
|
|
end
|
|
|
|
|
2019-12-19 09:23:27 -05:00
|
|
|
test "the user marks a conversation as read", %{user: user_one, conn: conn} do
|
2019-10-01 17:37:08 -04:00
|
|
|
user_two = insert(:user)
|
2020-05-30 06:02:37 -04:00
|
|
|
{:ok, direct} = create_direct_message(user_one, [user_two])
|
2019-10-01 17:37:08 -04:00
|
|
|
|
2019-10-20 15:29:56 -04:00
|
|
|
assert User.get_cached_by_id(user_one.id).unread_conversation_count == 0
|
|
|
|
assert User.get_cached_by_id(user_two.id).unread_conversation_count == 1
|
2019-10-17 08:25:15 -04:00
|
|
|
|
2019-12-19 09:23:27 -05:00
|
|
|
user_two_conn =
|
|
|
|
build_conn()
|
2019-10-17 08:25:15 -04:00
|
|
|
|> assign(:user, user_two)
|
2019-12-19 09:23:27 -05: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 17:37:08 -04:00
|
|
|
|> get("/api/v1/conversations")
|
2020-05-05 09:44:46 -04:00
|
|
|
|> json_response_and_validate_schema(200)
|
2019-10-01 17:37:08 -04:00
|
|
|
|
|
|
|
%{"unread" => false} =
|
2019-12-19 09:23:27 -05:00
|
|
|
user_two_conn
|
2019-10-01 17:37:08 -04:00
|
|
|
|> post("/api/v1/conversations/#{direct_conversation_id}/read")
|
2020-05-05 09:44:46 -04:00
|
|
|
|> json_response_and_validate_schema(200)
|
2019-10-01 17:37:08 -04:00
|
|
|
|
2019-10-16 14:59:21 -04:00
|
|
|
assert User.get_cached_by_id(user_one.id).unread_conversation_count == 0
|
2019-10-20 13:43:18 -04:00
|
|
|
assert User.get_cached_by_id(user_two.id).unread_conversation_count == 0
|
2019-10-01 17:37:08 -04:00
|
|
|
|
|
|
|
# The conversation is marked as unread on reply
|
|
|
|
{:ok, _} =
|
|
|
|
CommonAPI.post(user_two, %{
|
2020-05-12 15:59:26 -04:00
|
|
|
status: "reply",
|
|
|
|
visibility: "direct",
|
|
|
|
in_reply_to_status_id: direct.id
|
2019-10-01 17:37:08 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
[%{"unread" => true}] =
|
|
|
|
conn
|
|
|
|
|> get("/api/v1/conversations")
|
2020-05-05 09:44:46 -04:00
|
|
|
|> json_response_and_validate_schema(200)
|
2019-10-01 17:37:08 -04:00
|
|
|
|
2019-10-16 14:59:21 -04:00
|
|
|
assert User.get_cached_by_id(user_one.id).unread_conversation_count == 1
|
2019-10-20 13:43:18 -04:00
|
|
|
assert User.get_cached_by_id(user_two.id).unread_conversation_count == 0
|
2019-10-01 17:37:08 -04: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 15:59:26 -04:00
|
|
|
status: "reply",
|
|
|
|
visibility: "direct",
|
|
|
|
in_reply_to_status_id: direct.id
|
2019-10-01 17:37:08 -04:00
|
|
|
})
|
|
|
|
|
2019-10-16 14:59:21 -04:00
|
|
|
assert User.get_cached_by_id(user_one.id).unread_conversation_count == 1
|
2019-10-20 13:43:18 -04:00
|
|
|
assert User.get_cached_by_id(user_two.id).unread_conversation_count == 0
|
2019-10-01 17:37:08 -04:00
|
|
|
end
|
|
|
|
|
2019-12-19 09:23:27 -05:00
|
|
|
test "(vanilla) Mastodon frontend behaviour", %{user: user_one, conn: conn} do
|
2019-10-01 17:37:08 -04:00
|
|
|
user_two = insert(:user)
|
2020-05-30 06:02:37 -04:00
|
|
|
{:ok, direct} = create_direct_message(user_one, [user_two])
|
2019-09-30 05:52:07 -04:00
|
|
|
|
2019-12-19 09:23:27 -05:00
|
|
|
res_conn = get(conn, "/api/v1/statuses/#{direct.id}/context")
|
2019-09-30 05:52:07 -04:00
|
|
|
|
|
|
|
assert %{"ancestors" => [], "descendants" => []} == json_response(res_conn, 200)
|
|
|
|
end
|
2020-05-30 06:02:37 -04: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 05:52:07 -04:00
|
|
|
end
|