63cd89c735adedbf941ae164a62200e7c321a2d8
[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, async: true
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 "it marks all messages in a chat as read", %{conn: conn, user: user} do
47 other_user = insert(:user)
48
49 {:ok, create} = CommonAPI.post_chat_message(other_user, user, "sup")
50 {:ok, _create} = CommonAPI.post_chat_message(other_user, user, "sup part 2")
51 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
52 object = Object.normalize(create, false)
53 cm_ref = MessageReference.for_chat_and_object(chat, object)
54
55 assert cm_ref.unread == true
56
57 result =
58 conn
59 |> post("/api/v1/pleroma/chats/#{chat.id}/read")
60 |> json_response_and_validate_schema(200)
61
62 assert result["unread"] == 0
63
64 cm_ref = MessageReference.for_chat_and_object(chat, object)
65
66 assert cm_ref.unread == false
67 end
68
69 test "it given a `last_read_id` ", %{conn: conn, user: user} do
70 other_user = insert(:user)
71
72 {:ok, create} = CommonAPI.post_chat_message(other_user, user, "sup")
73 {:ok, _create} = CommonAPI.post_chat_message(other_user, user, "sup part 2")
74 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
75 object = Object.normalize(create, false)
76 cm_ref = MessageReference.for_chat_and_object(chat, object)
77
78 assert cm_ref.unread == true
79
80 result =
81 conn
82 |> put_req_header("content-type", "application/json")
83 |> post("/api/v1/pleroma/chats/#{chat.id}/read", %{"last_read_id" => cm_ref.id})
84 |> json_response_and_validate_schema(200)
85
86 assert result["unread"] == 1
87
88 cm_ref = MessageReference.for_chat_and_object(chat, object)
89
90 assert cm_ref.unread == false
91 end
92 end
93
94 describe "POST /api/v1/pleroma/chats/:id/messages" do
95 setup do: oauth_access(["write:chats"])
96
97 test "it posts a message to the chat", %{conn: conn, user: user} do
98 other_user = insert(:user)
99
100 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
101
102 result =
103 conn
104 |> put_req_header("content-type", "application/json")
105 |> post("/api/v1/pleroma/chats/#{chat.id}/messages", %{"content" => "Hallo!!"})
106 |> json_response_and_validate_schema(200)
107
108 assert result["content"] == "Hallo!!"
109 assert result["chat_id"] == chat.id |> to_string()
110 end
111
112 test "it fails if there is no content", %{conn: conn, user: user} do
113 other_user = insert(:user)
114
115 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
116
117 result =
118 conn
119 |> put_req_header("content-type", "application/json")
120 |> post("/api/v1/pleroma/chats/#{chat.id}/messages")
121 |> json_response_and_validate_schema(400)
122
123 assert result
124 end
125
126 test "it works with an attachment", %{conn: conn, user: user} do
127 file = %Plug.Upload{
128 content_type: "image/jpg",
129 path: Path.absname("test/fixtures/image.jpg"),
130 filename: "an_image.jpg"
131 }
132
133 {:ok, upload} = ActivityPub.upload(file, actor: user.ap_id)
134
135 other_user = insert(:user)
136
137 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
138
139 result =
140 conn
141 |> put_req_header("content-type", "application/json")
142 |> post("/api/v1/pleroma/chats/#{chat.id}/messages", %{
143 "media_id" => to_string(upload.id)
144 })
145 |> json_response_and_validate_schema(200)
146
147 assert result["attachment"]
148 end
149 end
150
151 describe "DELETE /api/v1/pleroma/chats/:id/messages/:message_id" do
152 setup do: oauth_access(["write:chats"])
153
154 test "it deletes a message from the chat", %{conn: conn, user: user} do
155 recipient = insert(:user)
156
157 {:ok, message} =
158 CommonAPI.post_chat_message(user, recipient, "Hello darkness my old friend")
159
160 {:ok, other_message} = CommonAPI.post_chat_message(recipient, user, "nico nico ni")
161
162 object = Object.normalize(message, false)
163
164 chat = Chat.get(user.id, recipient.ap_id)
165
166 cm_ref = MessageReference.for_chat_and_object(chat, object)
167
168 # Deleting your own message removes the message and the reference
169 result =
170 conn
171 |> put_req_header("content-type", "application/json")
172 |> delete("/api/v1/pleroma/chats/#{chat.id}/messages/#{cm_ref.id}")
173 |> json_response_and_validate_schema(200)
174
175 assert result["id"] == cm_ref.id
176 refute MessageReference.get_by_id(cm_ref.id)
177 assert %{data: %{"type" => "Tombstone"}} = Object.get_by_id(object.id)
178
179 # Deleting other people's messages just removes the reference
180 object = Object.normalize(other_message, false)
181 cm_ref = MessageReference.for_chat_and_object(chat, object)
182
183 result =
184 conn
185 |> put_req_header("content-type", "application/json")
186 |> delete("/api/v1/pleroma/chats/#{chat.id}/messages/#{cm_ref.id}")
187 |> json_response_and_validate_schema(200)
188
189 assert result["id"] == cm_ref.id
190 refute MessageReference.get_by_id(cm_ref.id)
191 assert Object.get_by_id(object.id)
192 end
193 end
194
195 describe "GET /api/v1/pleroma/chats/:id/messages" do
196 setup do: oauth_access(["read:chats"])
197
198 test "it paginates", %{conn: conn, user: user} do
199 recipient = insert(:user)
200
201 Enum.each(1..30, fn _ ->
202 {:ok, _} = CommonAPI.post_chat_message(user, recipient, "hey")
203 end)
204
205 chat = Chat.get(user.id, recipient.ap_id)
206
207 result =
208 conn
209 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
210 |> json_response_and_validate_schema(200)
211
212 assert length(result) == 20
213
214 result =
215 conn
216 |> get("/api/v1/pleroma/chats/#{chat.id}/messages?max_id=#{List.last(result)["id"]}")
217 |> json_response_and_validate_schema(200)
218
219 assert length(result) == 10
220 end
221
222 test "it returns the messages for a given chat", %{conn: conn, user: user} do
223 other_user = insert(:user)
224 third_user = insert(:user)
225
226 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "hey")
227 {:ok, _} = CommonAPI.post_chat_message(user, third_user, "hey")
228 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "how are you?")
229 {:ok, _} = CommonAPI.post_chat_message(other_user, user, "fine, how about you?")
230
231 chat = Chat.get(user.id, other_user.ap_id)
232
233 result =
234 conn
235 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
236 |> json_response_and_validate_schema(200)
237
238 result
239 |> Enum.each(fn message ->
240 assert message["chat_id"] == chat.id |> to_string()
241 end)
242
243 assert length(result) == 3
244
245 # Trying to get the chat of a different user
246 result =
247 conn
248 |> assign(:user, other_user)
249 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
250
251 assert result |> json_response(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 users you blocked", %{conn: conn, user: user} do
291 recipient = insert(:user)
292
293 {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
294
295 result =
296 conn
297 |> get("/api/v1/pleroma/chats")
298 |> json_response_and_validate_schema(200)
299
300 assert length(result) == 1
301
302 User.block(user, recipient)
303
304 result =
305 conn
306 |> get("/api/v1/pleroma/chats")
307 |> json_response_and_validate_schema(200)
308
309 assert length(result) == 0
310 end
311
312 test "it paginates", %{conn: conn, user: user} do
313 Enum.each(1..30, fn _ ->
314 recipient = insert(:user)
315 {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
316 end)
317
318 result =
319 conn
320 |> get("/api/v1/pleroma/chats")
321 |> json_response_and_validate_schema(200)
322
323 assert length(result) == 20
324
325 result =
326 conn
327 |> get("/api/v1/pleroma/chats?max_id=#{List.last(result)["id"]}")
328 |> json_response_and_validate_schema(200)
329
330 assert length(result) == 10
331 end
332
333 test "it return a list of chats the current user is participating in, in descending order of updates",
334 %{conn: conn, user: user} do
335 har = insert(:user)
336 jafnhar = insert(:user)
337 tridi = insert(:user)
338
339 {:ok, chat_1} = Chat.get_or_create(user.id, har.ap_id)
340 :timer.sleep(1000)
341 {:ok, _chat_2} = Chat.get_or_create(user.id, jafnhar.ap_id)
342 :timer.sleep(1000)
343 {:ok, chat_3} = Chat.get_or_create(user.id, tridi.ap_id)
344 :timer.sleep(1000)
345
346 # bump the second one
347 {:ok, chat_2} = Chat.bump_or_create(user.id, jafnhar.ap_id)
348
349 result =
350 conn
351 |> get("/api/v1/pleroma/chats")
352 |> json_response_and_validate_schema(200)
353
354 ids = Enum.map(result, & &1["id"])
355
356 assert ids == [
357 chat_2.id |> to_string(),
358 chat_3.id |> to_string(),
359 chat_1.id |> to_string()
360 ]
361 end
362 end
363 end