Fix some test failures when frozen_dicts are enabled (#6642)

Fixes #4026
This commit is contained in:
Richard van der Hoff 2020-01-06 15:22:46 +00:00 committed by GitHub
parent 9f6c1befbb
commit ba897a7590
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 20 additions and 11 deletions

1
changelog.d/6642.misc Normal file
View File

@ -0,0 +1 @@
Fix errors when frozen_dicts are enabled.

View File

@ -14,7 +14,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import collections.abc
import hashlib import hashlib
import logging import logging
@ -40,8 +40,11 @@ def check_event_content_hash(event, hash_algorithm=hashlib.sha256):
# some malformed events lack a 'hashes'. Protect against it being missing # some malformed events lack a 'hashes'. Protect against it being missing
# or a weird type by basically treating it the same as an unhashed event. # or a weird type by basically treating it the same as an unhashed event.
hashes = event.get("hashes") hashes = event.get("hashes")
if not isinstance(hashes, dict): # nb it might be a frozendict or a dict
raise SynapseError(400, "Malformed 'hashes'", Codes.UNAUTHORIZED) if not isinstance(hashes, collections.abc.Mapping):
raise SynapseError(
400, "Malformed 'hashes': %s" % (type(hashes),), Codes.UNAUTHORIZED
)
if name not in hashes: if name not in hashes:
raise SynapseError( raise SynapseError(

View File

@ -16,7 +16,7 @@
# limitations under the License. # limitations under the License.
"""Contains functions for performing events on rooms.""" """Contains functions for performing events on rooms."""
import copy
import itertools import itertools
import logging import logging
import math import math
@ -368,13 +368,16 @@ class RoomCreationHandler(BaseHandler):
# Raise the requester's power level in the new room if necessary # Raise the requester's power level in the new room if necessary
current_power_level = power_levels["users"][user_id] current_power_level = power_levels["users"][user_id]
if current_power_level < needed_power_level: if current_power_level < needed_power_level:
# Perform a deepcopy in order to not modify the original power levels in a # make sure we copy the event content rather than overwriting it.
# room, as its contents are preserved as the state for the old room later on # note that if frozen_dicts are enabled, `power_levels` will be a frozen
new_power_levels = copy.deepcopy(power_levels) # dict so we can't just copy.deepcopy it.
initial_state[(EventTypes.PowerLevels, "")] = new_power_levels
# Assign this power level to the requester new_power_levels = {k: v for k, v in power_levels.items() if k != "users"}
new_power_levels["users"] = {
k: v for k, v in power_levels.get("users", {}).items() if k != user_id
}
new_power_levels["users"][user_id] = needed_power_level new_power_levels["users"][user_id] = needed_power_level
initial_state[(EventTypes.PowerLevels, "")] = new_power_levels
yield self._send_events_for_new_room( yield self._send_events_for_new_room(
requester, requester,

View File

@ -507,6 +507,8 @@ class RoomMemberHandler(object):
Returns: Returns:
Deferred Deferred
""" """
logger.info("Transferring room state from %s to %s", old_room_id, room_id)
# Find all local users that were in the old room and copy over each user's state # Find all local users that were in the old room and copy over each user's state
users = yield self.store.get_users_in_room(old_room_id) users = yield self.store.get_users_in_room(old_room_id)
yield self.copy_user_state_on_room_upgrade(old_room_id, room_id, users) yield self.copy_user_state_on_room_upgrade(old_room_id, room_id, users)

View File

@ -12,7 +12,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import collections.abc
import logging import logging
from collections import namedtuple from collections import namedtuple
from typing import Iterable, Tuple from typing import Iterable, Tuple
@ -107,7 +107,7 @@ class StateGroupWorkerStore(EventsWorkerStore, SQLBaseStore):
predecessor = create_event.content.get("predecessor", None) predecessor = create_event.content.get("predecessor", None)
# Ensure the key is a dictionary # Ensure the key is a dictionary
if not isinstance(predecessor, dict): if not isinstance(predecessor, collections.abc.Mapping):
return None return None
return predecessor return predecessor