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
70 describe "POST /api/v1/pleroma/chats/:id/messages" do
71 setup do: oauth_access(["write:chats"])
73 test "it posts a message to the chat", %{conn: conn, user: user} do
74 other_user = insert(:user)
76 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
80 |> put_req_header("content-type", "application/json")
81 |> post("/api/v1/pleroma/chats/#{chat.id}/messages", %{"content" => "Hallo!!"})
82 |> json_response_and_validate_schema(200)
84 assert result["content"] == "Hallo!!"
85 assert result["chat_id"] == chat.id |> to_string()
88 test "it fails if there is no content", %{conn: conn, user: user} do
89 other_user = insert(:user)
91 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
95 |> put_req_header("content-type", "application/json")
96 |> post("/api/v1/pleroma/chats/#{chat.id}/messages")
97 |> json_response_and_validate_schema(400)
102 test "it works with an attachment", %{conn: conn, user: user} do
104 content_type: "image/jpg",
105 path: Path.absname("test/fixtures/image.jpg"),
106 filename: "an_image.jpg"
109 {:ok, upload} = ActivityPub.upload(file, actor: user.ap_id)
111 other_user = insert(:user)
113 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
117 |> put_req_header("content-type", "application/json")
118 |> post("/api/v1/pleroma/chats/#{chat.id}/messages", %{
119 "media_id" => to_string(upload.id)
121 |> json_response_and_validate_schema(200)
123 assert result["attachment"]
127 describe "DELETE /api/v1/pleroma/chats/:id/messages/:message_id" do
128 setup do: oauth_access(["write:chats"])
130 test "it deletes a message from the chat", %{conn: conn, user: user} do
131 recipient = insert(:user)
134 CommonAPI.post_chat_message(user, recipient, "Hello darkness my old friend")
136 {:ok, other_message} = CommonAPI.post_chat_message(recipient, user, "nico nico ni")
138 object = Object.normalize(message, false)
140 chat = Chat.get(user.id, recipient.ap_id)
142 cm_ref = MessageReference.for_chat_and_object(chat, object)
144 # Deleting your own message removes the message and the reference
147 |> put_req_header("content-type", "application/json")
148 |> delete("/api/v1/pleroma/chats/#{chat.id}/messages/#{cm_ref.id}")
149 |> json_response_and_validate_schema(200)
151 assert result["id"] == cm_ref.id
152 refute MessageReference.get_by_id(cm_ref.id)
153 assert %{data: %{"type" => "Tombstone"}} = Object.get_by_id(object.id)
155 # Deleting other people's messages just removes the reference
156 object = Object.normalize(other_message, false)
157 cm_ref = MessageReference.for_chat_and_object(chat, object)
161 |> put_req_header("content-type", "application/json")
162 |> delete("/api/v1/pleroma/chats/#{chat.id}/messages/#{cm_ref.id}")
163 |> json_response_and_validate_schema(200)
165 assert result["id"] == cm_ref.id
166 refute MessageReference.get_by_id(cm_ref.id)
167 assert Object.get_by_id(object.id)
171 describe "GET /api/v1/pleroma/chats/:id/messages" do
172 setup do: oauth_access(["read:chats"])
174 test "it paginates", %{conn: conn, user: user} do
175 recipient = insert(:user)
177 Enum.each(1..30, fn _ ->
178 {:ok, _} = CommonAPI.post_chat_message(user, recipient, "hey")
181 chat = Chat.get(user.id, recipient.ap_id)
185 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
186 |> json_response_and_validate_schema(200)
188 assert length(result) == 20
192 |> get("/api/v1/pleroma/chats/#{chat.id}/messages?max_id=#{List.last(result)["id"]}")
193 |> json_response_and_validate_schema(200)
195 assert length(result) == 10
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)
202 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "hey")
203 {:ok, _} = CommonAPI.post_chat_message(user, third_user, "hey")
204 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "how are you?")
205 {:ok, _} = CommonAPI.post_chat_message(other_user, user, "fine, how about you?")
207 chat = Chat.get(user.id, other_user.ap_id)
211 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
212 |> json_response_and_validate_schema(200)
215 |> Enum.each(fn message ->
216 assert message["chat_id"] == chat.id |> to_string()
219 assert length(result) == 3
221 # Trying to get the chat of a different user
224 |> assign(:user, other_user)
225 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
227 assert result |> json_response(404)
231 describe "POST /api/v1/pleroma/chats/by-account-id/:id" do
232 setup do: oauth_access(["write:chats"])
234 test "it creates or returns a chat", %{conn: conn} do
235 other_user = insert(:user)
239 |> post("/api/v1/pleroma/chats/by-account-id/#{other_user.id}")
240 |> json_response_and_validate_schema(200)
246 describe "GET /api/v1/pleroma/chats/:id" do
247 setup do: oauth_access(["read:chats"])
249 test "it returns a chat", %{conn: conn, user: user} do
250 other_user = insert(:user)
252 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
256 |> get("/api/v1/pleroma/chats/#{chat.id}")
257 |> json_response_and_validate_schema(200)
259 assert result["id"] == to_string(chat.id)
263 describe "GET /api/v1/pleroma/chats" do
264 setup do: oauth_access(["read:chats"])
266 test "it does not return chats with users you blocked", %{conn: conn, user: user} do
267 recipient = insert(:user)
269 {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
273 |> get("/api/v1/pleroma/chats")
274 |> json_response_and_validate_schema(200)
276 assert length(result) == 1
278 User.block(user, recipient)
282 |> get("/api/v1/pleroma/chats")
283 |> json_response_and_validate_schema(200)
285 assert length(result) == 0
288 test "it paginates", %{conn: conn, user: user} do
289 Enum.each(1..30, fn _ ->
290 recipient = insert(:user)
291 {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
296 |> get("/api/v1/pleroma/chats")
297 |> json_response_and_validate_schema(200)
299 assert length(result) == 20
303 |> get("/api/v1/pleroma/chats?max_id=#{List.last(result)["id"]}")
304 |> json_response_and_validate_schema(200)
306 assert length(result) == 10
309 test "it return a list of chats the current user is participating in, in descending order of updates",
310 %{conn: conn, user: user} do
312 jafnhar = insert(:user)
313 tridi = insert(:user)
315 {:ok, chat_1} = Chat.get_or_create(user.id, har.ap_id)
317 {:ok, _chat_2} = Chat.get_or_create(user.id, jafnhar.ap_id)
319 {:ok, chat_3} = Chat.get_or_create(user.id, tridi.ap_id)
322 # bump the second one
323 {:ok, chat_2} = Chat.bump_or_create(user.id, jafnhar.ap_id)
327 |> get("/api/v1/pleroma/chats")
328 |> json_response_and_validate_schema(200)
330 ids = Enum.map(result, & &1["id"])
333 chat_2.id |> to_string(),
334 chat_3.id |> to_string(),
335 chat_1.id |> to_string()