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, async: true
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, 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, 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 |> post("/api/v1/pleroma/chats/#{chat.id}/messages", %{"content" => "Hallo!!"})
86 |> json_response_and_validate_schema(200)
88 assert result["content"] == "Hallo!!"
89 assert result["chat_id"] == chat.id |> to_string()
92 test "it fails if there is no content", %{conn: conn, user: user} do
93 other_user = insert(:user)
95 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
99 |> put_req_header("content-type", "application/json")
100 |> post("/api/v1/pleroma/chats/#{chat.id}/messages")
101 |> json_response_and_validate_schema(400)
103 assert %{"error" => "no_content"} == result
106 test "it works with an attachment", %{conn: conn, user: user} do
108 content_type: "image/jpg",
109 path: Path.absname("test/fixtures/image.jpg"),
110 filename: "an_image.jpg"
113 {:ok, upload} = ActivityPub.upload(file, actor: user.ap_id)
115 other_user = insert(:user)
117 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
121 |> put_req_header("content-type", "application/json")
122 |> post("/api/v1/pleroma/chats/#{chat.id}/messages", %{
123 "media_id" => to_string(upload.id)
125 |> json_response_and_validate_schema(200)
127 assert result["attachment"]
130 test "gets MRF reason when rejected", %{conn: conn, user: user} do
131 clear_config([:mrf_keyword, :reject], ["GNO"])
132 clear_config([:mrf, :policies], [Pleroma.Web.ActivityPub.MRF.KeywordPolicy])
134 other_user = insert(:user)
136 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
140 |> put_req_header("content-type", "application/json")
141 |> post("/api/v1/pleroma/chats/#{chat.id}/messages", %{"content" => "GNO/Linux"})
142 |> json_response_and_validate_schema(422)
144 assert %{"error" => "[KeywordPolicy] Matches with rejected keyword"} == result
148 describe "DELETE /api/v1/pleroma/chats/:id/messages/:message_id" do
149 setup do: oauth_access(["write:chats"])
151 test "it deletes a message from the chat", %{conn: conn, user: user} do
152 recipient = insert(:user)
155 CommonAPI.post_chat_message(user, recipient, "Hello darkness my old friend")
157 {:ok, other_message} = CommonAPI.post_chat_message(recipient, user, "nico nico ni")
159 object = Object.normalize(message, false)
161 chat = Chat.get(user.id, recipient.ap_id)
163 cm_ref = MessageReference.for_chat_and_object(chat, object)
165 # Deleting your own message removes the message and the reference
168 |> put_req_header("content-type", "application/json")
169 |> delete("/api/v1/pleroma/chats/#{chat.id}/messages/#{cm_ref.id}")
170 |> json_response_and_validate_schema(200)
172 assert result["id"] == cm_ref.id
173 refute MessageReference.get_by_id(cm_ref.id)
174 assert %{data: %{"type" => "Tombstone"}} = Object.get_by_id(object.id)
176 # Deleting other people's messages just removes the reference
177 object = Object.normalize(other_message, false)
178 cm_ref = MessageReference.for_chat_and_object(chat, object)
182 |> put_req_header("content-type", "application/json")
183 |> delete("/api/v1/pleroma/chats/#{chat.id}/messages/#{cm_ref.id}")
184 |> json_response_and_validate_schema(200)
186 assert result["id"] == cm_ref.id
187 refute MessageReference.get_by_id(cm_ref.id)
188 assert Object.get_by_id(object.id)
192 describe "GET /api/v1/pleroma/chats/:id/messages" do
193 setup do: oauth_access(["read:chats"])
195 test "it paginates", %{conn: conn, user: user} do
196 recipient = insert(:user)
198 Enum.each(1..30, fn _ ->
199 {:ok, _} = CommonAPI.post_chat_message(user, recipient, "hey")
202 chat = Chat.get(user.id, recipient.ap_id)
206 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
207 |> json_response_and_validate_schema(200)
209 assert length(result) == 20
213 |> get("/api/v1/pleroma/chats/#{chat.id}/messages?max_id=#{List.last(result)["id"]}")
214 |> json_response_and_validate_schema(200)
216 assert length(result) == 10
219 test "it returns the messages for a given chat", %{conn: conn, user: user} do
220 other_user = insert(:user)
221 third_user = insert(:user)
223 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "hey")
224 {:ok, _} = CommonAPI.post_chat_message(user, third_user, "hey")
225 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "how are you?")
226 {:ok, _} = CommonAPI.post_chat_message(other_user, user, "fine, how about you?")
228 chat = Chat.get(user.id, other_user.ap_id)
232 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
233 |> json_response_and_validate_schema(200)
236 |> Enum.each(fn message ->
237 assert message["chat_id"] == chat.id |> to_string()
240 assert length(result) == 3
242 # Trying to get the chat of a different user
245 |> assign(:user, other_user)
246 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
248 assert result |> json_response(404)
252 describe "POST /api/v1/pleroma/chats/by-account-id/:id" do
253 setup do: oauth_access(["write:chats"])
255 test "it creates or returns a chat", %{conn: conn} do
256 other_user = insert(:user)
260 |> post("/api/v1/pleroma/chats/by-account-id/#{other_user.id}")
261 |> json_response_and_validate_schema(200)
267 describe "GET /api/v1/pleroma/chats/:id" do
268 setup do: oauth_access(["read:chats"])
270 test "it returns a chat", %{conn: conn, user: user} do
271 other_user = insert(:user)
273 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
277 |> get("/api/v1/pleroma/chats/#{chat.id}")
278 |> json_response_and_validate_schema(200)
280 assert result["id"] == to_string(chat.id)
284 describe "GET /api/v1/pleroma/chats" do
285 setup do: oauth_access(["read:chats"])
287 test "it does not return chats with deleted users", %{conn: conn, user: user} do
288 recipient = insert(:user)
289 {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
291 Pleroma.Repo.delete(recipient)
292 User.invalidate_cache(recipient)
296 |> get("/api/v1/pleroma/chats")
297 |> json_response_and_validate_schema(200)
299 assert length(result) == 0
302 test "it does not return chats with users you blocked", %{conn: conn, user: user} do
303 recipient = insert(:user)
305 {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
309 |> get("/api/v1/pleroma/chats")
310 |> json_response_and_validate_schema(200)
312 assert length(result) == 1
314 User.block(user, recipient)
318 |> get("/api/v1/pleroma/chats")
319 |> json_response_and_validate_schema(200)
321 assert length(result) == 0
324 test "it returns all chats", %{conn: conn, user: user} do
325 Enum.each(1..30, fn _ ->
326 recipient = insert(:user)
327 {: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) == 30
338 test "it return a list of chats the current user is participating in, in descending order of updates",
339 %{conn: conn, user: user} do
341 jafnhar = insert(:user)
342 tridi = insert(:user)
344 {:ok, chat_1} = Chat.get_or_create(user.id, har.ap_id)
346 {:ok, _chat_2} = Chat.get_or_create(user.id, jafnhar.ap_id)
348 {:ok, chat_3} = Chat.get_or_create(user.id, tridi.ap_id)
351 # bump the second one
352 {:ok, chat_2} = Chat.bump_or_create(user.id, jafnhar.ap_id)
356 |> get("/api/v1/pleroma/chats")
357 |> json_response_and_validate_schema(200)
359 ids = Enum.map(result, & &1["id"])
362 chat_2.id |> to_string(),
363 chat_3.id |> to_string(),
364 chat_1.id |> to_string()
368 test "it is not affected by :restrict_unauthenticated setting (issue #1973)", %{
372 clear_config([:restrict_unauthenticated, :profiles, :local], true)
373 clear_config([:restrict_unauthenticated, :profiles, :remote], true)
375 user2 = insert(:user)
376 user3 = insert(:user, local: false)
378 {:ok, _chat_12} = Chat.get_or_create(user.id, user2.ap_id)
379 {:ok, _chat_13} = Chat.get_or_create(user.id, user3.ap_id)
383 |> get("/api/v1/pleroma/chats")
384 |> json_response_and_validate_schema(200)
386 account_ids = Enum.map(result, &get_in(&1, ["account", "id"]))
387 assert Enum.sort(account_ids) == Enum.sort([user2.id, user3.id])