优化联系人信息,解决总结时使用了其他群的备注信息
This commit is contained in:
@@ -21,6 +21,7 @@ class ContactManager:
|
||||
_initialized = False
|
||||
_logger = logger
|
||||
_friends: List[str] = []
|
||||
_group_members: List[Dict] = []
|
||||
_group_contacts_friends: Dict[str, Dict[str, str]] = {}
|
||||
# 定义公共好友列表
|
||||
_PUBLIC_FRIENDS = {
|
||||
@@ -51,7 +52,8 @@ class ContactManager:
|
||||
cls._instance = ContactManager()
|
||||
return cls._instance
|
||||
|
||||
def set_contacts(self, contacts: Dict[str, str], friends: List[str], head_imgs: Dict[str, str]) -> None:
|
||||
def set_contacts(self, contacts: Dict[str, str], friends: List[str], head_imgs: Dict[str, str],
|
||||
chatroom_members: List[dict]) -> None:
|
||||
"""设置联系人字典
|
||||
|
||||
Args:
|
||||
@@ -66,10 +68,12 @@ class ContactManager:
|
||||
"province": cnt.get("province", ""),
|
||||
"city": cnt.get("city", ""),
|
||||
"gender": gender}
|
||||
chatroom_members: 所有的群成员昵称信息
|
||||
"""
|
||||
self._contacts = contacts
|
||||
self._friends = friends
|
||||
self._head_images = head_imgs
|
||||
self._group_members = chatroom_members
|
||||
self._logger.info(f"联系人信息已更新,共 {len(contacts)} 个联系人")
|
||||
# 分类联系人
|
||||
self._classify_contacts()
|
||||
@@ -91,6 +95,12 @@ class ContactManager:
|
||||
# 判断是否为群组(wxid以@chatroom结尾)
|
||||
elif wxid.endswith('@chatroom'):
|
||||
self._group_contacts[wxid] = nickname
|
||||
# 获取群成员信息:
|
||||
for friend in self._group_members:
|
||||
if friend.get('chatroom_id') == wxid:
|
||||
self._group_contacts_friends[wxid].update(
|
||||
{friend.get('wxid'): friend.get('nick_name', friend.get('wxid'))})
|
||||
|
||||
else:
|
||||
# 判断 frinds 在contacts 里面,将在里面的用户分在
|
||||
if wxid in self._friends:
|
||||
@@ -170,6 +180,18 @@ class ContactManager:
|
||||
"""
|
||||
return self._head_images.get(wxid, "")
|
||||
|
||||
def update_head_image(self, wxid: str, head_image: str) -> bool:
|
||||
"""根据微信ID更新头像
|
||||
|
||||
Args:
|
||||
wxid: 微信ID
|
||||
head_image:头像地址
|
||||
Returns:
|
||||
对应的头像,如果不存在这返回""
|
||||
"""
|
||||
self._head_images.update({wxid: head_image})
|
||||
return True
|
||||
|
||||
def get_group_name(self, roomid: str, wxid: str) -> str:
|
||||
"""
|
||||
Args:
|
||||
@@ -192,6 +214,18 @@ class ContactManager:
|
||||
"""
|
||||
return self._group_contacts_friends.get(roomid, {})
|
||||
|
||||
def update_group_members(self, roomid: str, wxid: str, nick_name: str) -> bool:
|
||||
"""更新指定群的成员列表
|
||||
|
||||
Args:
|
||||
roomid: 群ID
|
||||
|
||||
Returns:
|
||||
群成员字典,格式为 {"wxid": "NickName"}
|
||||
"""
|
||||
self._group_contacts_friends[roomid].update({wxid: nick_name})
|
||||
return True
|
||||
|
||||
def update_contact(self, wxid: str, nickname: str) -> None:
|
||||
"""更新单个联系人信息
|
||||
|
||||
@@ -208,7 +242,10 @@ class ContactManager:
|
||||
elif wxid.endswith('@chatroom'):
|
||||
self._group_contacts[wxid] = nickname
|
||||
# 需要获取群成员昵称信息; 从数据库里面提取。
|
||||
# self._group_contacts_friends[wxid] = {}
|
||||
for friend in self._group_members:
|
||||
if friend.get('chatroom_id') == wxid:
|
||||
self._group_contacts_friends[wxid].update(
|
||||
{friend.get('wxid'): friend.get('nick_name', friend.get('wxid'))})
|
||||
else:
|
||||
if wxid in self._friends:
|
||||
self._personal_contacts[wxid] = nickname
|
||||
|
||||
Reference in New Issue
Block a user