forked from fair/matrix-puppeteer-line
Inbound images
This commit is contained in:
parent
04c09cea1f
commit
a5d9cc5ebc
|
@ -23,7 +23,7 @@ from mautrix.appservice import AppService, IntentAPI
|
||||||
from mautrix.bridge import BasePortal, NotificationDisabler
|
from mautrix.bridge import BasePortal, NotificationDisabler
|
||||||
from mautrix.types import (EventID, MessageEventContent, RoomID, EventType, MessageType,
|
from mautrix.types import (EventID, MessageEventContent, RoomID, EventType, MessageType,
|
||||||
TextMessageEventContent, MediaMessageEventContent, Membership,
|
TextMessageEventContent, MediaMessageEventContent, Membership,
|
||||||
ContentURI, EncryptedFile)
|
ContentURI, EncryptedFile, ImageInfo)
|
||||||
from mautrix.errors import MatrixError
|
from mautrix.errors import MatrixError
|
||||||
from mautrix.util.simple_lock import SimpleLock
|
from mautrix.util.simple_lock import SimpleLock
|
||||||
from mautrix.util.network_retry import call_with_net_retry
|
from mautrix.util.network_retry import call_with_net_retry
|
||||||
|
@ -194,11 +194,10 @@ class Portal(DBPortal, BasePortal):
|
||||||
return
|
return
|
||||||
|
|
||||||
event_id = None
|
event_id = None
|
||||||
if evt.image:
|
if evt.image_url:
|
||||||
content = await self._handle_remote_photo(source, intent, evt)
|
content = await self._handle_remote_photo(source, intent, evt)
|
||||||
if content:
|
|
||||||
event_id = await self._send_message(intent, content, timestamp=evt.timestamp)
|
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)
|
content = TextMessageEventContent(msgtype=MessageType.TEXT, body=evt.text)
|
||||||
event_id = await self._send_message(intent, content, timestamp=evt.timestamp)
|
event_id = await self._send_message(intent, content, timestamp=evt.timestamp)
|
||||||
if event_id:
|
if event_id:
|
||||||
|
@ -209,12 +208,22 @@ class Portal(DBPortal, BasePortal):
|
||||||
|
|
||||||
async def _handle_remote_photo(self, source: 'u.User', intent: IntentAPI, message: Message
|
async def _handle_remote_photo(self, source: 'u.User', intent: IntentAPI, message: Message
|
||||||
) -> Optional[MediaMessageEventContent]:
|
) -> Optional[MediaMessageEventContent]:
|
||||||
# TODO
|
resp = await source.client.read_image(message.image_url)
|
||||||
pass
|
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
|
decryption_info = None
|
||||||
if self.encrypted and encrypt_attachment:
|
if self.encrypted and encrypt_attachment:
|
||||||
data, decryption_info = encrypt_attachment(data)
|
data, decryption_info = encrypt_attachment(data)
|
||||||
|
|
|
@ -59,7 +59,7 @@ class Message(SerializableAttrs['Message']):
|
||||||
sender: Optional[Participant]
|
sender: Optional[Participant]
|
||||||
timestamp: int = None
|
timestamp: int = None
|
||||||
text: Optional[str] = None
|
text: Optional[str] = None
|
||||||
image: Optional[str] = None
|
image_url: Optional[str] = None
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
|
|
|
@ -211,8 +211,9 @@ class MautrixController {
|
||||||
// TODO Use "Inner" or not?
|
// TODO Use "Inner" or not?
|
||||||
messageData.text = messageElement.querySelector(".mdRGT07MsgTextInner")?.innerText
|
messageData.text = messageElement.querySelector(".mdRGT07MsgTextInner")?.innerText
|
||||||
} else if (messageElement.classList.contains("mdRGT07Image")) {
|
} else if (messageElement.classList.contains("mdRGT07Image")) {
|
||||||
// TODO Doesn't this need to be a URL?
|
// TODO Probably need a MutationObserver to wait for image to load.
|
||||||
messageData.image = true
|
// Should also catch "#_chat_message_image_failure"
|
||||||
|
messageData.image_url = messageElement.querySelector(".mdRGT07MsgImg > img")?.src
|
||||||
}
|
}
|
||||||
return messageData
|
return messageData
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue