Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 29 additions & 22 deletions matrix_client/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@
# 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 logging
import re
from uuid import uuid4

from .checks import check_room_id
from .user import User
from .errors import MatrixRequestError

logger = logging.getLogger(__name__)


class Room(object):
"""Call room-specific functions after joining a room from the client.
Expand Down Expand Up @@ -643,28 +646,32 @@ def _process_state_event(self, state_event):

# Don't keep track of room state if caching turned off
if clevel >= 0:
if etype == "m.room.name":
self.name = econtent.get("name")
elif etype == "m.room.canonical_alias":
self.canonical_alias = econtent.get("alias")
elif etype == "m.room.topic":
self.topic = econtent.get("topic")
elif etype == "m.room.aliases":
self.aliases = econtent.get("aliases")
elif etype == "m.room.join_rules":
self.invite_only = econtent["join_rule"] == "invite"
elif etype == "m.room.guest_access":
self.guest_access = econtent["guest_access"] == "can_join"
elif etype == "m.room.encryption":
if econtent.get("algorithm") == "m.megolm.v1.aes-sha2":
self.encrypted = True
elif etype == "m.room.member" and clevel == clevel.ALL:
# tracking room members can be large e.g. #matrix:matrix.org
if econtent["membership"] == "join":
user_id = state_event["state_key"]
self._add_member(user_id, econtent.get("displayname"))
elif econtent["membership"] in ("leave", "kick", "invite"):
self._members.pop(state_event["state_key"], None)
try:
if etype == "m.room.name":
self.name = econtent.get("name")
elif etype == "m.room.canonical_alias":
self.canonical_alias = econtent.get("alias")
elif etype == "m.room.topic":
self.topic = econtent.get("topic")
elif etype == "m.room.aliases":
self.aliases = econtent.get("aliases")
elif etype == "m.room.join_rules":
self.invite_only = econtent["join_rule"] == "invite"
elif etype == "m.room.guest_access":
self.guest_access = econtent["guest_access"] == "can_join"
elif etype == "m.room.encryption":
if econtent.get("algorithm") == "m.megolm.v1.aes-sha2":
self.encrypted = True
elif etype == "m.room.member" and clevel == clevel.ALL:
# tracking room members can be large e.g. #matrix:matrix.org
if econtent["membership"] == "join":
user_id = state_event["state_key"]
self._add_member(user_id, econtent.get("displayname"))
elif econtent["membership"] in ("leave", "kick", "invite"):
self._members.pop(state_event["state_key"], None)
except KeyError:
logger.exception("Unable to parse state event %s, passing over.",
state_event['event_id'])

for listener in self.state_listeners:
if (
Expand Down
10 changes: 9 additions & 1 deletion test/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ def test_state_event():

ev = {
"type": "m.room.name",
"content": {}
"content": {},
"event_id": "$10000000000000AAAAA:matrix.org"
}

room._process_state_event(ev)
Expand Down Expand Up @@ -152,6 +153,13 @@ def test_state_event():
room._process_state_event(ev)
assert room.guest_access

# test malformed event (check does not throw exception)
room.guest_access = False
ev["type"] = "m.room.guest_access"
ev["content"] = {}
room._process_state_event(ev)
assert not room.guest_access

# test encryption
room.encrypted = False
ev["type"] = "m.room.encryption"
Expand Down