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