Inbound images

This commit is contained in:
Andrew Ferrazzutti 2021-03-27 01:12:43 -04:00
parent 04c09cea1f
commit a5d9cc5ebc
3 changed files with 23 additions and 13 deletions

View File

@ -23,7 +23,7 @@ from mautrix.appservice import AppService, IntentAPI
from mautrix.bridge import BasePortal, NotificationDisabler
from mautrix.types import (EventID, MessageEventContent, RoomID, EventType, MessageType,
TextMessageEventContent, MediaMessageEventContent, Membership,
ContentURI, EncryptedFile)
ContentURI, EncryptedFile, ImageInfo)
from mautrix.errors import MatrixError
from mautrix.util.simple_lock import SimpleLock
from mautrix.util.network_retry import call_with_net_retry
@ -194,11 +194,10 @@ class Portal(DBPortal, BasePortal):
return
event_id = None
if evt.image:
if evt.image_url:
content = await self._handle_remote_photo(source, intent, evt)
if content:
event_id = await self._send_message(intent, content, timestamp=evt.timestamp)
if evt.text and not evt.text.isspace():
elif evt.text and not evt.text.isspace():
content = TextMessageEventContent(msgtype=MessageType.TEXT, body=evt.text)
event_id = await self._send_message(intent, content, timestamp=evt.timestamp)
if event_id:
@ -209,12 +208,22 @@ class Portal(DBPortal, BasePortal):
async def _handle_remote_photo(self, source: 'u.User', intent: IntentAPI, message: Message
) -> Optional[MediaMessageEventContent]:
# TODO
pass
resp = await source.client.read_image(message.image_url)
media_info = await self._reupload_remote_media(resp.data, intent, resp.mime)
return MediaMessageEventContent(url=media_info.mxc, file=media_info.decryption_info,
msgtype=MessageType.IMAGE, body=media_info.file_name,
info=ImageInfo(mimetype=media_info.mime_type, size=media_info.size))
async def _reupload_remote_media(self, data: bytes, intent: IntentAPI,
mime_type: str = None, file_name: str = None
) -> ReuploadedMediaInfo:
if not mime_type:
mime_type = magic.from_buffer(data, mime=True)
upload_mime_type = mime_type
if not file_name:
file_name = f"image{mimetypes.guess_extension(mime_type)}"
upload_file_name = file_name
async def _reupload_remote_media(self, data: bytes, intent: IntentAPI) -> ReuploadedMediaInfo:
upload_mime_type = mime_type = magic.from_buffer(data, mime=True)
upload_file_name = file_name = f"image{mimetypes.guess_extension(mime_type)}"
decryption_info = None
if self.encrypted and encrypt_attachment:
data, decryption_info = encrypt_attachment(data)

View File

@ -59,7 +59,7 @@ class Message(SerializableAttrs['Message']):
sender: Optional[Participant]
timestamp: int = None
text: Optional[str] = None
image: Optional[str] = None
image_url: Optional[str] = None
@dataclass

View File

@ -211,8 +211,9 @@ class MautrixController {
// TODO Use "Inner" or not?
messageData.text = messageElement.querySelector(".mdRGT07MsgTextInner")?.innerText
} else if (messageElement.classList.contains("mdRGT07Image")) {
// TODO Doesn't this need to be a URL?
messageData.image = true
// TODO Probably need a MutationObserver to wait for image to load.
// Should also catch "#_chat_message_image_failure"
messageData.image_url = messageElement.querySelector(".mdRGT07MsgImg > img")?.src
}
return messageData
}