2019-09-30 03:52:07 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
|
|
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# 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-10-01 15:37:08 -06:00
|
|
|
test "returns a list of conversations", %{conn: conn} do
|
2019-09-30 03:52:07 -06:00
|
|
|
user_one = insert(:user)
|
|
|
|
user_two = insert(:user)
|
|
|
|
user_three = insert(:user)
|
|
|
|
|
|
|
|
{:ok, user_two} = User.follow(user_two, user_one)
|
|
|
|
|
2019-10-16 12:59:21 -06:00
|
|
|
assert User.get_cached_by_id(user_two.id).unread_conversation_count == 0
|
2019-10-01 15:37:08 -06:00
|
|
|
|
2019-09-30 03:52:07 -06:00
|
|
|
{:ok, direct} =
|
|
|
|
CommonAPI.post(user_one, %{
|
|
|
|
"status" => "Hi @#{user_two.nickname}, @#{user_three.nickname}!",
|
|
|
|
"visibility" => "direct"
|
|
|
|
})
|
|
|
|
|
2019-10-16 12:59:21 -06:00
|
|
|
assert User.get_cached_by_id(user_two.id).unread_conversation_count == 1
|
2019-10-01 15:37:08 -06:00
|
|
|
|
2019-09-30 03:52:07 -06:00
|
|
|
{:ok, _follower_only} =
|
|
|
|
CommonAPI.post(user_one, %{
|
|
|
|
"status" => "Hi @#{user_two.nickname}!",
|
|
|
|
"visibility" => "private"
|
|
|
|
})
|
|
|
|
|
|
|
|
res_conn =
|
|
|
|
conn
|
|
|
|
|> assign(:user, user_one)
|
|
|
|
|> get("/api/v1/conversations")
|
|
|
|
|
|
|
|
assert response = json_response(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)
|
2019-10-17 06:25:15 -06:00
|
|
|
assert unread == false
|
2019-09-30 03:52:07 -06:00
|
|
|
assert res_last_status["id"] == direct.id
|
2019-10-20 11:43:18 -06:00
|
|
|
assert User.get_cached_by_id(user_one.id).unread_conversation_count == 0
|
2019-10-01 15:37:08 -06:00
|
|
|
end
|
2019-10-01 15:37:08 -06:00
|
|
|
|
2019-11-06 17:00:21 -07:00
|
|
|
test "filters conversations by recipients", %{conn: conn} do
|
|
|
|
user_one = insert(:user)
|
|
|
|
user_two = insert(:user)
|
|
|
|
user_three = insert(:user)
|
|
|
|
|
|
|
|
{:ok, direct1} =
|
|
|
|
CommonAPI.post(user_one, %{
|
|
|
|
"status" => "Hi @#{user_two.nickname}!",
|
|
|
|
"visibility" => "direct"
|
|
|
|
})
|
|
|
|
|
|
|
|
{:ok, _direct2} =
|
|
|
|
CommonAPI.post(user_one, %{
|
|
|
|
"status" => "Hi @#{user_three.nickname}!",
|
|
|
|
"visibility" => "direct"
|
|
|
|
})
|
|
|
|
|
|
|
|
{:ok, direct3} =
|
|
|
|
CommonAPI.post(user_one, %{
|
|
|
|
"status" => "Hi @#{user_two.nickname}, @#{user_three.nickname}!",
|
|
|
|
"visibility" => "direct"
|
|
|
|
})
|
|
|
|
|
|
|
|
{:ok, _direct4} =
|
|
|
|
CommonAPI.post(user_two, %{
|
|
|
|
"status" => "Hi @#{user_three.nickname}!",
|
|
|
|
"visibility" => "direct"
|
|
|
|
})
|
|
|
|
|
|
|
|
{:ok, direct5} =
|
|
|
|
CommonAPI.post(user_two, %{
|
|
|
|
"status" => "Hi @#{user_one.nickname}!",
|
|
|
|
"visibility" => "direct"
|
|
|
|
})
|
|
|
|
|
|
|
|
[conversation1, conversation2] =
|
|
|
|
conn
|
|
|
|
|> assign(:user, user_one)
|
|
|
|
|> get("/api/v1/conversations", %{"recipients" => [user_two.id]})
|
|
|
|
|> json_response(200)
|
|
|
|
|
|
|
|
assert conversation1["last_status"]["id"] == direct5.id
|
|
|
|
assert conversation2["last_status"]["id"] == direct1.id
|
|
|
|
|
|
|
|
[conversation1] =
|
|
|
|
conn
|
|
|
|
|> assign(:user, user_one)
|
|
|
|
|> get("/api/v1/conversations", %{"recipients" => [user_two.id, user_three.id]})
|
|
|
|
|> json_response(200)
|
|
|
|
|
|
|
|
assert conversation1["last_status"]["id"] == direct3.id
|
|
|
|
end
|
|
|
|
|
2019-10-01 15:37:08 -06:00
|
|
|
test "updates the last_status on reply", %{conn: conn} do
|
|
|
|
user_one = insert(:user)
|
|
|
|
user_two = insert(:user)
|
|
|
|
|
|
|
|
{:ok, direct} =
|
|
|
|
CommonAPI.post(user_one, %{
|
|
|
|
"status" => "Hi @#{user_two.nickname}",
|
|
|
|
"visibility" => "direct"
|
|
|
|
})
|
|
|
|
|
|
|
|
{:ok, direct_reply} =
|
|
|
|
CommonAPI.post(user_two, %{
|
|
|
|
"status" => "reply",
|
|
|
|
"visibility" => "direct",
|
|
|
|
"in_reply_to_status_id" => direct.id
|
|
|
|
})
|
|
|
|
|
|
|
|
[%{"last_status" => res_last_status}] =
|
2019-10-01 15:37:08 -06:00
|
|
|
conn
|
|
|
|
|> assign(:user, user_one)
|
2019-10-01 15:37:08 -06:00
|
|
|
|> get("/api/v1/conversations")
|
|
|
|
|> json_response(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
|
|
|
|
|
|
|
|
test "the user marks a conversation as read", %{conn: conn} do
|
|
|
|
user_one = insert(:user)
|
|
|
|
user_two = insert(:user)
|
|
|
|
|
|
|
|
{:ok, direct} =
|
|
|
|
CommonAPI.post(user_one, %{
|
|
|
|
"status" => "Hi @#{user_two.nickname}",
|
|
|
|
"visibility" => "direct"
|
|
|
|
})
|
|
|
|
|
2019-10-20 13:29:56 -06: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 06:25:15 -06:00
|
|
|
|
2019-10-01 15:37:08 -06:00
|
|
|
[%{"id" => direct_conversation_id, "unread" => true}] =
|
|
|
|
conn
|
2019-10-17 06:25:15 -06:00
|
|
|
|> assign(:user, user_two)
|
2019-10-01 15:37:08 -06:00
|
|
|
|> get("/api/v1/conversations")
|
|
|
|
|> json_response(200)
|
|
|
|
|
|
|
|
%{"unread" => false} =
|
|
|
|
conn
|
2019-10-17 06:25:15 -06:00
|
|
|
|> assign(:user, user_two)
|
2019-10-01 15:37:08 -06:00
|
|
|
|> post("/api/v1/conversations/#{direct_conversation_id}/read")
|
|
|
|
|> json_response(200)
|
|
|
|
|
2019-10-16 12:59:21 -06:00
|
|
|
assert User.get_cached_by_id(user_one.id).unread_conversation_count == 0
|
2019-10-20 11:43:18 -06:00
|
|
|
assert User.get_cached_by_id(user_two.id).unread_conversation_count == 0
|
2019-10-01 15:37:08 -06:00
|
|
|
|
|
|
|
# The conversation is marked as unread on reply
|
|
|
|
{:ok, _} =
|
|
|
|
CommonAPI.post(user_two, %{
|
|
|
|
"status" => "reply",
|
|
|
|
"visibility" => "direct",
|
|
|
|
"in_reply_to_status_id" => direct.id
|
|
|
|
})
|
|
|
|
|
|
|
|
[%{"unread" => true}] =
|
|
|
|
conn
|
|
|
|
|> assign(:user, user_one)
|
|
|
|
|> get("/api/v1/conversations")
|
|
|
|
|> json_response(200)
|
|
|
|
|
2019-10-16 12:59:21 -06:00
|
|
|
assert User.get_cached_by_id(user_one.id).unread_conversation_count == 1
|
2019-10-20 11:43:18 -06:00
|
|
|
assert User.get_cached_by_id(user_two.id).unread_conversation_count == 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, %{
|
|
|
|
"status" => "reply",
|
|
|
|
"visibility" => "direct",
|
|
|
|
"in_reply_to_status_id" => direct.id
|
|
|
|
})
|
|
|
|
|
2019-10-16 12:59:21 -06:00
|
|
|
assert User.get_cached_by_id(user_one.id).unread_conversation_count == 1
|
2019-10-20 11:43:18 -06:00
|
|
|
assert User.get_cached_by_id(user_two.id).unread_conversation_count == 0
|
2019-10-01 15:37:08 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
test "(vanilla) Mastodon frontend behaviour", %{conn: conn} do
|
|
|
|
user_one = insert(:user)
|
|
|
|
user_two = insert(:user)
|
|
|
|
|
|
|
|
{:ok, direct} =
|
|
|
|
CommonAPI.post(user_one, %{
|
|
|
|
"status" => "Hi @#{user_two.nickname}!",
|
|
|
|
"visibility" => "direct"
|
|
|
|
})
|
2019-09-30 03:52:07 -06:00
|
|
|
|
|
|
|
res_conn =
|
|
|
|
conn
|
|
|
|
|> assign(:user, user_one)
|
2019-10-01 15:37:08 -06:00
|
|
|
|> get("/api/v1/statuses/#{direct.id}/context")
|
2019-09-30 03:52:07 -06:00
|
|
|
|
|
|
|
assert %{"ancestors" => [], "descendants" => []} == json_response(res_conn, 200)
|
|
|
|
end
|
|
|
|
end
|