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

Read the room version from database when fetching events #6874

Merged
merged 4 commits into from
Mar 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/6874.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Refactoring work in preparation for changing the event redaction algorithm.
84 changes: 68 additions & 16 deletions synapse/storage/data_stores/main/events_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@

from synapse.api.constants import EventTypes
from synapse.api.errors import NotFoundError
from synapse.api.room_versions import EventFormatVersions
from synapse.events import FrozenEvent, event_type_from_format_version # noqa: F401
from synapse.events.snapshot import EventContext # noqa: F401
from synapse.api.room_versions import (
KNOWN_ROOM_VERSIONS,
EventFormatVersions,
RoomVersions,
)
from synapse.events import make_event_from_dict
from synapse.events.utils import prune_event
from synapse.logging.context import LoggingContext, PreserveLoggingContext
from synapse.metrics.background_process_metrics import run_as_background_process
Expand Down Expand Up @@ -580,8 +583,49 @@ def _get_events_from_db(self, event_ids, allow_rejected=False):
# of a event format version, so it must be a V1 event.
format_version = EventFormatVersions.V1

original_ev = event_type_from_format_version(format_version)(
room_version_id = row["room_version_id"]

if not room_version_id:
# this should only happen for out-of-band membership events
if not internal_metadata.get("out_of_band_membership"):
logger.warning(
"Room %s for event %s is unknown", d["room_id"], event_id
)
continue

# take a wild stab at the room version based on the event format
if format_version == EventFormatVersions.V1:
room_version = RoomVersions.V1
elif format_version == EventFormatVersions.V2:
room_version = RoomVersions.V3
else:
room_version = RoomVersions.V5
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the idea that for out of band memberships after this commit will have a room version stored? I.e. we should never get here once for later room versions?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, out-of-band-memberships since #6983. But yes.

else:
room_version = KNOWN_ROOM_VERSIONS.get(room_version_id)
if not room_version:
logger.error(
"Event %s in room %s has unknown room version %s",
event_id,
d["room_id"],
room_version_id,
)
continue

if room_version.event_format != format_version:
logger.error(
"Event %s in room %s with version %s has wrong format: "
"expected %s, was %s",
event_id,
d["room_id"],
room_version_id,
room_version.event_format,
format_version,
)
continue

original_ev = make_event_from_dict(
event_dict=d,
room_version=room_version,
internal_metadata_dict=internal_metadata,
rejected_reason=rejected_reason,
)
Expand Down Expand Up @@ -661,6 +705,12 @@ def _fetch_event_rows(self, txn, event_ids):
of EventFormatVersions. 'None' means the event predates
EventFormatVersions (so the event is format V1).

* room_version_id (str|None): The version of the room which contains the event.
Hopefully one of RoomVersions.

Due to historical reasons, there may be a few events in the database which
do not have an associated room; in this case None will be returned here.

* rejected_reason (str|None): if the event was rejected, the reason
why.

Expand All @@ -676,17 +726,18 @@ def _fetch_event_rows(self, txn, event_ids):
"""
event_dict = {}
for evs in batch_iter(event_ids, 200):
sql = (
"SELECT "
" e.event_id, "
" e.internal_metadata,"
" e.json,"
" e.format_version, "
" rej.reason "
" FROM event_json as e"
" LEFT JOIN rejections as rej USING (event_id)"
" WHERE "
)
sql = """\
SELECT
e.event_id,
e.internal_metadata,
e.json,
e.format_version,
r.room_version,
rej.reason
FROM event_json as e
LEFT JOIN rooms r USING (room_id)
LEFT JOIN rejections as rej USING (event_id)
WHERE """

clause, args = make_in_list_sql_clause(
txn.database_engine, "e.event_id", evs
Expand All @@ -701,7 +752,8 @@ def _fetch_event_rows(self, txn, event_ids):
"internal_metadata": row[1],
"json": row[2],
"format_version": row[3],
"rejected_reason": row[4],
"room_version_id": row[4],
"rejected_reason": row[5],
"redactions": [],
}

Expand Down
10 changes: 10 additions & 0 deletions tests/replication/slave/storage/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from canonicaljson import encode_canonical_json

from synapse.api.room_versions import RoomVersions
from synapse.events import FrozenEvent, _EventInternalMetadata, make_event_from_dict
from synapse.events.snapshot import EventContext
from synapse.handlers.room import RoomEventSource
Expand Down Expand Up @@ -58,6 +59,15 @@ def setUp(self):
self.unpatches = [patch__eq__(_EventInternalMetadata), patch__eq__(FrozenEvent)]
return super(SlavedEventStoreTestCase, self).setUp()

def prepare(self, *args, **kwargs):
super().prepare(*args, **kwargs)

self.get_success(
self.master_store.store_room(
ROOM_ID, USER_ID, is_public=False, room_version=RoomVersions.V1,
)
)

def tearDown(self):
[unpatch() for unpatch in self.unpatches]

Expand Down