summary refs log tree commit diff
path: root/synapse/state.py
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2014-09-12 17:11:00 +0100
committerErik Johnston <erik@matrix.org>2014-09-12 17:11:09 +0100
commit39e3fc69e5a190371aa6936bfea57e9f8bd5255b (patch)
tree9039a84cc75dd1f0ac17c310907728c8815e8c96 /synapse/state.py
parentFix bug where we incorrectly removed a remote host from the list of hosts in ... (diff)
downloadsynapse-39e3fc69e5a190371aa6936bfea57e9f8bd5255b.tar.xz
Make the state resolution use actual power levels rather than taking them from a Pdu key.
Diffstat (limited to 'synapse/state.py')
-rw-r--r--synapse/state.py46
1 files changed, 35 insertions, 11 deletions
diff --git a/synapse/state.py b/synapse/state.py
index 0cc1344d51..9db84c9b5c 100644
--- a/synapse/state.py
+++ b/synapse/state.py
@@ -115,6 +115,8 @@ class StateHandler(object):
 
         is_new = yield self._handle_new_state(new_pdu)
 
+        logger.debug("is_new: %s %s %s", is_new, new_pdu.pdu_id, new_pdu.origin)
+
         if is_new:
             yield self.store.update_current_state(
                 pdu_id=new_pdu.pdu_id,
@@ -187,11 +189,12 @@ class StateHandler(object):
             # We didn't find a common ancestor. This is probably fine.
             pass
 
-        result = self._do_conflict_res(
+        result = yield self._do_conflict_res(
             new_branch, current_branch, common_ancestor
         )
         defer.returnValue(result)
 
+    @defer.inlineCallbacks
     def _do_conflict_res(self, new_branch, current_branch, common_ancestor):
         conflict_res = [
             self._do_power_level_conflict_res,
@@ -200,7 +203,8 @@ class StateHandler(object):
         ]
 
         for algo in conflict_res:
-            new_res, curr_res = algo(
+            new_res, curr_res = yield defer.maybeDeferred(
+                algo,
                 new_branch, current_branch, common_ancestor
             )
 
@@ -211,19 +215,39 @@ class StateHandler(object):
 
         raise Exception("Conflict resolution failed.")
 
+    @defer.inlineCallbacks
     def _do_power_level_conflict_res(self, new_branch, current_branch,
                                      common_ancestor):
-        max_power_new = max(
-            new_branch[:-1] if common_ancestor else new_branch,
-            key=lambda t: t.power_level
-        ).power_level
+        new_powers_deferreds = []
+        for e in new_branch[:-1] if common_ancestor else new_branch:
+            if hasattr(e, "user_id"):
+                new_powers_deferreds.append(
+                    self.store.get_power_level(e.context, e.user_id)
+                )
+
+        current_powers_deferreds = []
+        for e in current_branch[:-1] if common_ancestor else current_branch:
+            if hasattr(e, "user_id"):
+                current_powers_deferreds.append(
+                    self.store.get_power_level(e.context, e.user_id)
+                )
+
+        new_powers = yield defer.gatherResults(
+            new_powers_deferreds,
+            consumeErrors=True
+        )
 
-        max_power_current = max(
-            current_branch[:-1] if common_ancestor else current_branch,
-            key=lambda t: t.power_level
-        ).power_level
+        current_powers = yield defer.gatherResults(
+            current_powers_deferreds,
+            consumeErrors=True
+        )
+
+        max_power_new = max(new_powers)
+        max_power_current = max(current_powers)
 
-        return (max_power_new, max_power_current)
+        defer.returnValue(
+            (max_power_new, max_power_current)
+        )
 
     def _do_chain_length_conflict_res(self, new_branch, current_branch,
                                       common_ancestor):