Skip to content

Commit

Permalink
m.tag implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
cy8aer committed Jul 11, 2019
1 parent a73c7c3 commit ca82783
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -11,3 +11,4 @@ doc/build
matrix_nio.egg-info/
dist
doc/html
*.swp
26 changes: 26 additions & 0 deletions nio/events/account_data.py
Expand Up @@ -46,6 +46,8 @@ def parse_event(

if event_dict["type"] == "m.fully_read":
return FullyReadEvent.from_dict(event_dict)
elif event_dict["type"] == "m.tag":
return TagEvent.from_dict(event_dict)

return UnknownAccountDataEvent.from_dict(event_dict)

Expand Down Expand Up @@ -76,6 +78,30 @@ def from_dict(cls, event_dict):
)


@attr.s
class TagEvent(AccountDataEvent):
"""Informs the client tags of a room. Room tags may include
- m.favourite for favourite rooms
- m.lowpriority for low priority room
A tag may have an optinal order float 0 <= order <=1 for position
this room to other rooms.
Attributes:
tags (Dict[string, Optional[Dict[string, float]]): The tag dictionary.
"""

tags = attr.ib()

@classmethod
@verify(Schemas.tags)
def from_dict(cls, event_dict):
"""Construct a TagEvent from a dictionary."""
content = event_dict.pop("content")
return cls(
content["tags"]
)


@attr.s
class UnknownAccountDataEvent(AccountDataEvent):
"""Account data event of an unknown type.
Expand Down
26 changes: 26 additions & 0 deletions nio/schemas.py
Expand Up @@ -1262,6 +1262,32 @@ class Schemas(object):
],
}

tags = {
"type": "object",
"properties": {
"type": {"type": "string"},
"content": {
"type": "object",
"properties": {
"tags": {"type:": "object"},
"properties": {
"order": {"type": "float"}
},
"required": [
"type"
],
},
"required": [
"tags",
],
}
},
"required": [
"type",
"content",
],
}

upload = {
"type": "object",
"properties": {"content_uri": {"type": "string"}},
Expand Down
10 changes: 10 additions & 0 deletions tests/data/events/tag.json
@@ -0,0 +1,10 @@
{
"content": {
"tags": {
"u.work": {
"order": 0.9
}
}
},
"type": "m.tag"
}
6 changes: 6 additions & 0 deletions tests/event_test.py
Expand Up @@ -77,6 +77,12 @@ def test_room_avatar_event(self):
event = RoomAvatarEvent.from_dict(parsed_dict)
assert isinstance(event, RoomAvatarEvent)

def test_tag_event(self):
parsed_dict = TestClass._load_response(
"tests/data/events/tag.json")
event = AccountDataEvent.parse_event(parsed_dict)
assert isinstance(event, TagEvent)

def test_name_event(self):
parsed_dict = TestClass._load_response(
"tests/data/events/name.json")
Expand Down

0 comments on commit ca82783

Please sign in to comment.