Merge branch 'develop' of git.pleroma.social:pleroma/pleroma into issue/2115
[akkoma] / test / web / pleroma_api / controllers / chat_controller_test.exs
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
6
7 alias Pleroma.Chat
8 alias Pleroma.Chat.MessageReference
9 alias Pleroma.Object
10 alias Pleroma.User
11 alias Pleroma.Web.ActivityPub.ActivityPub
12 alias Pleroma.Web.CommonAPI
13
14 import Pleroma.Factory
15
16 describe "POST /api/v1/pleroma/chats/:id/messages/:message_id/read" do
17 setup do: oauth_access(["write:chats"])
18
19 test "it marks one message as read", %{conn: conn, user: user} do
20 other_user = insert(:user)
21
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)
27
28 assert cm_ref.unread == true
29
30 result =
31 conn
32 |> post("/api/v1/pleroma/chats/#{chat.id}/messages/#{cm_ref.id}/read")
33 |> json_response_and_validate_schema(200)
34
35 assert result["unread"] == false
36
37 cm_ref = MessageReference.for_chat_and_object(chat, object)
38
39 assert cm_ref.unread == false
40 end
41 end
42
43 describe "POST /api/v1/pleroma/chats/:id/read" do
44 setup do: oauth_access(["write:chats"])
45
46 test "given a `last_read_id`, it marks everything until then as read", %{
47 conn: conn,
48 user: user
49 } do
50 other_user = insert(:user)
51
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)
57
58 assert cm_ref.unread == true
59
60 result =
61 conn
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)
65
66 assert result["unread"] == 1
67
68 cm_ref = MessageReference.for_chat_and_object(chat, object)
69
70 assert cm_ref.unread == false
71 end
72 end
73
74 describe "POST /api/v1/pleroma/chats/:id/messages" do
75 setup do: oauth_access(["write:chats"])
76
77 test "it posts a message to the chat", %{conn: conn, user: user} do
78 other_user = insert(:user)
79
80 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
81
82 result =
83 conn
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)
87
88 assert result["content"] == "Hallo!!"
89 assert result["chat_id"] == chat.id |> to_string()
90 end
91
92 test "it fails if there is no content", %{conn: conn, user: user} do
93 other_user = insert(:user)
94
95 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
96
97 result =
98 conn
99 |> put_req_header("content-type", "application/json")
100 |> post("/api/v1/pleroma/chats/#{chat.id}/messages")
101 |> json_response_and_validate_schema(400)
102
103 assert %{"error" => "no_content"} == result
104 end
105
106 test "it works with an attachment", %{conn: conn, user: user} do
107 file = %Plug.Upload{
108 content_type: "image/jpg",
109 path: Path.absname("test/fixtures/image.jpg"),
110 filename: "an_image.jpg"
111 }
112
113 {:ok, upload} = ActivityPub.upload(file, actor: user.ap_id)
114
115 other_user = insert(:user)
116
117 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
118
119 result =
120 conn
121 |> put_req_header("content-type", "application/json")
122 |> post("/api/v1/pleroma/chats/#{chat.id}/messages", %{
123 "media_id" => to_string(upload.id)
124 })
125 |> json_response_and_validate_schema(200)
126
127 assert result["attachment"]
128 end
129
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])
133
134 other_user = insert(:user)
135
136 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
137
138 result =
139 conn
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)
143
144 assert %{"error" => "[KeywordPolicy] Matches with rejected keyword"} == result
145 end
146 end
147
148 describe "DELETE /api/v1/pleroma/chats/:id/messages/:message_id" do
149 setup do: oauth_access(["write:chats"])
150
151 test "it deletes a message from the chat", %{conn: conn, user: user} do
152 recipient = insert(:user)
153
154 {:ok, message} =
155 CommonAPI.post_chat_message(user, recipient, "Hello darkness my old friend")
156
157 {:ok, other_message} = CommonAPI.post_chat_message(recipient, user, "nico nico ni")
158
159 object = Object.normalize(message, false)
160
161 chat = Chat.get(user.id, recipient.ap_id)
162
163 cm_ref = MessageReference.for_chat_and_object(chat, object)
164
165 # Deleting your own message removes the message and the reference
166 result =
167 conn
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)
171
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)
175
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)
179
180 result =
181 conn
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)
185
186 assert result["id"] == cm_ref.id
187 refute MessageReference.get_by_id(cm_ref.id)
188 assert Object.get_by_id(object.id)
189 end
190 end
191
192 describe "GET /api/v1/pleroma/chats/:id/messages" do
193 setup do: oauth_access(["read:chats"])
194
195 test "it paginates", %{conn: conn, user: user} do
196 recipient = insert(:user)
197
198 Enum.each(1..30, fn _ ->
199 {:ok, _} = CommonAPI.post_chat_message(user, recipient, "hey")
200 end)
201
202 chat = Chat.get(user.id, recipient.ap_id)
203
204 response = get(conn, "/api/v1/pleroma/chats/#{chat.id}/messages")
205 result = json_response_and_validate_schema(response, 200)
206
207 [next, prev] = get_resp_header(response, "link") |> hd() |> String.split(", ")
208 api_endpoint = "/api/v1/pleroma/chats/"
209
210 assert String.match?(
211 next,
212 ~r(#{api_endpoint}.*/messages\?id=.*&limit=\d+&max_id=.*; rel=\"next\"$)
213 )
214
215 assert String.match?(
216 prev,
217 ~r(#{api_endpoint}.*/messages\?id=.*&limit=\d+&min_id=.*; rel=\"prev\"$)
218 )
219
220 assert length(result) == 20
221
222 response =
223 get(conn, "/api/v1/pleroma/chats/#{chat.id}/messages?max_id=#{List.last(result)["id"]}")
224
225 result = json_response_and_validate_schema(response, 200)
226 [next, prev] = get_resp_header(response, "link") |> hd() |> String.split(", ")
227
228 assert String.match?(
229 next,
230 ~r(#{api_endpoint}.*/messages\?id=.*&limit=\d+&max_id=.*; rel=\"next\"$)
231 )
232
233 assert String.match?(
234 prev,
235 ~r(#{api_endpoint}.*/messages\?id=.*&limit=\d+&max_id=.*&min_id=.*; rel=\"prev\"$)
236 )
237
238 assert length(result) == 10
239 end
240
241 test "it returns the messages for a given chat", %{conn: conn, user: user} do
242 other_user = insert(:user)
243 third_user = insert(:user)
244
245 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "hey")
246 {:ok, _} = CommonAPI.post_chat_message(user, third_user, "hey")
247 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "how are you?")
248 {:ok, _} = CommonAPI.post_chat_message(other_user, user, "fine, how about you?")
249
250 chat = Chat.get(user.id, other_user.ap_id)
251
252 result =
253 conn
254 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
255 |> json_response_and_validate_schema(200)
256
257 result
258 |> Enum.each(fn message ->
259 assert message["chat_id"] == chat.id |> to_string()
260 end)
261
262 assert length(result) == 3
263
264 # Trying to get the chat of a different user
265 conn
266 |> assign(:user, other_user)
267 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
268 |> json_response_and_validate_schema(404)
269 end
270 end
271
272 describe "POST /api/v1/pleroma/chats/by-account-id/:id" do
273 setup do: oauth_access(["write:chats"])
274
275 test "it creates or returns a chat", %{conn: conn} do
276 other_user = insert(:user)
277
278 result =
279 conn
280 |> post("/api/v1/pleroma/chats/by-account-id/#{other_user.id}")
281 |> json_response_and_validate_schema(200)
282
283 assert result["id"]
284 end
285 end
286
287 describe "GET /api/v1/pleroma/chats/:id" do
288 setup do: oauth_access(["read:chats"])
289
290 test "it returns a chat", %{conn: conn, user: user} do
291 other_user = insert(:user)
292
293 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
294
295 result =
296 conn
297 |> get("/api/v1/pleroma/chats/#{chat.id}")
298 |> json_response_and_validate_schema(200)
299
300 assert result["id"] == to_string(chat.id)
301 end
302 end
303
304 describe "GET /api/v1/pleroma/chats" do
305 setup do: oauth_access(["read:chats"])
306
307 test "it does not return chats with deleted users", %{conn: conn, user: user} do
308 recipient = insert(:user)
309 {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
310
311 Pleroma.Repo.delete(recipient)
312 User.invalidate_cache(recipient)
313
314 result =
315 conn
316 |> get("/api/v1/pleroma/chats")
317 |> json_response_and_validate_schema(200)
318
319 assert length(result) == 0
320 end
321
322 test "it does not return chats with users you blocked", %{conn: conn, user: user} do
323 recipient = insert(:user)
324
325 {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
326
327 result =
328 conn
329 |> get("/api/v1/pleroma/chats")
330 |> json_response_and_validate_schema(200)
331
332 assert length(result) == 1
333
334 User.block(user, recipient)
335
336 result =
337 conn
338 |> get("/api/v1/pleroma/chats")
339 |> json_response_and_validate_schema(200)
340
341 assert length(result) == 0
342 end
343
344 test "it returns all chats", %{conn: conn, user: user} do
345 Enum.each(1..30, fn _ ->
346 recipient = insert(:user)
347 {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
348 end)
349
350 result =
351 conn
352 |> get("/api/v1/pleroma/chats")
353 |> json_response_and_validate_schema(200)
354
355 assert length(result) == 30
356 end
357
358 test "it return a list of chats the current user is participating in, in descending order of updates",
359 %{conn: conn, user: user} do
360 har = insert(:user)
361 jafnhar = insert(:user)
362 tridi = insert(:user)
363
364 {:ok, chat_1} = Chat.get_or_create(user.id, har.ap_id)
365 :timer.sleep(1000)
366 {:ok, _chat_2} = Chat.get_or_create(user.id, jafnhar.ap_id)
367 :timer.sleep(1000)
368 {:ok, chat_3} = Chat.get_or_create(user.id, tridi.ap_id)
369 :timer.sleep(1000)
370
371 # bump the second one
372 {:ok, chat_2} = Chat.bump_or_create(user.id, jafnhar.ap_id)
373
374 result =
375 conn
376 |> get("/api/v1/pleroma/chats")
377 |> json_response_and_validate_schema(200)
378
379 ids = Enum.map(result, & &1["id"])
380
381 assert ids == [
382 chat_2.id |> to_string(),
383 chat_3.id |> to_string(),
384 chat_1.id |> to_string()
385 ]
386 end
387
388 test "it is not affected by :restrict_unauthenticated setting (issue #1973)", %{
389 conn: conn,
390 user: user
391 } do
392 clear_config([:restrict_unauthenticated, :profiles, :local], true)
393 clear_config([:restrict_unauthenticated, :profiles, :remote], true)
394
395 user2 = insert(:user)
396 user3 = insert(:user, local: false)
397
398 {:ok, _chat_12} = Chat.get_or_create(user.id, user2.ap_id)
399 {:ok, _chat_13} = Chat.get_or_create(user.id, user3.ap_id)
400
401 result =
402 conn
403 |> get("/api/v1/pleroma/chats")
404 |> json_response_and_validate_schema(200)
405
406 account_ids = Enum.map(result, &get_in(&1, ["account", "id"]))
407 assert Enum.sort(account_ids) == Enum.sort([user2.id, user3.id])
408 end
409 end
410 end