ChatController: Use new oauth scope *:chats.
[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 end
69
70 describe "POST /api/v1/pleroma/chats/:id/messages" do
71 setup do: oauth_access(["write:chats"])
72
73 test "it posts a message to the chat", %{conn: conn, user: user} do
74 other_user = insert(:user)
75
76 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
77
78 result =
79 conn
80 |> put_req_header("content-type", "application/json")
81 |> post("/api/v1/pleroma/chats/#{chat.id}/messages", %{"content" => "Hallo!!"})
82 |> json_response_and_validate_schema(200)
83
84 assert result["content"] == "Hallo!!"
85 assert result["chat_id"] == chat.id |> to_string()
86 end
87
88 test "it fails if there is no content", %{conn: conn, user: user} do
89 other_user = insert(:user)
90
91 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
92
93 result =
94 conn
95 |> put_req_header("content-type", "application/json")
96 |> post("/api/v1/pleroma/chats/#{chat.id}/messages")
97 |> json_response_and_validate_schema(400)
98
99 assert result
100 end
101
102 test "it works with an attachment", %{conn: conn, user: user} do
103 file = %Plug.Upload{
104 content_type: "image/jpg",
105 path: Path.absname("test/fixtures/image.jpg"),
106 filename: "an_image.jpg"
107 }
108
109 {:ok, upload} = ActivityPub.upload(file, actor: user.ap_id)
110
111 other_user = insert(:user)
112
113 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
114
115 result =
116 conn
117 |> put_req_header("content-type", "application/json")
118 |> post("/api/v1/pleroma/chats/#{chat.id}/messages", %{
119 "media_id" => to_string(upload.id)
120 })
121 |> json_response_and_validate_schema(200)
122
123 assert result["attachment"]
124 end
125 end
126
127 describe "DELETE /api/v1/pleroma/chats/:id/messages/:message_id" do
128 setup do: oauth_access(["write:chats"])
129
130 test "it deletes a message from the chat", %{conn: conn, user: user} do
131 recipient = insert(:user)
132
133 {:ok, message} =
134 CommonAPI.post_chat_message(user, recipient, "Hello darkness my old friend")
135
136 {:ok, other_message} = CommonAPI.post_chat_message(recipient, user, "nico nico ni")
137
138 object = Object.normalize(message, false)
139
140 chat = Chat.get(user.id, recipient.ap_id)
141
142 cm_ref = MessageReference.for_chat_and_object(chat, object)
143
144 # Deleting your own message removes the message and the reference
145 result =
146 conn
147 |> put_req_header("content-type", "application/json")
148 |> delete("/api/v1/pleroma/chats/#{chat.id}/messages/#{cm_ref.id}")
149 |> json_response_and_validate_schema(200)
150
151 assert result["id"] == cm_ref.id
152 refute MessageReference.get_by_id(cm_ref.id)
153 assert %{data: %{"type" => "Tombstone"}} = Object.get_by_id(object.id)
154
155 # Deleting other people's messages just removes the reference
156 object = Object.normalize(other_message, false)
157 cm_ref = MessageReference.for_chat_and_object(chat, object)
158
159 result =
160 conn
161 |> put_req_header("content-type", "application/json")
162 |> delete("/api/v1/pleroma/chats/#{chat.id}/messages/#{cm_ref.id}")
163 |> json_response_and_validate_schema(200)
164
165 assert result["id"] == cm_ref.id
166 refute MessageReference.get_by_id(cm_ref.id)
167 assert Object.get_by_id(object.id)
168 end
169 end
170
171 describe "GET /api/v1/pleroma/chats/:id/messages" do
172 setup do: oauth_access(["read:chats"])
173
174 test "it paginates", %{conn: conn, user: user} do
175 recipient = insert(:user)
176
177 Enum.each(1..30, fn _ ->
178 {:ok, _} = CommonAPI.post_chat_message(user, recipient, "hey")
179 end)
180
181 chat = Chat.get(user.id, recipient.ap_id)
182
183 result =
184 conn
185 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
186 |> json_response_and_validate_schema(200)
187
188 assert length(result) == 20
189
190 result =
191 conn
192 |> get("/api/v1/pleroma/chats/#{chat.id}/messages?max_id=#{List.last(result)["id"]}")
193 |> json_response_and_validate_schema(200)
194
195 assert length(result) == 10
196 end
197
198 test "it returns the messages for a given chat", %{conn: conn, user: user} do
199 other_user = insert(:user)
200 third_user = insert(:user)
201
202 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "hey")
203 {:ok, _} = CommonAPI.post_chat_message(user, third_user, "hey")
204 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "how are you?")
205 {:ok, _} = CommonAPI.post_chat_message(other_user, user, "fine, how about you?")
206
207 chat = Chat.get(user.id, other_user.ap_id)
208
209 result =
210 conn
211 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
212 |> json_response_and_validate_schema(200)
213
214 result
215 |> Enum.each(fn message ->
216 assert message["chat_id"] == chat.id |> to_string()
217 end)
218
219 assert length(result) == 3
220
221 # Trying to get the chat of a different user
222 result =
223 conn
224 |> assign(:user, other_user)
225 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
226
227 assert result |> json_response(404)
228 end
229 end
230
231 describe "POST /api/v1/pleroma/chats/by-account-id/:id" do
232 setup do: oauth_access(["write:chats"])
233
234 test "it creates or returns a chat", %{conn: conn} do
235 other_user = insert(:user)
236
237 result =
238 conn
239 |> post("/api/v1/pleroma/chats/by-account-id/#{other_user.id}")
240 |> json_response_and_validate_schema(200)
241
242 assert result["id"]
243 end
244 end
245
246 describe "GET /api/v1/pleroma/chats/:id" do
247 setup do: oauth_access(["read:chats"])
248
249 test "it returns a chat", %{conn: conn, user: user} do
250 other_user = insert(:user)
251
252 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
253
254 result =
255 conn
256 |> get("/api/v1/pleroma/chats/#{chat.id}")
257 |> json_response_and_validate_schema(200)
258
259 assert result["id"] == to_string(chat.id)
260 end
261 end
262
263 describe "GET /api/v1/pleroma/chats" do
264 setup do: oauth_access(["read:chats"])
265
266 test "it does not return chats with users you blocked", %{conn: conn, user: user} do
267 recipient = insert(:user)
268
269 {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
270
271 result =
272 conn
273 |> get("/api/v1/pleroma/chats")
274 |> json_response_and_validate_schema(200)
275
276 assert length(result) == 1
277
278 User.block(user, recipient)
279
280 result =
281 conn
282 |> get("/api/v1/pleroma/chats")
283 |> json_response_and_validate_schema(200)
284
285 assert length(result) == 0
286 end
287
288 test "it paginates", %{conn: conn, user: user} do
289 Enum.each(1..30, fn _ ->
290 recipient = insert(:user)
291 {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
292 end)
293
294 result =
295 conn
296 |> get("/api/v1/pleroma/chats")
297 |> json_response_and_validate_schema(200)
298
299 assert length(result) == 20
300
301 result =
302 conn
303 |> get("/api/v1/pleroma/chats?max_id=#{List.last(result)["id"]}")
304 |> json_response_and_validate_schema(200)
305
306 assert length(result) == 10
307 end
308
309 test "it return a list of chats the current user is participating in, in descending order of updates",
310 %{conn: conn, user: user} do
311 har = insert(:user)
312 jafnhar = insert(:user)
313 tridi = insert(:user)
314
315 {:ok, chat_1} = Chat.get_or_create(user.id, har.ap_id)
316 :timer.sleep(1000)
317 {:ok, _chat_2} = Chat.get_or_create(user.id, jafnhar.ap_id)
318 :timer.sleep(1000)
319 {:ok, chat_3} = Chat.get_or_create(user.id, tridi.ap_id)
320 :timer.sleep(1000)
321
322 # bump the second one
323 {:ok, chat_2} = Chat.bump_or_create(user.id, jafnhar.ap_id)
324
325 result =
326 conn
327 |> get("/api/v1/pleroma/chats")
328 |> json_response_and_validate_schema(200)
329
330 ids = Enum.map(result, & &1["id"])
331
332 assert ids == [
333 chat_2.id |> to_string(),
334 chat_3.id |> to_string(),
335 chat_1.id |> to_string()
336 ]
337 end
338 end
339 end