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