Skip to content

Commit

Permalink
Support MSC2530 fields in media event types
Browse files Browse the repository at this point in the history
Add `filename`, `format` and `formatted_body` to media event content
types.

Signed-off-by: Joe Groocock <[email protected]>
  • Loading branch information
frebib committed Mar 22, 2024
1 parent 47e2157 commit 2544238
Showing 1 changed file with 28 additions and 27 deletions.
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

0 comments on commit 2544238

Please sign in to comment.