aboutsummaryrefslogtreecommitdiff
path: root/test/web/matrix_controller_test.exs
blob: ec24855e0d5aa9d3c1e82face0e17802071f53b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# 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

  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

  describe "setting read markers" do
    test "it sets all messages up to a point as read", %{
      conn: conn,
      cmr: cmr,
      chat: chat,
      user: user,
      other_user: other_user
    } do
      {:ok, chat_activity} = CommonAPI.post_chat_message(other_user, user, "morning weebs 2")
      chat_object = Object.normalize(chat_activity)

      conn
      |> post("/_matrix/client/r0/rooms/#{chat.id}/read_markers", %{"m.fully_read": cmr.id})

      cmr_two = MessageReference.for_chat_and_object(chat, chat_object)
      assert cmr_two.unread

      cmr = MessageReference.get_by_id(cmr.id)
      refute cmr.unread
    end
  end

  describe "sync" do
    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