1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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
8 alias Pleroma.Chat.MessageReference
11 alias Pleroma.Web.ActivityPub.ActivityPub
12 alias Pleroma.Web.CommonAPI
14 import Pleroma.Factory
16 describe "POST /api/v1/pleroma/chats/:id/messages/:message_id/read" do
17 setup do: oauth_access(["write:chats"])
19 test "it marks one message as read", %{conn: conn, user: user} do
20 other_user = insert(:user)
22 {:ok, create} = CommonAPI.post_chat_message(other_user, user, "sup")
23 {:ok, _create} = CommonAPI.post_chat_message(other_user, user, "sup part 2")
24 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
25 object = Object.normalize(create, fetch: false)
26 cm_ref = MessageReference.for_chat_and_object(chat, object)
28 assert cm_ref.unread == true
32 |> post("/api/v1/pleroma/chats/#{chat.id}/messages/#{cm_ref.id}/read")
33 |> json_response_and_validate_schema(200)
35 assert result["unread"] == false
37 cm_ref = MessageReference.for_chat_and_object(chat, object)
39 assert cm_ref.unread == false
43 describe "POST /api/v1/pleroma/chats/:id/read" do
44 setup do: oauth_access(["write:chats"])
46 test "given a `last_read_id`, it marks everything until then as read", %{
50 other_user = insert(:user)
52 {:ok, create} = CommonAPI.post_chat_message(other_user, user, "sup")
53 {:ok, _create} = CommonAPI.post_chat_message(other_user, user, "sup part 2")
54 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
55 object = Object.normalize(create, fetch: false)
56 cm_ref = MessageReference.for_chat_and_object(chat, object)
58 assert cm_ref.unread == true
62 |> put_req_header("content-type", "application/json")
63 |> post("/api/v1/pleroma/chats/#{chat.id}/read", %{"last_read_id" => cm_ref.id})
64 |> json_response_and_validate_schema(200)
66 assert result["unread"] == 1
68 cm_ref = MessageReference.for_chat_and_object(chat, object)
70 assert cm_ref.unread == false
74 describe "POST /api/v1/pleroma/chats/:id/messages" do
75 setup do: oauth_access(["write:chats"])
77 test "it posts a message to the chat", %{conn: conn, user: user} do
78 other_user = insert(:user)
80 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
84 |> put_req_header("content-type", "application/json")
85 |> put_req_header("idempotency-key", "123")
86 |> post("/api/v1/pleroma/chats/#{chat.id}/messages", %{"content" => "Hallo!!"})
87 |> json_response_and_validate_schema(200)
89 assert result["content"] == "Hallo!!"
90 assert result["chat_id"] == chat.id |> to_string()
91 assert result["idempotency_key"] == "123"
94 test "it fails if there is no content", %{conn: conn, user: user} do
95 other_user = insert(:user)
97 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
101 |> put_req_header("content-type", "application/json")
102 |> post("/api/v1/pleroma/chats/#{chat.id}/messages")
103 |> json_response_and_validate_schema(400)
105 assert %{"error" => "no_content"} == result
108 test "it works with an attachment", %{conn: conn, user: user} do
109 clear_config([Pleroma.Upload, :uploader], Pleroma.Uploaders.Local)
110 clear_config([Pleroma.Uploaders.Local, :uploads], "uploads")
113 content_type: "image/jpeg",
114 path: Path.absname("test/fixtures/image.jpg"),
115 filename: "an_image.jpg"
118 {:ok, upload} = ActivityPub.upload(file, actor: user.ap_id)
120 other_user = insert(:user)
122 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
126 |> put_req_header("content-type", "application/json")
127 |> post("/api/v1/pleroma/chats/#{chat.id}/messages", %{
128 "media_id" => to_string(upload.id)
130 |> json_response_and_validate_schema(200)
132 assert result["attachment"]
135 test "gets MRF reason when rejected", %{conn: conn, user: user} do
136 clear_config([:mrf_keyword, :reject], ["GNO"])
137 clear_config([:mrf, :policies], [Pleroma.Web.ActivityPub.MRF.KeywordPolicy])
139 other_user = insert(:user)
141 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
145 |> put_req_header("content-type", "application/json")
146 |> post("/api/v1/pleroma/chats/#{chat.id}/messages", %{"content" => "GNO/Linux"})
147 |> json_response_and_validate_schema(422)
149 assert %{"error" => "[KeywordPolicy] Matches with rejected keyword"} == result
153 describe "DELETE /api/v1/pleroma/chats/:id/messages/:message_id" do
154 setup do: oauth_access(["write:chats"])
156 test "it deletes a message from the chat", %{conn: conn, user: user} do
157 recipient = insert(:user)
160 CommonAPI.post_chat_message(user, recipient, "Hello darkness my old friend")
162 {:ok, other_message} = CommonAPI.post_chat_message(recipient, user, "nico nico ni")
164 object = Object.normalize(message, fetch: false)
166 chat = Chat.get(user.id, recipient.ap_id)
168 cm_ref = MessageReference.for_chat_and_object(chat, object)
170 # Deleting your own message removes the message and the reference
173 |> put_req_header("content-type", "application/json")
174 |> delete("/api/v1/pleroma/chats/#{chat.id}/messages/#{cm_ref.id}")
175 |> json_response_and_validate_schema(200)
177 assert result["id"] == cm_ref.id
178 refute MessageReference.get_by_id(cm_ref.id)
179 assert %{data: %{"type" => "Tombstone"}} = Object.get_by_id(object.id)
181 # Deleting other people's messages just removes the reference
182 object = Object.normalize(other_message, fetch: false)
183 cm_ref = MessageReference.for_chat_and_object(chat, object)
187 |> put_req_header("content-type", "application/json")
188 |> delete("/api/v1/pleroma/chats/#{chat.id}/messages/#{cm_ref.id}")
189 |> json_response_and_validate_schema(200)
191 assert result["id"] == cm_ref.id
192 refute MessageReference.get_by_id(cm_ref.id)
193 assert Object.get_by_id(object.id)
197 describe "GET /api/v1/pleroma/chats/:id/messages" do
198 setup do: oauth_access(["read:chats"])
200 test "it paginates", %{conn: conn, user: user} do
201 recipient = insert(:user)
203 Enum.each(1..30, fn _ ->
204 {:ok, _} = CommonAPI.post_chat_message(user, recipient, "hey")
207 chat = Chat.get(user.id, recipient.ap_id)
209 response = get(conn, "/api/v1/pleroma/chats/#{chat.id}/messages")
210 result = json_response_and_validate_schema(response, 200)
212 [next, prev] = get_resp_header(response, "link") |> hd() |> String.split(", ")
213 api_endpoint = "/api/v1/pleroma/chats/"
215 assert String.match?(
217 ~r(#{api_endpoint}.*/messages\?limit=\d+&max_id=.*; rel=\"next\"$)
220 assert String.match?(
222 ~r(#{api_endpoint}.*/messages\?limit=\d+&min_id=.*; rel=\"prev\"$)
225 assert length(result) == 20
228 get(conn, "/api/v1/pleroma/chats/#{chat.id}/messages?max_id=#{List.last(result)["id"]}")
230 result = json_response_and_validate_schema(response, 200)
231 [next, prev] = get_resp_header(response, "link") |> hd() |> String.split(", ")
233 assert String.match?(
235 ~r(#{api_endpoint}.*/messages\?limit=\d+&max_id=.*; rel=\"next\"$)
238 assert String.match?(
240 ~r(#{api_endpoint}.*/messages\?limit=\d+&max_id=.*&min_id=.*; rel=\"prev\"$)
243 assert length(result) == 10
246 test "it returns the messages for a given chat", %{conn: conn, user: user} do
247 other_user = insert(:user)
248 third_user = insert(:user)
250 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "hey")
251 {:ok, _} = CommonAPI.post_chat_message(user, third_user, "hey")
252 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "how are you?")
253 {:ok, _} = CommonAPI.post_chat_message(other_user, user, "fine, how about you?")
255 chat = Chat.get(user.id, other_user.ap_id)
259 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
260 |> json_response_and_validate_schema(200)
263 |> Enum.each(fn message ->
264 assert message["chat_id"] == chat.id |> to_string()
267 assert length(result) == 3
269 # Trying to get the chat of a different user
270 other_user_chat = Chat.get(other_user.id, user.ap_id)
273 |> get("/api/v1/pleroma/chats/#{other_user_chat.id}/messages")
274 |> json_response_and_validate_schema(404)
278 describe "POST /api/v1/pleroma/chats/by-account-id/:id" do
279 setup do: oauth_access(["write:chats"])
281 test "it creates or returns a chat", %{conn: conn} do
282 other_user = insert(:user)
286 |> post("/api/v1/pleroma/chats/by-account-id/#{other_user.id}")
287 |> json_response_and_validate_schema(200)
293 describe "GET /api/v1/pleroma/chats/:id" do
294 setup do: oauth_access(["read:chats"])
296 test "it returns a chat", %{conn: conn, user: user} do
297 other_user = insert(:user)
299 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
303 |> get("/api/v1/pleroma/chats/#{chat.id}")
304 |> json_response_and_validate_schema(200)
306 assert result["id"] == to_string(chat.id)
310 describe "GET /api/v2/pleroma/chats" do
311 setup do: oauth_access(["read:chats"])
313 test "it does not return chats with deleted users", %{conn: conn, user: user} do
314 recipient = insert(:user)
315 {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
317 Pleroma.Repo.delete(recipient)
318 User.invalidate_cache(recipient)
322 |> get("/api/v2/pleroma/chats")
323 |> json_response_and_validate_schema(200)
325 assert length(result) == 0
328 test "it does not return chats with users you blocked", %{conn: conn, user: user} do
329 recipient = insert(:user)
331 {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
335 |> get("/api/v2/pleroma/chats")
336 |> json_response_and_validate_schema(200)
338 assert length(result) == 1
340 User.block(user, recipient)
344 |> get("/api/v2/pleroma/chats")
345 |> json_response_and_validate_schema(200)
347 assert length(result) == 0
350 test "it does not return chats with users you muted", %{conn: conn, user: user} do
351 recipient = insert(:user)
353 {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
357 |> get("/api/v2/pleroma/chats")
358 |> json_response_and_validate_schema(200)
360 assert length(result) == 1
362 User.mute(user, recipient)
366 |> get("/api/v2/pleroma/chats")
367 |> json_response_and_validate_schema(200)
369 assert length(result) == 0
373 |> get("/api/v2/pleroma/chats?with_muted=true")
374 |> json_response_and_validate_schema(200)
376 assert length(result) == 1
379 test "it paginates chats", %{conn: conn, user: user} do
380 Enum.each(1..30, fn _ ->
381 recipient = insert(:user)
382 {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
387 |> get("/api/v2/pleroma/chats")
388 |> json_response_and_validate_schema(200)
390 assert length(result) == 20
391 last_id = List.last(result)["id"]
395 |> get("/api/v2/pleroma/chats?max_id=#{last_id}")
396 |> json_response_and_validate_schema(200)
398 assert length(result) == 10
401 test "it return a list of chats the current user is participating in, in descending order of updates",
402 %{conn: conn, user: user} do
404 jafnhar = insert(:user)
405 tridi = insert(:user)
407 {:ok, chat_1} = Chat.get_or_create(user.id, har.ap_id)
408 {:ok, chat_1} = time_travel(chat_1, -3)
409 {:ok, chat_2} = Chat.get_or_create(user.id, jafnhar.ap_id)
410 {:ok, _chat_2} = time_travel(chat_2, -2)
411 {:ok, chat_3} = Chat.get_or_create(user.id, tridi.ap_id)
412 {:ok, chat_3} = time_travel(chat_3, -1)
414 # bump the second one
415 {:ok, chat_2} = Chat.bump_or_create(user.id, jafnhar.ap_id)
419 |> get("/api/v2/pleroma/chats")
420 |> json_response_and_validate_schema(200)
422 ids = Enum.map(result, & &1["id"])
425 chat_2.id |> to_string(),
426 chat_3.id |> to_string(),
427 chat_1.id |> to_string()
431 test "it is not affected by :restrict_unauthenticated setting (issue #1973)", %{
435 clear_config([:restrict_unauthenticated, :profiles, :local], true)
436 clear_config([:restrict_unauthenticated, :profiles, :remote], true)
438 user2 = insert(:user)
439 user3 = insert(:user, local: false)
441 {:ok, _chat_12} = Chat.get_or_create(user.id, user2.ap_id)
442 {:ok, _chat_13} = Chat.get_or_create(user.id, user3.ap_id)
446 |> get("/api/v2/pleroma/chats")
447 |> json_response_and_validate_schema(200)
449 account_ids = Enum.map(result, &get_in(&1, ["account", "id"]))
450 assert Enum.sort(account_ids) == Enum.sort([user2.id, user3.id])