2014-08-20 20:15:47 +02:00
|
|
|
#
|
2023-11-21 21:29:58 +01:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
#
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
#
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
2014-08-20 20:15:47 +02:00
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
|
|
import json
|
2023-01-26 11:15:50 +01:00
|
|
|
from typing import Dict, List, Set
|
2023-08-25 01:38:46 +02:00
|
|
|
from unittest.mock import ANY, AsyncMock, Mock, call
|
2018-07-09 08:09:20 +02:00
|
|
|
|
2023-07-18 10:49:21 +02:00
|
|
|
from netaddr import IPSet
|
|
|
|
|
2022-03-21 14:43:16 +01:00
|
|
|
from twisted.test.proto_helpers import MemoryReactor
|
2020-12-02 16:26:25 +01:00
|
|
|
from twisted.web.resource import Resource
|
2014-08-20 20:15:47 +02:00
|
|
|
|
2022-05-27 13:14:36 +02:00
|
|
|
from synapse.api.constants import EduTypes
|
2014-12-11 19:11:43 +01:00
|
|
|
from synapse.api.errors import AuthError
|
2020-12-02 16:26:25 +01:00
|
|
|
from synapse.federation.transport.server import TransportLayerServer
|
2023-01-26 11:15:50 +01:00
|
|
|
from synapse.handlers.typing import TypingWriterHandler
|
2023-07-18 10:49:21 +02:00
|
|
|
from synapse.http.federation.matrix_federation_agent import MatrixFederationAgent
|
2022-03-21 14:43:16 +01:00
|
|
|
from synapse.server import HomeServer
|
2023-10-04 17:28:40 +02:00
|
|
|
from synapse.types import JsonDict, Requester, StreamKeyType, UserID, create_requester
|
2022-03-21 14:43:16 +01:00
|
|
|
from synapse.util import Clock
|
2014-12-10 00:53:07 +01:00
|
|
|
|
2018-07-09 08:09:20 +02:00
|
|
|
from tests import unittest
|
2023-01-26 11:15:50 +01:00
|
|
|
from tests.server import ThreadedMemoryReactorClock
|
2019-11-27 22:54:07 +01:00
|
|
|
from tests.unittest import override_config
|
2018-07-09 08:09:20 +02:00
|
|
|
|
2019-03-04 11:05:39 +01:00
|
|
|
# Some local users to test with
|
|
|
|
U_APPLE = UserID.from_string("@apple:test")
|
|
|
|
U_BANANA = UserID.from_string("@banana:test")
|
|
|
|
|
|
|
|
# Remote user
|
|
|
|
U_ONION = UserID.from_string("@onion:farm")
|
|
|
|
|
|
|
|
# Test room id
|
|
|
|
ROOM_ID = "a-room"
|
2018-07-09 08:09:20 +02:00
|
|
|
|
2021-07-06 15:31:13 +02:00
|
|
|
# Room we're not in
|
|
|
|
OTHER_ROOM_ID = "another-room"
|
|
|
|
|
2014-08-20 20:15:47 +02:00
|
|
|
|
2022-03-21 14:43:16 +01:00
|
|
|
def _expect_edu_transaction(
|
|
|
|
edu_type: str, content: JsonDict, origin: str = "test"
|
|
|
|
) -> JsonDict:
|
2014-08-20 20:15:47 +02:00
|
|
|
return {
|
|
|
|
"origin": origin,
|
2014-10-17 18:12:25 +02:00
|
|
|
"origin_server_ts": 1000000,
|
2014-08-20 20:15:47 +02:00
|
|
|
"pdus": [],
|
|
|
|
"edus": [{"edu_type": edu_type, "content": content}],
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-03-21 14:43:16 +01:00
|
|
|
def _make_edu_transaction_json(edu_type: str, content: JsonDict) -> bytes:
|
2018-09-06 18:58:18 +02:00
|
|
|
return json.dumps(_expect_edu_transaction(edu_type, content)).encode("utf8")
|
2014-08-20 20:15:47 +02:00
|
|
|
|
|
|
|
|
2019-03-04 11:05:39 +01:00
|
|
|
class TypingNotificationsTestCase(unittest.HomeserverTestCase):
|
2023-01-26 11:15:50 +01:00
|
|
|
def make_homeserver(
|
|
|
|
self,
|
|
|
|
reactor: ThreadedMemoryReactorClock,
|
|
|
|
clock: Clock,
|
|
|
|
) -> HomeServer:
|
2019-03-04 11:05:39 +01:00
|
|
|
# we mock out the keyring so as to skip the authentication check on the
|
|
|
|
# federation API call.
|
|
|
|
mock_keyring = Mock(spec=["verify_json_for_server"])
|
2023-08-25 01:38:46 +02:00
|
|
|
mock_keyring.verify_json_for_server = AsyncMock(return_value=True)
|
2014-08-20 20:15:47 +02:00
|
|
|
|
2019-03-04 11:05:39 +01:00
|
|
|
# we mock out the federation client too
|
2023-08-25 01:38:46 +02:00
|
|
|
self.mock_federation_client = AsyncMock(spec=["put_json"])
|
|
|
|
self.mock_federation_client.put_json.return_value = (200, "OK")
|
2023-07-18 10:49:21 +02:00
|
|
|
self.mock_federation_client.agent = MatrixFederationAgent(
|
|
|
|
reactor,
|
|
|
|
tls_client_options_factory=None,
|
|
|
|
user_agent=b"SynapseInTrialTest/0.0.0",
|
|
|
|
ip_allowlist=None,
|
|
|
|
ip_blocklist=IPSet(),
|
|
|
|
)
|
2014-12-10 21:48:12 +01:00
|
|
|
|
2020-01-30 18:06:38 +01:00
|
|
|
# the tests assume that we are starting at unix time 1000
|
|
|
|
reactor.pump((1000,))
|
|
|
|
|
2023-01-26 11:15:50 +01:00
|
|
|
self.mock_hs_notifier = Mock()
|
2019-03-04 11:05:39 +01:00
|
|
|
hs = self.setup_test_homeserver(
|
2023-01-26 11:15:50 +01:00
|
|
|
notifier=self.mock_hs_notifier,
|
2023-02-14 20:03:35 +01:00
|
|
|
federation_http_client=self.mock_federation_client,
|
2020-05-22 15:21:54 +02:00
|
|
|
keyring=mock_keyring,
|
|
|
|
replication_streams={},
|
2015-02-11 12:37:30 +01:00
|
|
|
)
|
2014-08-20 20:15:47 +02:00
|
|
|
|
2019-03-04 11:05:39 +01:00
|
|
|
return hs
|
|
|
|
|
2020-12-02 16:26:25 +01:00
|
|
|
def create_resource_dict(self) -> Dict[str, Resource]:
|
|
|
|
d = super().create_resource_dict()
|
|
|
|
d["/_matrix/federation"] = TransportLayerServer(self.hs)
|
|
|
|
return d
|
|
|
|
|
2022-03-21 14:43:16 +01:00
|
|
|
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
|
2023-01-26 11:15:50 +01:00
|
|
|
self.on_new_event = self.mock_hs_notifier.on_new_event
|
2019-03-04 11:05:39 +01:00
|
|
|
|
2023-01-26 11:15:50 +01:00
|
|
|
# hs.get_typing_handler will return a TypingWriterHandler when calling it
|
|
|
|
# from the main process, and a FollowerTypingHandler on workers.
|
|
|
|
# We rely on methods only available on the former, so assert we have the
|
|
|
|
# correct type here. We have to assign self.handler after the assert,
|
|
|
|
# otherwise mypy will treat it as a FollowerTypingHandler
|
|
|
|
handler = hs.get_typing_handler()
|
|
|
|
assert isinstance(handler, TypingWriterHandler)
|
|
|
|
self.handler = handler
|
2014-08-20 20:15:47 +02:00
|
|
|
|
2021-09-21 19:34:26 +02:00
|
|
|
self.event_source = hs.get_event_sources().sources.typing
|
2014-08-20 20:15:47 +02:00
|
|
|
|
2022-02-23 12:04:02 +01:00
|
|
|
self.datastore = hs.get_datastores().main
|
2023-01-26 11:15:50 +01:00
|
|
|
|
2023-08-29 16:38:56 +02:00
|
|
|
self.datastore.get_device_updates_by_remote = AsyncMock( # type: ignore[method-assign]
|
2023-08-25 01:38:46 +02:00
|
|
|
return_value=(0, [])
|
2020-02-07 14:56:38 +01:00
|
|
|
)
|
2017-01-25 15:27:27 +01:00
|
|
|
|
2023-08-29 16:38:56 +02:00
|
|
|
self.datastore.get_destination_last_successful_stream_ordering = AsyncMock( # type: ignore[method-assign]
|
2023-08-25 01:38:46 +02:00
|
|
|
return_value=None
|
2020-09-15 10:07:19 +02:00
|
|
|
)
|
|
|
|
|
2023-08-29 16:38:56 +02:00
|
|
|
self.datastore.get_received_txn_response = AsyncMock( # type: ignore[method-assign]
|
2023-08-25 01:38:46 +02:00
|
|
|
return_value=None
|
2023-01-26 11:15:50 +01:00
|
|
|
)
|
2014-08-20 20:15:47 +02:00
|
|
|
|
2023-01-26 11:15:50 +01:00
|
|
|
self.room_members: List[UserID] = []
|
2014-08-20 20:15:47 +02:00
|
|
|
|
2022-08-22 15:17:59 +02:00
|
|
|
async def check_user_in_room(room_id: str, requester: Requester) -> None:
|
|
|
|
if requester.user.to_string() not in [
|
|
|
|
u.to_string() for u in self.room_members
|
|
|
|
]:
|
2014-12-11 19:11:43 +01:00
|
|
|
raise AuthError(401, "User is not in the room")
|
2020-08-06 14:30:06 +02:00
|
|
|
return None
|
2019-05-10 07:12:11 +02:00
|
|
|
|
2023-08-29 16:38:56 +02:00
|
|
|
hs.get_auth().check_user_in_room = Mock( # type: ignore[method-assign]
|
2023-01-26 11:15:50 +01:00
|
|
|
side_effect=check_user_in_room
|
|
|
|
)
|
2014-12-11 19:11:43 +01:00
|
|
|
|
2022-03-21 14:43:16 +01:00
|
|
|
async def check_host_in_room(room_id: str, server_name: str) -> bool:
|
2021-07-06 15:31:13 +02:00
|
|
|
return room_id == ROOM_ID
|
|
|
|
|
2023-08-29 16:38:56 +02:00
|
|
|
hs.get_event_auth_handler().is_host_in_room = Mock( # type: ignore[method-assign]
|
2023-01-26 11:15:50 +01:00
|
|
|
side_effect=check_host_in_room
|
|
|
|
)
|
2021-07-06 15:31:13 +02:00
|
|
|
|
2023-01-26 11:15:50 +01:00
|
|
|
async def get_current_hosts_in_room(room_id: str) -> Set[str]:
|
2020-02-21 13:15:07 +01:00
|
|
|
return {member.domain for member in self.room_members}
|
2018-08-10 15:54:09 +02:00
|
|
|
|
2023-08-29 16:38:56 +02:00
|
|
|
hs.get_storage_controllers().state.get_current_hosts_in_room = Mock( # type: ignore[method-assign]
|
2023-01-26 11:15:50 +01:00
|
|
|
side_effect=get_current_hosts_in_room
|
2022-06-06 17:46:11 +02:00
|
|
|
)
|
2016-05-16 20:48:07 +02:00
|
|
|
|
2023-08-29 16:38:56 +02:00
|
|
|
hs.get_storage_controllers().state.get_current_hosts_in_room_or_partial_state_approximation = Mock( # type: ignore[method-assign]
|
2023-01-26 11:15:50 +01:00
|
|
|
side_effect=get_current_hosts_in_room
|
2022-09-26 17:33:32 +02:00
|
|
|
)
|
|
|
|
|
2023-01-26 11:15:50 +01:00
|
|
|
async def get_users_in_room(room_id: str) -> Set[str]:
|
2020-10-13 14:20:32 +02:00
|
|
|
return {str(u) for u in self.room_members}
|
2019-05-10 07:12:11 +02:00
|
|
|
|
2023-01-26 11:15:50 +01:00
|
|
|
self.datastore.get_users_in_room = Mock(side_effect=get_users_in_room)
|
2016-08-26 16:06:14 +02:00
|
|
|
|
2023-08-29 16:38:56 +02:00
|
|
|
self.datastore.get_user_directory_stream_pos = AsyncMock( # type: ignore[method-assign]
|
2023-08-25 01:38:46 +02:00
|
|
|
# we deliberately return a non-None stream pos to avoid
|
|
|
|
# doing an initial_sync
|
|
|
|
return_value=1
|
2017-12-04 18:10:03 +01:00
|
|
|
)
|
|
|
|
|
2023-10-16 13:35:22 +02:00
|
|
|
self.datastore.get_partial_current_state_deltas = Mock(return_value=(0, [])) # type: ignore[method-assign]
|
2017-12-04 18:10:03 +01:00
|
|
|
|
2023-08-29 16:38:56 +02:00
|
|
|
self.datastore.get_to_device_stream_token = Mock( # type: ignore[method-assign]
|
2023-08-25 01:38:46 +02:00
|
|
|
return_value=0
|
2023-01-26 11:15:50 +01:00
|
|
|
)
|
2023-08-29 16:38:56 +02:00
|
|
|
self.datastore.get_new_device_msgs_for_remote = AsyncMock( # type: ignore[method-assign]
|
2023-08-25 01:38:46 +02:00
|
|
|
return_value=([], 0)
|
2020-02-07 14:56:38 +01:00
|
|
|
)
|
2023-08-29 16:38:56 +02:00
|
|
|
self.datastore.delete_device_msgs_for_remote = AsyncMock( # type: ignore[method-assign]
|
2023-08-25 01:38:46 +02:00
|
|
|
return_value=None
|
2020-08-27 23:24:46 +02:00
|
|
|
)
|
2023-08-29 16:38:56 +02:00
|
|
|
self.datastore.set_received_txn_response = AsyncMock( # type: ignore[method-assign]
|
2023-08-25 01:38:46 +02:00
|
|
|
return_value=None
|
2019-10-29 15:05:32 +01:00
|
|
|
)
|
2016-09-06 19:30:03 +02:00
|
|
|
|
2022-03-21 14:43:16 +01:00
|
|
|
def test_started_typing_local(self) -> None:
|
2019-03-04 11:05:39 +01:00
|
|
|
self.room_members = [U_APPLE, U_BANANA]
|
2014-08-20 20:15:47 +02:00
|
|
|
|
2022-02-28 13:12:29 +01:00
|
|
|
self.assertEqual(self.event_source.get_current_key(), 0)
|
2014-12-10 22:01:49 +01:00
|
|
|
|
2020-06-17 16:37:59 +02:00
|
|
|
self.get_success(
|
2019-05-10 07:12:11 +02:00
|
|
|
self.handler.started_typing(
|
2020-08-26 18:05:36 +02:00
|
|
|
target_user=U_APPLE,
|
|
|
|
requester=create_requester(U_APPLE),
|
|
|
|
room_id=ROOM_ID,
|
|
|
|
timeout=20000,
|
2019-05-10 07:12:11 +02:00
|
|
|
)
|
2014-08-20 20:15:47 +02:00
|
|
|
)
|
|
|
|
|
2023-10-04 17:28:40 +02:00
|
|
|
self.on_new_event.assert_has_calls(
|
|
|
|
[call(StreamKeyType.TYPING, 1, rooms=[ROOM_ID])]
|
|
|
|
)
|
2019-05-10 07:12:11 +02:00
|
|
|
|
2022-02-28 13:12:29 +01:00
|
|
|
self.assertEqual(self.event_source.get_current_key(), 1)
|
2019-12-06 11:14:59 +01:00
|
|
|
events = self.get_success(
|
2021-09-21 19:34:26 +02:00
|
|
|
self.event_source.get_new_events(
|
2023-01-26 11:15:50 +01:00
|
|
|
user=U_APPLE, from_key=0, limit=0, room_ids=[ROOM_ID], is_guest=False
|
2021-09-21 19:34:26 +02:00
|
|
|
)
|
2019-12-06 11:14:59 +01:00
|
|
|
)
|
2022-02-28 13:12:29 +01:00
|
|
|
self.assertEqual(
|
2015-04-16 00:09:35 +02:00
|
|
|
events[0],
|
2014-12-10 22:01:49 +01:00
|
|
|
[
|
|
|
|
{
|
2022-05-27 13:14:36 +02:00
|
|
|
"type": EduTypes.TYPING,
|
2019-03-04 11:05:39 +01:00
|
|
|
"room_id": ROOM_ID,
|
|
|
|
"content": {"user_ids": [U_APPLE.to_string()]},
|
2014-12-12 12:59:46 +01:00
|
|
|
}
|
2014-12-10 22:01:49 +01:00
|
|
|
],
|
|
|
|
)
|
|
|
|
|
2022-12-01 13:38:27 +01:00
|
|
|
# Enable federation sending on the main process.
|
|
|
|
@override_config({"federation_sender_instances": None})
|
2022-03-21 14:43:16 +01:00
|
|
|
def test_started_typing_remote_send(self) -> None:
|
2019-03-04 11:05:39 +01:00
|
|
|
self.room_members = [U_APPLE, U_ONION]
|
2014-08-20 20:15:47 +02:00
|
|
|
|
2020-06-17 16:37:59 +02:00
|
|
|
self.get_success(
|
2019-05-10 07:12:11 +02:00
|
|
|
self.handler.started_typing(
|
2020-08-26 18:05:36 +02:00
|
|
|
target_user=U_APPLE,
|
|
|
|
requester=create_requester(U_APPLE),
|
|
|
|
room_id=ROOM_ID,
|
|
|
|
timeout=20000,
|
2019-05-10 07:12:11 +02:00
|
|
|
)
|
|
|
|
)
|
2014-08-20 20:15:47 +02:00
|
|
|
|
2023-02-14 20:03:35 +01:00
|
|
|
self.mock_federation_client.put_json.assert_called_once_with(
|
2019-03-04 11:05:39 +01:00
|
|
|
"farm",
|
2019-03-26 12:35:29 +01:00
|
|
|
path="/_matrix/federation/v1/send/1000000",
|
2019-03-04 11:05:39 +01:00
|
|
|
data=_expect_edu_transaction(
|
2022-05-27 13:14:36 +02:00
|
|
|
EduTypes.TYPING,
|
2019-03-04 11:05:39 +01:00
|
|
|
content={
|
|
|
|
"room_id": ROOM_ID,
|
|
|
|
"user_id": U_APPLE.to_string(),
|
|
|
|
"typing": True,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
json_data_callback=ANY,
|
|
|
|
long_retries=True,
|
2019-03-13 19:26:06 +01:00
|
|
|
try_trailing_slash_on_400=True,
|
2023-09-04 15:04:43 +02:00
|
|
|
backoff_on_all_error_codes=True,
|
2019-03-04 11:05:39 +01:00
|
|
|
)
|
2014-08-20 20:15:47 +02:00
|
|
|
|
2022-03-21 14:43:16 +01:00
|
|
|
def test_started_typing_remote_recv(self) -> None:
|
2019-03-04 11:05:39 +01:00
|
|
|
self.room_members = [U_APPLE, U_ONION]
|
2014-08-20 20:15:47 +02:00
|
|
|
|
2022-02-28 13:12:29 +01:00
|
|
|
self.assertEqual(self.event_source.get_current_key(), 0)
|
2014-12-10 22:01:49 +01:00
|
|
|
|
2020-12-15 15:44:04 +01:00
|
|
|
channel = self.make_request(
|
2016-02-19 16:34:38 +01:00
|
|
|
"PUT",
|
2019-03-26 12:35:29 +01:00
|
|
|
"/_matrix/federation/v1/send/1000000",
|
2018-09-04 02:23:18 +02:00
|
|
|
_make_edu_transaction_json(
|
2022-05-27 13:14:36 +02:00
|
|
|
EduTypes.TYPING,
|
2014-08-20 20:15:47 +02:00
|
|
|
content={
|
2019-03-04 11:05:39 +01:00
|
|
|
"room_id": ROOM_ID,
|
|
|
|
"user_id": U_ONION.to_string(),
|
2014-08-20 20:15:47 +02:00
|
|
|
"typing": True,
|
|
|
|
},
|
2016-09-12 11:46:02 +02:00
|
|
|
),
|
2018-09-04 02:23:18 +02:00
|
|
|
federation_auth_origin=b"farm",
|
2014-08-20 20:15:47 +02:00
|
|
|
)
|
2019-03-04 11:05:39 +01:00
|
|
|
self.assertEqual(channel.code, 200)
|
2014-08-20 20:15:47 +02:00
|
|
|
|
2023-10-04 17:28:40 +02:00
|
|
|
self.on_new_event.assert_has_calls(
|
|
|
|
[call(StreamKeyType.TYPING, 1, rooms=[ROOM_ID])]
|
|
|
|
)
|
2014-08-20 20:15:47 +02:00
|
|
|
|
2022-02-28 13:12:29 +01:00
|
|
|
self.assertEqual(self.event_source.get_current_key(), 1)
|
2019-12-06 11:14:59 +01:00
|
|
|
events = self.get_success(
|
2021-09-21 19:34:26 +02:00
|
|
|
self.event_source.get_new_events(
|
2023-01-26 11:15:50 +01:00
|
|
|
user=U_APPLE, from_key=0, limit=0, room_ids=[ROOM_ID], is_guest=False
|
2021-09-21 19:34:26 +02:00
|
|
|
)
|
2019-12-06 11:14:59 +01:00
|
|
|
)
|
2022-02-28 13:12:29 +01:00
|
|
|
self.assertEqual(
|
2015-04-16 00:09:35 +02:00
|
|
|
events[0],
|
2018-08-10 15:54:09 +02:00
|
|
|
[
|
|
|
|
{
|
2022-05-27 13:14:36 +02:00
|
|
|
"type": EduTypes.TYPING,
|
2019-03-04 11:05:39 +01:00
|
|
|
"room_id": ROOM_ID,
|
|
|
|
"content": {"user_ids": [U_ONION.to_string()]},
|
2018-08-10 15:54:09 +02:00
|
|
|
}
|
|
|
|
],
|
2015-11-05 15:32:26 +01:00
|
|
|
)
|
2014-12-10 22:01:49 +01:00
|
|
|
|
2022-03-21 14:43:16 +01:00
|
|
|
def test_started_typing_remote_recv_not_in_room(self) -> None:
|
2021-07-06 15:31:13 +02:00
|
|
|
self.room_members = [U_APPLE, U_ONION]
|
|
|
|
|
2022-02-28 13:12:29 +01:00
|
|
|
self.assertEqual(self.event_source.get_current_key(), 0)
|
2021-07-06 15:31:13 +02:00
|
|
|
|
|
|
|
channel = self.make_request(
|
|
|
|
"PUT",
|
|
|
|
"/_matrix/federation/v1/send/1000000",
|
|
|
|
_make_edu_transaction_json(
|
2022-05-27 13:14:36 +02:00
|
|
|
EduTypes.TYPING,
|
2021-07-06 15:31:13 +02:00
|
|
|
content={
|
|
|
|
"room_id": OTHER_ROOM_ID,
|
|
|
|
"user_id": U_ONION.to_string(),
|
|
|
|
"typing": True,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
federation_auth_origin=b"farm",
|
|
|
|
)
|
|
|
|
self.assertEqual(channel.code, 200)
|
|
|
|
|
|
|
|
self.on_new_event.assert_not_called()
|
|
|
|
|
2022-02-28 13:12:29 +01:00
|
|
|
self.assertEqual(self.event_source.get_current_key(), 0)
|
2021-07-06 15:31:13 +02:00
|
|
|
events = self.get_success(
|
2021-09-21 19:34:26 +02:00
|
|
|
self.event_source.get_new_events(
|
|
|
|
user=U_APPLE,
|
|
|
|
from_key=0,
|
2023-01-26 11:15:50 +01:00
|
|
|
limit=0,
|
2021-09-21 19:34:26 +02:00
|
|
|
room_ids=[OTHER_ROOM_ID],
|
|
|
|
is_guest=False,
|
|
|
|
)
|
2021-07-06 15:31:13 +02:00
|
|
|
)
|
2022-02-28 13:12:29 +01:00
|
|
|
self.assertEqual(events[0], [])
|
|
|
|
self.assertEqual(events[1], 0)
|
2021-07-06 15:31:13 +02:00
|
|
|
|
2022-12-01 13:38:27 +01:00
|
|
|
# Enable federation sending on the main process.
|
|
|
|
@override_config({"federation_sender_instances": None})
|
2022-03-21 14:43:16 +01:00
|
|
|
def test_stopped_typing(self) -> None:
|
2019-03-04 11:05:39 +01:00
|
|
|
self.room_members = [U_APPLE, U_BANANA, U_ONION]
|
2014-08-20 20:15:47 +02:00
|
|
|
|
|
|
|
# Gut-wrenching
|
|
|
|
from synapse.handlers.typing import RoomMember
|
2018-08-10 15:54:09 +02:00
|
|
|
|
2019-03-04 11:05:39 +01:00
|
|
|
member = RoomMember(ROOM_ID, U_APPLE.to_string())
|
2014-12-10 20:39:01 +01:00
|
|
|
self.handler._member_typing_until[member] = 1002000
|
2020-02-21 13:15:07 +01:00
|
|
|
self.handler._room_typing[ROOM_ID] = {U_APPLE.to_string()}
|
2014-08-20 20:15:47 +02:00
|
|
|
|
2022-02-28 13:12:29 +01:00
|
|
|
self.assertEqual(self.event_source.get_current_key(), 0)
|
2014-08-20 20:15:47 +02:00
|
|
|
|
2020-06-17 16:37:59 +02:00
|
|
|
self.get_success(
|
2019-05-10 07:12:11 +02:00
|
|
|
self.handler.stopped_typing(
|
2020-08-26 18:05:36 +02:00
|
|
|
target_user=U_APPLE,
|
|
|
|
requester=create_requester(U_APPLE),
|
|
|
|
room_id=ROOM_ID,
|
2019-05-10 07:12:11 +02:00
|
|
|
)
|
2014-08-20 20:15:47 +02:00
|
|
|
)
|
|
|
|
|
2023-10-04 17:28:40 +02:00
|
|
|
self.on_new_event.assert_has_calls(
|
|
|
|
[call(StreamKeyType.TYPING, 1, rooms=[ROOM_ID])]
|
|
|
|
)
|
2019-05-10 07:12:11 +02:00
|
|
|
|
2023-02-14 20:03:35 +01:00
|
|
|
self.mock_federation_client.put_json.assert_called_once_with(
|
2019-03-04 11:05:39 +01:00
|
|
|
"farm",
|
2019-03-26 12:35:29 +01:00
|
|
|
path="/_matrix/federation/v1/send/1000000",
|
2019-03-04 11:05:39 +01:00
|
|
|
data=_expect_edu_transaction(
|
2022-05-27 13:14:36 +02:00
|
|
|
EduTypes.TYPING,
|
2019-03-04 11:05:39 +01:00
|
|
|
content={
|
|
|
|
"room_id": ROOM_ID,
|
|
|
|
"user_id": U_APPLE.to_string(),
|
|
|
|
"typing": False,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
json_data_callback=ANY,
|
|
|
|
long_retries=True,
|
2023-09-04 15:04:43 +02:00
|
|
|
backoff_on_all_error_codes=True,
|
2019-03-13 19:26:06 +01:00
|
|
|
try_trailing_slash_on_400=True,
|
2019-03-04 11:05:39 +01:00
|
|
|
)
|
2014-12-10 20:39:01 +01:00
|
|
|
|
2022-02-28 13:12:29 +01:00
|
|
|
self.assertEqual(self.event_source.get_current_key(), 1)
|
2019-12-06 11:14:59 +01:00
|
|
|
events = self.get_success(
|
2021-09-21 19:34:26 +02:00
|
|
|
self.event_source.get_new_events(
|
2023-01-26 11:15:50 +01:00
|
|
|
user=U_APPLE, from_key=0, limit=0, room_ids=[ROOM_ID], is_guest=False
|
2021-09-21 19:34:26 +02:00
|
|
|
)
|
2019-12-06 11:14:59 +01:00
|
|
|
)
|
2022-02-28 13:12:29 +01:00
|
|
|
self.assertEqual(
|
2015-04-16 00:09:35 +02:00
|
|
|
events[0],
|
2022-05-27 13:14:36 +02:00
|
|
|
[
|
|
|
|
{
|
|
|
|
"type": EduTypes.TYPING,
|
|
|
|
"room_id": ROOM_ID,
|
|
|
|
"content": {"user_ids": []},
|
|
|
|
}
|
|
|
|
],
|
2015-11-05 15:32:26 +01:00
|
|
|
)
|
2014-12-10 22:01:49 +01:00
|
|
|
|
2022-03-21 14:43:16 +01:00
|
|
|
def test_typing_timeout(self) -> None:
|
2019-03-04 11:05:39 +01:00
|
|
|
self.room_members = [U_APPLE, U_BANANA]
|
2014-12-10 20:39:01 +01:00
|
|
|
|
2022-02-28 13:12:29 +01:00
|
|
|
self.assertEqual(self.event_source.get_current_key(), 0)
|
2014-12-10 22:01:49 +01:00
|
|
|
|
2020-06-17 16:37:59 +02:00
|
|
|
self.get_success(
|
2019-05-10 07:12:11 +02:00
|
|
|
self.handler.started_typing(
|
2020-08-26 18:05:36 +02:00
|
|
|
target_user=U_APPLE,
|
|
|
|
requester=create_requester(U_APPLE),
|
|
|
|
room_id=ROOM_ID,
|
|
|
|
timeout=10000,
|
2019-05-10 07:12:11 +02:00
|
|
|
)
|
2014-12-10 20:39:01 +01:00
|
|
|
)
|
2019-05-10 07:12:11 +02:00
|
|
|
|
2023-10-04 17:28:40 +02:00
|
|
|
self.on_new_event.assert_has_calls(
|
|
|
|
[call(StreamKeyType.TYPING, 1, rooms=[ROOM_ID])]
|
|
|
|
)
|
2015-07-02 12:40:56 +02:00
|
|
|
self.on_new_event.reset_mock()
|
2014-12-10 20:39:01 +01:00
|
|
|
|
2022-02-28 13:12:29 +01:00
|
|
|
self.assertEqual(self.event_source.get_current_key(), 1)
|
2019-12-06 11:14:59 +01:00
|
|
|
events = self.get_success(
|
2021-09-21 19:34:26 +02:00
|
|
|
self.event_source.get_new_events(
|
|
|
|
user=U_APPLE,
|
|
|
|
from_key=0,
|
2023-01-26 11:15:50 +01:00
|
|
|
limit=0,
|
2021-09-21 19:34:26 +02:00
|
|
|
room_ids=[ROOM_ID],
|
|
|
|
is_guest=False,
|
|
|
|
)
|
2019-12-06 11:14:59 +01:00
|
|
|
)
|
2022-02-28 13:12:29 +01:00
|
|
|
self.assertEqual(
|
2015-04-16 00:09:35 +02:00
|
|
|
events[0],
|
2018-08-10 15:54:09 +02:00
|
|
|
[
|
|
|
|
{
|
2022-05-27 13:14:36 +02:00
|
|
|
"type": EduTypes.TYPING,
|
2019-03-04 11:05:39 +01:00
|
|
|
"room_id": ROOM_ID,
|
|
|
|
"content": {"user_ids": [U_APPLE.to_string()]},
|
2018-08-10 15:54:09 +02:00
|
|
|
}
|
|
|
|
],
|
2015-11-05 15:32:26 +01:00
|
|
|
)
|
2014-12-10 22:01:49 +01:00
|
|
|
|
2019-05-10 07:12:11 +02:00
|
|
|
self.reactor.pump([16])
|
2014-12-10 20:39:01 +01:00
|
|
|
|
2023-10-04 17:28:40 +02:00
|
|
|
self.on_new_event.assert_has_calls(
|
|
|
|
[call(StreamKeyType.TYPING, 2, rooms=[ROOM_ID])]
|
|
|
|
)
|
2014-12-10 22:01:49 +01:00
|
|
|
|
2022-02-28 13:12:29 +01:00
|
|
|
self.assertEqual(self.event_source.get_current_key(), 2)
|
2019-12-06 11:14:59 +01:00
|
|
|
events = self.get_success(
|
2021-09-21 19:34:26 +02:00
|
|
|
self.event_source.get_new_events(
|
|
|
|
user=U_APPLE,
|
|
|
|
from_key=1,
|
2023-01-26 11:15:50 +01:00
|
|
|
limit=0,
|
2021-09-21 19:34:26 +02:00
|
|
|
room_ids=[ROOM_ID],
|
|
|
|
is_guest=False,
|
|
|
|
)
|
2019-12-06 11:14:59 +01:00
|
|
|
)
|
2022-02-28 13:12:29 +01:00
|
|
|
self.assertEqual(
|
2015-04-16 00:09:35 +02:00
|
|
|
events[0],
|
2022-05-27 13:14:36 +02:00
|
|
|
[
|
|
|
|
{
|
|
|
|
"type": EduTypes.TYPING,
|
|
|
|
"room_id": ROOM_ID,
|
|
|
|
"content": {"user_ids": []},
|
|
|
|
}
|
|
|
|
],
|
2015-11-05 15:32:26 +01:00
|
|
|
)
|
2015-01-12 19:01:33 +01:00
|
|
|
|
|
|
|
# SYN-230 - see if we can still set after timeout
|
|
|
|
|
2020-06-17 16:37:59 +02:00
|
|
|
self.get_success(
|
2019-05-10 07:12:11 +02:00
|
|
|
self.handler.started_typing(
|
2020-08-26 18:05:36 +02:00
|
|
|
target_user=U_APPLE,
|
|
|
|
requester=create_requester(U_APPLE),
|
|
|
|
room_id=ROOM_ID,
|
|
|
|
timeout=10000,
|
2019-05-10 07:12:11 +02:00
|
|
|
)
|
2015-01-12 19:01:33 +01:00
|
|
|
)
|
2019-05-10 07:12:11 +02:00
|
|
|
|
2023-10-04 17:28:40 +02:00
|
|
|
self.on_new_event.assert_has_calls(
|
|
|
|
[call(StreamKeyType.TYPING, 3, rooms=[ROOM_ID])]
|
|
|
|
)
|
2015-07-02 12:40:56 +02:00
|
|
|
self.on_new_event.reset_mock()
|
2015-01-12 19:01:33 +01:00
|
|
|
|
2022-02-28 13:12:29 +01:00
|
|
|
self.assertEqual(self.event_source.get_current_key(), 3)
|
2019-12-06 11:14:59 +01:00
|
|
|
events = self.get_success(
|
2021-09-21 19:34:26 +02:00
|
|
|
self.event_source.get_new_events(
|
|
|
|
user=U_APPLE,
|
|
|
|
from_key=0,
|
2023-01-26 11:15:50 +01:00
|
|
|
limit=0,
|
2021-09-21 19:34:26 +02:00
|
|
|
room_ids=[ROOM_ID],
|
|
|
|
is_guest=False,
|
|
|
|
)
|
2019-12-06 11:14:59 +01:00
|
|
|
)
|
2022-02-28 13:12:29 +01:00
|
|
|
self.assertEqual(
|
2015-04-16 00:09:35 +02:00
|
|
|
events[0],
|
2018-08-10 15:54:09 +02:00
|
|
|
[
|
|
|
|
{
|
2022-05-27 13:14:36 +02:00
|
|
|
"type": EduTypes.TYPING,
|
2019-03-04 11:05:39 +01:00
|
|
|
"room_id": ROOM_ID,
|
|
|
|
"content": {"user_ids": [U_APPLE.to_string()]},
|
2018-08-10 15:54:09 +02:00
|
|
|
}
|
|
|
|
],
|
2015-11-05 15:32:26 +01:00
|
|
|
)
|