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" do
157 setup do: oauth_access(["read:statuses"])
158
159 test "it paginates", %{conn: conn, user: user} do
160 Enum.each(1..30, fn _ ->
161 recipient = insert(:user)
162 {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
163 end)
164
165 result =
166 conn
167 |> get("/api/v1/pleroma/chats")
168 |> json_response_and_validate_schema(200)
169
170 assert length(result) == 20
171
172 result =
173 conn
174 |> get("/api/v1/pleroma/chats?max_id=#{List.last(result)["id"]}")
175 |> json_response_and_validate_schema(200)
176
177 assert length(result) == 10
178 end
179
180 test "it return a list of chats the current user is participating in, in descending order of updates",
181 %{conn: conn, user: user} do
182 har = insert(:user)
183 jafnhar = insert(:user)
184 tridi = insert(:user)
185
186 {:ok, chat_1} = Chat.get_or_create(user.id, har.ap_id)
187 :timer.sleep(1000)
188 {:ok, _chat_2} = Chat.get_or_create(user.id, jafnhar.ap_id)
189 :timer.sleep(1000)
190 {:ok, chat_3} = Chat.get_or_create(user.id, tridi.ap_id)
191 :timer.sleep(1000)
192
193 # bump the second one
194 {:ok, chat_2} = Chat.bump_or_create(user.id, jafnhar.ap_id)
195
196 result =
197 conn
198 |> get("/api/v1/pleroma/chats")
199 |> json_response_and_validate_schema(200)
200
201 ids = Enum.map(result, & &1["id"])
202
203 assert ids == [
204 chat_2.id |> to_string(),
205 chat_3.id |> to_string(),
206 chat_1.id |> to_string()
207 ]
208 end
209 end
210 end