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

Commit

Permalink
Merge pull request #6820 from matrix-org/rav/get_room_version_id
Browse files Browse the repository at this point in the history
* commit '160522e32':
  newsfile
  Add `get_room_version` method
  s/get_room_version/get_room_version_id/
  • Loading branch information
anoadragon453 committed Mar 23, 2020
2 parents bea0e0d + 160522e commit 8c7c6e1
Show file tree
Hide file tree
Showing 14 changed files with 64 additions and 38 deletions.
1 change: 1 addition & 0 deletions changelog.d/6820.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Refactoring work in preparation for changing the event redaction algorithm.
6 changes: 2 additions & 4 deletions synapse/api/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,11 +410,9 @@ class UnsupportedRoomVersionError(SynapseError):
"""The client's request to create a room used a room version that the server does
not support."""

def __init__(self):
def __init__(self, msg="Homeserver does not support this room version"):
super(UnsupportedRoomVersionError, self).__init__(
code=400,
msg="Homeserver does not support this room version",
errcode=Codes.UNSUPPORTED_ROOM_VERSION,
code=400, msg=msg, errcode=Codes.UNSUPPORTED_ROOM_VERSION,
)


Expand Down
10 changes: 5 additions & 5 deletions synapse/federation/federation_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def backfill(self, dest, room_id, limit, extremities):

logger.debug("backfill transaction_data=%r", transaction_data)

room_version = yield self.store.get_room_version(room_id)
room_version = yield self.store.get_room_version_id(room_id)
format_ver = room_version_to_event_format(room_version)

pdus = [
Expand Down Expand Up @@ -336,7 +336,7 @@ def get_room_state_ids(self, destination: str, room_id: str, event_id: str):
def get_event_auth(self, destination, room_id, event_id):
res = yield self.transport_layer.get_event_auth(destination, room_id, event_id)

room_version = yield self.store.get_room_version(room_id)
room_version = yield self.store.get_room_version_id(room_id)
format_ver = room_version_to_event_format(room_version)

auth_chain = [
Expand Down Expand Up @@ -649,15 +649,15 @@ def _do_send_join(self, destination, pdu):

@defer.inlineCallbacks
def send_invite(self, destination, room_id, event_id, pdu):
room_version = yield self.store.get_room_version(room_id)
room_version = yield self.store.get_room_version_id(room_id)

content = yield self._do_send_invite(destination, pdu, room_version)

pdu_dict = content["event"]

logger.debug("Got response to send_invite: %s", pdu_dict)

room_version = yield self.store.get_room_version(room_id)
room_version = yield self.store.get_room_version_id(room_id)
format_ver = room_version_to_event_format(room_version)

pdu = event_from_pdu_json(pdu_dict, format_ver)
Expand Down Expand Up @@ -859,7 +859,7 @@ def get_missing_events(
timeout=timeout,
)

room_version = yield self.store.get_room_version(room_id)
room_version = yield self.store.get_room_version_id(room_id)
format_ver = room_version_to_event_format(room_version)

events = [
Expand Down
16 changes: 8 additions & 8 deletions synapse/federation/federation_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ async def _handle_pdus_in_txn(
continue

try:
room_version = await self.store.get_room_version(room_id)
room_version = await self.store.get_room_version_id(room_id)
except NotFoundError:
logger.info("Ignoring PDU for unknown room_id: %s", room_id)
continue
Expand Down Expand Up @@ -334,7 +334,7 @@ async def on_context_state_request(self, origin, room_id, event_id):
)
)

room_version = await self.store.get_room_version(room_id)
room_version = await self.store.get_room_version_id(room_id)
resp["room_version"] = room_version

return 200, resp
Expand Down Expand Up @@ -385,7 +385,7 @@ async def on_make_join_request(self, origin, room_id, user_id, supported_version
origin_host, _ = parse_server_name(origin)
await self.check_server_matches_acl(origin_host, room_id)

room_version = await self.store.get_room_version(room_id)
room_version = await self.store.get_room_version_id(room_id)
if room_version not in supported_versions:
logger.warning(
"Room version %s not in %s", room_version, supported_versions
Expand Down Expand Up @@ -417,7 +417,7 @@ async def on_invite_request(self, origin, content, room_version):
async def on_send_join_request(self, origin, content, room_id):
logger.debug("on_send_join_request: content: %s", content)

room_version = await self.store.get_room_version(room_id)
room_version = await self.store.get_room_version_id(room_id)
format_ver = room_version_to_event_format(room_version)
pdu = event_from_pdu_json(content, format_ver)

Expand All @@ -440,15 +440,15 @@ async def on_make_leave_request(self, origin, room_id, user_id):
await self.check_server_matches_acl(origin_host, room_id)
pdu = await self.handler.on_make_leave_request(origin, room_id, user_id)

room_version = await self.store.get_room_version(room_id)
room_version = await self.store.get_room_version_id(room_id)

time_now = self._clock.time_msec()
return {"event": pdu.get_pdu_json(time_now), "room_version": room_version}

async def on_send_leave_request(self, origin, content, room_id):
logger.debug("on_send_leave_request: content: %s", content)

room_version = await self.store.get_room_version(room_id)
room_version = await self.store.get_room_version_id(room_id)
format_ver = room_version_to_event_format(room_version)
pdu = event_from_pdu_json(content, format_ver)

Expand Down Expand Up @@ -495,7 +495,7 @@ async def on_query_auth_request(self, origin, content, room_id, event_id):
origin_host, _ = parse_server_name(origin)
await self.check_server_matches_acl(origin_host, room_id)

room_version = await self.store.get_room_version(room_id)
room_version = await self.store.get_room_version_id(room_id)
format_ver = room_version_to_event_format(room_version)

auth_chain = [
Expand Down Expand Up @@ -664,7 +664,7 @@ async def _handle_received_pdu(self, origin, pdu):
logger.info("Accepting join PDU %s from %s", pdu.event_id, origin)

# We've already checked that we know the room version by this point
room_version = await self.store.get_room_version(pdu.room_id)
room_version = await self.store.get_room_version_id(pdu.room_id)

# Check signature.
try:
Expand Down
18 changes: 9 additions & 9 deletions synapse/handlers/federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ async def on_receive_pdu(self, origin, pdu, sent_to_us_directly=False) -> None:
for x in remote_state:
event_map[x.event_id] = x

room_version = await self.store.get_room_version(room_id)
room_version = await self.store.get_room_version_id(room_id)
state_map = await resolve_events_with_store(
room_id,
room_version,
Expand Down Expand Up @@ -1114,7 +1114,7 @@ async def _get_events_and_persist(
Logs a warning if we can't find the given event.
"""

room_version = await self.store.get_room_version(room_id)
room_version = await self.store.get_room_version_id(room_id)

event_infos = []

Expand Down Expand Up @@ -1377,7 +1377,7 @@ def on_make_join_request(self, origin, room_id, user_id):

event_content = {"membership": Membership.JOIN}

room_version = yield self.store.get_room_version(room_id)
room_version = yield self.store.get_room_version_id(room_id)

builder = self.event_builder_factory.new(
room_version,
Expand Down Expand Up @@ -1618,7 +1618,7 @@ def on_make_leave_request(self, origin, room_id, user_id):
)
raise SynapseError(403, "User not from origin", Codes.FORBIDDEN)

room_version = yield self.store.get_room_version(room_id)
room_version = yield self.store.get_room_version_id(room_id)
builder = self.event_builder_factory.new(
room_version,
{
Expand Down Expand Up @@ -2070,7 +2070,7 @@ def _check_for_soft_fail(
do_soft_fail_check = False

if do_soft_fail_check:
room_version = yield self.store.get_room_version(event.room_id)
room_version = yield self.store.get_room_version_id(event.room_id)
room_version_obj = KNOWN_ROOM_VERSIONS[room_version]

# Calculate the "current state".
Expand Down Expand Up @@ -2206,7 +2206,7 @@ def do_auth(self, origin, event, context, auth_events):
Returns:
defer.Deferred[EventContext]: updated context object
"""
room_version = yield self.store.get_room_version(event.room_id)
room_version = yield self.store.get_room_version_id(event.room_id)
room_version_obj = KNOWN_ROOM_VERSIONS[room_version]

try:
Expand Down Expand Up @@ -2378,7 +2378,7 @@ def _update_auth_events_and_context_for_auth(
remote_auth_events.update({(d.type, d.state_key): d for d in different_events})
remote_state = remote_auth_events.values()

room_version = yield self.store.get_room_version(event.room_id)
room_version = yield self.store.get_room_version_id(event.room_id)
new_state = yield self.state_handler.resolve_events(
room_version, (local_state, remote_state), event
)
Expand Down Expand Up @@ -2602,7 +2602,7 @@ def exchange_third_party_invite(
}

if (yield self.auth.check_host_in_room(room_id, self.hs.hostname)):
room_version = yield self.store.get_room_version(room_id)
room_version = yield self.store.get_room_version_id(room_id)
builder = self.event_builder_factory.new(room_version, event_dict)

EventValidator().validate_builder(builder)
Expand Down Expand Up @@ -2665,7 +2665,7 @@ def on_exchange_third_party_invite_request(self, room_id, event_dict):
Returns:
Deferred: resolves (to None)
"""
room_version = yield self.store.get_room_version(room_id)
room_version = yield self.store.get_room_version_id(room_id)

# NB: event_dict has a particular specced format we might need to fudge
# if we change event formats too much.
Expand Down
8 changes: 5 additions & 3 deletions synapse/handlers/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,9 @@ def create_event(
room_version = event_dict["content"]["room_version"]
else:
try:
room_version = yield self.store.get_room_version(event_dict["room_id"])
room_version = yield self.store.get_room_version_id(
event_dict["room_id"]
)
except NotFoundError:
raise AuthError(403, "Unknown room")

Expand Down Expand Up @@ -788,7 +790,7 @@ def handle_new_client_event(
):
room_version = event.content.get("room_version", RoomVersions.V1.identifier)
else:
room_version = yield self.store.get_room_version(event.room_id)
room_version = yield self.store.get_room_version_id(event.room_id)

event_allowed = yield self.third_party_event_rules.check_event_allowed(
event, context
Expand Down Expand Up @@ -963,7 +965,7 @@ def is_inviter_member_event(e):
auth_events = yield self.store.get_events(auth_events_ids)
auth_events = {(e.type, e.state_key): e for e in auth_events.values()}

room_version = yield self.store.get_room_version(event.room_id)
room_version = yield self.store.get_room_version_id(event.room_id)
room_version_obj = KNOWN_ROOM_VERSIONS[room_version]

if event_auth.check_redaction(
Expand Down
2 changes: 1 addition & 1 deletion synapse/handlers/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ async def purge_room(self, room_id):
"""Purge the given room from the database"""
with (await self.pagination_lock.write(room_id)):
# check we know about the room
await self.store.get_room_version(room_id)
await self.store.get_room_version_id(room_id)

# first check that we have no users in this room
joined = await defer.maybeDeferred(
Expand Down
2 changes: 1 addition & 1 deletion synapse/handlers/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def _upgrade_room(self, requester, old_room_id, new_version):
},
token_id=requester.access_token_id,
)
old_room_version = yield self.store.get_room_version(old_room_id)
old_room_version = yield self.store.get_room_version_id(old_room_id)
yield self.auth.check_from_context(
old_room_version, tombstone_event, tombstone_context
)
Expand Down
2 changes: 1 addition & 1 deletion synapse/state/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ def resolve_state_groups_for_events(self, room_id, event_ids):
delta_ids=delta_ids,
)

room_version = yield self.store.get_room_version(room_id)
room_version = yield self.store.get_room_version_id(room_id)

result = yield self._state_resolution_handler.resolve_state_groups(
room_id,
Expand Down
27 changes: 25 additions & 2 deletions synapse/storage/data_stores/main/state.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
# Copyright 2014-2016 OpenMarket Ltd
# Copyright 2020 The Matrix.org Foundation C.I.C.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -22,7 +23,8 @@
from twisted.internet import defer

from synapse.api.constants import EventTypes, Membership
from synapse.api.errors import NotFoundError
from synapse.api.errors import NotFoundError, UnsupportedRoomVersionError
from synapse.api.room_versions import KNOWN_ROOM_VERSIONS, RoomVersion
from synapse.events import EventBase
from synapse.events.snapshot import EventContext
from synapse.storage._base import SQLBaseStore
Expand Down Expand Up @@ -61,8 +63,29 @@ class StateGroupWorkerStore(EventsWorkerStore, SQLBaseStore):
def __init__(self, database: Database, db_conn, hs):
super(StateGroupWorkerStore, self).__init__(database, db_conn, hs)

async def get_room_version(self, room_id: str) -> RoomVersion:
"""Get the room_version of a given room
Raises:
NotFoundError: if the room is unknown
UnsupportedRoomVersionError: if the room uses an unknown room version.
Typically this happens if support for the room's version has been
removed from Synapse.
"""
room_version_id = await self.get_room_version_id(room_id)
v = KNOWN_ROOM_VERSIONS.get(room_version_id)

if not v:
raise UnsupportedRoomVersionError(
"Room %s uses a room version %s which is no longer supported"
% (room_id, room_version_id)
)

return v

@cached(max_entries=10000)
async def get_room_version(self, room_id: str) -> str:
async def get_room_version_id(self, room_id: str) -> str:
"""Get the room_version of a given room
Raises:
Expand Down
2 changes: 1 addition & 1 deletion synapse/storage/persist_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ async def _get_new_state_after_events(
break

if not room_version:
room_version = await self.main_store.get_room_version(room_id)
room_version = await self.main_store.get_room_version_id(room_id)

logger.debug("calling resolve_state_groups from preserve_events")
res = await self._state_resolution_handler.resolve_state_groups(
Expand Down
2 changes: 1 addition & 1 deletion tests/handlers/test_presence.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ def _add_new_user(self, room_id, user_id):

hostname = get_domain_from_id(user_id)

room_version = self.get_success(self.store.get_room_version(room_id))
room_version = self.get_success(self.store.get_room_version_id(room_id))

builder = EventBuilder(
state=self.state,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def register_event_context(self, event, context):
def register_event_id_state_group(self, event_id, state_group):
self._event_to_state_group[event_id] = state_group

def get_room_version(self, room_id):
def get_room_version_id(self, room_id):
return RoomVersions.V1.identifier


Expand Down
4 changes: 3 additions & 1 deletion tests/unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,9 @@ def inject_room_member(self, room: str, user: str, membership: Membership) -> No
event_builder_factory = self.hs.get_event_builder_factory()
event_creation_handler = self.hs.get_event_creation_handler()

room_version = self.get_success(self.hs.get_datastore().get_room_version(room))
room_version = self.get_success(
self.hs.get_datastore().get_room_version_id(room)
)

builder = event_builder_factory.for_room_version(
KNOWN_ROOM_VERSIONS[room_version],
Expand Down

0 comments on commit 8c7c6e1

Please sign in to comment.