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
10 alias Pleroma.Web.ActivityPub.ActivityPub
11 alias Pleroma.Web.CommonAPI
13 import Pleroma.Factory
15 describe "POST /api/v1/pleroma/chats/:id/read" do
16 setup do: oauth_access(["write:statuses"])
18 test "it marks all messages in a chat as read", %{conn: conn, user: user} do
19 other_user = insert(:user)
21 {:ok, chat} = Chat.bump_or_create(user.id, other_user.ap_id)
23 assert chat.unread == 1
27 |> post("/api/v1/pleroma/chats/#{chat.id}/read")
28 |> json_response_and_validate_schema(200)
30 assert result["unread"] == 0
32 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
34 assert chat.unread == 0
38 describe "POST /api/v1/pleroma/chats/:id/messages" do
39 setup do: oauth_access(["write:statuses"])
41 test "it posts a message to the chat", %{conn: conn, user: user} do
42 other_user = insert(:user)
44 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
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)
52 assert result["content"] == "Hallo!!"
53 assert result["chat_id"] == chat.id |> to_string()
56 test "it works with an attachment", %{conn: conn, user: user} do
58 content_type: "image/jpg",
59 path: Path.absname("test/fixtures/image.jpg"),
60 filename: "an_image.jpg"
63 {:ok, upload} = ActivityPub.upload(file, actor: user.ap_id)
65 other_user = insert(:user)
67 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
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)
76 |> json_response_and_validate_schema(200)
78 assert result["content"] == "Hallo!!"
79 assert result["chat_id"] == chat.id |> to_string()
83 describe "DELETE /api/v1/pleroma/chats/:id/messages/:message_id" do
84 setup do: oauth_access(["write:statuses"])
86 test "it deletes a message for the author of the message", %{conn: conn, user: user} do
87 recipient = insert(:user)
90 CommonAPI.post_chat_message(user, recipient, "Hello darkness my old friend")
92 {:ok, other_message} = CommonAPI.post_chat_message(recipient, user, "nico nico ni")
94 object = Object.normalize(message, false)
96 chat = Chat.get(user.id, recipient.ap_id)
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)
104 assert result["id"] == to_string(object.id)
106 object = Object.normalize(other_message, false)
110 |> put_req_header("content-type", "application/json")
111 |> delete("/api/v1/pleroma/chats/#{chat.id}/messages/#{object.id}")
112 |> json_response(400)
114 assert result == %{"error" => "could_not_delete"}
118 describe "GET /api/v1/pleroma/chats/:id/messages" do
119 setup do: oauth_access(["read:statuses"])
121 test "it paginates", %{conn: conn, user: user} do
122 recipient = insert(:user)
124 Enum.each(1..30, fn _ ->
125 {:ok, _} = CommonAPI.post_chat_message(user, recipient, "hey")
128 chat = Chat.get(user.id, recipient.ap_id)
132 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
133 |> json_response_and_validate_schema(200)
135 assert length(result) == 20
139 |> get("/api/v1/pleroma/chats/#{chat.id}/messages?max_id=#{List.last(result)["id"]}")
140 |> json_response_and_validate_schema(200)
142 assert length(result) == 10
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)
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?")
154 chat = Chat.get(user.id, other_user.ap_id)
158 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
159 |> json_response_and_validate_schema(200)
162 |> Enum.each(fn message ->
163 assert message["chat_id"] == chat.id |> to_string()
166 assert length(result) == 3
168 # Trying to get the chat of a different user
171 |> assign(:user, other_user)
172 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
174 assert result |> json_response(404)
178 describe "POST /api/v1/pleroma/chats/by-account-id/:id" do
179 setup do: oauth_access(["write:statuses"])
181 test "it creates or returns a chat", %{conn: conn} do
182 other_user = insert(:user)
186 |> post("/api/v1/pleroma/chats/by-account-id/#{other_user.id}")
187 |> json_response_and_validate_schema(200)
193 describe "GET /api/v1/pleroma/chats/:id" do
194 setup do: oauth_access(["read:statuses"])
196 test "it returns a chat", %{conn: conn, user: user} do
197 other_user = insert(:user)
199 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
203 |> get("/api/v1/pleroma/chats/#{chat.id}")
204 |> json_response_and_validate_schema(200)
206 assert result["id"] == to_string(chat.id)
210 describe "GET /api/v1/pleroma/chats" do
211 setup do: oauth_access(["read:statuses"])
213 test "it does not return chats with users you blocked", %{conn: conn, user: user} do
214 recipient = insert(:user)
216 {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
220 |> get("/api/v1/pleroma/chats")
221 |> json_response_and_validate_schema(200)
223 assert length(result) == 1
225 User.block(user, recipient)
229 |> get("/api/v1/pleroma/chats")
230 |> json_response_and_validate_schema(200)
232 assert length(result) == 0
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)
243 |> get("/api/v1/pleroma/chats")
244 |> json_response_and_validate_schema(200)
246 assert length(result) == 20
250 |> get("/api/v1/pleroma/chats?max_id=#{List.last(result)["id"]}")
251 |> json_response_and_validate_schema(200)
253 assert length(result) == 10
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
259 jafnhar = insert(:user)
260 tridi = insert(:user)
262 {:ok, chat_1} = Chat.get_or_create(user.id, har.ap_id)
264 {:ok, _chat_2} = Chat.get_or_create(user.id, jafnhar.ap_id)
266 {:ok, chat_3} = Chat.get_or_create(user.id, tridi.ap_id)
269 # bump the second one
270 {:ok, chat_2} = Chat.bump_or_create(user.id, jafnhar.ap_id)
274 |> get("/api/v1/pleroma/chats")
275 |> json_response_and_validate_schema(200)
277 ids = Enum.map(result, & &1["id"])
280 chat_2.id |> to_string(),
281 chat_3.id |> to_string(),
282 chat_1.id |> to_string()