|
|
@@ -12,6 +12,8 @@ from fastapi import FastAPI, Depends |
|
|
|
from common.singleton import singleton |
|
|
|
from typing import Optional |
|
|
|
from model.models import WxChatMessage |
|
|
|
from pydantic import ValidationError |
|
|
|
|
|
|
|
|
|
|
|
from aiohttp import ClientError |
|
|
|
from json.decoder import JSONDecodeError |
|
|
@@ -1561,6 +1563,69 @@ class GeWeService: |
|
|
|
#TypeAdapter.validate_python(List[Models.AddGroupContactsHistory], data) |
|
|
|
return [AddGroupContactsHistory.model_validate(item) for item in data] |
|
|
|
|
|
|
|
# async def get_group_add_contacts_history_all_async(self,wxid,contact_wxid)->list[AddGroupContactsHistory]: |
|
|
|
# ''' |
|
|
|
# 获取所有群加好友历史 |
|
|
|
# ''' |
|
|
|
# contacts_history=[AddGroupContactsHistory] |
|
|
|
# history_keys = [] |
|
|
|
# async for key in self.redis_service.client.scan_iter(match=f'__AI_OPS_WX__:GROUPS_ADD_CONTACT_HISTORY:{wxid}:*'): |
|
|
|
# history_keys.append(key) |
|
|
|
|
|
|
|
# for hash_key in history_keys: |
|
|
|
# data_str=await self.redis_service.get_hash_field(hash_key,contact_wxid) |
|
|
|
# data=json.loads(data_str) if data_str else [] |
|
|
|
# contacts_history.append([AddGroupContactsHistory.model_validate(item) for item in data]) |
|
|
|
|
|
|
|
# return contacts_history |
|
|
|
|
|
|
|
async def get_group_add_contacts_history_all_async(self, wxid: str, contact_wxid: str) -> list[AddGroupContactsHistory]: |
|
|
|
""" |
|
|
|
获取指定用户的所有群加好友历史记录 |
|
|
|
|
|
|
|
Args: |
|
|
|
wxid: 微信用户ID |
|
|
|
contact_wxid: 联系人微信ID |
|
|
|
|
|
|
|
Returns: |
|
|
|
验证通过的群加好友历史记录列表 |
|
|
|
|
|
|
|
Raises: |
|
|
|
ValidationError: 当数据验证失败时抛出(可选) |
|
|
|
""" |
|
|
|
contacts_history: list[AddGroupContactsHistory] = [] |
|
|
|
|
|
|
|
# 异步收集所有匹配的Redis键 |
|
|
|
history_keys = [ |
|
|
|
key async for key in self.redis_service.client.scan_iter( |
|
|
|
match=f'__AI_OPS_WX__:GROUPS_ADD_CONTACT_HISTORY:{wxid}:*' |
|
|
|
) |
|
|
|
] |
|
|
|
|
|
|
|
# 并发处理所有Redis键 |
|
|
|
async def process_key(hash_key: str) -> None: |
|
|
|
try: |
|
|
|
if data_str := await self.redis_service.get_hash_field(hash_key, contact_wxid): |
|
|
|
data = json.loads(data_str) |
|
|
|
# 批量验证数据模型 |
|
|
|
validated = [] |
|
|
|
for item in data: |
|
|
|
try: |
|
|
|
validated.append(AddGroupContactsHistory.model_validate(item)) |
|
|
|
except ValidationError as e: |
|
|
|
logger.error(f"数据验证失败: {item},错误:{e}") |
|
|
|
# 可根据需要决定是否抛出异常 |
|
|
|
contacts_history.extend(validated) |
|
|
|
except json.JSONDecodeError as e: |
|
|
|
logger.error(f"JSON解析失败: {hash_key},数据:{data_str},错误:{e}") |
|
|
|
except Exception as e: |
|
|
|
logger.error(f"处理Redis键 {hash_key} 时发生异常:{e}") |
|
|
|
|
|
|
|
# 并发执行所有处理任务 |
|
|
|
await asyncio.gather(*(process_key(key) for key in history_keys)) |
|
|
|
|
|
|
|
return contacts_history |
|
|
|
|
|
|
|
async def check_wixd_group_add_contacts_history_async(self,wxid,chatroom_id): |
|
|
|
''' |
|
|
|
返回群发送好友达到2次的wxid |
|
|
|