Inbound multiphoto, video, audio

This commit is contained in:
Andrew Ferrazzutti 2022-03-28 03:36:16 -04:00
parent ace4eefec7
commit 48ff7b3ceb
4 changed files with 207 additions and 44 deletions

View File

@ -27,7 +27,6 @@ class Attachment(SerializableAttrs):
shout: Optional[bool] = None shout: Optional[bool] = None
mentions: Optional[list[MentionStruct]] = None mentions: Optional[list[MentionStruct]] = None
urls: Optional[list[str]] = None urls: Optional[list[str]] = None
url: Optional[str] = None # NOTE Added since this may have replaced urls
@dataclass @dataclass

View File

@ -21,14 +21,20 @@ from . import Attachment
@dataclass @dataclass
class MediaKeyAttachment(Attachment): class MediaAttachment(Attachment):
k: str # NOTE Added to cover Attachments that need a url but might not have a key
url: str url: str
s: int s: int
@dataclass @dataclass
class PhotoAttachment(Attachment): class MediaKeyAttachment(MediaAttachment):
k: str
@dataclass
class PhotoAttachment(MediaKeyAttachment):
# NOTE Changed superclass from Attachment
w: int w: int
h: int h: int
thumbnailUrl: str thumbnailUrl: str
@ -48,11 +54,13 @@ class MultiPhotoAttachment(Attachment):
thumbnailUrls: list[str] thumbnailUrls: list[str]
thumbnailWidths: list[int] thumbnailWidths: list[int]
thumbnailHeights: list[int] thumbnailHeights: list[int]
sl: int sl: list[int] # NOTE Changed to a list
mtl: list[str] # NOTE Added
@dataclass @dataclass
class VideoAttachment(Attachment): class VideoAttachment(MediaAttachment):
# NOTE Changed superclass from Attachment
tk: str tk: str
w: int w: int
h: int h: int
@ -61,7 +69,8 @@ class VideoAttachment(Attachment):
@dataclass @dataclass
class FileAttachment(Attachment): class FileAttachment(MediaKeyAttachment):
# NOTE Changed superclass from Attachment
name: str name: str
size: int size: int
expire: int expire: int
@ -69,7 +78,8 @@ class FileAttachment(Attachment):
@dataclass @dataclass
class AudioAttachment(Attachment): class AudioAttachment(MediaKeyAttachment):
# NOTE Changed superclass from Attachment
d: int d: int
expire: Optional[int] = None expire: Optional[int] = None
@ -83,6 +93,7 @@ class LongTextAttachment(Attachment):
__all__ = [ __all__ = [
"MediaAttachment",
"MediaKeyAttachment", "MediaKeyAttachment",
"PhotoAttachment", "PhotoAttachment",
"MultiPhotoAttachment", "MultiPhotoAttachment",

View File

@ -35,6 +35,7 @@ from .attachment import (
FileAttachment, FileAttachment,
PostAttachment, PostAttachment,
ReplyAttachment, ReplyAttachment,
MultiPhotoAttachment,
) )
from .chat_type import ChatType, KnownChatType from .chat_type import ChatType, KnownChatType
@ -74,6 +75,7 @@ _attachment_type_map: dict[KnownChatType, Type[Attachment]] = {
KnownChatType.FILE: FileAttachment, KnownChatType.FILE: FileAttachment,
KnownChatType.POST: PostAttachment, KnownChatType.POST: PostAttachment,
KnownChatType.REPLY: ReplyAttachment, KnownChatType.REPLY: ReplyAttachment,
KnownChatType.MULTIPHOTO: MultiPhotoAttachment,
KnownChatType.OPEN_SCHEDULE: OpenScheduleAttachment, KnownChatType.OPEN_SCHEDULE: OpenScheduleAttachment,
KnownChatType.OPEN_VOTE: VoteAttachment, KnownChatType.OPEN_VOTE: VoteAttachment,
KnownChatType.OPEN_POST: PostAttachment, KnownChatType.OPEN_POST: PostAttachment,

View File

@ -42,6 +42,7 @@ from mautrix.types import (
FileInfo, FileInfo,
ImageInfo, ImageInfo,
LocationMessageEventContent, LocationMessageEventContent,
MediaInfo,
MediaMessageEventContent, MediaMessageEventContent,
Membership, Membership,
MessageEventContent, MessageEventContent,
@ -68,7 +69,13 @@ from .kt.types.channel.channel_info import ChannelInfo
from .kt.types.channel.channel_type import KnownChannelType, ChannelType from .kt.types.channel.channel_type import KnownChannelType, ChannelType
from .kt.types.chat import Chatlog, KnownChatType from .kt.types.chat import Chatlog, KnownChatType
from .kt.types.chat.attachment import ( from .kt.types.chat.attachment import (
Attachment,
AudioAttachment,
#FileAttachment,
MediaAttachment,
MultiPhotoAttachment,
PhotoAttachment, PhotoAttachment,
VideoAttachment,
) )
from .kt.client.types import ( from .kt.client.types import (
@ -183,14 +190,25 @@ class Portal(DBPortal, BasePortal):
NotificationDisabler.config_enabled = cls.config["bridge.backfill.disable_notifications"] NotificationDisabler.config_enabled = cls.config["bridge.backfill.disable_notifications"]
# TODO More # TODO More
cls._message_handler_type_map: dict[ cls._message_type_handler_map: dict[
KnownChatType, KnownChatType,
Callable[[Portal, u.User, IntentAPI, Chatlog], Awaitable[list[EventID]]] Callable[
[
Portal,
u.User,
IntentAPI,
Attachment | None,
int,
str | None
],
Awaitable[list[EventID]]
]
] = { ] = {
KnownChatType.TEXT: cls._handle_remote_text, KnownChatType.TEXT: cls._handle_remote_text,
KnownChatType.PHOTO: cls._handle_remote_photo, KnownChatType.PHOTO: cls._handle_remote_photo,
#KnownChatType.VIDEO: cls._handle_remote_video, KnownChatType.MULTIPHOTO: cls._handle_remote_multiphoto,
#KnownChatType.AUDIO: cls._handle_remote_audio, KnownChatType.VIDEO: cls._handle_remote_video,
KnownChatType.AUDIO: cls._handle_remote_audio,
#KnownChatType.FILE: cls._handle_remote_file, #KnownChatType.FILE: cls._handle_remote_file,
} }
@ -312,6 +330,7 @@ class Portal(DBPortal, BasePortal):
intent: IntentAPI, intent: IntentAPI,
*, *,
filename: str | None = None, filename: str | None = None,
mimetype: str | None,
encrypt: bool = False, encrypt: bool = False,
find_size: bool = False, find_size: bool = False,
convert_audio: bool = False, convert_audio: bool = False,
@ -325,18 +344,19 @@ class Portal(DBPortal, BasePortal):
if length > cls.matrix.media_config.upload_size: if length > cls.matrix.media_config.upload_size:
raise ValueError("File not available: too large") raise ValueError("File not available: too large")
data = await resp.read() data = await resp.read()
mime = magic.mimetype(data) if not mimetype:
if convert_audio and mime != "audio/ogg": mimetype = magic.mimetype(data)
if convert_audio and mimetype != "audio/ogg":
data = await ffmpeg.convert_bytes( data = await ffmpeg.convert_bytes(
data, ".ogg", output_args=("-c:a", "libopus"), input_mime=mime data, ".ogg", output_args=("-c:a", "libopus"), input_mime=mimetype
) )
mime = "audio/ogg" mimetype = "audio/ogg"
info = FileInfo(mimetype=mime, size=len(data)) info = FileInfo(mimetype=mimetype, size=len(data))
if Image and mime.startswith("image/") and find_size: if Image and mimetype.startswith("image/") and find_size:
with Image.open(BytesIO(data)) as img: with Image.open(BytesIO(data)) as img:
width, height = img.size width, height = img.size
info = ImageInfo(mimetype=mime, size=len(data), width=width, height=height) info = ImageInfo(mimetype=mimetype, size=len(data), width=width, height=height)
upload_mime_type = mime upload_mime_type = mimetype
decryption_info = None decryption_info = None
if encrypt and encrypt_attachment: if encrypt and encrypt_attachment:
data, decryption_info = encrypt_attachment(data) data, decryption_info = encrypt_attachment(data)
@ -807,7 +827,7 @@ class Portal(DBPortal, BasePortal):
data = await self.main_intent.download_media(message.url) data = await self.main_intent.download_media(message.url)
else: else:
raise NotImplementedError("No file or URL specified") raise NotImplementedError("No file or URL specified")
mime = message.info.mimetype or magic.mimetype(data) mimetype = message.info.mimetype or magic.mimetype(data)
""" TODO Replies """ TODO Replies
reply_to = None reply_to = None
if message.relates_to.rel_type == RelationType.REPLY: if message.relates_to.rel_type == RelationType.REPLY:
@ -822,7 +842,6 @@ class Portal(DBPortal, BasePortal):
""" """
filename = message.body filename = message.body
width, height = None, None width, height = None, None
# TODO Find out why/if stickers are always blank
if message.info in (MessageType.IMAGE, MessageType.STICKER, MessageType.VIDEO): if message.info in (MessageType.IMAGE, MessageType.STICKER, MessageType.VIDEO):
width = message.info.width width = message.info.width
height = message.info.height height = message.info.height
@ -834,7 +853,7 @@ class Portal(DBPortal, BasePortal):
filename, filename,
width=width, width=width,
height=height, height=height,
ext=guess_extension(mime)[1:], ext=guess_extension(mimetype)[1:],
# TODO # TODO
#reply_to=reply_to, #reply_to=reply_to,
) )
@ -977,16 +996,24 @@ class Portal(DBPortal, BasePortal):
await intent.ensure_joined(self.mxid) await intent.ensure_joined(self.mxid)
self._backfill_leave.add(intent) self._backfill_leave.add(intent)
event_ids = [] handler = self._message_type_handler_map.get(message.type)
handler = self._message_handler_type_map.get(message.type)
if not handler: if not handler:
self.log.warning(f"No handler for message type {message.type}, falling back to text") self.log.warning(f"No handler for message type {message.type}, falling back to text")
handler = Portal._handle_remote_text handler = Portal._handle_remote_text
event_ids += await handler(self, source, intent, message) event_ids = [
event_id for event_id in
await handler(
self,
source,
intent,
message.attachment,
message.sendAt,
message.text)
if event_id
]
if not event_ids: if not event_ids:
self.log.warning(f"Unhandled KakaoTalk message {message.logId}") self.log.warning(f"Unhandled KakaoTalk message {message.logId}")
return return
event_ids = [event_id for event_id in event_ids if event_id]
self.log.debug(f"Handled KakaoTalk message {message.logId} -> {event_ids}") self.log.debug(f"Handled KakaoTalk message {message.logId} -> {event_ids}")
await DBMessage.bulk_create( await DBMessage.bulk_create(
ktid=message.logId, ktid=message.logId,
@ -998,38 +1025,162 @@ class Portal(DBPortal, BasePortal):
) )
await self._send_delivery_receipt(event_ids[-1]) await self._send_delivery_receipt(event_ids[-1])
async def _handle_remote_text(self, source: u.User, intent: IntentAPI, message: Chatlog) -> list[EventID]: async def _handle_remote_text(
self,
source: u.User,
intent: IntentAPI,
attachment: None,
timestamp: int,
message_text: str | None,
) -> list[EventID]:
# TODO Handle mentions properly # TODO Handle mentions properly
content = await kakaotalk_to_matrix(message.text) content = await kakaotalk_to_matrix(message_text)
# TODO Replies # TODO Replies
return [await self._send_message(intent, content, timestamp=message.sendAt)] return [await self._send_message(intent, content, timestamp=timestamp)]
async def _handle_remote_photo(self, source: u.User, intent: IntentAPI, message: Chatlog) -> list[EventID]: def _handle_remote_photo(
assert message.attachment self,
assert message.attachment.url or message.attachment.urls source: u.User,
url = message.attachment.url or message.attachment.urls[0] intent: IntentAPI,
assert isinstance(message.attachment, PhotoAttachment) attachment: PhotoAttachment,
info = ImageInfo( timestamp: int,
width=message.attachment.w, message_text: str | None,
height=message.attachment.h, ) -> Awaitable[list[EventID]]:
return asyncio.gather(self._handle_remote_uniphoto(
source, intent, attachment, timestamp, message_text
))
def _handle_remote_multiphoto(
self,
source: u.User,
intent: IntentAPI,
attachment: MultiPhotoAttachment,
timestamp: int,
message_text: str | None,
) -> Awaitable[list[EventID]]:
# TODO Upload media concurrently, but post messages sequentially
return asyncio.gather(
*[
self._handle_remote_uniphoto(
source, intent,
PhotoAttachment(
shout=attachment.shout,
mentions=attachment.mentions,
urls=attachment.urls,
url=attachment.imageUrls[i],
s=attachment.sl[i],
k=attachment.kl[i],
w=attachment.wl[i],
h=attachment.hl[i],
thumbnailUrl=attachment.thumbnailUrls[i],
thumbnailWidth=attachment.thumbnailWidths[i],
thumbnailHeight=attachment.thumbnailHeights[i],
cs=attachment.csl[i],
mt=attachment.mtl[i],
),
timestamp, message_text,
)
for i in range(len(attachment.imageUrls))
]
) )
# TODO Animated images?
def _handle_remote_uniphoto(
self,
source: u.User,
intent: IntentAPI,
attachment: PhotoAttachment,
timestamp: int,
message_text: str | None,
) -> Awaitable[EventID]:
return self._handle_remote_media(
source, intent, attachment, timestamp, message_text,
ImageInfo(
mimetype=attachment.mt,
size=attachment.s,
width=attachment.w,
height=attachment.h,
),
MessageType.IMAGE,
)
def _handle_remote_video(
self,
source: u.User,
intent: IntentAPI,
attachment: VideoAttachment,
timestamp: int,
message_text: str | None,
) -> Awaitable[list[EventID]]:
return asyncio.gather(self._handle_remote_media(
source, intent, attachment, timestamp, message_text,
VideoInfo(
duration=attachment.d,
width=attachment.w,
height=attachment.h,
),
MessageType.VIDEO,
))
def _handle_remote_audio(
self,
source: u.User,
intent: IntentAPI,
attachment: AudioAttachment,
timestamp: int,
message_text: str | None,
) -> Awaitable[list[EventID]]:
return asyncio.gather(self._handle_remote_media(
source, intent, attachment, timestamp, message_text,
AudioInfo(
size=attachment.s,
duration=attachment.d,
),
MessageType.AUDIO,
))
""" TODO Find what auth is required for reading file contents
def _handle_remote_file(
self,
source: u.User,
intent: IntentAPI,
attachment: FileAttachment,
timestamp: int,
message_text: str | None,
) -> Awaitable[list[EventID]]:
return asyncio.gather(self._handle_remote_media(
source, intent, attachment, timestamp, message_text,
FileInfo(
size=attachment.size,
),
MessageType.FILE,
))
"""
async def _handle_remote_media(
self,
source: u.User,
intent: IntentAPI,
attachment: MediaAttachment,
timestamp: int,
message_text: str | None,
info: MediaInfo,
msgtype: MessageType,
) -> EventID:
mxc, additional_info, decryption_info = await self._reupload_remote_file( mxc, additional_info, decryption_info = await self._reupload_remote_file(
url, attachment.url,
source, source,
intent, intent,
mimetype=info.mimetype,
encrypt=self.encrypted, encrypt=self.encrypted,
find_size=False, find_size=False,
) )
info.size = additional_info.size info.size = additional_info.size
info.mimetype = additional_info.mimetype info.mimetype = additional_info.mimetype
content = MediaMessageEventContent( content = MediaMessageEventContent(
url=mxc, file=decryption_info, msgtype=MessageType.IMAGE, body=message.text, info=info url=mxc, file=decryption_info, msgtype=msgtype, body=message_text, info=info
) )
if not content:
return []
# TODO Replies # TODO Replies
return [await self._send_message(intent, content, timestamp=message.sendAt)] return await self._send_message(intent, content, timestamp=timestamp)
# TODO Many more remote handlers # TODO Many more remote handlers