Refcator EventContext to accept state during init

This commit is contained in:
Erik Johnston 2018-07-23 12:17:16 +01:00
parent 354a99c968
commit acbfdc3442
2 changed files with 82 additions and 63 deletions

View File

@ -60,22 +60,22 @@ class EventContext(object):
"app_service", "app_service",
] ]
def __init__(self): def __init__(self, state_group, current_state_ids, prev_state_ids,
prev_group=None, delta_ids=None):
# The current state including the current event # The current state including the current event
self.current_state_ids = None self.current_state_ids = current_state_ids
# The current state excluding the current event # The current state excluding the current event
self.prev_state_ids = None self.prev_state_ids = prev_state_ids
self.state_group = None self.state_group = state_group
self.rejected = False
# A previously persisted state group and a delta between that # A previously persisted state group and a delta between that
# and this state. # and this state.
self.prev_group = None self.prev_group = prev_group
self.delta_ids = None self.delta_ids = delta_ids
self.prev_state_events = None self.prev_state_events = []
self.rejected = False
self.app_service = None self.app_service = None
def serialize(self, event): def serialize(self, event):
@ -123,27 +123,33 @@ class EventContext(object):
Returns: Returns:
EventContext EventContext
""" """
context = EventContext()
context.state_group = input["state_group"]
context.rejected = input["rejected"]
context.prev_group = input["prev_group"]
context.delta_ids = _decode_state_dict(input["delta_ids"])
context.prev_state_events = input["prev_state_events"]
# We use the state_group and prev_state_id stuff to pull the # We use the state_group and prev_state_id stuff to pull the
# current_state_ids out of the DB and construct prev_state_ids. # current_state_ids out of the DB and construct prev_state_ids.
prev_state_id = input["prev_state_id"] prev_state_id = input["prev_state_id"]
event_type = input["event_type"] event_type = input["event_type"]
event_state_key = input["event_state_key"] event_state_key = input["event_state_key"]
context.current_state_ids = yield store.get_state_ids_for_group( state_group = input["state_group"]
context.state_group,
current_state_ids = yield store.get_state_ids_for_group(
state_group,
) )
if prev_state_id and event_state_key: if prev_state_id and event_state_key:
context.prev_state_ids = dict(context.current_state_ids) prev_state_ids = dict(current_state_ids)
context.prev_state_ids[(event_type, event_state_key)] = prev_state_id prev_state_ids[(event_type, event_state_key)] = prev_state_id
else: else:
context.prev_state_ids = context.current_state_ids prev_state_ids = current_state_ids
context = EventContext(
state_group=state_group,
current_state_ids=current_state_ids,
prev_state_ids=prev_state_ids,
prev_group=input["prev_group"],
delta_ids = _decode_state_dict(input["delta_ids"]),
)
context.rejected = input["rejected"]
context.prev_state_events = input["prev_state_events"]
app_service_id = input["app_service_id"] app_service_id = input["app_service_id"]
if app_service_id: if app_service_id:

View File

@ -203,25 +203,27 @@ class StateHandler(object):
# If this is an outlier, then we know it shouldn't have any current # If this is an outlier, then we know it shouldn't have any current
# state. Certainly store.get_current_state won't return any, and # state. Certainly store.get_current_state won't return any, and
# persisting the event won't store the state group. # persisting the event won't store the state group.
context = EventContext()
if old_state: if old_state:
context.prev_state_ids = { prev_state_ids = {
(s.type, s.state_key): s.event_id for s in old_state (s.type, s.state_key): s.event_id for s in old_state
} }
if event.is_state(): if event.is_state():
context.current_state_ids = dict(context.prev_state_ids) current_state_ids = dict(prev_state_ids)
key = (event.type, event.state_key) key = (event.type, event.state_key)
context.current_state_ids[key] = event.event_id current_state_ids[key] = event.event_id
else: else:
context.current_state_ids = context.prev_state_ids current_state_ids = prev_state_ids
else: else:
context.current_state_ids = {} current_state_ids = {}
context.prev_state_ids = {} prev_state_ids = {}
context.prev_state_events = []
# We don't store state for outliers, so we don't generate a state # We don't store state for outliers, so we don't generate a state
# froup for it. # group for it.
context.state_group = None context = EventContext(
state_group=None,
current_state_ids=current_state_ids,
prev_state_ids=prev_state_ids,
)
defer.returnValue(context) defer.returnValue(context)
@ -230,31 +232,35 @@ class StateHandler(object):
# Let's just correctly fill out the context and create a # Let's just correctly fill out the context and create a
# new state group for it. # new state group for it.
context = EventContext() prev_state_ids = {
context.prev_state_ids = {
(s.type, s.state_key): s.event_id for s in old_state (s.type, s.state_key): s.event_id for s in old_state
} }
if event.is_state(): if event.is_state():
key = (event.type, event.state_key) key = (event.type, event.state_key)
if key in context.prev_state_ids: if key in prev_state_ids:
replaces = context.prev_state_ids[key] replaces = prev_state_ids[key]
if replaces != event.event_id: # Paranoia check if replaces != event.event_id: # Paranoia check
event.unsigned["replaces_state"] = replaces event.unsigned["replaces_state"] = replaces
context.current_state_ids = dict(context.prev_state_ids) current_state_ids = dict(prev_state_ids)
context.current_state_ids[key] = event.event_id current_state_ids[key] = event.event_id
else: else:
context.current_state_ids = context.prev_state_ids current_state_ids = prev_state_ids
context.state_group = yield self.store.store_state_group( state_group = yield self.store.store_state_group(
event.event_id, event.event_id,
event.room_id, event.room_id,
prev_group=None, prev_group=None,
delta_ids=None, delta_ids=None,
current_state_ids=context.current_state_ids, current_state_ids=current_state_ids,
)
context = EventContext(
state_group=state_group,
current_state_ids=current_state_ids,
prev_state_ids=prev_state_ids,
) )
context.prev_state_events = []
defer.returnValue(context) defer.returnValue(context)
logger.debug("calling resolve_state_groups from compute_event_context") logger.debug("calling resolve_state_groups from compute_event_context")
@ -262,47 +268,47 @@ class StateHandler(object):
event.room_id, [e for e, _ in event.prev_events], event.room_id, [e for e, _ in event.prev_events],
) )
curr_state = entry.state prev_state_ids = entry.state
prev_group = None
delta_ids = None
context = EventContext()
context.prev_state_ids = curr_state
if event.is_state(): if event.is_state():
# If this is a state event then we need to create a new state # If this is a state event then we need to create a new state
# group for the state after this event. # group for the state after this event.
key = (event.type, event.state_key) key = (event.type, event.state_key)
if key in context.prev_state_ids: if key in prev_state_ids:
replaces = context.prev_state_ids[key] replaces = prev_state_ids[key]
event.unsigned["replaces_state"] = replaces event.unsigned["replaces_state"] = replaces
context.current_state_ids = dict(context.prev_state_ids) current_state_ids = dict(prev_state_ids)
context.current_state_ids[key] = event.event_id current_state_ids[key] = event.event_id
if entry.state_group: if entry.state_group:
# If the state at the event has a state group assigned then # If the state at the event has a state group assigned then
# we can use that as the prev group # we can use that as the prev group
context.prev_group = entry.state_group prev_group = entry.state_group
context.delta_ids = { delta_ids = {
key: event.event_id key: event.event_id
} }
elif entry.prev_group: elif entry.prev_group:
# If the state at the event only has a prev group, then we can # If the state at the event only has a prev group, then we can
# use that as a prev group too. # use that as a prev group too.
context.prev_group = entry.prev_group prev_group = entry.prev_group
context.delta_ids = dict(entry.delta_ids) delta_ids = dict(entry.delta_ids)
context.delta_ids[key] = event.event_id delta_ids[key] = event.event_id
context.state_group = yield self.store.store_state_group( state_group = yield self.store.store_state_group(
event.event_id, event.event_id,
event.room_id, event.room_id,
prev_group=context.prev_group, prev_group=prev_group,
delta_ids=context.delta_ids, delta_ids=delta_ids,
current_state_ids=context.current_state_ids, current_state_ids=current_state_ids,
) )
else: else:
context.current_state_ids = context.prev_state_ids current_state_ids = prev_state_ids
context.prev_group = entry.prev_group prev_group = entry.prev_group
context.delta_ids = entry.delta_ids delta_ids = entry.delta_ids
if entry.state_group is None: if entry.state_group is None:
entry.state_group = yield self.store.store_state_group( entry.state_group = yield self.store.store_state_group(
@ -310,13 +316,20 @@ class StateHandler(object):
event.room_id, event.room_id,
prev_group=entry.prev_group, prev_group=entry.prev_group,
delta_ids=entry.delta_ids, delta_ids=entry.delta_ids,
current_state_ids=context.current_state_ids, current_state_ids=current_state_ids,
) )
entry.state_id = entry.state_group entry.state_id = entry.state_group
context.state_group = entry.state_group state_group = entry.state_group
context = EventContext(
state_group=state_group,
current_state_ids=current_state_ids,
prev_state_ids=prev_state_ids,
prev_group=prev_group,
delta_ids=delta_ids,
)
context.prev_state_events = []
defer.returnValue(context) defer.returnValue(context)
@defer.inlineCallbacks @defer.inlineCallbacks