Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
SyncResultBuilder: remove excluded rooms
Browse files Browse the repository at this point in the history
  • Loading branch information
David Robertson committed Jan 19, 2023
1 parent e1e0703 commit 282b229
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
17 changes: 6 additions & 11 deletions synapse/handlers/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -1387,15 +1387,6 @@ async def generate_sync_result(
if await self.store.is_partial_state_room(room_id)
)

# Now we have our list of joined room IDs, exclude as configured and freeze
joined_room_ids = frozenset(
(
room_id
for room_id in mutable_joined_room_ids
if room_id not in mutable_rooms_to_exclude
)
)

logger.debug(
"Calculating sync response for %r between %s and %s",
sync_config.user,
Expand All @@ -1408,7 +1399,7 @@ async def generate_sync_result(
full_state,
since_token=since_token,
now_token=now_token,
joined_room_ids=joined_room_ids,
joined_room_ids=frozenset(mutable_joined_room_ids),
excluded_room_ids=frozenset(mutable_rooms_to_exclude),
membership_change_events=membership_change_events,
)
Expand Down Expand Up @@ -2561,7 +2552,8 @@ class SyncResultBuilder:
full_state: The full_state flag as specified by user
since_token: The token supplied by user, or None.
now_token: The token to sync up to.
joined_room_ids: List of rooms the user is joined to
joined_room_ids: Set of rooms the user is joined to. The excluded_room_ids
are automatically discarded from this set.
excluded_room_ids: Set of room ids we should omit from the /sync response.
# The following mirror the fields in a sync response
Expand Down Expand Up @@ -2590,6 +2582,9 @@ class SyncResultBuilder:
archived: List[ArchivedSyncResult] = attr.Factory(list)
to_device: List[JsonDict] = attr.Factory(list)

def __attrs_post_init__(self) -> None:
self.joined_room_ids = self.joined_room_ids - self.excluded_room_ids

def calculate_user_changes(self) -> Tuple[AbstractSet[str], AbstractSet[str]]:
"""Work out which other users have joined or left rooms we are joined to.
Expand Down
21 changes: 19 additions & 2 deletions tests/handlers/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest as stdlib_unittest
from typing import Optional
from unittest.mock import MagicMock, Mock, patch

Expand All @@ -20,18 +21,34 @@
from synapse.api.errors import Codes, ResourceLimitError
from synapse.api.filtering import Filtering
from synapse.api.room_versions import RoomVersions
from synapse.handlers.sync import SyncConfig, SyncResult
from synapse.handlers.sync import SyncConfig, SyncResult, SyncResultBuilder
from synapse.rest import admin
from synapse.rest.client import knock, login, room
from synapse.server import HomeServer
from synapse.types import UserID, create_requester
from synapse.types import StreamToken, UserID, create_requester
from synapse.util import Clock

import tests.unittest
import tests.utils
from tests.test_utils import make_awaitable


class TestSyncResultBuilder(stdlib_unittest.TestCase):
def test_excluded_rooms_set_is_discard_from_joined_rooms_set(self) -> None:
builder = SyncResultBuilder(
sync_config=generate_sync_config("@alice:wonderland"),
full_state=False,
since_token=None,
now_token=StreamToken.START,
joined_room_ids=frozenset(["a", "b", "c"]),
excluded_room_ids=frozenset(["c", "d"]),
membership_change_events=[],
)

self.assertEqual(builder.joined_room_ids, {"a", "b"})
self.assertEqual(builder.excluded_room_ids, {"c", "d"})


class SyncTestCase(tests.unittest.HomeserverTestCase):
"""Tests Sync Handler."""

Expand Down

0 comments on commit 282b229

Please sign in to comment.