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
9 alias Pleroma.Web.ActivityPub.ActivityPub
10 alias Pleroma.Web.CommonAPI
12 import Pleroma.Factory
14 describe "POST /api/v1/pleroma/chats/:id/read" do
15 setup do: oauth_access(["write:statuses"])
17 test "it marks all messages in a chat as read", %{conn: conn, user: user} do
18 other_user = insert(:user)
20 {:ok, chat} = Chat.bump_or_create(user.id, other_user.ap_id)
22 assert chat.unread == 1
26 |> post("/api/v1/pleroma/chats/#{chat.id}/read")
27 |> json_response_and_validate_schema(200)
29 assert result["unread"] == 0
31 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
33 assert chat.unread == 0
37 describe "POST /api/v1/pleroma/chats/:id/messages" do
38 setup do: oauth_access(["write:statuses"])
40 test "it posts a message to the chat", %{conn: conn, user: user} do
41 other_user = insert(:user)
43 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
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)
51 assert result["content"] == "Hallo!!"
52 assert result["chat_id"] == chat.id |> to_string()
55 test "it works with an attachment", %{conn: conn, user: user} do
57 content_type: "image/jpg",
58 path: Path.absname("test/fixtures/image.jpg"),
59 filename: "an_image.jpg"
62 {:ok, upload} = ActivityPub.upload(file, actor: user.ap_id)
64 other_user = insert(:user)
66 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
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)
75 |> json_response_and_validate_schema(200)
77 assert result["content"] == "Hallo!!"
78 assert result["chat_id"] == chat.id |> to_string()
82 describe "DELETE /api/v1/pleroma/chats/:id/messages/:message_id" do
83 setup do: oauth_access(["write:statuses"])
85 test "it deletes a message for the author of the message", %{conn: conn, user: user} do
86 recipient = insert(:user)
89 CommonAPI.post_chat_message(user, recipient, "Hello darkness my old friend")
91 object = Object.normalize(message, false)
93 chat = Chat.get(user.id, recipient.ap_id)
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)
101 assert result["id"] == to_string(object.id)
105 describe "GET /api/v1/pleroma/chats/:id/messages" do
106 setup do: oauth_access(["read:statuses"])
108 test "it paginates", %{conn: conn, user: user} do
109 recipient = insert(:user)
111 Enum.each(1..30, fn _ ->
112 {:ok, _} = CommonAPI.post_chat_message(user, recipient, "hey")
115 chat = Chat.get(user.id, recipient.ap_id)
119 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
120 |> json_response_and_validate_schema(200)
122 assert length(result) == 20
126 |> get("/api/v1/pleroma/chats/#{chat.id}/messages?max_id=#{List.last(result)["id"]}")
127 |> json_response_and_validate_schema(200)
129 assert length(result) == 10
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)
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?")
141 chat = Chat.get(user.id, other_user.ap_id)
145 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
146 |> json_response_and_validate_schema(200)
149 |> Enum.each(fn message ->
150 assert message["chat_id"] == chat.id |> to_string()
153 assert length(result) == 3
155 # Trying to get the chat of a different user
158 |> assign(:user, other_user)
159 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
161 assert result |> json_response(404)
165 describe "POST /api/v1/pleroma/chats/by-account-id/:id" do
166 setup do: oauth_access(["write:statuses"])
168 test "it creates or returns a chat", %{conn: conn} do
169 other_user = insert(:user)
173 |> post("/api/v1/pleroma/chats/by-account-id/#{other_user.id}")
174 |> json_response_and_validate_schema(200)
180 describe "GET /api/v1/pleroma/chats/:id" do
181 setup do: oauth_access(["read:statuses"])
183 test "it returns a chat", %{conn: conn, user: user} do
184 other_user = insert(:user)
186 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
190 |> get("/api/v1/pleroma/chats/#{chat.id}")
191 |> json_response_and_validate_schema(200)
193 assert result["id"] == to_string(chat.id)
197 describe "GET /api/v1/pleroma/chats" do
198 setup do: oauth_access(["read:statuses"])
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)
208 |> get("/api/v1/pleroma/chats")
209 |> json_response_and_validate_schema(200)
211 assert length(result) == 20
215 |> get("/api/v1/pleroma/chats?max_id=#{List.last(result)["id"]}")
216 |> json_response_and_validate_schema(200)
218 assert length(result) == 10
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
224 jafnhar = insert(:user)
225 tridi = insert(:user)
227 {:ok, chat_1} = Chat.get_or_create(user.id, har.ap_id)
229 {:ok, _chat_2} = Chat.get_or_create(user.id, jafnhar.ap_id)
231 {:ok, chat_3} = Chat.get_or_create(user.id, tridi.ap_id)
234 # bump the second one
235 {:ok, chat_2} = Chat.bump_or_create(user.id, jafnhar.ap_id)
239 |> get("/api/v1/pleroma/chats")
240 |> json_response_and_validate_schema(200)
242 ids = Enum.map(result, & &1["id"])
245 chat_2.id |> to_string(),
246 chat_3.id |> to_string(),
247 chat_1.id |> to_string()