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
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
110 content_type: "image/jpeg",
111 path: Path.absname("test/fixtures/image.jpg"),
112 filename: "an_image.jpg"
115 {:ok, upload} = ActivityPub.upload(file, actor: user.ap_id)
117 other_user = insert(:user)
119 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
123 |> put_req_header("content-type", "application/json")
124 |> post("/api/v1/pleroma/chats/#{chat.id}/messages", %{
125 "media_id" => to_string(upload.id)
127 |> json_response_and_validate_schema(200)
129 assert result["attachment"]
132 test "gets MRF reason when rejected", %{conn: conn, user: user} do
133 clear_config([:mrf_keyword, :reject], ["GNO"])
134 clear_config([:mrf, :policies], [Pleroma.Web.ActivityPub.MRF.KeywordPolicy])
136 other_user = insert(:user)
138 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
142 |> put_req_header("content-type", "application/json")
143 |> post("/api/v1/pleroma/chats/#{chat.id}/messages", %{"content" => "GNO/Linux"})
144 |> json_response_and_validate_schema(422)
146 assert %{"error" => "[KeywordPolicy] Matches with rejected keyword"} == result
150 describe "DELETE /api/v1/pleroma/chats/:id/messages/:message_id" do
151 setup do: oauth_access(["write:chats"])
153 test "it deletes a message from the chat", %{conn: conn, user: user} do
154 recipient = insert(:user)
157 CommonAPI.post_chat_message(user, recipient, "Hello darkness my old friend")
159 {:ok, other_message} = CommonAPI.post_chat_message(recipient, user, "nico nico ni")
161 object = Object.normalize(message, fetch: false)
163 chat = Chat.get(user.id, recipient.ap_id)
165 cm_ref = MessageReference.for_chat_and_object(chat, object)
167 # Deleting your own message removes the message and the reference
170 |> put_req_header("content-type", "application/json")
171 |> delete("/api/v1/pleroma/chats/#{chat.id}/messages/#{cm_ref.id}")
172 |> json_response_and_validate_schema(200)
174 assert result["id"] == cm_ref.id
175 refute MessageReference.get_by_id(cm_ref.id)
176 assert %{data: %{"type" => "Tombstone"}} = Object.get_by_id(object.id)
178 # Deleting other people's messages just removes the reference
179 object = Object.normalize(other_message, fetch: false)
180 cm_ref = MessageReference.for_chat_and_object(chat, object)
184 |> put_req_header("content-type", "application/json")
185 |> delete("/api/v1/pleroma/chats/#{chat.id}/messages/#{cm_ref.id}")
186 |> json_response_and_validate_schema(200)
188 assert result["id"] == cm_ref.id
189 refute MessageReference.get_by_id(cm_ref.id)
190 assert Object.get_by_id(object.id)
194 describe "GET /api/v1/pleroma/chats/:id/messages" do
195 setup do: oauth_access(["read:chats"])
197 test "it paginates", %{conn: conn, user: user} do
198 recipient = insert(:user)
200 Enum.each(1..30, fn _ ->
201 {:ok, _} = CommonAPI.post_chat_message(user, recipient, "hey")
204 chat = Chat.get(user.id, recipient.ap_id)
206 response = get(conn, "/api/v1/pleroma/chats/#{chat.id}/messages")
207 result = json_response_and_validate_schema(response, 200)
209 [next, prev] = get_resp_header(response, "link") |> hd() |> String.split(", ")
210 api_endpoint = "/api/v1/pleroma/chats/"
212 assert String.match?(
214 ~r(#{api_endpoint}.*/messages\?id=.*&limit=\d+&max_id=.*; rel=\"next\"$)
217 assert String.match?(
219 ~r(#{api_endpoint}.*/messages\?id=.*&limit=\d+&min_id=.*; rel=\"prev\"$)
222 assert length(result) == 20
225 get(conn, "/api/v1/pleroma/chats/#{chat.id}/messages?max_id=#{List.last(result)["id"]}")
227 result = json_response_and_validate_schema(response, 200)
228 [next, prev] = get_resp_header(response, "link") |> hd() |> String.split(", ")
230 assert String.match?(
232 ~r(#{api_endpoint}.*/messages\?id=.*&limit=\d+&max_id=.*; rel=\"next\"$)
235 assert String.match?(
237 ~r(#{api_endpoint}.*/messages\?id=.*&limit=\d+&max_id=.*&min_id=.*; rel=\"prev\"$)
240 assert length(result) == 10
243 test "it returns the messages for a given chat", %{conn: conn, user: user} do
244 other_user = insert(:user)
245 third_user = insert(:user)
247 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "hey")
248 {:ok, _} = CommonAPI.post_chat_message(user, third_user, "hey")
249 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "how are you?")
250 {:ok, _} = CommonAPI.post_chat_message(other_user, user, "fine, how about you?")
252 chat = Chat.get(user.id, other_user.ap_id)
256 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
257 |> json_response_and_validate_schema(200)
260 |> Enum.each(fn message ->
261 assert message["chat_id"] == chat.id |> to_string()
264 assert length(result) == 3
266 # Trying to get the chat of a different user
267 other_user_chat = Chat.get(other_user.id, user.ap_id)
270 |> get("/api/v1/pleroma/chats/#{other_user_chat.id}/messages")
271 |> json_response_and_validate_schema(404)
275 describe "POST /api/v1/pleroma/chats/by-account-id/:id" do
276 setup do: oauth_access(["write:chats"])
278 test "it creates or returns a chat", %{conn: conn} do
279 other_user = insert(:user)
283 |> post("/api/v1/pleroma/chats/by-account-id/#{other_user.id}")
284 |> json_response_and_validate_schema(200)
290 describe "GET /api/v1/pleroma/chats/:id" do
291 setup do: oauth_access(["read:chats"])
293 test "it returns a chat", %{conn: conn, user: user} do
294 other_user = insert(:user)
296 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
300 |> get("/api/v1/pleroma/chats/#{chat.id}")
301 |> json_response_and_validate_schema(200)
303 assert result["id"] == to_string(chat.id)
307 describe "GET /api/v1/pleroma/chats" do
308 setup do: oauth_access(["read:chats"])
310 test "it does not return chats with deleted users", %{conn: conn, user: user} do
311 recipient = insert(:user)
312 {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
314 Pleroma.Repo.delete(recipient)
315 User.invalidate_cache(recipient)
319 |> get("/api/v1/pleroma/chats")
320 |> json_response_and_validate_schema(200)
322 assert length(result) == 0
325 test "it does not return chats with users you blocked", %{conn: conn, user: user} do
326 recipient = insert(:user)
328 {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
332 |> get("/api/v1/pleroma/chats")
333 |> json_response_and_validate_schema(200)
335 assert length(result) == 1
337 User.block(user, recipient)
341 |> get("/api/v1/pleroma/chats")
342 |> json_response_and_validate_schema(200)
344 assert length(result) == 0
347 test "it does not return chats with users you muted", %{conn: conn, user: user} do
348 recipient = insert(:user)
350 {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
354 |> get("/api/v1/pleroma/chats")
355 |> json_response_and_validate_schema(200)
357 assert length(result) == 1
359 User.mute(user, recipient)
363 |> get("/api/v1/pleroma/chats")
364 |> json_response_and_validate_schema(200)
366 assert length(result) == 0
370 |> get("/api/v1/pleroma/chats?with_muted=true")
371 |> json_response_and_validate_schema(200)
373 assert length(result) == 1
376 test "it returns all chats", %{conn: conn, user: user} do
377 Enum.each(1..30, fn _ ->
378 recipient = insert(:user)
379 {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
384 |> get("/api/v1/pleroma/chats")
385 |> json_response_and_validate_schema(200)
387 assert length(result) == 30
390 test "it return a list of chats the current user is participating in, in descending order of updates",
391 %{conn: conn, user: user} do
393 jafnhar = insert(:user)
394 tridi = insert(:user)
396 {:ok, chat_1} = Chat.get_or_create(user.id, har.ap_id)
397 {:ok, chat_1} = time_travel(chat_1, -3)
398 {:ok, chat_2} = Chat.get_or_create(user.id, jafnhar.ap_id)
399 {:ok, _chat_2} = time_travel(chat_2, -2)
400 {:ok, chat_3} = Chat.get_or_create(user.id, tridi.ap_id)
401 {:ok, chat_3} = time_travel(chat_3, -1)
403 # bump the second one
404 {:ok, chat_2} = Chat.bump_or_create(user.id, jafnhar.ap_id)
408 |> get("/api/v1/pleroma/chats")
409 |> json_response_and_validate_schema(200)
411 ids = Enum.map(result, & &1["id"])
414 chat_2.id |> to_string(),
415 chat_3.id |> to_string(),
416 chat_1.id |> to_string()
420 test "it is not affected by :restrict_unauthenticated setting (issue #1973)", %{
424 clear_config([:restrict_unauthenticated, :profiles, :local], true)
425 clear_config([:restrict_unauthenticated, :profiles, :remote], true)
427 user2 = insert(:user)
428 user3 = insert(:user, local: false)
430 {:ok, _chat_12} = Chat.get_or_create(user.id, user2.ap_id)
431 {:ok, _chat_13} = Chat.get_or_create(user.id, user3.ap_id)
435 |> get("/api/v1/pleroma/chats")
436 |> json_response_and_validate_schema(200)
438 account_ids = Enum.map(result, &get_in(&1, ["account", "id"]))
439 assert Enum.sort(account_ids) == Enum.sort([user2.id, user3.id])