分开个人好友和群好友

This commit is contained in:
liuwei
2025-05-26 16:43:31 +08:00
parent da0d95d635
commit 6adef5277b
2 changed files with 20 additions and 14 deletions

View File

@@ -183,8 +183,9 @@ class Robot:
# 登录成功后加载联系人信息 # 登录成功后加载联系人信息
self.allContacts = self.get_all_contacts() self.allContacts = self.get_all_contacts()
friends = await self.ipad_bot.get_contract_list()
self.head_images = self.get_all_head_images() self.head_images = self.get_all_head_images()
self.contact_manager.set_contacts(self.allContacts, self.head_images) self.contact_manager.set_contacts(self.allContacts, friends, self.head_images)
self.message_storage = MessageStorage(self.ipad_bot) self.message_storage = MessageStorage(self.ipad_bot)
@@ -407,7 +408,8 @@ class Robot:
self.head_images[wxid] = small_head_img_url self.head_images[wxid] = small_head_img_url
self.contact_manager.set_contacts(self.allContacts, self.head_images) friends = await self.ipad_bot.get_contract_list()
self.contact_manager.set_contacts(self.allContacts, friends, self.head_images)
self.LOG.info(f"已更新群 {group_id} 的成员信息") self.LOG.info(f"已更新群 {group_id} 的成员信息")
except Exception as e: except Exception as e:
self.LOG.error(f"获取群成员信息失败: {e}") self.LOG.error(f"获取群成员信息失败: {e}")
@@ -454,10 +456,11 @@ class Robot:
time.sleep(1) time.sleep(1)
# 添加一个方法用于刷新联系人信息 # 添加一个方法用于刷新联系人信息
def refresh_contacts(self): async def refresh_contacts(self):
"""刷新联系人信息""" """刷新联系人信息"""
self.allContacts = self.get_all_contacts() self.allContacts = self.get_all_contacts()
self.contact_manager.refresh_contacts(self.allContacts) friends = await self.ipad_bot.get_contract_list()
self.contact_manager.refresh_contacts(self.allContacts,friends)
self.LOG.info("联系人信息已刷新") self.LOG.info("联系人信息已刷新")
async def send_group_txt_message(self, msg: str, feature: Feature): async def send_group_txt_message(self, msg: str, feature: Feature):
@@ -607,8 +610,8 @@ class Robot:
self.allContacts[wxid] = nick_name self.allContacts[wxid] = nick_name
self.head_images[wxid] = small_head_img_url self.head_images[wxid] = small_head_img_url
friends = await self.ipad_bot.get_contract_list()
self.contact_manager.set_contacts(self.allContacts, self.head_images) self.contact_manager.set_contacts(self.allContacts, friends, self.head_images)
self.LOG.info(f"已更新群 {group_id} 的成员信息") self.LOG.info(f"已更新群 {group_id} 的成员信息")
else: else:
self.LOG.error(f"获取群 {group_id} 信息失败,证明用户无该群信息,删除群的相关资料。") self.LOG.error(f"获取群 {group_id} 信息失败,证明用户无该群信息,删除群的相关资料。")

View File

@@ -20,7 +20,7 @@ class ContactManager:
_head_images: Dict[str, str] = {} # 头像信息 _head_images: Dict[str, str] = {} # 头像信息
_initialized = False _initialized = False
_logger = logger _logger = logger
_friends: List[Dict] = [] _friends: List[str] = []
_group_contacts_friends: Dict[str, Dict[str, str]] = {} _group_contacts_friends: Dict[str, Dict[str, str]] = {}
# 定义公共好友列表 # 定义公共好友列表
_PUBLIC_FRIENDS = { _PUBLIC_FRIENDS = {
@@ -51,7 +51,7 @@ class ContactManager:
cls._instance = ContactManager() cls._instance = ContactManager()
return cls._instance return cls._instance
def set_contacts(self, contacts: Dict[str, str], head_imgs: Dict[str, str]) -> None: def set_contacts(self, contacts: Dict[str, str], friends: List[str], head_imgs: Dict[str, str]) -> None:
"""设置联系人字典 """设置联系人字典
Args: Args:
@@ -68,7 +68,7 @@ class ContactManager:
"gender": gender} "gender": gender}
""" """
self._contacts = contacts self._contacts = contacts
self._friends = contacts self._friends = friends
self._head_images = head_imgs self._head_images = head_imgs
self._logger.info(f"联系人信息已更新,共 {len(contacts)} 个联系人") self._logger.info(f"联系人信息已更新,共 {len(contacts)} 个联系人")
# 分类联系人 # 分类联系人
@@ -92,7 +92,9 @@ class ContactManager:
elif wxid.endswith('@chatroom'): elif wxid.endswith('@chatroom'):
self._group_contacts[wxid] = nickname self._group_contacts[wxid] = nickname
else: else:
self._personal_contacts[wxid] = nickname # 判断 frinds 在contacts 里面,将在里面的用户分在
if wxid in self._friends:
self._personal_contacts[wxid] = nickname
self._logger.info(f"联系人分类完成: {len(self._group_contacts)} 个群组, " self._logger.info(f"联系人分类完成: {len(self._group_contacts)} 个群组, "
f"{len(self._personal_contacts)} 个个人联系人, " f"{len(self._personal_contacts)} 个个人联系人, "
f"{len(self._public_contacts)} 个公共好友, " f"{len(self._public_contacts)} 个公共好友, "
@@ -208,19 +210,20 @@ class ContactManager:
# 需要获取群成员昵称信息; 从数据库里面提取。 # 需要获取群成员昵称信息; 从数据库里面提取。
# self._group_contacts_friends[wxid] = {} # self._group_contacts_friends[wxid] = {}
else: else:
self._personal_contacts[wxid] = nickname if wxid in self._friends:
self._personal_contacts[wxid] = nickname
self._logger.debug(f"已更新联系人: {wxid} -> {nickname}") self._logger.debug(f"已更新联系人: {wxid} -> {nickname}")
def refresh_contacts(self, new_contacts: Dict[str, str]) -> None: def refresh_contacts(self, new_contacts: Dict[str, str], friends: List[str]) -> None:
"""刷新联系人信息 """刷新联系人信息
Args: Args:
new_contacts: 新的联系人字典 new_contacts: 新的联系人字典
head_imgs: 联系人头像字典,格式为 {"wxid": "http://xxxxx"} friends: 联系人信息,格式为 ["wxid","wxid2"]
wcf :wcf
""" """
self._contacts = new_contacts self._contacts = new_contacts
self._friends = friends
# friends: 好友清单 contact = { # friends: 好友清单 contact = {
# "wxid": cnt.get("wxid", ""), # "wxid": cnt.get("wxid", ""),
# "code": cnt.get("code", ""), # "code": cnt.get("code", ""),