# Copyright (c) 2018 Tulir Asokan
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from typing import Dict, Union
from attr import dataclass
import attr
from .....api import JSON
from ..util import SerializableAttrs, Obj, deserializer
from .base import EventType, BaseEvent
[docs]@dataclass
class RoomTagInfo(SerializableAttrs['RoomTagInfo']):
order: int = None
[docs]@dataclass
class RoomTagAccountDataEventContent(SerializableAttrs['RoomTagAccountDataEventContent']):
tags: Dict[str, RoomTagInfo] = attr.ib(default=None, metadata={"json": "tags"})
AccountDataEventContent = Union[RoomTagAccountDataEventContent, Obj]
account_data_event_content_map = {
EventType.TAG: RoomTagAccountDataEventContent
}
# TODO remaining account data event types
[docs]@dataclass
class AccountDataEvent(BaseEvent, SerializableAttrs['AccountDataEvent']):
content: AccountDataEventContent
[docs] @classmethod
def deserialize(cls, data: JSON) -> 'AccountDataEvent':
try:
data.get("content", {})["__mautrix_event_type"] = EventType.find(data.get("type"))
except ValueError:
return Obj(**data)
return super().deserialize(data)
[docs] @staticmethod
@deserializer(AccountDataEventContent)
def deserialize_content(data: JSON) -> AccountDataEventContent:
evt_type = data.pop("__mautrix_event_type", None)
content_type = account_data_event_content_map.get(evt_type, None)
if not content_type:
return Obj(**data)
return content_type.deserialize(data)