Merge branch 'develop' of git.pleroma.social:pleroma/pleroma into remake-remodel-dms
[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.Object
9 alias Pleroma.User
10 alias Pleroma.Web.ActivityPub.ActivityPub
11 alias Pleroma.Web.CommonAPI
12
13 import Pleroma.Factory
14
15 describe "POST /api/v1/pleroma/chats/:id/read" do
16 setup do: oauth_access(["write:statuses"])
17
18 test "it marks all messages in a chat as read", %{conn: conn, user: user} do
19 other_user = insert(:user)
20
21 {:ok, chat} = Chat.bump_or_create(user.id, other_user.ap_id)
22
23 assert chat.unread == 1
24
25 result =
26 conn
27 |> post("/api/v1/pleroma/chats/#{chat.id}/read")
28 |> json_response_and_validate_schema(200)
29
30 assert result["unread"] == 0
31
32 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
33
34 assert chat.unread == 0
35 end
36 end
37
38 describe "POST /api/v1/pleroma/chats/:id/messages" do
39 setup do: oauth_access(["write:statuses"])
40
41 test "it posts a message to the chat", %{conn: conn, user: user} do
42 other_user = insert(:user)
43
44 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
45
46 result =
47 conn
48 |> put_req_header("content-type", "application/json")
49 |> post("/api/v1/pleroma/chats/#{chat.id}/messages", %{"content" => "Hallo!!"})
50 |> json_response_and_validate_schema(200)
51
52 assert result["content"] == "Hallo!!"
53 assert result["chat_id"] == chat.id |> to_string()
54 end
55
56 test "it fails if there is no content", %{conn: conn, user: user} do
57 other_user = insert(:user)
58
59 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
60
61 result =
62 conn
63 |> put_req_header("content-type", "application/json")
64 |> post("/api/v1/pleroma/chats/#{chat.id}/messages")
65 |> json_response_and_validate_schema(400)
66
67 assert result
68 end
69
70 test "it works with an attachment", %{conn: conn, user: user} do
71 file = %Plug.Upload{
72 content_type: "image/jpg",
73 path: Path.absname("test/fixtures/image.jpg"),
74 filename: "an_image.jpg"
75 }
76
77 {:ok, upload} = ActivityPub.upload(file, actor: user.ap_id)
78
79 other_user = insert(:user)
80
81 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
82
83 result =
84 conn
85 |> put_req_header("content-type", "application/json")
86 |> post("/api/v1/pleroma/chats/#{chat.id}/messages", %{
87 "media_id" => to_string(upload.id)
88 })
89 |> json_response_and_validate_schema(200)
90
91 assert result["attachment"]
92 end
93 end
94
95 describe "DELETE /api/v1/pleroma/chats/:id/messages/:message_id" do
96 setup do: oauth_access(["write:statuses"])
97
98 test "it deletes a message for the author of the message", %{conn: conn, user: user} do
99 recipient = insert(:user)
100
101 {:ok, message} =
102 CommonAPI.post_chat_message(user, recipient, "Hello darkness my old friend")
103
104 {:ok, other_message} = CommonAPI.post_chat_message(recipient, user, "nico nico ni")
105
106 object = Object.normalize(message, false)
107
108 chat = Chat.get(user.id, recipient.ap_id)
109
110 result =
111 conn
112 |> put_req_header("content-type", "application/json")
113 |> delete("/api/v1/pleroma/chats/#{chat.id}/messages/#{object.id}")
114 |> json_response_and_validate_schema(200)
115
116 assert result["id"] == to_string(object.id)
117
118 object = Object.normalize(other_message, false)
119
120 result =
121 conn
122 |> put_req_header("content-type", "application/json")
123 |> delete("/api/v1/pleroma/chats/#{chat.id}/messages/#{object.id}")
124 |> json_response(400)
125
126 assert result == %{"error" => "could_not_delete"}
127 end
128 end
129
130 describe "GET /api/v1/pleroma/chats/:id/messages" do
131 setup do: oauth_access(["read:statuses"])
132
133 test "it paginates", %{conn: conn, user: user} do
134 recipient = insert(:user)
135
136 Enum.each(1..30, fn _ ->
137 {:ok, _} = CommonAPI.post_chat_message(user, recipient, "hey")
138 end)
139
140 chat = Chat.get(user.id, recipient.ap_id)
141
142 result =
143 conn
144 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
145 |> json_response_and_validate_schema(200)
146
147 assert length(result) == 20
148
149 result =
150 conn
151 |> get("/api/v1/pleroma/chats/#{chat.id}/messages?max_id=#{List.last(result)["id"]}")
152 |> json_response_and_validate_schema(200)
153
154 assert length(result) == 10
155 end
156
157 test "it returns the messages for a given chat", %{conn: conn, user: user} do
158 other_user = insert(:user)
159 third_user = insert(:user)
160
161 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "hey")
162 {:ok, _} = CommonAPI.post_chat_message(user, third_user, "hey")
163 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "how are you?")
164 {:ok, _} = CommonAPI.post_chat_message(other_user, user, "fine, how about you?")
165
166 chat = Chat.get(user.id, other_user.ap_id)
167
168 result =
169 conn
170 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
171 |> json_response_and_validate_schema(200)
172
173 result
174 |> Enum.each(fn message ->
175 assert message["chat_id"] == chat.id |> to_string()
176 end)
177
178 assert length(result) == 3
179
180 # Trying to get the chat of a different user
181 result =
182 conn
183 |> assign(:user, other_user)
184 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
185
186 assert result |> json_response(404)
187 end
188 end
189
190 describe "POST /api/v1/pleroma/chats/by-account-id/:id" do
191 setup do: oauth_access(["write:statuses"])
192
193 test "it creates or returns a chat", %{conn: conn} do
194 other_user = insert(:user)
195
196 result =
197 conn
198 |> post("/api/v1/pleroma/chats/by-account-id/#{other_user.id}")
199 |> json_response_and_validate_schema(200)
200
201 assert result["id"]
202 end
203 end
204
205 describe "GET /api/v1/pleroma/chats/:id" do
206 setup do: oauth_access(["read:statuses"])
207
208 test "it returns a chat", %{conn: conn, user: user} do
209 other_user = insert(:user)
210
211 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
212
213 result =
214 conn
215 |> get("/api/v1/pleroma/chats/#{chat.id}")
216 |> json_response_and_validate_schema(200)
217
218 assert result["id"] == to_string(chat.id)
219 end
220 end
221
222 describe "GET /api/v1/pleroma/chats" do
223 setup do: oauth_access(["read:statuses"])
224
225 test "it does not return chats with users you blocked", %{conn: conn, user: user} do
226 recipient = insert(:user)
227
228 {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
229
230 result =
231 conn
232 |> get("/api/v1/pleroma/chats")
233 |> json_response_and_validate_schema(200)
234
235 assert length(result) == 1
236
237 User.block(user, recipient)
238
239 result =
240 conn
241 |> get("/api/v1/pleroma/chats")
242 |> json_response_and_validate_schema(200)
243
244 assert length(result) == 0
245 end
246
247 test "it paginates", %{conn: conn, user: user} do
248 Enum.each(1..30, fn _ ->
249 recipient = insert(:user)
250 {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
251 end)
252
253 result =
254 conn
255 |> get("/api/v1/pleroma/chats")
256 |> json_response_and_validate_schema(200)
257
258 assert length(result) == 20
259
260 result =
261 conn
262 |> get("/api/v1/pleroma/chats?max_id=#{List.last(result)["id"]}")
263 |> json_response_and_validate_schema(200)
264
265 assert length(result) == 10
266 end
267
268 test "it return a list of chats the current user is participating in, in descending order of updates",
269 %{conn: conn, user: user} do
270 har = insert(:user)
271 jafnhar = insert(:user)
272 tridi = insert(:user)
273
274 {:ok, chat_1} = Chat.get_or_create(user.id, har.ap_id)
275 :timer.sleep(1000)
276 {:ok, _chat_2} = Chat.get_or_create(user.id, jafnhar.ap_id)
277 :timer.sleep(1000)
278 {:ok, chat_3} = Chat.get_or_create(user.id, tridi.ap_id)
279 :timer.sleep(1000)
280
281 # bump the second one
282 {:ok, chat_2} = Chat.bump_or_create(user.id, jafnhar.ap_id)
283
284 result =
285 conn
286 |> get("/api/v1/pleroma/chats")
287 |> json_response_and_validate_schema(200)
288
289 ids = Enum.map(result, & &1["id"])
290
291 assert ids == [
292 chat_2.id |> to_string(),
293 chat_3.id |> to_string(),
294 chat_1.id |> to_string()
295 ]
296 end
297 end
298 end