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/>.
|
|
|
|
"""Custom wrapper classes around types defined by the KakaoTalk API."""
|
|
|
|
|
2022-03-09 02:25:28 -05:00
|
|
|
from typing import Optional, NewType, Union
|
2022-02-26 04:06:26 -05:00
|
|
|
|
2022-02-25 02:22:50 -05:00
|
|
|
from attr import dataclass
|
|
|
|
|
2022-03-26 03:37:53 -04:00
|
|
|
from mautrix.types import (
|
|
|
|
SerializableAttrs,
|
|
|
|
JSON,
|
|
|
|
deserializer,
|
|
|
|
MessageType,
|
|
|
|
)
|
2022-02-25 02:22:50 -05:00
|
|
|
|
2022-04-10 22:56:51 -04:00
|
|
|
from ..types.api.struct import MoreSettingsStruct, LessSettingsStruct
|
2022-03-21 01:33:22 -04:00
|
|
|
from ..types.bson import Long
|
2022-02-25 02:22:50 -05:00
|
|
|
from ..types.channel.channel_info import NormalChannelInfo
|
2022-03-21 01:33:22 -04:00
|
|
|
from ..types.channel.channel_type import ChannelType
|
2022-03-26 03:37:53 -04:00
|
|
|
from ..types.chat import KnownChatType
|
2022-02-25 02:22:50 -05:00
|
|
|
from ..types.openlink.open_channel_info import OpenChannelInfo
|
|
|
|
from ..types.user.channel_user_info import NormalChannelUserInfo, OpenChannelUserInfo
|
|
|
|
|
|
|
|
|
2022-04-10 22:56:51 -04:00
|
|
|
@dataclass
|
|
|
|
class SettingsStruct(SerializableAttrs):
|
|
|
|
more: MoreSettingsStruct
|
|
|
|
less: LessSettingsStruct
|
|
|
|
|
|
|
|
|
2022-03-09 02:25:28 -05:00
|
|
|
ChannelInfoUnion = NewType("ChannelInfoUnion", Union[NormalChannelInfo, OpenChannelInfo])
|
|
|
|
|
|
|
|
@deserializer(ChannelInfoUnion)
|
|
|
|
def deserialize_channel_info_union(data: JSON) -> ChannelInfoUnion:
|
|
|
|
if "openLink" in data:
|
|
|
|
return OpenChannelInfo.deserialize(data)
|
|
|
|
else:
|
|
|
|
return NormalChannelInfo.deserialize(data)
|
|
|
|
|
|
|
|
setattr(ChannelInfoUnion, "deserialize", deserialize_channel_info_union)
|
|
|
|
|
|
|
|
|
|
|
|
UserInfoUnion = NewType("UserInfoUnion", Union[NormalChannelUserInfo, OpenChannelUserInfo])
|
|
|
|
|
|
|
|
@deserializer(UserInfoUnion)
|
|
|
|
def deserialize_user_info_union(data: JSON) -> UserInfoUnion:
|
|
|
|
if "perm" in data:
|
|
|
|
return OpenChannelUserInfo.deserialize(data)
|
|
|
|
else:
|
|
|
|
return NormalChannelUserInfo.deserialize(data)
|
|
|
|
|
|
|
|
setattr(UserInfoUnion, "deserialize", deserialize_user_info_union)
|
|
|
|
|
2022-02-25 02:22:50 -05:00
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class PortalChannelInfo(SerializableAttrs):
|
|
|
|
name: str
|
2022-03-09 02:25:28 -05:00
|
|
|
participants: list[UserInfoUnion]
|
2022-02-25 02:22:50 -05:00
|
|
|
# TODO Image
|
2022-02-26 04:06:26 -05:00
|
|
|
channel_info: Optional[ChannelInfoUnion] = None # Should be set manually by caller
|
2022-03-21 01:33:22 -04:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class ChannelProps(SerializableAttrs):
|
|
|
|
id: Long
|
|
|
|
type: ChannelType
|
2022-03-26 03:37:53 -04:00
|
|
|
|
|
|
|
|
|
|
|
# TODO Add non-media types, like polls & maps
|
|
|
|
TO_MSGTYPE_MAP: dict[MessageType, KnownChatType] = {
|
|
|
|
MessageType.TEXT: KnownChatType.TEXT,
|
|
|
|
MessageType.IMAGE: KnownChatType.PHOTO,
|
|
|
|
MessageType.STICKER: KnownChatType.PHOTO,
|
|
|
|
MessageType.VIDEO: KnownChatType.VIDEO,
|
|
|
|
MessageType.AUDIO: KnownChatType.AUDIO,
|
|
|
|
MessageType.FILE: KnownChatType.FILE,
|
|
|
|
}
|
|
|
|
|
|
|
|
# https://stackoverflow.com/a/483833
|
|
|
|
FROM_MSGTYPE_MAP: dict[KnownChatType, MessageType] = {v: k for k, v in TO_MSGTYPE_MAP.items()}
|