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