logo

pleroma

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

chat_controller_test.exs (12864B)


  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.PleromaAPI.ChatControllerTest do
  5. use Pleroma.Web.ConnCase
  6. alias Pleroma.Chat
  7. alias Pleroma.Chat.MessageReference
  8. alias Pleroma.Object
  9. alias Pleroma.User
  10. alias Pleroma.Web.ActivityPub.ActivityPub
  11. alias Pleroma.Web.CommonAPI
  12. import Pleroma.Factory
  13. describe "POST /api/v1/pleroma/chats/:id/messages/:message_id/read" do
  14. setup do: oauth_access(["write:chats"])
  15. test "it marks one message as read", %{conn: conn, user: user} do
  16. other_user = insert(:user)
  17. {:ok, create} = CommonAPI.post_chat_message(other_user, user, "sup")
  18. {:ok, _create} = CommonAPI.post_chat_message(other_user, user, "sup part 2")
  19. {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
  20. object = Object.normalize(create, false)
  21. cm_ref = MessageReference.for_chat_and_object(chat, object)
  22. assert cm_ref.unread == true
  23. result =
  24. conn
  25. |> post("/api/v1/pleroma/chats/#{chat.id}/messages/#{cm_ref.id}/read")
  26. |> json_response_and_validate_schema(200)
  27. assert result["unread"] == false
  28. cm_ref = MessageReference.for_chat_and_object(chat, object)
  29. assert cm_ref.unread == false
  30. end
  31. end
  32. describe "POST /api/v1/pleroma/chats/:id/read" do
  33. setup do: oauth_access(["write:chats"])
  34. test "given a `last_read_id`, it marks everything until then as read", %{
  35. conn: conn,
  36. user: user
  37. } do
  38. other_user = insert(:user)
  39. {:ok, create} = CommonAPI.post_chat_message(other_user, user, "sup")
  40. {:ok, _create} = CommonAPI.post_chat_message(other_user, user, "sup part 2")
  41. {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
  42. object = Object.normalize(create, false)
  43. cm_ref = MessageReference.for_chat_and_object(chat, object)
  44. assert cm_ref.unread == true
  45. result =
  46. conn
  47. |> put_req_header("content-type", "application/json")
  48. |> post("/api/v1/pleroma/chats/#{chat.id}/read", %{"last_read_id" => cm_ref.id})
  49. |> json_response_and_validate_schema(200)
  50. assert result["unread"] == 1
  51. cm_ref = MessageReference.for_chat_and_object(chat, object)
  52. assert cm_ref.unread == false
  53. end
  54. end
  55. describe "POST /api/v1/pleroma/chats/:id/messages" do
  56. setup do: oauth_access(["write:chats"])
  57. test "it posts a message to the chat", %{conn: conn, user: user} do
  58. other_user = insert(:user)
  59. {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
  60. result =
  61. conn
  62. |> put_req_header("content-type", "application/json")
  63. |> post("/api/v1/pleroma/chats/#{chat.id}/messages", %{"content" => "Hallo!!"})
  64. |> json_response_and_validate_schema(200)
  65. assert result["content"] == "Hallo!!"
  66. assert result["chat_id"] == chat.id |> to_string()
  67. end
  68. test "it fails if there is no content", %{conn: conn, user: user} do
  69. other_user = insert(:user)
  70. {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
  71. result =
  72. conn
  73. |> put_req_header("content-type", "application/json")
  74. |> post("/api/v1/pleroma/chats/#{chat.id}/messages")
  75. |> json_response_and_validate_schema(400)
  76. assert %{"error" => "no_content"} == result
  77. end
  78. test "it works with an attachment", %{conn: conn, user: user} do
  79. file = %Plug.Upload{
  80. content_type: "image/jpg",
  81. path: Path.absname("test/fixtures/image.jpg"),
  82. filename: "an_image.jpg"
  83. }
  84. {:ok, upload} = ActivityPub.upload(file, actor: user.ap_id)
  85. other_user = insert(:user)
  86. {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
  87. result =
  88. conn
  89. |> put_req_header("content-type", "application/json")
  90. |> post("/api/v1/pleroma/chats/#{chat.id}/messages", %{
  91. "media_id" => to_string(upload.id)
  92. })
  93. |> json_response_and_validate_schema(200)
  94. assert result["attachment"]
  95. end
  96. test "gets MRF reason when rejected", %{conn: conn, user: user} do
  97. clear_config([:mrf_keyword, :reject], ["GNO"])
  98. clear_config([:mrf, :policies], [Pleroma.Web.ActivityPub.MRF.KeywordPolicy])
  99. other_user = insert(:user)
  100. {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
  101. result =
  102. conn
  103. |> put_req_header("content-type", "application/json")
  104. |> post("/api/v1/pleroma/chats/#{chat.id}/messages", %{"content" => "GNO/Linux"})
  105. |> json_response_and_validate_schema(422)
  106. assert %{"error" => "[KeywordPolicy] Matches with rejected keyword"} == result
  107. end
  108. end
  109. describe "DELETE /api/v1/pleroma/chats/:id/messages/:message_id" do
  110. setup do: oauth_access(["write:chats"])
  111. test "it deletes a message from the chat", %{conn: conn, user: user} do
  112. recipient = insert(:user)
  113. {:ok, message} =
  114. CommonAPI.post_chat_message(user, recipient, "Hello darkness my old friend")
  115. {:ok, other_message} = CommonAPI.post_chat_message(recipient, user, "nico nico ni")
  116. object = Object.normalize(message, false)
  117. chat = Chat.get(user.id, recipient.ap_id)
  118. cm_ref = MessageReference.for_chat_and_object(chat, object)
  119. # Deleting your own message removes the message and the reference
  120. result =
  121. conn
  122. |> put_req_header("content-type", "application/json")
  123. |> delete("/api/v1/pleroma/chats/#{chat.id}/messages/#{cm_ref.id}")
  124. |> json_response_and_validate_schema(200)
  125. assert result["id"] == cm_ref.id
  126. refute MessageReference.get_by_id(cm_ref.id)
  127. assert %{data: %{"type" => "Tombstone"}} = Object.get_by_id(object.id)
  128. # Deleting other people's messages just removes the reference
  129. object = Object.normalize(other_message, false)
  130. cm_ref = MessageReference.for_chat_and_object(chat, object)
  131. result =
  132. conn
  133. |> put_req_header("content-type", "application/json")
  134. |> delete("/api/v1/pleroma/chats/#{chat.id}/messages/#{cm_ref.id}")
  135. |> json_response_and_validate_schema(200)
  136. assert result["id"] == cm_ref.id
  137. refute MessageReference.get_by_id(cm_ref.id)
  138. assert Object.get_by_id(object.id)
  139. end
  140. end
  141. describe "GET /api/v1/pleroma/chats/:id/messages" do
  142. setup do: oauth_access(["read:chats"])
  143. test "it paginates", %{conn: conn, user: user} do
  144. recipient = insert(:user)
  145. Enum.each(1..30, fn _ ->
  146. {:ok, _} = CommonAPI.post_chat_message(user, recipient, "hey")
  147. end)
  148. chat = Chat.get(user.id, recipient.ap_id)
  149. response = get(conn, "/api/v1/pleroma/chats/#{chat.id}/messages")
  150. result = json_response_and_validate_schema(response, 200)
  151. [next, prev] = get_resp_header(response, "link") |> hd() |> String.split(", ")
  152. api_endpoint = "/api/v1/pleroma/chats/"
  153. assert String.match?(
  154. next,
  155. ~r(#{api_endpoint}.*/messages\?id=.*&limit=\d+&max_id=.*; rel=\"next\"$)
  156. )
  157. assert String.match?(
  158. prev,
  159. ~r(#{api_endpoint}.*/messages\?id=.*&limit=\d+&min_id=.*; rel=\"prev\"$)
  160. )
  161. assert length(result) == 20
  162. response =
  163. get(conn, "/api/v1/pleroma/chats/#{chat.id}/messages?max_id=#{List.last(result)["id"]}")
  164. result = json_response_and_validate_schema(response, 200)
  165. [next, prev] = get_resp_header(response, "link") |> hd() |> String.split(", ")
  166. assert String.match?(
  167. next,
  168. ~r(#{api_endpoint}.*/messages\?id=.*&limit=\d+&max_id=.*; rel=\"next\"$)
  169. )
  170. assert String.match?(
  171. prev,
  172. ~r(#{api_endpoint}.*/messages\?id=.*&limit=\d+&max_id=.*&min_id=.*; rel=\"prev\"$)
  173. )
  174. assert length(result) == 10
  175. end
  176. test "it returns the messages for a given chat", %{conn: conn, user: user} do
  177. other_user = insert(:user)
  178. third_user = insert(:user)
  179. {:ok, _} = CommonAPI.post_chat_message(user, other_user, "hey")
  180. {:ok, _} = CommonAPI.post_chat_message(user, third_user, "hey")
  181. {:ok, _} = CommonAPI.post_chat_message(user, other_user, "how are you?")
  182. {:ok, _} = CommonAPI.post_chat_message(other_user, user, "fine, how about you?")
  183. chat = Chat.get(user.id, other_user.ap_id)
  184. result =
  185. conn
  186. |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
  187. |> json_response_and_validate_schema(200)
  188. result
  189. |> Enum.each(fn message ->
  190. assert message["chat_id"] == chat.id |> to_string()
  191. end)
  192. assert length(result) == 3
  193. # Trying to get the chat of a different user
  194. conn
  195. |> assign(:user, other_user)
  196. |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
  197. |> json_response_and_validate_schema(404)
  198. end
  199. end
  200. describe "POST /api/v1/pleroma/chats/by-account-id/:id" do
  201. setup do: oauth_access(["write:chats"])
  202. test "it creates or returns a chat", %{conn: conn} do
  203. other_user = insert(:user)
  204. result =
  205. conn
  206. |> post("/api/v1/pleroma/chats/by-account-id/#{other_user.id}")
  207. |> json_response_and_validate_schema(200)
  208. assert result["id"]
  209. end
  210. end
  211. describe "GET /api/v1/pleroma/chats/:id" do
  212. setup do: oauth_access(["read:chats"])
  213. test "it returns a chat", %{conn: conn, user: user} do
  214. other_user = insert(:user)
  215. {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
  216. result =
  217. conn
  218. |> get("/api/v1/pleroma/chats/#{chat.id}")
  219. |> json_response_and_validate_schema(200)
  220. assert result["id"] == to_string(chat.id)
  221. end
  222. end
  223. describe "GET /api/v1/pleroma/chats" do
  224. setup do: oauth_access(["read:chats"])
  225. test "it does not return chats with deleted users", %{conn: conn, user: user} do
  226. recipient = insert(:user)
  227. {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
  228. Pleroma.Repo.delete(recipient)
  229. User.invalidate_cache(recipient)
  230. result =
  231. conn
  232. |> get("/api/v1/pleroma/chats")
  233. |> json_response_and_validate_schema(200)
  234. assert length(result) == 0
  235. end
  236. test "it does not return chats with users you blocked", %{conn: conn, user: user} do
  237. recipient = insert(:user)
  238. {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
  239. result =
  240. conn
  241. |> get("/api/v1/pleroma/chats")
  242. |> json_response_and_validate_schema(200)
  243. assert length(result) == 1
  244. User.block(user, recipient)
  245. result =
  246. conn
  247. |> get("/api/v1/pleroma/chats")
  248. |> json_response_and_validate_schema(200)
  249. assert length(result) == 0
  250. end
  251. test "it returns all chats", %{conn: conn, user: user} do
  252. Enum.each(1..30, fn _ ->
  253. recipient = insert(:user)
  254. {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
  255. end)
  256. result =
  257. conn
  258. |> get("/api/v1/pleroma/chats")
  259. |> json_response_and_validate_schema(200)
  260. assert length(result) == 30
  261. end
  262. test "it return a list of chats the current user is participating in, in descending order of updates",
  263. %{conn: conn, user: user} do
  264. har = insert(:user)
  265. jafnhar = insert(:user)
  266. tridi = insert(:user)
  267. {:ok, chat_1} = Chat.get_or_create(user.id, har.ap_id)
  268. :timer.sleep(1000)
  269. {:ok, _chat_2} = Chat.get_or_create(user.id, jafnhar.ap_id)
  270. :timer.sleep(1000)
  271. {:ok, chat_3} = Chat.get_or_create(user.id, tridi.ap_id)
  272. :timer.sleep(1000)
  273. # bump the second one
  274. {:ok, chat_2} = Chat.bump_or_create(user.id, jafnhar.ap_id)
  275. result =
  276. conn
  277. |> get("/api/v1/pleroma/chats")
  278. |> json_response_and_validate_schema(200)
  279. ids = Enum.map(result, & &1["id"])
  280. assert ids == [
  281. chat_2.id |> to_string(),
  282. chat_3.id |> to_string(),
  283. chat_1.id |> to_string()
  284. ]
  285. end
  286. test "it is not affected by :restrict_unauthenticated setting (issue #1973)", %{
  287. conn: conn,
  288. user: user
  289. } do
  290. clear_config([:restrict_unauthenticated, :profiles, :local], true)
  291. clear_config([:restrict_unauthenticated, :profiles, :remote], true)
  292. user2 = insert(:user)
  293. user3 = insert(:user, local: false)
  294. {:ok, _chat_12} = Chat.get_or_create(user.id, user2.ap_id)
  295. {:ok, _chat_13} = Chat.get_or_create(user.id, user3.ap_id)
  296. result =
  297. conn
  298. |> get("/api/v1/pleroma/chats")
  299. |> json_response_and_validate_schema(200)
  300. account_ids = Enum.map(result, &get_in(&1, ["account", "id"]))
  301. assert Enum.sort(account_ids) == Enum.sort([user2.id, user3.id])
  302. end
  303. end
  304. end