Compare commits

..

No commits in common. "d7b0b9013f30beab723dacbf75e1dd5fc8183eaa" and "ace4eefec7efeab3cf19336fd023dfd4dd46dca1" have entirely different histories.

5 changed files with 51 additions and 217 deletions

View File

@ -4,11 +4,11 @@
* [ ] Message content
* [x] Text
* [ ] Media
* [x] Stickers
* [x] Files
* [x] Voice messages
* [x] Videos
* [x] Images
* [ ] Stickers
* [ ] Files
* [ ] Voice messages
* [ ] Videos
* [ ] Images
* [ ] Locations
* [ ] Formatting
* [ ] Replies
@ -32,11 +32,8 @@
* [x] Text
* [ ] Media
* [ ] Stickers
* [ ] Files
* [x] Voice messages
* [x] Videos
* [x] Images
* [ ] Locations
* [ ] Videos
* [ ] Images
* [ ] Formatting
* [ ] Replies
* [ ] Mentions

View File

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

View File

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

View File

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

View File

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