2022-02-25 02:22:50 -05:00
|
|
|
# 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/>.
|
2022-03-24 19:32:46 -04:00
|
|
|
from typing import Optional, Union, Type
|
2022-02-26 04:06:26 -05:00
|
|
|
|
2022-02-25 02:22:50 -05:00
|
|
|
from attr import dataclass
|
|
|
|
|
2022-03-24 19:32:46 -04:00
|
|
|
from mautrix.types import SerializableAttrs, JSON
|
2022-02-25 02:22:50 -05:00
|
|
|
|
|
|
|
from ..bson import Long
|
|
|
|
from ..user.channel_user import ChannelUser
|
2022-03-24 19:32:46 -04:00
|
|
|
from .attachment import (
|
|
|
|
Attachment,
|
|
|
|
PhotoAttachment,
|
|
|
|
VideoAttachment,
|
|
|
|
ContactAttachment,
|
|
|
|
AudioAttachment,
|
|
|
|
EmoticonAttachment,
|
|
|
|
OpenScheduleAttachment,
|
|
|
|
VoteAttachment,
|
|
|
|
MapAttachment,
|
|
|
|
ProfileAttachment,
|
|
|
|
FileAttachment,
|
|
|
|
PostAttachment,
|
|
|
|
ReplyAttachment,
|
|
|
|
)
|
|
|
|
from .chat_type import ChatType, KnownChatType
|
2022-02-25 02:22:50 -05:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class ChatTypeComponent(SerializableAttrs):
|
|
|
|
type: ChatType # Substitute for T, where T extends ChatType = ChatType
|
|
|
|
|
|
|
|
@dataclass(kw_only=True)
|
|
|
|
class Chat(ChatTypeComponent):
|
2022-02-26 04:06:26 -05:00
|
|
|
text: Optional[str] = None
|
|
|
|
attachment: Optional[Attachment] = None
|
|
|
|
supplement: Optional[dict] = None
|
2022-02-25 02:22:50 -05:00
|
|
|
|
2022-03-24 19:32:46 -04:00
|
|
|
@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.OPEN_SCHEDULE: OpenScheduleAttachment,
|
|
|
|
KnownChatType.OPEN_VOTE: VoteAttachment,
|
|
|
|
KnownChatType.OPEN_POST: PostAttachment,
|
|
|
|
}
|
|
|
|
|
2022-02-25 02:22:50 -05:00
|
|
|
|
|
|
|
@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
|
2022-02-26 04:06:26 -05:00
|
|
|
messageId: Union[int, Long]
|
2022-02-25 02:22:50 -05:00
|
|
|
|
|
|
|
|
|
|
|
@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):
|
2022-02-26 04:06:26 -05:00
|
|
|
shout: Optional[bool] = None
|
2022-02-25 02:22:50 -05:00
|
|
|
|
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
"ChatTypeComponent",
|
|
|
|
"Chat",
|
|
|
|
"TypedChat",
|
|
|
|
"ChatLogged",
|
|
|
|
"ChatLoggedType",
|
|
|
|
"ChatLogLinked",
|
|
|
|
"ChatWritten",
|
|
|
|
"Chatlog",
|
|
|
|
"TypedChatlog",
|
|
|
|
"ChatOptions",
|
|
|
|
]
|