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 "it marks all messages in a chat as read", %{conn: conn, user: user} do
47 other_user = insert(:user)
49 {:ok, create} = CommonAPI.post_chat_message(other_user, user, "sup")
50 {:ok, _create} = CommonAPI.post_chat_message(other_user, user, "sup part 2")
51 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
52 object = Object.normalize(create, false)
53 cm_ref = MessageReference.for_chat_and_object(chat, object)
55 assert cm_ref.unread == true
59 |> post("/api/v1/pleroma/chats/#{chat.id}/read")
60 |> json_response_and_validate_schema(200)
62 assert result["unread"] == 0
64 cm_ref = MessageReference.for_chat_and_object(chat, object)
66 assert cm_ref.unread == false
69 test "it given a `last_read_id` ", %{conn: conn, user: user} do
70 other_user = insert(:user)
72 {:ok, create} = CommonAPI.post_chat_message(other_user, user, "sup")
73 {:ok, _create} = CommonAPI.post_chat_message(other_user, user, "sup part 2")
74 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
75 object = Object.normalize(create, false)
76 cm_ref = MessageReference.for_chat_and_object(chat, object)
78 assert cm_ref.unread == true
82 |> put_req_header("content-type", "application/json")
83 |> post("/api/v1/pleroma/chats/#{chat.id}/read", %{"last_read_id" => cm_ref.id})
84 |> json_response_and_validate_schema(200)
86 assert result["unread"] == 1
88 cm_ref = MessageReference.for_chat_and_object(chat, object)
90 assert cm_ref.unread == false
94 describe "POST /api/v1/pleroma/chats/:id/messages" do
95 setup do: oauth_access(["write:chats"])
97 test "it posts a message to the chat", %{conn: conn, user: user} do
98 other_user = insert(:user)
100 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
104 |> put_req_header("content-type", "application/json")
105 |> post("/api/v1/pleroma/chats/#{chat.id}/messages", %{"content" => "Hallo!!"})
106 |> json_response_and_validate_schema(200)
108 assert result["content"] == "Hallo!!"
109 assert result["chat_id"] == chat.id |> to_string()
112 test "it fails if there is no content", %{conn: conn, user: user} do
113 other_user = insert(:user)
115 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
119 |> put_req_header("content-type", "application/json")
120 |> post("/api/v1/pleroma/chats/#{chat.id}/messages")
121 |> json_response_and_validate_schema(400)
126 test "it works with an attachment", %{conn: conn, user: user} do
128 content_type: "image/jpg",
129 path: Path.absname("test/fixtures/image.jpg"),
130 filename: "an_image.jpg"
133 {:ok, upload} = ActivityPub.upload(file, actor: user.ap_id)
135 other_user = insert(:user)
137 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
141 |> put_req_header("content-type", "application/json")
142 |> post("/api/v1/pleroma/chats/#{chat.id}/messages", %{
143 "media_id" => to_string(upload.id)
145 |> json_response_and_validate_schema(200)
147 assert result["attachment"]
151 describe "DELETE /api/v1/pleroma/chats/:id/messages/:message_id" do
152 setup do: oauth_access(["write:chats"])
154 test "it deletes a message from the chat", %{conn: conn, user: user} do
155 recipient = insert(:user)
158 CommonAPI.post_chat_message(user, recipient, "Hello darkness my old friend")
160 {:ok, other_message} = CommonAPI.post_chat_message(recipient, user, "nico nico ni")
162 object = Object.normalize(message, false)
164 chat = Chat.get(user.id, recipient.ap_id)
166 cm_ref = MessageReference.for_chat_and_object(chat, object)
168 # Deleting your own message removes the message and the reference
171 |> put_req_header("content-type", "application/json")
172 |> delete("/api/v1/pleroma/chats/#{chat.id}/messages/#{cm_ref.id}")
173 |> json_response_and_validate_schema(200)
175 assert result["id"] == cm_ref.id
176 refute MessageReference.get_by_id(cm_ref.id)
177 assert %{data: %{"type" => "Tombstone"}} = Object.get_by_id(object.id)
179 # Deleting other people's messages just removes the reference
180 object = Object.normalize(other_message, false)
181 cm_ref = MessageReference.for_chat_and_object(chat, object)
185 |> put_req_header("content-type", "application/json")
186 |> delete("/api/v1/pleroma/chats/#{chat.id}/messages/#{cm_ref.id}")
187 |> json_response_and_validate_schema(200)
189 assert result["id"] == cm_ref.id
190 refute MessageReference.get_by_id(cm_ref.id)
191 assert Object.get_by_id(object.id)
195 describe "GET /api/v1/pleroma/chats/:id/messages" do
196 setup do: oauth_access(["read:chats"])
198 test "it paginates", %{conn: conn, user: user} do
199 recipient = insert(:user)
201 Enum.each(1..30, fn _ ->
202 {:ok, _} = CommonAPI.post_chat_message(user, recipient, "hey")
205 chat = Chat.get(user.id, recipient.ap_id)
209 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
210 |> json_response_and_validate_schema(200)
212 assert length(result) == 20
216 |> get("/api/v1/pleroma/chats/#{chat.id}/messages?max_id=#{List.last(result)["id"]}")
217 |> json_response_and_validate_schema(200)
219 assert length(result) == 10
222 test "it returns the messages for a given chat", %{conn: conn, user: user} do
223 other_user = insert(:user)
224 third_user = insert(:user)
226 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "hey")
227 {:ok, _} = CommonAPI.post_chat_message(user, third_user, "hey")
228 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "how are you?")
229 {:ok, _} = CommonAPI.post_chat_message(other_user, user, "fine, how about you?")
231 chat = Chat.get(user.id, other_user.ap_id)
235 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
236 |> json_response_and_validate_schema(200)
239 |> Enum.each(fn message ->
240 assert message["chat_id"] == chat.id |> to_string()
243 assert length(result) == 3
245 # Trying to get the chat of a different user
248 |> assign(:user, other_user)
249 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
251 assert result |> json_response(404)
255 describe "POST /api/v1/pleroma/chats/by-account-id/:id" do
256 setup do: oauth_access(["write:chats"])
258 test "it creates or returns a chat", %{conn: conn} do
259 other_user = insert(:user)
263 |> post("/api/v1/pleroma/chats/by-account-id/#{other_user.id}")
264 |> json_response_and_validate_schema(200)
270 describe "GET /api/v1/pleroma/chats/:id" do
271 setup do: oauth_access(["read:chats"])
273 test "it returns a chat", %{conn: conn, user: user} do
274 other_user = insert(:user)
276 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
280 |> get("/api/v1/pleroma/chats/#{chat.id}")
281 |> json_response_and_validate_schema(200)
283 assert result["id"] == to_string(chat.id)
287 describe "GET /api/v1/pleroma/chats" do
288 setup do: oauth_access(["read:chats"])
290 test "it does not return chats with users you blocked", %{conn: conn, user: user} do
291 recipient = insert(:user)
293 {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
297 |> get("/api/v1/pleroma/chats")
298 |> json_response_and_validate_schema(200)
300 assert length(result) == 1
302 User.block(user, recipient)
306 |> get("/api/v1/pleroma/chats")
307 |> json_response_and_validate_schema(200)
309 assert length(result) == 0
312 test "it paginates", %{conn: conn, user: user} do
313 Enum.each(1..30, fn _ ->
314 recipient = insert(:user)
315 {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
320 |> get("/api/v1/pleroma/chats")
321 |> json_response_and_validate_schema(200)
323 assert length(result) == 20
327 |> get("/api/v1/pleroma/chats?max_id=#{List.last(result)["id"]}")
328 |> json_response_and_validate_schema(200)
330 assert length(result) == 10
333 test "it return a list of chats the current user is participating in, in descending order of updates",
334 %{conn: conn, user: user} do
336 jafnhar = insert(:user)
337 tridi = insert(:user)
339 {:ok, chat_1} = Chat.get_or_create(user.id, har.ap_id)
341 {:ok, _chat_2} = Chat.get_or_create(user.id, jafnhar.ap_id)
343 {:ok, chat_3} = Chat.get_or_create(user.id, tridi.ap_id)
346 # bump the second one
347 {:ok, chat_2} = Chat.bump_or_create(user.id, jafnhar.ap_id)
351 |> get("/api/v1/pleroma/chats")
352 |> json_response_and_validate_schema(200)
354 ids = Enum.map(result, & &1["id"])
357 chat_2.id |> to_string(),
358 chat_3.id |> to_string(),
359 chat_1.id |> to_string()