added paginate links to headers for /chats/:id/messages
[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 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 end
130
131 describe "DELETE /api/v1/pleroma/chats/:id/messages/:message_id" do
132 setup do: oauth_access(["write:chats"])
133
134 test "it deletes a message from the chat", %{conn: conn, user: user} do
135 recipient = insert(:user)
136
137 {:ok, message} =
138 CommonAPI.post_chat_message(user, recipient, "Hello darkness my old friend")
139
140 {:ok, other_message} = CommonAPI.post_chat_message(recipient, user, "nico nico ni")
141
142 object = Object.normalize(message, false)
143
144 chat = Chat.get(user.id, recipient.ap_id)
145
146 cm_ref = MessageReference.for_chat_and_object(chat, object)
147
148 # Deleting your own message removes the message and the reference
149 result =
150 conn
151 |> put_req_header("content-type", "application/json")
152 |> delete("/api/v1/pleroma/chats/#{chat.id}/messages/#{cm_ref.id}")
153 |> json_response_and_validate_schema(200)
154
155 assert result["id"] == cm_ref.id
156 refute MessageReference.get_by_id(cm_ref.id)
157 assert %{data: %{"type" => "Tombstone"}} = Object.get_by_id(object.id)
158
159 # Deleting other people's messages just removes the reference
160 object = Object.normalize(other_message, false)
161 cm_ref = MessageReference.for_chat_and_object(chat, object)
162
163 result =
164 conn
165 |> put_req_header("content-type", "application/json")
166 |> delete("/api/v1/pleroma/chats/#{chat.id}/messages/#{cm_ref.id}")
167 |> json_response_and_validate_schema(200)
168
169 assert result["id"] == cm_ref.id
170 refute MessageReference.get_by_id(cm_ref.id)
171 assert Object.get_by_id(object.id)
172 end
173 end
174
175 describe "GET /api/v1/pleroma/chats/:id/messages" do
176 setup do: oauth_access(["read:chats"])
177
178 test "it paginates", %{conn: conn, user: user} do
179 recipient = insert(:user)
180
181 Enum.each(1..30, fn _ ->
182 {:ok, _} = CommonAPI.post_chat_message(user, recipient, "hey")
183 end)
184
185 chat = Chat.get(user.id, recipient.ap_id)
186
187 response = get(conn, "/api/v1/pleroma/chats/#{chat.id}/messages")
188 result = json_response_and_validate_schema(response, 200)
189
190 [next, prev] = get_resp_header(response, "link") |> hd() |> String.split(", ")
191 api_endpoint = "/api/v1/pleroma/chats/"
192
193 assert String.match?(
194 next,
195 ~r(#{api_endpoint}.*/messages\?id=.*&limit=\d+&max_id=.*; rel=\"next\"$)
196 )
197
198 assert String.match?(
199 prev,
200 ~r(#{api_endpoint}.*/messages\?id=.*&limit=\d+&min_id=.*; rel=\"prev\"$)
201 )
202
203 assert length(result) == 20
204
205 response =
206 get(conn, "/api/v1/pleroma/chats/#{chat.id}/messages?max_id=#{List.last(result)["id"]}")
207
208 result = json_response_and_validate_schema(response, 200)
209 [next, prev] = get_resp_header(response, "link") |> hd() |> String.split(", ")
210
211 assert String.match?(
212 next,
213 ~r(#{api_endpoint}.*/messages\?id=.*&limit=\d+&max_id=.*; rel=\"next\"$)
214 )
215
216 assert String.match?(
217 prev,
218 ~r(#{api_endpoint}.*/messages\?id=.*&limit=\d+&max_id=.*&min_id=.*; rel=\"prev\"$)
219 )
220
221 assert length(result) == 10
222 end
223
224 test "it returns the messages for a given chat", %{conn: conn, user: user} do
225 other_user = insert(:user)
226 third_user = insert(:user)
227
228 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "hey")
229 {:ok, _} = CommonAPI.post_chat_message(user, third_user, "hey")
230 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "how are you?")
231 {:ok, _} = CommonAPI.post_chat_message(other_user, user, "fine, how about you?")
232
233 chat = Chat.get(user.id, other_user.ap_id)
234
235 result =
236 conn
237 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
238 |> json_response_and_validate_schema(200)
239
240 result
241 |> Enum.each(fn message ->
242 assert message["chat_id"] == chat.id |> to_string()
243 end)
244
245 assert length(result) == 3
246
247 # Trying to get the chat of a different user
248 conn
249 |> assign(:user, other_user)
250 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
251 |> json_response_and_validate_schema(404)
252 end
253 end
254
255 describe "POST /api/v1/pleroma/chats/by-account-id/:id" do
256 setup do: oauth_access(["write:chats"])
257
258 test "it creates or returns a chat", %{conn: conn} do
259 other_user = insert(:user)
260
261 result =
262 conn
263 |> post("/api/v1/pleroma/chats/by-account-id/#{other_user.id}")
264 |> json_response_and_validate_schema(200)
265
266 assert result["id"]
267 end
268 end
269
270 describe "GET /api/v1/pleroma/chats/:id" do
271 setup do: oauth_access(["read:chats"])
272
273 test "it returns a chat", %{conn: conn, user: user} do
274 other_user = insert(:user)
275
276 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
277
278 result =
279 conn
280 |> get("/api/v1/pleroma/chats/#{chat.id}")
281 |> json_response_and_validate_schema(200)
282
283 assert result["id"] == to_string(chat.id)
284 end
285 end
286
287 describe "GET /api/v1/pleroma/chats" do
288 setup do: oauth_access(["read:chats"])
289
290 test "it does not return chats with deleted users", %{conn: conn, user: user} do
291 recipient = insert(:user)
292 {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
293
294 Pleroma.Repo.delete(recipient)
295 User.invalidate_cache(recipient)
296
297 result =
298 conn
299 |> get("/api/v1/pleroma/chats")
300 |> json_response_and_validate_schema(200)
301
302 assert length(result) == 0
303 end
304
305 test "it does not return chats with users you blocked", %{conn: conn, user: user} do
306 recipient = insert(:user)
307
308 {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
309
310 result =
311 conn
312 |> get("/api/v1/pleroma/chats")
313 |> json_response_and_validate_schema(200)
314
315 assert length(result) == 1
316
317 User.block(user, recipient)
318
319 result =
320 conn
321 |> get("/api/v1/pleroma/chats")
322 |> json_response_and_validate_schema(200)
323
324 assert length(result) == 0
325 end
326
327 test "it returns all chats", %{conn: conn, user: user} do
328 Enum.each(1..30, fn _ ->
329 recipient = insert(:user)
330 {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
331 end)
332
333 result =
334 conn
335 |> get("/api/v1/pleroma/chats")
336 |> json_response_and_validate_schema(200)
337
338 assert length(result) == 30
339 end
340
341 test "it return a list of chats the current user is participating in, in descending order of updates",
342 %{conn: conn, user: user} do
343 har = insert(:user)
344 jafnhar = insert(:user)
345 tridi = insert(:user)
346
347 {:ok, chat_1} = Chat.get_or_create(user.id, har.ap_id)
348 :timer.sleep(1000)
349 {:ok, _chat_2} = Chat.get_or_create(user.id, jafnhar.ap_id)
350 :timer.sleep(1000)
351 {:ok, chat_3} = Chat.get_or_create(user.id, tridi.ap_id)
352 :timer.sleep(1000)
353
354 # bump the second one
355 {:ok, chat_2} = Chat.bump_or_create(user.id, jafnhar.ap_id)
356
357 result =
358 conn
359 |> get("/api/v1/pleroma/chats")
360 |> json_response_and_validate_schema(200)
361
362 ids = Enum.map(result, & &1["id"])
363
364 assert ids == [
365 chat_2.id |> to_string(),
366 chat_3.id |> to_string(),
367 chat_1.id |> to_string()
368 ]
369 end
370
371 test "it is not affected by :restrict_unauthenticated setting (issue #1973)", %{
372 conn: conn,
373 user: user
374 } do
375 clear_config([:restrict_unauthenticated, :profiles, :local], true)
376 clear_config([:restrict_unauthenticated, :profiles, :remote], true)
377
378 user2 = insert(:user)
379 user3 = insert(:user, local: false)
380
381 {:ok, _chat_12} = Chat.get_or_create(user.id, user2.ap_id)
382 {:ok, _chat_13} = Chat.get_or_create(user.id, user3.ap_id)
383
384 result =
385 conn
386 |> get("/api/v1/pleroma/chats")
387 |> json_response_and_validate_schema(200)
388
389 account_ids = Enum.map(result, &get_in(&1, ["account", "id"]))
390 assert Enum.sort(account_ids) == Enum.sort([user2.id, user3.id])
391 end
392 end
393 end