logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

chat_controller_test.exs (15925B)


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