logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

conversation_controller_test.exs (6788B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.MastodonAPI.ConversationControllerTest do
  5. use Pleroma.Web.ConnCase
  6. alias Pleroma.User
  7. alias Pleroma.Web.CommonAPI
  8. import Pleroma.Factory
  9. setup do: oauth_access(["read:statuses"])
  10. describe "returns a list of conversations" do
  11. setup(%{user: user_one, conn: conn}) do
  12. user_two = insert(:user)
  13. user_three = insert(:user)
  14. {:ok, user_two} = User.follow(user_two, user_one)
  15. {:ok, %{user: user_one, user_two: user_two, user_three: user_three, conn: conn}}
  16. end
  17. test "returns correct conversations", %{
  18. user: user_one,
  19. user_two: user_two,
  20. user_three: user_three,
  21. conn: conn
  22. } do
  23. assert User.get_cached_by_id(user_two.id).unread_conversation_count == 0
  24. {:ok, direct} = create_direct_message(user_one, [user_two, user_three])
  25. assert User.get_cached_by_id(user_two.id).unread_conversation_count == 1
  26. {:ok, _follower_only} =
  27. CommonAPI.post(user_one, %{
  28. status: "Hi @#{user_two.nickname}!",
  29. visibility: "private"
  30. })
  31. res_conn = get(conn, "/api/v1/conversations")
  32. assert response = json_response_and_validate_schema(res_conn, 200)
  33. assert [
  34. %{
  35. "id" => res_id,
  36. "accounts" => res_accounts,
  37. "last_status" => res_last_status,
  38. "unread" => unread
  39. }
  40. ] = response
  41. account_ids = Enum.map(res_accounts, & &1["id"])
  42. assert length(res_accounts) == 2
  43. assert user_two.id in account_ids
  44. assert user_three.id in account_ids
  45. assert is_binary(res_id)
  46. assert unread == false
  47. assert res_last_status["id"] == direct.id
  48. assert User.get_cached_by_id(user_one.id).unread_conversation_count == 0
  49. end
  50. test "observes limit params", %{
  51. user: user_one,
  52. user_two: user_two,
  53. user_three: user_three,
  54. conn: conn
  55. } do
  56. {:ok, _} = create_direct_message(user_one, [user_two, user_three])
  57. {:ok, _} = create_direct_message(user_two, [user_one, user_three])
  58. {:ok, _} = create_direct_message(user_three, [user_two, user_one])
  59. res_conn = get(conn, "/api/v1/conversations?limit=1")
  60. assert response = json_response_and_validate_schema(res_conn, 200)
  61. assert Enum.count(response) == 1
  62. res_conn = get(conn, "/api/v1/conversations?limit=2")
  63. assert response = json_response_and_validate_schema(res_conn, 200)
  64. assert Enum.count(response) == 2
  65. end
  66. end
  67. test "filters conversations by recipients", %{user: user_one, conn: conn} do
  68. user_two = insert(:user)
  69. user_three = insert(:user)
  70. {:ok, direct1} = create_direct_message(user_one, [user_two])
  71. {:ok, _direct2} = create_direct_message(user_one, [user_three])
  72. {:ok, direct3} = create_direct_message(user_one, [user_two, user_three])
  73. {:ok, _direct4} = create_direct_message(user_two, [user_three])
  74. {:ok, direct5} = create_direct_message(user_two, [user_one])
  75. assert [conversation1, conversation2] =
  76. conn
  77. |> get("/api/v1/conversations?recipients[]=#{user_two.id}")
  78. |> json_response_and_validate_schema(200)
  79. assert conversation1["last_status"]["id"] == direct5.id
  80. assert conversation2["last_status"]["id"] == direct1.id
  81. [conversation1] =
  82. conn
  83. |> get("/api/v1/conversations?recipients[]=#{user_two.id}&recipients[]=#{user_three.id}")
  84. |> json_response_and_validate_schema(200)
  85. assert conversation1["last_status"]["id"] == direct3.id
  86. end
  87. test "updates the last_status on reply", %{user: user_one, conn: conn} do
  88. user_two = insert(:user)
  89. {:ok, direct} = create_direct_message(user_one, [user_two])
  90. {:ok, direct_reply} =
  91. CommonAPI.post(user_two, %{
  92. status: "reply",
  93. visibility: "direct",
  94. in_reply_to_status_id: direct.id
  95. })
  96. [%{"last_status" => res_last_status}] =
  97. conn
  98. |> get("/api/v1/conversations")
  99. |> json_response_and_validate_schema(200)
  100. assert res_last_status["id"] == direct_reply.id
  101. end
  102. test "the user marks a conversation as read", %{user: user_one, conn: conn} do
  103. user_two = insert(:user)
  104. {:ok, direct} = create_direct_message(user_one, [user_two])
  105. assert User.get_cached_by_id(user_one.id).unread_conversation_count == 0
  106. assert User.get_cached_by_id(user_two.id).unread_conversation_count == 1
  107. user_two_conn =
  108. build_conn()
  109. |> assign(:user, user_two)
  110. |> assign(
  111. :token,
  112. insert(:oauth_token, user: user_two, scopes: ["read:statuses", "write:conversations"])
  113. )
  114. [%{"id" => direct_conversation_id, "unread" => true}] =
  115. user_two_conn
  116. |> get("/api/v1/conversations")
  117. |> json_response_and_validate_schema(200)
  118. %{"unread" => false} =
  119. user_two_conn
  120. |> post("/api/v1/conversations/#{direct_conversation_id}/read")
  121. |> json_response_and_validate_schema(200)
  122. assert User.get_cached_by_id(user_one.id).unread_conversation_count == 0
  123. assert User.get_cached_by_id(user_two.id).unread_conversation_count == 0
  124. # The conversation is marked as unread on reply
  125. {:ok, _} =
  126. CommonAPI.post(user_two, %{
  127. status: "reply",
  128. visibility: "direct",
  129. in_reply_to_status_id: direct.id
  130. })
  131. [%{"unread" => true}] =
  132. conn
  133. |> get("/api/v1/conversations")
  134. |> json_response_and_validate_schema(200)
  135. assert User.get_cached_by_id(user_one.id).unread_conversation_count == 1
  136. assert User.get_cached_by_id(user_two.id).unread_conversation_count == 0
  137. # A reply doesn't increment the user's unread_conversation_count if the conversation is unread
  138. {:ok, _} =
  139. CommonAPI.post(user_two, %{
  140. status: "reply",
  141. visibility: "direct",
  142. in_reply_to_status_id: direct.id
  143. })
  144. assert User.get_cached_by_id(user_one.id).unread_conversation_count == 1
  145. assert User.get_cached_by_id(user_two.id).unread_conversation_count == 0
  146. end
  147. test "(vanilla) Mastodon frontend behaviour", %{user: user_one, conn: conn} do
  148. user_two = insert(:user)
  149. {:ok, direct} = create_direct_message(user_one, [user_two])
  150. res_conn = get(conn, "/api/v1/statuses/#{direct.id}/context")
  151. assert %{"ancestors" => [], "descendants" => []} == json_response(res_conn, 200)
  152. end
  153. defp create_direct_message(sender, recips) do
  154. hellos =
  155. recips
  156. |> Enum.map(fn s -> "@#{s.nickname}" end)
  157. |> Enum.join(", ")
  158. CommonAPI.post(sender, %{
  159. status: "Hi #{hellos}!",
  160. visibility: "direct"
  161. })
  162. end
  163. end