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.ChatMessageReference
11 alias Pleroma.Web.ActivityPub.ActivityPub
12 alias Pleroma.Web.CommonAPI
14 import Pleroma.Factory
16 describe "POST /api/v1/pleroma/chats/:id/read" do
17 setup do: oauth_access(["write:statuses"])
19 test "it marks all messages in a chat 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, chat} = Chat.get_or_create(user.id, other_user.ap_id)
24 object = Object.normalize(create, false)
25 cm_ref = ChatMessageReference.for_chat_and_object(chat, object)
27 assert cm_ref.seen == false
31 |> post("/api/v1/pleroma/chats/#{chat.id}/read")
32 |> json_response_and_validate_schema(200)
34 assert result["unread"] == 0
36 cm_ref = ChatMessageReference.for_chat_and_object(chat, object)
38 assert cm_ref.seen == true
42 describe "POST /api/v1/pleroma/chats/:id/messages" do
43 setup do: oauth_access(["write:statuses"])
45 test "it posts a message to the chat", %{conn: conn, user: user} do
46 other_user = insert(:user)
48 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
52 |> put_req_header("content-type", "application/json")
53 |> post("/api/v1/pleroma/chats/#{chat.id}/messages", %{"content" => "Hallo!!"})
54 |> json_response_and_validate_schema(200)
56 assert result["content"] == "Hallo!!"
57 assert result["chat_id"] == chat.id |> to_string()
60 test "it fails if there is no content", %{conn: conn, user: user} do
61 other_user = insert(:user)
63 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
67 |> put_req_header("content-type", "application/json")
68 |> post("/api/v1/pleroma/chats/#{chat.id}/messages")
69 |> json_response_and_validate_schema(400)
74 test "it works with an attachment", %{conn: conn, user: user} do
76 content_type: "image/jpg",
77 path: Path.absname("test/fixtures/image.jpg"),
78 filename: "an_image.jpg"
81 {:ok, upload} = ActivityPub.upload(file, actor: user.ap_id)
83 other_user = insert(:user)
85 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
89 |> put_req_header("content-type", "application/json")
90 |> post("/api/v1/pleroma/chats/#{chat.id}/messages", %{
91 "media_id" => to_string(upload.id)
93 |> json_response_and_validate_schema(200)
95 assert result["attachment"]
99 describe "DELETE /api/v1/pleroma/chats/:id/messages/:message_id" do
100 setup do: oauth_access(["write:statuses"])
102 test "it deletes a message from the chat", %{conn: conn, user: user} do
103 recipient = insert(:user)
106 CommonAPI.post_chat_message(user, recipient, "Hello darkness my old friend")
108 {:ok, other_message} = CommonAPI.post_chat_message(recipient, user, "nico nico ni")
110 object = Object.normalize(message, false)
112 chat = Chat.get(user.id, recipient.ap_id)
114 cm_ref = ChatMessageReference.for_chat_and_object(chat, object)
116 # Deleting your own message removes the message and the reference
119 |> put_req_header("content-type", "application/json")
120 |> delete("/api/v1/pleroma/chats/#{chat.id}/messages/#{cm_ref.id}")
121 |> json_response_and_validate_schema(200)
123 assert result["id"] == cm_ref.id
124 refute ChatMessageReference.get_by_id(cm_ref.id)
125 assert %{data: %{"type" => "Tombstone"}} = Object.get_by_id(object.id)
127 # Deleting other people's messages just removes the reference
128 object = Object.normalize(other_message, false)
129 cm_ref = ChatMessageReference.for_chat_and_object(chat, object)
133 |> put_req_header("content-type", "application/json")
134 |> delete("/api/v1/pleroma/chats/#{chat.id}/messages/#{cm_ref.id}")
135 |> json_response_and_validate_schema(200)
137 assert result["id"] == cm_ref.id
138 refute ChatMessageReference.get_by_id(cm_ref.id)
139 assert Object.get_by_id(object.id)
143 describe "GET /api/v1/pleroma/chats/:id/messages" do
144 setup do: oauth_access(["read:statuses"])
146 test "it paginates", %{conn: conn, user: user} do
147 recipient = insert(:user)
149 Enum.each(1..30, fn _ ->
150 {:ok, _} = CommonAPI.post_chat_message(user, recipient, "hey")
153 chat = Chat.get(user.id, recipient.ap_id)
157 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
158 |> json_response_and_validate_schema(200)
160 assert length(result) == 20
164 |> get("/api/v1/pleroma/chats/#{chat.id}/messages?max_id=#{List.last(result)["id"]}")
165 |> json_response_and_validate_schema(200)
167 assert length(result) == 10
170 test "it returns the messages for a given chat", %{conn: conn, user: user} do
171 other_user = insert(:user)
172 third_user = insert(:user)
174 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "hey")
175 {:ok, _} = CommonAPI.post_chat_message(user, third_user, "hey")
176 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "how are you?")
177 {:ok, _} = CommonAPI.post_chat_message(other_user, user, "fine, how about you?")
179 chat = Chat.get(user.id, other_user.ap_id)
183 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
184 |> json_response_and_validate_schema(200)
187 |> Enum.each(fn message ->
188 assert message["chat_id"] == chat.id |> to_string()
191 assert length(result) == 3
193 # Trying to get the chat of a different user
196 |> assign(:user, other_user)
197 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
199 assert result |> json_response(404)
203 describe "POST /api/v1/pleroma/chats/by-account-id/:id" do
204 setup do: oauth_access(["write:statuses"])
206 test "it creates or returns a chat", %{conn: conn} do
207 other_user = insert(:user)
211 |> post("/api/v1/pleroma/chats/by-account-id/#{other_user.id}")
212 |> json_response_and_validate_schema(200)
218 describe "GET /api/v1/pleroma/chats/:id" do
219 setup do: oauth_access(["read:statuses"])
221 test "it returns a chat", %{conn: conn, user: user} do
222 other_user = insert(:user)
224 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
228 |> get("/api/v1/pleroma/chats/#{chat.id}")
229 |> json_response_and_validate_schema(200)
231 assert result["id"] == to_string(chat.id)
235 describe "GET /api/v1/pleroma/chats" do
236 setup do: oauth_access(["read:statuses"])
238 test "it does not return chats with users you blocked", %{conn: conn, user: user} do
239 recipient = insert(:user)
241 {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
245 |> get("/api/v1/pleroma/chats")
246 |> json_response_and_validate_schema(200)
248 assert length(result) == 1
250 User.block(user, recipient)
254 |> get("/api/v1/pleroma/chats")
255 |> json_response_and_validate_schema(200)
257 assert length(result) == 0
260 test "it paginates", %{conn: conn, user: user} do
261 Enum.each(1..30, fn _ ->
262 recipient = insert(:user)
263 {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
268 |> get("/api/v1/pleroma/chats")
269 |> json_response_and_validate_schema(200)
271 assert length(result) == 20
275 |> get("/api/v1/pleroma/chats?max_id=#{List.last(result)["id"]}")
276 |> json_response_and_validate_schema(200)
278 assert length(result) == 10
281 test "it return a list of chats the current user is participating in, in descending order of updates",
282 %{conn: conn, user: user} do
284 jafnhar = insert(:user)
285 tridi = insert(:user)
287 {:ok, chat_1} = Chat.get_or_create(user.id, har.ap_id)
289 {:ok, _chat_2} = Chat.get_or_create(user.id, jafnhar.ap_id)
291 {:ok, chat_3} = Chat.get_or_create(user.id, tridi.ap_id)
294 # bump the second one
295 {:ok, chat_2} = Chat.bump_or_create(user.id, jafnhar.ap_id)
299 |> get("/api/v1/pleroma/chats")
300 |> json_response_and_validate_schema(200)
302 ids = Enum.map(result, & &1["id"])
305 chat_2.id |> to_string(),
306 chat_3.id |> to_string(),
307 chat_1.id |> to_string()