X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;ds=sidebyside;f=test%2Fweb%2Fmastodon_api%2Fcontrollers%2Fconversation_controller_test.exs;h=801b0259b90a140c184b8d2006254a14f3ced4fd;hb=dda65f7799e9dfa2e7b87389848eeee10993a858;hp=7117fc76a3d7399ee01f4a7bba891eb6c077b9b3;hpb=0e356cc8003665ec05a42e39b3a5c47e31ec384c;p=akkoma
diff --git a/test/web/mastodon_api/controllers/conversation_controller_test.exs b/test/web/mastodon_api/controllers/conversation_controller_test.exs
index 7117fc76a..801b0259b 100644
--- a/test/web/mastodon_api/controllers/conversation_controller_test.exs
+++ b/test/web/mastodon_api/controllers/conversation_controller_test.exs
@@ -1,5 +1,5 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2019 Pleroma Authors
+# Copyright © 2017-2020 Pleroma Authors
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.MastodonAPI.ConversationControllerTest do
@@ -10,29 +10,31 @@ defmodule Pleroma.Web.MastodonAPI.ConversationControllerTest do
import Pleroma.Factory
- test "Conversations", %{conn: conn} do
- user_one = insert(:user)
+ setup do: oauth_access(["read:statuses"])
+
+ test "returns a list of conversations", %{user: user_one, conn: conn} do
user_two = insert(:user)
user_three = insert(:user)
{:ok, user_two} = User.follow(user_two, user_one)
+ assert User.get_cached_by_id(user_two.id).unread_conversation_count == 0
+
{:ok, direct} =
CommonAPI.post(user_one, %{
"status" => "Hi @#{user_two.nickname}, @#{user_three.nickname}!",
"visibility" => "direct"
})
+ 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 =
- conn
- |> assign(:user, user_one)
- |> get("/api/v1/conversations")
+ res_conn = get(conn, "/api/v1/conversations")
assert response = json_response(res_conn, 200)
@@ -50,25 +52,156 @@ defmodule Pleroma.Web.MastodonAPI.ConversationControllerTest do
assert user_two.id in account_ids
assert user_three.id in account_ids
assert is_binary(res_id)
- assert unread == true
+ 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 "filters conversations by recipients", %{user: user_one, conn: conn} do
+ 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"
+ })
- # Apparently undocumented API endpoint
- res_conn =
+ {: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)
- |> post("/api/v1/conversations/#{res_id}/read")
+ |> get("/api/v1/conversations", %{"recipients" => [user_two.id]})
+ |> json_response(200)
- assert response = json_response(res_conn, 200)
- assert length(response["accounts"]) == 2
- assert response["last_status"]["id"] == direct.id
- assert response["unread"] == false
+ assert conversation1["last_status"]["id"] == direct5.id
+ assert conversation2["last_status"]["id"] == direct1.id
+
+ [conversation1] =
+ conn
+ |> get("/api/v1/conversations", %{"recipients" => [user_two.id, user_three.id]})
+ |> json_response(200)
+
+ assert conversation1["last_status"]["id"] == direct3.id
+ end
+
+ test "updates the last_status on reply", %{user: user_one, conn: conn} do
+ user_two = insert(:user)
+
+ {:ok, direct} =
+ CommonAPI.post(user_one, %{
+ "status" => "Hi @#{user_two.nickname}",
+ "visibility" => "direct"
+ })
- # (vanilla) Mastodon frontend behaviour
- res_conn =
+ {:ok, direct_reply} =
+ CommonAPI.post(user_two, %{
+ "status" => "reply",
+ "visibility" => "direct",
+ "in_reply_to_status_id" => direct.id
+ })
+
+ [%{"last_status" => res_last_status}] =
conn
- |> assign(:user, user_one)
- |> get("/api/v1/statuses/#{res_last_status["id"]}/context")
+ |> get("/api/v1/conversations")
+ |> json_response(200)
+
+ assert res_last_status["id"] == direct_reply.id
+ end
+
+ test "the user marks a conversation as read", %{user: user_one, conn: conn} do
+ user_two = insert(:user)
+
+ {:ok, direct} =
+ CommonAPI.post(user_one, %{
+ "status" => "Hi @#{user_two.nickname}",
+ "visibility" => "direct"
+ })
+
+ 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
+
+ user_two_conn =
+ build_conn()
+ |> assign(:user, user_two)
+ |> assign(
+ :token,
+ insert(:oauth_token, user: user_two, scopes: ["read:statuses", "write:conversations"])
+ )
+
+ [%{"id" => direct_conversation_id, "unread" => true}] =
+ user_two_conn
+ |> get("/api/v1/conversations")
+ |> json_response(200)
+
+ %{"unread" => false} =
+ user_two_conn
+ |> post("/api/v1/conversations/#{direct_conversation_id}/read")
+ |> json_response(200)
+
+ 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 == 0
+
+ # 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
+ |> get("/api/v1/conversations")
+ |> json_response(200)
+
+ assert User.get_cached_by_id(user_one.id).unread_conversation_count == 1
+ assert User.get_cached_by_id(user_two.id).unread_conversation_count == 0
+
+ # 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
+ })
+
+ assert User.get_cached_by_id(user_one.id).unread_conversation_count == 1
+ assert User.get_cached_by_id(user_two.id).unread_conversation_count == 0
+ end
+
+ test "(vanilla) Mastodon frontend behaviour", %{user: user_one, conn: conn} do
+ user_two = insert(:user)
+
+ {:ok, direct} =
+ CommonAPI.post(user_one, %{
+ "status" => "Hi @#{user_two.nickname}!",
+ "visibility" => "direct"
+ })
+
+ res_conn = get(conn, "/api/v1/statuses/#{direct.id}/context")
assert %{"ancestors" => [], "descendants" => []} == json_response(res_conn, 200)
end