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