1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
# Copyright (c) 2022 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 NewType, Union
from ..primitive import JSON
from ..util import Obj, deserializer
from .account_data import AccountDataEvent, AccountDataEventContent
from .base import EventType, GenericEvent
from .beeper import BeeperMessageStatusEvent, BeeperMessageStatusEventContent
from .encrypted import EncryptedEvent, EncryptedEventContent
from .ephemeral import (
EphemeralEvent,
PresenceEvent,
ReceiptEvent,
ReceiptEventContent,
TypingEvent,
TypingEventContent,
)
from .message import MessageEvent, MessageEventContent
from .reaction import ReactionEvent, ReactionEventContent
from .redaction import RedactionEvent, RedactionEventContent
from .state import StateEvent, StateEventContent
from .to_device import ASToDeviceEvent, ToDeviceEvent, ToDeviceEventContent
from .voip import CallEvent, CallEventContent, type_to_class as voip_types
Event = NewType(
"Event",
Union[
MessageEvent,
ReactionEvent,
RedactionEvent,
StateEvent,
TypingEvent,
ReceiptEvent,
PresenceEvent,
EncryptedEvent,
ToDeviceEvent,
ASToDeviceEvent,
CallEvent,
BeeperMessageStatusEvent,
GenericEvent,
],
)
EventContent = Union[
MessageEventContent,
RedactionEventContent,
ReactionEventContent,
StateEventContent,
AccountDataEventContent,
ReceiptEventContent,
TypingEventContent,
EncryptedEventContent,
ToDeviceEventContent,
CallEventContent,
BeeperMessageStatusEventContent,
Obj,
]
@deserializer(Event)
def deserialize_event(data: JSON) -> Event:
event_type = EventType.find(data.get("type", None))
if event_type == EventType.ROOM_MESSAGE:
return MessageEvent.deserialize(data)
elif event_type == EventType.STICKER:
data.get("content", {})["msgtype"] = "m.sticker"
return MessageEvent.deserialize(data)
elif event_type == EventType.REACTION:
return ReactionEvent.deserialize(data)
elif event_type == EventType.ROOM_REDACTION:
return RedactionEvent.deserialize(data)
elif event_type == EventType.ROOM_ENCRYPTED:
return EncryptedEvent.deserialize(data)
elif event_type in voip_types.keys():
return CallEvent.deserialize(data, event_type=event_type)
elif event_type.is_to_device:
return ToDeviceEvent.deserialize(data)
elif event_type.is_state:
return StateEvent.deserialize(data)
elif event_type.is_account_data:
return AccountDataEvent.deserialize(data)
elif event_type.is_ephemeral:
return EphemeralEvent.deserialize(data)
elif event_type == EventType.BEEPER_MESSAGE_STATUS:
return BeeperMessageStatusEvent.deserialize(data)
else:
return GenericEvent.deserialize(data)
setattr(Event, "deserialize", deserialize_event)
|