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

Commit

Permalink
Revert "Merge pull request #6790 from matrix-org/rav/msc2260.1"
Browse files Browse the repository at this point in the history
This reverts commit 281551f, reversing
changes made to 6b9e101.
  • Loading branch information
clokep committed Mar 13, 2020
1 parent 77d0a45 commit d43db60
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 104 deletions.
37 changes: 1 addition & 36 deletions synapse/events/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import collections

import re
from typing import Mapping, Union

from six import string_types

Expand Down Expand Up @@ -415,37 +414,3 @@ def serialize_events(self, events, time_now, **kwargs):
return yieldable_gather_results(
self.serialize_event, events, time_now=time_now, **kwargs
)


def copy_power_levels_contents(
old_power_levels: Mapping[str, Union[int, Mapping[str, int]]]
):
"""Copy the content of a power_levels event, unfreezing frozendicts along the way
Raises:
TypeError if the input does not look like a valid power levels event content
"""
if not isinstance(old_power_levels, collections.Mapping):
raise TypeError("Not a valid power-levels content: %r" % (old_power_levels,))

power_levels = {}
for k, v in old_power_levels.items():

if isinstance(v, int):
power_levels[k] = v
continue

if isinstance(v, collections.Mapping):
power_levels[k] = h = {}
for k1, v1 in v.items():
# we should only have one level of nesting
if not isinstance(v1, int):
raise TypeError(
"Invalid power_levels value for %s.%s: %r" % (k, k1, v1)
)
h[k1] = v1
continue

raise TypeError("Invalid power_levels value for %s: %r" % (k, v))

return power_levels
39 changes: 13 additions & 26 deletions synapse/handlers/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
from synapse.api.constants import EventTypes, JoinRules, RoomCreationPreset
from synapse.api.errors import AuthError, Codes, NotFoundError, StoreError, SynapseError
from synapse.api.room_versions import KNOWN_ROOM_VERSIONS, RoomVersion
from synapse.events.utils import copy_power_levels_contents
from synapse.http.endpoint import parse_and_validate_server_name
from synapse.storage.state import StateFilter
from synapse.types import (
Expand Down Expand Up @@ -292,24 +291,14 @@ def _update_upgraded_room_pls(
except AuthError as e:
logger.warning("Unable to update PLs in old room: %s", e)

new_pl_content = copy_power_levels_contents(old_room_pl_state.content)

# pre-msc2260 rooms may not have the right setting for aliases. If no other
# value is set, set it now.
events_default = new_pl_content.get("events_default", 0)
new_pl_content.setdefault("events", {}).setdefault(
EventTypes.Aliases, events_default
)

logger.debug("Setting correct PLs in new room to %s", new_pl_content)
yield self.event_creation_handler.create_and_send_nonmember_event(
requester,
{
"type": EventTypes.PowerLevels,
"state_key": "",
"room_id": new_room_id,
"sender": requester.user.to_string(),
"content": new_pl_content,
"content": old_room_pl_state.content,
},
ratelimit=False,
)
Expand Down Expand Up @@ -382,15 +371,6 @@ def clone_existing_room(
if old_event:
initial_state[k] = old_event.content

# deep-copy the power-levels event before we start modifying it
# note that if frozen_dicts are enabled, `power_levels` will be a frozen
# dict so we can't just copy.deepcopy it.
initial_state[
(EventTypes.PowerLevels, "")
] = power_levels = copy_power_levels_contents(
initial_state[(EventTypes.PowerLevels, "")]
)

# Resolve the minimum power level required to send any state event
# We will give the upgrading user this power level temporarily (if necessary) such that
# they are able to copy all of the state events over, then revert them back to their
Expand All @@ -399,6 +379,8 @@ def clone_existing_room(
# Copy over user power levels now as this will not be possible with >100PL users once
# the room has been created

power_levels = initial_state[(EventTypes.PowerLevels, "")]

# Calculate the minimum power level needed to clone the room
event_power_levels = power_levels.get("events", {})
state_default = power_levels.get("state_default", 0)
Expand All @@ -408,7 +390,16 @@ def clone_existing_room(
# Raise the requester's power level in the new room if necessary
current_power_level = power_levels["users"][user_id]
if current_power_level < needed_power_level:
power_levels["users"][user_id] = needed_power_level
# make sure we copy the event content rather than overwriting it.
# note that if frozen_dicts are enabled, `power_levels` will be a frozen
# dict so we can't just copy.deepcopy it.

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
initial_state[(EventTypes.PowerLevels, "")] = new_power_levels

yield self._send_events_for_new_room(
requester,
Expand Down Expand Up @@ -814,10 +805,6 @@ def send(etype, content, **kwargs):
EventTypes.RoomHistoryVisibility: 100,
EventTypes.CanonicalAlias: 50,
EventTypes.RoomAvatar: 50,
# MSC2260: Allow everybody to send alias events by default
# This will be reudundant on pre-MSC2260 rooms, since the
# aliases event is special-cased.
EventTypes.Aliases: 0,
EventTypes.Tombstone: 100,
EventTypes.ServerACL: 100,
},
Expand Down
43 changes: 1 addition & 42 deletions tests/events/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@

from synapse.api.room_versions import RoomVersions
from synapse.events import make_event_from_dict
from synapse.events.utils import (
copy_power_levels_contents,
prune_event,
serialize_event,
)
from synapse.util.frozenutils import freeze
from synapse.events.utils import prune_event, serialize_event

from tests import unittest

Expand Down Expand Up @@ -278,39 +273,3 @@ def test_event_fields_fail_if_fields_not_str(self):
self.serialize(
MockEvent(room_id="!foo:bar", content={"foo": "bar"}), ["room_id", 4]
)


class CopyPowerLevelsContentTestCase(unittest.TestCase):
def setUp(self) -> None:
self.test_content = {
"ban": 50,
"events": {"m.room.name": 100, "m.room.power_levels": 100},
"events_default": 0,
"invite": 50,
"kick": 50,
"notifications": {"room": 20},
"redact": 50,
"state_default": 50,
"users": {"@example:localhost": 100},
"users_default": 0,
}

def _test(self, input):
a = copy_power_levels_contents(input)

self.assertEqual(a["ban"], 50)
self.assertEqual(a["events"]["m.room.name"], 100)

# make sure that changing the copy changes the copy and not the orig
a["ban"] = 10
a["events"]["m.room.power_levels"] = 20

self.assertEqual(input["ban"], 50)
self.assertEqual(input["events"]["m.room.power_levels"], 100)

def test_unfrozen(self):
self._test(self.test_content)

def test_frozen(self):
input = freeze(self.test_content)
self._test(input)

0 comments on commit d43db60

Please sign in to comment.