aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlain <lain@soykaf.club>2020-08-22 20:45:16 +0200
committerlain <lain@soykaf.club>2020-08-22 20:45:16 +0200
commitd49fdb315f4ea1a03e22063b14ba804104b8527b (patch)
tree61e43092f714fa4d57fb6e26bc9ea45c830c8a41
parentb5ea5fe851e404c3b1fca90eb4a57e6a34ff205a (diff)
downloadpleroma-d49fdb315f4ea1a03e22063b14ba804104b8527b.tar.gz
MatrixController: Add basic tests, respect the 'since' parameter.
-rw-r--r--lib/pleroma/web/matrix_controller.ex24
-rw-r--r--test/web/matrix_controller_test.exs105
2 files changed, 125 insertions, 4 deletions
diff --git a/lib/pleroma/web/matrix_controller.ex b/lib/pleroma/web/matrix_controller.ex
index d02d5859b..00f27924d 100644
--- a/lib/pleroma/web/matrix_controller.ex
+++ b/lib/pleroma/web/matrix_controller.ex
@@ -161,7 +161,7 @@ defmodule Pleroma.Web.MatrixController do
end
def sync(%{assigns: %{user: user}} = conn, params) do
- with {:ok, timeout} <- Ecto.Type.cast(:integer, params["timeout"]) do
+ with {:ok, timeout} when not is_nil(timeout) <- Ecto.Type.cast(:integer, params["timeout"]) do
:timer.sleep(timeout)
end
@@ -183,9 +183,21 @@ defmodule Pleroma.Web.MatrixController do
[user, recipient]
|> membership_events_from_list(chat)
- messages =
+ q =
chat
|> MessageReference.for_chat_query()
+
+ q =
+ if since = params["since"] do
+ from(mr in q,
+ where: mr.id > ^since
+ )
+ else
+ q
+ end
+
+ messages =
+ q
|> Repo.all()
|> Enum.map(fn message ->
chat_data = message.object.data
@@ -273,12 +285,16 @@ defmodule Pleroma.Web.MatrixController do
}
}
- Map.merge(acc, room)
+ if length(messages) > 0 do
+ Map.merge(acc, room)
+ else
+ acc
+ end
end)
most_recent_cmr_id =
Enum.reduce(chats, nil, fn {_k, chat}, acc ->
- id = List.first(chat.timeline.events).event_id
+ id = List.last(chat.timeline.events).event_id
if !acc || (acc && acc < id) do
id
diff --git a/test/web/matrix_controller_test.exs b/test/web/matrix_controller_test.exs
new file mode 100644
index 000000000..ec35998f9
--- /dev/null
+++ b/test/web/matrix_controller_test.exs
@@ -0,0 +1,105 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Web.MatrixControllerTest do
+ use Pleroma.Web.ConnCase
+
+ alias Pleroma.Web.CommonAPI
+ alias Pleroma.Chat
+ alias Pleroma.Chat.MessageReference
+ alias Pleroma.Object
+ import Pleroma.Factory
+
+ describe "sync" do
+ setup do
+ %{user: user, conn: conn} = oauth_access(["read"])
+ other_user = insert(:user)
+ third_user = insert(:user)
+
+ {:ok, chat_activity} = CommonAPI.post_chat_message(user, other_user, "Hey")
+ {:ok, chat_activity_two} = CommonAPI.post_chat_message(third_user, user, "Henlo")
+
+ chat = Chat.get(user.id, other_user.ap_id)
+ chat_two = Chat.get(user.id, third_user.ap_id)
+
+ chat_object = Object.normalize(chat_activity)
+ cmr = MessageReference.for_chat_and_object(chat, chat_object)
+
+ chat_object_two = Object.normalize(chat_activity_two)
+ cmr_two = MessageReference.for_chat_and_object(chat_two, chat_object_two)
+
+ %{
+ user: user,
+ other_user: other_user,
+ third_user: third_user,
+ conn: conn,
+ chat_activity: chat_activity,
+ chat_activity_two: chat_activity_two,
+ chat: chat,
+ chat_two: chat_two,
+ cmr: cmr,
+ cmr_two: cmr_two
+ }
+ end
+
+ test "without options, it returns all chat messages the user has", %{
+ conn: conn,
+ chat: chat,
+ chat_two: chat_two,
+ cmr: cmr,
+ cmr_two: cmr_two
+ } do
+ %{
+ "next_batch" => next_batch,
+ "rooms" => %{
+ "join" => joined_rooms
+ }
+ } =
+ conn
+ |> get("_matrix/client/r0/sync")
+ |> json_response(200)
+
+ assert chat_room = joined_rooms[chat.id]
+ assert chat_room_two = joined_rooms[chat_two.id]
+
+ assert [message] = chat_room["timeline"]["events"]
+ assert [message_two] = chat_room_two["timeline"]["events"]
+
+ assert message["content"]["formatted_body"] == "Hey"
+ assert message_two["content"]["formatted_body"] == "Henlo"
+
+ assert message["event_id"] == cmr.id
+ assert message_two["event_id"] == cmr_two.id
+
+ # Next batch contains the largest ChatMessageReference id
+
+ assert next_batch == cmr_two.id
+ end
+
+ test "given a `since` option, it only returns chat messages after that point", %{
+ conn: conn,
+ cmr_two: cmr_two,
+ chat: chat,
+ chat_two: chat_two,
+ user: user,
+ other_user: other_user
+ } do
+ {:ok, _} = CommonAPI.post_chat_message(user, other_user, "morning weebs")
+
+ %{
+ "rooms" => %{
+ "join" => joined_rooms
+ }
+ } =
+ conn
+ |> get("_matrix/client/r0/sync?since=#{cmr_two.id}")
+ |> json_response(200)
+
+ refute joined_rooms[chat_two.id]
+ assert chat_room = joined_rooms[chat.id]
+ assert [message] = chat_room["timeline"]["events"]
+ assert message["content"]["body"] == "morning weebs"
+ end
+ end
+end