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

128 lines
4.1 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/>.
"""Custom wrapper classes around types defined by the KakaoTalk API."""
from typing import Optional, NewType, Union
from attr import dataclass
from mautrix.types import (
SerializableAttrs,
JSON,
deserializer,
MessageType,
)
from ..types.api.struct import MoreSettingsStruct, LessSettingsStruct
from ..types.bson import Long
from ..types.channel.channel_info import NormalChannelInfo
from ..types.channel.channel_type import ChannelType
from ..types.chat import KnownChatType
from ..types.openlink.open_channel_info import OpenChannelInfo
from ..types.openlink.open_link_type import OpenChannelUserPerm
from ..types.user.channel_user_info import NormalChannelUserInfo, OpenChannelUserInfo
@dataclass
class SettingsStruct(SerializableAttrs):
more: MoreSettingsStruct
less: LessSettingsStruct
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)
@dataclass
class Receipt(SerializableAttrs):
userId: Long
chatId: Long
@dataclass
class PortalChannelParticipantInfo(SerializableAttrs):
participants: list[UserInfoUnion]
kickedUserIds: list[Long]
@dataclass
class PortalChannelInfo(SerializableAttrs):
name: str
description: Optional[str] = None
photoURL: Optional[str] = None
participantInfo: Optional[PortalChannelParticipantInfo] = None # May set to None to skip participant update
channel_info: Optional[ChannelInfoUnion] = None # Should be set manually by caller
@dataclass
class ChannelProps(SerializableAttrs):
id: Long
type: ChannelType
# 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()}
# TODO Consider allowing custom power/perm mappings
# But must update default user level & permissions to match!
FROM_PERM_MAP: dict[OpenChannelUserPerm, int] = {
OpenChannelUserPerm.OWNER: 100,
OpenChannelUserPerm.MANAGER: 50,
# TODO Decide on an appropriate value for this
OpenChannelUserPerm.BOT: 25,
OpenChannelUserPerm.NONE: 0
}
# NOTE Using a class to make this look like a dict
class TO_PERM_MAP:
@staticmethod
def get(key: int) -> OpenChannelUserPerm:
if key >= 100:
return OpenChannelUserPerm.OWNER
if key >= 50:
return OpenChannelUserPerm.MANAGER
return OpenChannelUserPerm.NONE