Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support MSC2530 fields in media event types #170

Merged
merged 1 commit into from
Mar 28, 2024
Merged
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
55 changes: 28 additions & 27 deletions mautrix/types/event/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,33 +271,6 @@ class LocationInfo(SerializableAttrs):
# region Event content


@dataclass
class MediaMessageEventContent(BaseMessageEventContent, SerializableAttrs):
"""The content of a media message event (m.image, m.audio, m.video, m.file)"""

url: Optional[ContentURI] = None
info: Optional[MediaInfo] = None
file: Optional[EncryptedFile] = None

@staticmethod
@deserializer(MediaInfo)
@deserializer(Optional[MediaInfo])
def deserialize_info(data: JSON) -> MediaInfo:
if not isinstance(data, dict):
return Obj()
msgtype = data.pop("__mautrix_msgtype", None)
if msgtype == "m.image" or msgtype == "m.sticker":
return ImageInfo.deserialize(data)
elif msgtype == "m.video":
return VideoInfo.deserialize(data)
elif msgtype == "m.audio":
return AudioInfo.deserialize(data)
elif msgtype == "m.file":
return FileInfo.deserialize(data)
else:
return Obj(**data)


@dataclass
class LocationMessageEventContent(BaseMessageEventContent, SerializableAttrs):
geo_uri: str = None
Expand Down Expand Up @@ -364,6 +337,34 @@ def _trim_reply_fallback_html(self) -> None:
self.formatted_body = html_reply_fallback_regex.sub("", self.formatted_body)


@dataclass
class MediaMessageEventContent(TextMessageEventContent, SerializableAttrs):
"""The content of a media message event (m.image, m.audio, m.video, m.file)"""

url: Optional[ContentURI] = None
info: Optional[MediaInfo] = None
file: Optional[EncryptedFile] = None
filename: Optional[str] = None

@staticmethod
@deserializer(MediaInfo)
@deserializer(Optional[MediaInfo])
def deserialize_info(data: JSON) -> MediaInfo:
if not isinstance(data, dict):
return Obj()
msgtype = data.pop("__mautrix_msgtype", None)
if msgtype == "m.image" or msgtype == "m.sticker":
return ImageInfo.deserialize(data)
elif msgtype == "m.video":
return VideoInfo.deserialize(data)
elif msgtype == "m.audio":
return AudioInfo.deserialize(data)
elif msgtype == "m.file":
return FileInfo.deserialize(data)
else:
return Obj(**data)


MessageEventContent = Union[
TextMessageEventContent, MediaMessageEventContent, LocationMessageEventContent, Obj
]
Expand Down