a817a2dba181276dd62391e691968d4a3e463024
[akkoma] / test / pleroma / web / pleroma_api / controllers / chat_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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, fetch: 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, fetch: 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 |> put_req_header("idempotency-key", "123")
86 |> post("/api/v1/pleroma/chats/#{chat.id}/messages", %{"content" => "Hallo!!"})
87 |> json_response_and_validate_schema(200)
88
89 assert result["content"] == "Hallo!!"
90 assert result["chat_id"] == chat.id |> to_string()
91 assert result["idempotency_key"] == "123"
92 end
93
94 test "it fails if there is no content", %{conn: conn, user: user} do
95 other_user = insert(:user)
96
97 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
98
99 result =
100 conn
101 |> put_req_header("content-type", "application/json")
102 |> post("/api/v1/pleroma/chats/#{chat.id}/messages")
103 |> json_response_and_validate_schema(400)
104
105 assert %{"error" => "no_content"} == result
106 end
107
108 test "it works with an attachment", %{conn: conn, user: user} do
109 clear_config([Pleroma.Upload, :uploader], Pleroma.Uploaders.Local)
110 clear_config([Pleroma.Uploaders.Local, :uploads], "uploads")
111
112 file = %Plug.Upload{
113 content_type: "image/jpeg",
114 path: Path.absname("test/fixtures/image.jpg"),
115 filename: "an_image.jpg"
116 }
117
118 {:ok, upload} = ActivityPub.upload(file, actor: user.ap_id)
119
120 other_user = insert(:user)
121
122 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
123
124 result =
125 conn
126 |> put_req_header("content-type", "application/json")
127 |> post("/api/v1/pleroma/chats/#{chat.id}/messages", %{
128 "media_id" => to_string(upload.id)
129 })
130 |> json_response_and_validate_schema(200)
131
132 assert result["attachment"]
133 end
134
135 test "gets MRF reason when rejected", %{conn: conn, user: user} do
136 clear_config([:mrf_keyword, :reject], ["GNO"])
137 clear_config([:mrf, :policies], [Pleroma.Web.ActivityPub.MRF.KeywordPolicy])
138
139 other_user = insert(:user)
140
141 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
142
143 result =
144 conn
145 |> put_req_header("content-type", "application/json")
146 |> post("/api/v1/pleroma/chats/#{chat.id}/messages", %{"content" => "GNO/Linux"})
147 |> json_response_and_validate_schema(422)
148
149 assert %{"error" => "[KeywordPolicy] Matches with rejected keyword"} == result
150 end
151 end
152
153 describe "DELETE /api/v1/pleroma/chats/:id/messages/:message_id" do
154 setup do: oauth_access(["write:chats"])
155
156 test "it deletes a message from the chat", %{conn: conn, user: user} do
157 recipient = insert(:user)
158
159 {:ok, message} =
160 CommonAPI.post_chat_message(user, recipient, "Hello darkness my old friend")
161
162 {:ok, other_message} = CommonAPI.post_chat_message(recipient, user, "nico nico ni")
163
164 object = Object.normalize(message, fetch: false)
165
166 chat = Chat.get(user.id, recipient.ap_id)
167
168 cm_ref = MessageReference.for_chat_and_object(chat, object)
169
170 # Deleting your own message removes the message and the reference
171 result =
172 conn
173 |> put_req_header("content-type", "application/json")
174 |> delete("/api/v1/pleroma/chats/#{chat.id}/messages/#{cm_ref.id}")
175 |> json_response_and_validate_schema(200)
176
177 assert result["id"] == cm_ref.id
178 refute MessageReference.get_by_id(cm_ref.id)
179 assert %{data: %{"type" => "Tombstone"}} = Object.get_by_id(object.id)
180
181 # Deleting other people's messages just removes the reference
182 object = Object.normalize(other_message, fetch: false)
183 cm_ref = MessageReference.for_chat_and_object(chat, object)
184
185 result =
186 conn
187 |> put_req_header("content-type", "application/json")
188 |> delete("/api/v1/pleroma/chats/#{chat.id}/messages/#{cm_ref.id}")
189 |> json_response_and_validate_schema(200)
190
191 assert result["id"] == cm_ref.id
192 refute MessageReference.get_by_id(cm_ref.id)
193 assert Object.get_by_id(object.id)
194 end
195 end
196
197 describe "GET /api/v1/pleroma/chats/:id/messages" do
198 setup do: oauth_access(["read:chats"])
199
200 test "it paginates", %{conn: conn, user: user} do
201 recipient = insert(:user)
202
203 Enum.each(1..30, fn _ ->
204 {:ok, _} = CommonAPI.post_chat_message(user, recipient, "hey")
205 end)
206
207 chat = Chat.get(user.id, recipient.ap_id)
208
209 response = get(conn, "/api/v1/pleroma/chats/#{chat.id}/messages")
210 result = json_response_and_validate_schema(response, 200)
211
212 [next, prev] = get_resp_header(response, "link") |> hd() |> String.split(", ")
213 api_endpoint = "/api/v1/pleroma/chats/"
214
215 assert String.match?(
216 next,
217 ~r(#{api_endpoint}.*/messages\?limit=\d+&max_id=.*; rel=\"next\"$)
218 )
219
220 assert String.match?(
221 prev,
222 ~r(#{api_endpoint}.*/messages\?limit=\d+&min_id=.*; rel=\"prev\"$)
223 )
224
225 assert length(result) == 20
226
227 response =
228 get(conn, "/api/v1/pleroma/chats/#{chat.id}/messages?max_id=#{List.last(result)["id"]}")
229
230 result = json_response_and_validate_schema(response, 200)
231 [next, prev] = get_resp_header(response, "link") |> hd() |> String.split(", ")
232
233 assert String.match?(
234 next,
235 ~r(#{api_endpoint}.*/messages\?limit=\d+&max_id=.*; rel=\"next\"$)
236 )
237
238 assert String.match?(
239 prev,
240 ~r(#{api_endpoint}.*/messages\?limit=\d+&max_id=.*&min_id=.*; rel=\"prev\"$)
241 )
242
243 assert length(result) == 10
244 end
245
246 test "it returns the messages for a given chat", %{conn: conn, user: user} do
247 other_user = insert(:user)
248 third_user = insert(:user)
249
250 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "hey")
251 {:ok, _} = CommonAPI.post_chat_message(user, third_user, "hey")
252 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "how are you?")
253 {:ok, _} = CommonAPI.post_chat_message(other_user, user, "fine, how about you?")
254
255 chat = Chat.get(user.id, other_user.ap_id)
256
257 result =
258 conn
259 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
260 |> json_response_and_validate_schema(200)
261
262 result
263 |> Enum.each(fn message ->
264 assert message["chat_id"] == chat.id |> to_string()
265 end)
266
267 assert length(result) == 3
268
269 # Trying to get the chat of a different user
270 other_user_chat = Chat.get(other_user.id, user.ap_id)
271
272 conn
273 |> get("/api/v1/pleroma/chats/#{other_user_chat.id}/messages")
274 |> json_response_and_validate_schema(404)
275 end
276 end
277
278 describe "POST /api/v1/pleroma/chats/by-account-id/:id" do
279 setup do: oauth_access(["write:chats"])
280
281 test "it creates or returns a chat", %{conn: conn} do
282 other_user = insert(:user)
283
284 result =
285 conn
286 |> post("/api/v1/pleroma/chats/by-account-id/#{other_user.id}")
287 |> json_response_and_validate_schema(200)
288
289 assert result["id"]
290 end
291 end
292
293 describe "GET /api/v1/pleroma/chats/:id" do
294 setup do: oauth_access(["read:chats"])
295
296 test "it returns a chat", %{conn: conn, user: user} do
297 other_user = insert(:user)
298
299 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
300
301 result =
302 conn
303 |> get("/api/v1/pleroma/chats/#{chat.id}")
304 |> json_response_and_validate_schema(200)
305
306 assert result["id"] == to_string(chat.id)
307 end
308 end
309
310 for tested_endpoint <- ["/api/v1/pleroma/chats", "/api/v2/pleroma/chats"] do
311 describe "GET #{tested_endpoint}" do
312 setup do: oauth_access(["read:chats"])
313
314 test "it does not return chats with deleted users", %{conn: conn, user: user} do
315 recipient = insert(:user)
316 {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
317
318 Pleroma.Repo.delete(recipient)
319 User.invalidate_cache(recipient)
320
321 result =
322 conn
323 |> get(unquote(tested_endpoint))
324 |> json_response_and_validate_schema(200)
325
326 assert length(result) == 0
327 end
328
329 test "it does not return chats with users you blocked", %{conn: conn, user: user} do
330 recipient = insert(:user)
331
332 {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
333
334 result =
335 conn
336 |> get(unquote(tested_endpoint))
337 |> json_response_and_validate_schema(200)
338
339 assert length(result) == 1
340
341 User.block(user, recipient)
342
343 result =
344 conn
345 |> get(unquote(tested_endpoint))
346 |> json_response_and_validate_schema(200)
347
348 assert length(result) == 0
349 end
350
351 test "it does not return chats with users you muted", %{conn: conn, user: user} do
352 recipient = insert(:user)
353
354 {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
355
356 result =
357 conn
358 |> get(unquote(tested_endpoint))
359 |> json_response_and_validate_schema(200)
360
361 assert length(result) == 1
362
363 User.mute(user, recipient)
364
365 result =
366 conn
367 |> get(unquote(tested_endpoint))
368 |> json_response_and_validate_schema(200)
369
370 assert length(result) == 0
371
372 result =
373 conn
374 |> get("#{unquote(tested_endpoint)}?with_muted=true")
375 |> json_response_and_validate_schema(200)
376
377 assert length(result) == 1
378 end
379
380 if tested_endpoint == "/api/v1/pleroma/chats" do
381 test "it returns all chats", %{conn: conn, user: user} do
382 Enum.each(1..30, fn _ ->
383 recipient = insert(:user)
384 {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
385 end)
386
387 result =
388 conn
389 |> get(unquote(tested_endpoint))
390 |> json_response_and_validate_schema(200)
391
392 assert length(result) == 30
393 end
394 else
395 test "it paginates chats", %{conn: conn, user: user} do
396 Enum.each(1..30, fn _ ->
397 recipient = insert(:user)
398 {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
399 end)
400
401 result =
402 conn
403 |> get(unquote(tested_endpoint))
404 |> json_response_and_validate_schema(200)
405
406 assert length(result) == 20
407 last_id = List.last(result)["id"]
408
409 result =
410 conn
411 |> get(unquote(tested_endpoint) <> "?max_id=#{last_id}")
412 |> json_response_and_validate_schema(200)
413
414 assert length(result) == 10
415 end
416 end
417
418 test "it return a list of chats the current user is participating in, in descending order of updates",
419 %{conn: conn, user: user} do
420 har = insert(:user)
421 jafnhar = insert(:user)
422 tridi = insert(:user)
423
424 {:ok, chat_1} = Chat.get_or_create(user.id, har.ap_id)
425 {:ok, chat_1} = time_travel(chat_1, -3)
426 {:ok, chat_2} = Chat.get_or_create(user.id, jafnhar.ap_id)
427 {:ok, _chat_2} = time_travel(chat_2, -2)
428 {:ok, chat_3} = Chat.get_or_create(user.id, tridi.ap_id)
429 {:ok, chat_3} = time_travel(chat_3, -1)
430
431 # bump the second one
432 {:ok, chat_2} = Chat.bump_or_create(user.id, jafnhar.ap_id)
433
434 result =
435 conn
436 |> get(unquote(tested_endpoint))
437 |> json_response_and_validate_schema(200)
438
439 ids = Enum.map(result, & &1["id"])
440
441 assert ids == [
442 chat_2.id |> to_string(),
443 chat_3.id |> to_string(),
444 chat_1.id |> to_string()
445 ]
446 end
447
448 test "it is not affected by :restrict_unauthenticated setting (issue #1973)", %{
449 conn: conn,
450 user: user
451 } do
452 clear_config([:restrict_unauthenticated, :profiles, :local], true)
453 clear_config([:restrict_unauthenticated, :profiles, :remote], true)
454
455 user2 = insert(:user)
456 user3 = insert(:user, local: false)
457
458 {:ok, _chat_12} = Chat.get_or_create(user.id, user2.ap_id)
459 {:ok, _chat_13} = Chat.get_or_create(user.id, user3.ap_id)
460
461 result =
462 conn
463 |> get(unquote(tested_endpoint))
464 |> json_response_and_validate_schema(200)
465
466 account_ids = Enum.map(result, &get_in(&1, ["account", "id"]))
467 assert Enum.sort(account_ids) == Enum.sort([user2.id, user3.id])
468 end
469 end
470 end
471 end