matrix-appservice-kakaotalk/matrix_appservice_kakaotalk/kt/types/chat/chat.py

139 lines
3.7 KiB
Python

# matrix-appservice-kakaotalk - A Matrix-KakaoTalk puppeting bridge.
# Copyright (C) 2022 Tulir Asokan, Andrew Ferrazzutti
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from typing import Optional, Union, Type
from attr import dataclass
from mautrix.types import SerializableAttrs, JSON
from ..bson import Long
from ..user.channel_user import ChannelUser
from .attachment import (
Attachment,
PhotoAttachment,
VideoAttachment,
ContactAttachment,
AudioAttachment,
EmoticonAttachment,
OpenScheduleAttachment,
VoteAttachment,
MapAttachment,
ProfileAttachment,
FileAttachment,
PostAttachment,
ReplyAttachment,
MultiPhotoAttachment,
)
from .chat_type import ChatType, KnownChatType
@dataclass
class ChatTypeComponent(SerializableAttrs):
type: ChatType # Substitute for T, where T extends ChatType = ChatType
@dataclass(kw_only=True)
class Chat(ChatTypeComponent):
text: Optional[str] = None
attachment: Optional[Attachment] = None
supplement: Optional[dict] = None
@classmethod
def deserialize(cls, data: JSON) -> "Chat":
if "attachment" in data:
attachment = _attachment_type_map.get(int(data["type"]), Attachment).deserialize(data["attachment"])
del data["attachment"]
else:
attachment = None
obj = super().deserialize(data)
obj.attachment = attachment
return obj
# TODO More
_attachment_type_map: dict[KnownChatType, Type[Attachment]] = {
KnownChatType.PHOTO: PhotoAttachment,
KnownChatType.VIDEO: VideoAttachment,
KnownChatType.CONTACT: ContactAttachment,
KnownChatType.AUDIO: AudioAttachment,
KnownChatType.DITEMEMOTICON: EmoticonAttachment,
KnownChatType.SCHEDULE: OpenScheduleAttachment,
KnownChatType.VOTE: VoteAttachment,
KnownChatType.MAP: MapAttachment,
KnownChatType.PROFILE: ProfileAttachment,
KnownChatType.FILE: FileAttachment,
KnownChatType.POST: PostAttachment,
KnownChatType.REPLY: ReplyAttachment,
KnownChatType.MULTIPHOTO: MultiPhotoAttachment,
KnownChatType.OPEN_SCHEDULE: OpenScheduleAttachment,
KnownChatType.OPEN_VOTE: VoteAttachment,
KnownChatType.OPEN_POST: PostAttachment,
}
@dataclass
class TypedChat(Chat, ChatTypeComponent):
"""Substitute for TypedChat<T extends ChatType> = Chat & ChatTypeComponent<T>"""
pass
@dataclass
class ChatLogged(SerializableAttrs):
logId: Long
@dataclass
class ChatLoggedType(ChatLogged):
type: ChatType
@dataclass
class ChatLogLinked(ChatLogged):
prevLogId: Long
@dataclass
class ChatWritten(Chat):
sender: ChannelUser
sendAt: int
messageId: Union[int, Long]
@dataclass
class Chatlog(ChatLogLinked, ChatWritten):
pass
@dataclass
class TypedChatlog(Chatlog, TypedChat):
"""Substitute for TypedChatlog<T extends ChatType> = Chatlog & TypedChat<T>"""
pass
@dataclass(kw_only=True)
class ChatOptions(SerializableAttrs):
shout: Optional[bool] = None
__all__ = [
"ChatTypeComponent",
"Chat",
"TypedChat",
"ChatLogged",
"ChatLoggedType",
"ChatLogLinked",
"ChatWritten",
"Chatlog",
"TypedChatlog",
"ChatOptions",
]