rooms: Restructure room info handling.

This commit is contained in:
poljar (Damir Jelić) 2018-03-19 12:24:59 +01:00
parent b4a315b6fa
commit ee321b21d9
2 changed files with 34 additions and 31 deletions

View file

@ -422,24 +422,30 @@ class MatrixSyncEvent(MatrixEvent):
return MatrixErrorEvent.from_dict(server, "Error syncing", False, return MatrixErrorEvent.from_dict(server, "Error syncing", False,
parsed_dict) parsed_dict)
def _execute_joined_info(self, info): def _execute_joined_info(self):
server = self.server server = self.server
while self.joined_room_infos:
info = self.joined_room_infos.pop()
if info.room_id not in server.buffers: if info.room_id not in server.buffers:
matrix_create_room_buffer(server, info.room_id) matrix_create_room_buffer(server, info.room_id)
room = server.rooms[info.room_id] room = server.rooms[info.room_id]
buf = server.buffers[info.room_id]
if not room.prev_batch: if not room.prev_batch:
room.prev_batch = info.prev_batch room.prev_batch = info.prev_batch
tags = tags_for_message("message")
for event in info.membership_events:
event.execute(server, room, buf, list(tags))
for event in info.events: for event in info.events:
self._execute_room_event(event, info.room_id)
def _execute_room_event(self, event, room_id):
server = self.server
room = server.rooms[room_id]
buf = server.buffers[room_id]
tags = tags_for_message("message")
event.execute(server, room, buf, list(tags)) event.execute(server, room, buf, list(tags))
def execute(self): def execute(self):
@ -450,8 +456,7 @@ class MatrixSyncEvent(MatrixEvent):
server.sync() server.sync()
return return
for room_info in self.joined_room_infos: self._execute_joined_info()
self._execute_joined_info(room_info)
server.next_batch = self.next_batch server.next_batch = self.next_batch
server.sync() server.sync()

View file

@ -188,11 +188,10 @@ def matrix_create_room_buffer(server, room_id):
class RoomInfo(): class RoomInfo():
def __init__(self, room_id, prev_batch, membership_events, events): def __init__(self, room_id, prev_batch, events):
# type: (str, str, List[Any], List[Any]) -> None # type: (str, str, List[Any], List[Any]) -> None
self.room_id = room_id self.room_id = room_id
self.prev_batch = prev_batch self.prev_batch = prev_batch
self.membership_events = membership_events
self.events = events self.events = events
@staticmethod @staticmethod
@ -245,8 +244,8 @@ class RoomInfo():
@staticmethod @staticmethod
def parse_event(event_dict): def parse_event(event_dict):
# type: (Dict[Any, Any]) -> (RoomEvent, RoomEvent) # type: (Dict[Any, Any]) -> (RoomEvent, RoomEvent)
message_event = None
state_event = None state_event = None
message_event = None
if event_dict["type"] == "m.room.message": if event_dict["type"] == "m.room.message":
message_event = RoomInfo._message_from_event(event_dict) message_event = RoomInfo._message_from_event(event_dict)
@ -266,18 +265,18 @@ class RoomInfo():
elif event_dict["type"] == "m.room.encryption": elif event_dict["type"] == "m.room.encryption":
message_event = RoomEncryptionEvent.from_dict(event_dict) message_event = RoomEncryptionEvent.from_dict(event_dict)
return message_event, state_event return state_event, message_event
@staticmethod @staticmethod
def _parse_events(parsed_dict): def _parse_events(parsed_dict):
membership_events = [] state_events = []
other_events = [] message_events = []
try: try:
for event in parsed_dict: for event in parsed_dict:
m_event, s_event = RoomInfo.parse_event(event) m_event, s_event = RoomInfo.parse_event(event)
membership_events.append(m_event) state_events.append(m_event)
other_events.append(s_event) message_events.append(s_event)
except (ValueError, TypeError, KeyError) as error: except (ValueError, TypeError, KeyError) as error:
message = ("{prefix}matrix: Error parsing " message = ("{prefix}matrix: Error parsing "
"room event of type {type}: {error}").format( "room event of type {type}: {error}").format(
@ -287,7 +286,9 @@ class RoomInfo():
W.prnt("", message) W.prnt("", message)
raise raise
return (membership_events, other_events) events = state_events + message_events
return events
@classmethod @classmethod
def from_dict(cls, room_id, parsed_dict): def from_dict(cls, room_id, parsed_dict):
@ -296,15 +297,12 @@ class RoomInfo():
state_dict = parsed_dict['state']['events'] state_dict = parsed_dict['state']['events']
timeline_dict = parsed_dict['timeline']['events'] timeline_dict = parsed_dict['timeline']['events']
membership_events, other_events = RoomInfo._parse_events(state_dict) state_events = RoomInfo._parse_events(state_dict)
timeline_member_events, timeline_events = RoomInfo._parse_events( timeline_events = RoomInfo._parse_events(timeline_dict)
timeline_dict)
membership_events.extend(timeline_member_events) events = state_events + timeline_events
other_events.extend(timeline_events)
return cls(room_id, prev_batch, list(filter(None, membership_events)), return cls(room_id, prev_batch, list(filter(None, events)))
list(filter(None, other_events)))
class RoomEvent(): class RoomEvent():