2021-02-15 15:32:47 +00:00
|
|
|
import contextlib
|
|
|
|
import logging
|
|
|
|
|
2021-05-30 17:40:01 +00:00
|
|
|
from efb_qq_slave import QQMessengerChannel
|
2021-02-15 15:32:47 +00:00
|
|
|
from ehforwarderbot import Chat
|
|
|
|
from ehforwarderbot.chat import GroupChat, PrivateChat, SystemChat
|
|
|
|
from ehforwarderbot.types import ChatID
|
|
|
|
|
|
|
|
|
|
|
|
class ChatManager:
|
2022-02-05 12:58:20 +00:00
|
|
|
def __init__(self, channel: "QQMessengerChannel"):
|
|
|
|
self.channel: "QQMessengerChannel" = channel
|
2021-02-15 15:32:47 +00:00
|
|
|
self.logger: logging.Logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
self.MISSING_GROUP: GroupChat = GroupChat(
|
2022-02-05 12:58:20 +00:00
|
|
|
channel=self.channel, uid=ChatID("__error_group__"), name="Group Missing"
|
2021-02-15 15:32:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
self.MISSING_CHAT: PrivateChat = PrivateChat(
|
2022-02-05 12:58:20 +00:00
|
|
|
channel=self.channel, uid=ChatID("__error_chat__"), name="Chat Missing"
|
2021-02-15 15:32:47 +00:00
|
|
|
)
|
|
|
|
|
2022-05-02 10:34:48 +00:00
|
|
|
async def build_efb_chat_as_private(self, context):
|
2022-02-05 12:58:20 +00:00
|
|
|
uid = context["user_id"]
|
|
|
|
if "sender" not in context or "nickname" not in context["sender"]:
|
2022-05-02 10:34:48 +00:00
|
|
|
i: dict = await self.channel.QQClient.get_stranger_info(uid)
|
2021-02-15 15:32:47 +00:00
|
|
|
chat_name = ""
|
|
|
|
if i:
|
2022-02-05 12:58:20 +00:00
|
|
|
chat_name = i["nickname"]
|
2021-02-15 15:32:47 +00:00
|
|
|
else:
|
2022-02-05 12:58:20 +00:00
|
|
|
chat_name = context["sender"]["nickname"]
|
|
|
|
efb_chat = PrivateChat(
|
|
|
|
channel=self.channel,
|
|
|
|
uid="private" + "_" + str(uid),
|
|
|
|
name=str(chat_name),
|
|
|
|
alias=None if "alias" not in context else str(context["alias"]),
|
|
|
|
)
|
2021-02-15 15:32:47 +00:00
|
|
|
return efb_chat
|
|
|
|
|
2022-05-02 10:34:48 +00:00
|
|
|
async def build_or_get_efb_member(self, chat: Chat, context):
|
2022-02-05 12:58:20 +00:00
|
|
|
member_uid = context["user_id"]
|
2021-02-15 15:32:47 +00:00
|
|
|
with contextlib.suppress(KeyError):
|
|
|
|
return chat.get_member(str(member_uid))
|
2022-02-05 12:58:20 +00:00
|
|
|
chat_name = ""
|
|
|
|
if "nickname" not in context:
|
2022-05-02 10:34:48 +00:00
|
|
|
i: dict = await self.channel.QQClient.get_stranger_info(member_uid)
|
2021-02-15 15:32:47 +00:00
|
|
|
chat_name = ""
|
|
|
|
if i:
|
2022-02-05 12:58:20 +00:00
|
|
|
chat_name = i["nickname"]
|
2021-02-15 15:32:47 +00:00
|
|
|
else:
|
2022-02-05 12:58:20 +00:00
|
|
|
chat_name = context["nickname"]
|
|
|
|
return chat.add_member(
|
|
|
|
name=str(chat_name),
|
|
|
|
alias=None if "alias" not in context else str(context["alias"]),
|
|
|
|
uid=str(member_uid),
|
|
|
|
)
|
2021-02-15 15:32:47 +00:00
|
|
|
|
2022-05-02 10:34:48 +00:00
|
|
|
async def build_efb_chat_as_group(self, context, update_member=False): # Should be cached
|
2022-02-05 12:58:20 +00:00
|
|
|
is_discuss = False if context["message_type"] == "group" else True
|
|
|
|
chat_uid = context["discuss_id"] if is_discuss else context["group_id"]
|
|
|
|
efb_chat = GroupChat(channel=self.channel, uid=str(chat_uid))
|
2021-02-15 15:32:47 +00:00
|
|
|
if not is_discuss:
|
2022-02-05 12:58:20 +00:00
|
|
|
efb_chat.uid = "group" + "_" + str(chat_uid)
|
2022-05-02 10:34:48 +00:00
|
|
|
i = await self.channel.QQClient.get_group_info(chat_uid)
|
2021-02-15 15:32:47 +00:00
|
|
|
if i is not None:
|
2022-02-05 12:58:20 +00:00
|
|
|
efb_chat.name = str(i["group_name"]) if "group_name" not in context else str(context["group_name"])
|
2021-02-15 15:32:47 +00:00
|
|
|
else:
|
|
|
|
efb_chat.name = str(chat_uid)
|
2022-02-05 12:58:20 +00:00
|
|
|
efb_chat.vendor_specific = {"is_discuss": False}
|
2021-02-15 15:32:47 +00:00
|
|
|
if update_member:
|
2022-05-02 10:34:48 +00:00
|
|
|
members = await self.channel.QQClient.get_group_member_list(chat_uid, False)
|
2021-02-15 15:32:47 +00:00
|
|
|
if members:
|
|
|
|
for member in members:
|
2022-02-05 12:58:20 +00:00
|
|
|
efb_chat.add_member(
|
|
|
|
name=str(member["card"]),
|
|
|
|
alias=str(member["nickname"]),
|
|
|
|
uid=str(member["user_id"]),
|
|
|
|
)
|
2021-02-15 15:32:47 +00:00
|
|
|
else:
|
2022-02-05 12:58:20 +00:00
|
|
|
efb_chat.uid = "discuss" + "_" + str(chat_uid)
|
|
|
|
efb_chat.name = "Discuss Group" + "_" + str(chat_uid)
|
2021-02-15 15:32:47 +00:00
|
|
|
# todo Find a way to distinguish from different discuss group
|
2022-02-05 12:58:20 +00:00
|
|
|
efb_chat.vendor_specific = {"is_discuss": True}
|
2021-02-15 15:32:47 +00:00
|
|
|
return efb_chat
|
|
|
|
|
|
|
|
def build_efb_chat_as_anonymous_user(self, chat: Chat, context):
|
2022-02-05 12:58:20 +00:00
|
|
|
anonymous_data = context["anonymous"]
|
|
|
|
member_uid = "anonymous" + "_" + anonymous_data["flag"]
|
2021-02-15 15:32:47 +00:00
|
|
|
with contextlib.suppress(KeyError):
|
|
|
|
return chat.get_member(member_uid)
|
2022-02-05 12:58:20 +00:00
|
|
|
chat_name = "[Anonymous] " + anonymous_data["name"]
|
|
|
|
return chat.add_member(
|
|
|
|
name=str(chat_name),
|
|
|
|
alias=None if "alias" not in context else str(context["alias"]),
|
|
|
|
uid=str(member_uid),
|
|
|
|
vendor_specific={
|
|
|
|
"is_anonymous": True,
|
|
|
|
"anonymous_id": anonymous_data["id"],
|
|
|
|
},
|
|
|
|
)
|
2021-02-15 15:32:47 +00:00
|
|
|
|
|
|
|
def build_efb_chat_as_system_user(self, context): # System user only!
|
2022-02-05 12:58:20 +00:00
|
|
|
return SystemChat(
|
|
|
|
channel=self.channel,
|
|
|
|
name=str(context["event_description"]),
|
|
|
|
uid=ChatID("__{context[uid_prefix]}__".format(context=context)),
|
|
|
|
)
|