remove all endpoints marked as deprecated (#91)
[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 describe "GET /api/v2/pleroma/chats" do
311 setup do: oauth_access(["read:chats"])
312
313 test "it does not return chats with deleted users", %{conn: conn, user: user} do
314 recipient = insert(:user)
315 {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
316
317 Pleroma.Repo.delete(recipient)
318 User.invalidate_cache(recipient)
319
320 result =
321 conn
322 |> get("/api/v2/pleroma/chats")
323 |> json_response_and_validate_schema(200)
324
325 assert length(result) == 0
326 end
327
328 test "it does not return chats with users you blocked", %{conn: conn, user: user} do
329 recipient = insert(:user)
330
331 {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
332
333 result =
334 conn
335 |> get("/api/v2/pleroma/chats")
336 |> json_response_and_validate_schema(200)
337
338 assert length(result) == 1
339
340 User.block(user, recipient)
341
342 result =
343 conn
344 |> get("/api/v2/pleroma/chats")
345 |> json_response_and_validate_schema(200)
346
347 assert length(result) == 0
348 end
349
350 test "it does not return chats with users you muted", %{conn: conn, user: user} do
351 recipient = insert(:user)
352
353 {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
354
355 result =
356 conn
357 |> get("/api/v2/pleroma/chats")
358 |> json_response_and_validate_schema(200)
359
360 assert length(result) == 1
361
362 User.mute(user, recipient)
363
364 result =
365 conn
366 |> get("/api/v2/pleroma/chats")
367 |> json_response_and_validate_schema(200)
368
369 assert length(result) == 0
370
371 result =
372 conn
373 |> get("/api/v2/pleroma/chats?with_muted=true")
374 |> json_response_and_validate_schema(200)
375
376 assert length(result) == 1
377 end
378
379 test "it paginates chats", %{conn: conn, user: user} do
380 Enum.each(1..30, fn _ ->
381 recipient = insert(:user)
382 {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
383 end)
384
385 result =
386 conn
387 |> get("/api/v2/pleroma/chats")
388 |> json_response_and_validate_schema(200)
389
390 assert length(result) == 20
391 last_id = List.last(result)["id"]
392
393 result =
394 conn
395 |> get("/api/v2/pleroma/chats?max_id=#{last_id}")
396 |> json_response_and_validate_schema(200)
397
398 assert length(result) == 10
399 end
400
401 test "it return a list of chats the current user is participating in, in descending order of updates",
402 %{conn: conn, user: user} do
403 har = insert(:user)
404 jafnhar = insert(:user)
405 tridi = insert(:user)
406
407 {:ok, chat_1} = Chat.get_or_create(user.id, har.ap_id)
408 {:ok, chat_1} = time_travel(chat_1, -3)
409 {:ok, chat_2} = Chat.get_or_create(user.id, jafnhar.ap_id)
410 {:ok, _chat_2} = time_travel(chat_2, -2)
411 {:ok, chat_3} = Chat.get_or_create(user.id, tridi.ap_id)
412 {:ok, chat_3} = time_travel(chat_3, -1)
413
414 # bump the second one
415 {:ok, chat_2} = Chat.bump_or_create(user.id, jafnhar.ap_id)
416
417 result =
418 conn
419 |> get("/api/v2/pleroma/chats")
420 |> json_response_and_validate_schema(200)
421
422 ids = Enum.map(result, & &1["id"])
423
424 assert ids == [
425 chat_2.id |> to_string(),
426 chat_3.id |> to_string(),
427 chat_1.id |> to_string()
428 ]
429 end
430
431 test "it is not affected by :restrict_unauthenticated setting (issue #1973)", %{
432 conn: conn,
433 user: user
434 } do
435 clear_config([:restrict_unauthenticated, :profiles, :local], true)
436 clear_config([:restrict_unauthenticated, :profiles, :remote], true)
437
438 user2 = insert(:user)
439 user3 = insert(:user, local: false)
440
441 {:ok, _chat_12} = Chat.get_or_create(user.id, user2.ap_id)
442 {:ok, _chat_13} = Chat.get_or_create(user.id, user3.ap_id)
443
444 result =
445 conn
446 |> get("/api/v2/pleroma/chats")
447 |> json_response_and_validate_schema(200)
448
449 account_ids = Enum.map(result, &get_in(&1, ["account", "id"]))
450 assert Enum.sort(account_ids) == Enum.sort([user2.id, user3.id])
451 end
452 end
453 end