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