1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MastodonAPI.ConversationControllerTest do
6 use Pleroma.Web.ConnCase, async: true
8 alias Pleroma.Conversation.Participation
10 alias Pleroma.Web.CommonAPI
12 import Pleroma.Factory
14 setup do: oauth_access(["read:statuses"])
16 describe "returns a list of conversations" do
17 setup(%{user: user_one, conn: conn}) do
18 user_two = insert(:user)
19 user_three = insert(:user)
21 {:ok, user_two, user_one} = User.follow(user_two, user_one)
23 {:ok, %{user: user_one, user_two: user_two, user_three: user_three, conn: conn}}
26 test "returns correct conversations", %{
29 user_three: user_three,
32 assert Participation.unread_count(user_two) == 0
33 {:ok, direct} = create_direct_message(user_one, [user_two, user_three])
35 assert Participation.unread_count(user_two) == 1
37 {:ok, _follower_only} =
38 CommonAPI.post(user_one, %{
39 status: "Hi @#{user_two.nickname}!",
43 res_conn = get(conn, "/api/v1/conversations")
45 assert response = json_response_and_validate_schema(res_conn, 200)
50 "accounts" => res_accounts,
51 "last_status" => res_last_status,
56 account_ids = Enum.map(res_accounts, & &1["id"])
57 assert length(res_accounts) == 2
58 assert user_one.id not in account_ids
59 assert user_two.id in account_ids
60 assert user_three.id in account_ids
61 assert is_binary(res_id)
62 assert unread == false
63 assert res_last_status["id"] == direct.id
64 assert res_last_status["account"]["id"] == user_one.id
65 assert Participation.unread_count(user_one) == 0
68 test "includes the user if the user is the only participant", %{
72 {:ok, _direct} = create_direct_message(user_one, [])
74 res_conn = get(conn, "/api/v1/conversations")
76 assert response = json_response_and_validate_schema(res_conn, 200)
80 "accounts" => [account]
84 assert user_one.id == account["id"]
87 test "observes limit params", %{
90 user_three: user_three,
93 {:ok, _} = create_direct_message(user_one, [user_two, user_three])
94 {:ok, _} = create_direct_message(user_two, [user_one, user_three])
95 {:ok, _} = create_direct_message(user_three, [user_two, user_one])
97 res_conn = get(conn, "/api/v1/conversations?limit=1")
99 assert response = json_response_and_validate_schema(res_conn, 200)
101 assert Enum.count(response) == 1
103 res_conn = get(conn, "/api/v1/conversations?limit=2")
105 assert response = json_response_and_validate_schema(res_conn, 200)
107 assert Enum.count(response) == 2
111 test "filters conversations by recipients", %{user: user_one, conn: conn} do
112 user_two = insert(:user)
113 user_three = insert(:user)
114 {:ok, direct1} = create_direct_message(user_one, [user_two])
115 {:ok, _direct2} = create_direct_message(user_one, [user_three])
116 {:ok, direct3} = create_direct_message(user_one, [user_two, user_three])
117 {:ok, _direct4} = create_direct_message(user_two, [user_three])
118 {:ok, direct5} = create_direct_message(user_two, [user_one])
120 assert [conversation1, conversation2] =
122 |> get("/api/v1/conversations?recipients[]=#{user_two.id}")
123 |> json_response_and_validate_schema(200)
125 assert conversation1["last_status"]["id"] == direct5.id
126 assert conversation2["last_status"]["id"] == direct1.id
130 |> get("/api/v1/conversations?recipients[]=#{user_two.id}&recipients[]=#{user_three.id}")
131 |> json_response_and_validate_schema(200)
133 assert conversation1["last_status"]["id"] == direct3.id
136 test "updates the last_status on reply", %{user: user_one, conn: conn} do
137 user_two = insert(:user)
138 {:ok, direct} = create_direct_message(user_one, [user_two])
140 {:ok, direct_reply} =
141 CommonAPI.post(user_two, %{
143 visibility: "direct",
144 in_reply_to_status_id: direct.id
147 [%{"last_status" => res_last_status}] =
149 |> get("/api/v1/conversations")
150 |> json_response_and_validate_schema(200)
152 assert res_last_status["id"] == direct_reply.id
155 test "the user marks a conversation as read", %{user: user_one, conn: conn} do
156 user_two = insert(:user)
157 {:ok, direct} = create_direct_message(user_one, [user_two])
159 assert Participation.unread_count(user_one) == 0
160 assert Participation.unread_count(user_two) == 1
164 |> assign(:user, user_two)
167 insert(:oauth_token, user: user_two, scopes: ["read:statuses", "write:conversations"])
170 [%{"id" => direct_conversation_id, "unread" => true}] =
172 |> get("/api/v1/conversations")
173 |> json_response_and_validate_schema(200)
175 %{"unread" => false} =
177 |> post("/api/v1/conversations/#{direct_conversation_id}/read")
178 |> json_response_and_validate_schema(200)
180 assert Participation.unread_count(user_one) == 0
181 assert Participation.unread_count(user_two) == 0
183 # The conversation is marked as unread on reply
185 CommonAPI.post(user_two, %{
187 visibility: "direct",
188 in_reply_to_status_id: direct.id
191 [%{"unread" => true}] =
193 |> get("/api/v1/conversations")
194 |> json_response_and_validate_schema(200)
196 assert Participation.unread_count(user_one) == 1
197 assert Participation.unread_count(user_two) == 0
199 # A reply doesn't increment the user's unread_conversation_count if the conversation is unread
201 CommonAPI.post(user_two, %{
203 visibility: "direct",
204 in_reply_to_status_id: direct.id
207 assert Participation.unread_count(user_one) == 1
208 assert Participation.unread_count(user_two) == 0
211 test "(vanilla) Mastodon frontend behaviour", %{user: user_one, conn: conn} do
212 user_two = insert(:user)
213 {:ok, direct} = create_direct_message(user_one, [user_two])
215 res_conn = get(conn, "/api/v1/statuses/#{direct.id}/context")
217 assert %{"ancestors" => [], "descendants" => []} ==
218 json_response_and_validate_schema(res_conn, 200)
221 test "Removes a conversation", %{user: user_one, conn: conn} do
222 user_two = insert(:user)
223 token = insert(:oauth_token, user: user_one, scopes: ["read:statuses", "write:conversations"])
225 {:ok, _direct} = create_direct_message(user_one, [user_two])
226 {:ok, _direct} = create_direct_message(user_one, [user_two])
228 assert [%{"id" => conv1_id}, %{"id" => conv2_id}] =
230 |> assign(:token, token)
231 |> get("/api/v1/conversations")
232 |> json_response_and_validate_schema(200)
236 |> assign(:token, token)
237 |> delete("/api/v1/conversations/#{conv1_id}")
238 |> json_response_and_validate_schema(200)
240 assert [%{"id" => ^conv2_id}] =
242 |> assign(:token, token)
243 |> get("/api/v1/conversations")
244 |> json_response_and_validate_schema(200)
247 defp create_direct_message(sender, recips) do
250 |> Enum.map(fn s -> "@#{s.nickname}" end)
253 CommonAPI.post(sender, %{
254 status: "Hi #{hellos}!",