| | |
| | | from app.services.redis_queue import redis_queue |
| | | from app.models.database import get_db |
| | | from app.models.contact import Contact |
| | | from app.services.silence_service import silence_service |
| | | from config import settings |
| | | |
| | | |
| | |
| | | """ |
| | | try: |
| | | with next(get_db()) as db: |
| | | contact = db.query(Contact).filter(Contact.nick_name == nickname).first() |
| | | contact = ( |
| | | db.query(Contact).filter(Contact.nick_name == nickname).first() |
| | | ) |
| | | if contact: |
| | | return contact.wc_id |
| | | wc_id = contact.wc_id |
| | | if contact.work_wc_id: |
| | | wc_id += f",{contact.work_wc_id}" |
| | | return wc_id |
| | | else: |
| | | logger.warning(f"未找到昵称为 '{nickname}' 的联系人") |
| | | return None |
| | |
| | | logger.info("好友列表为空,无需添加到忽略列表") |
| | | return True |
| | | |
| | | # 清空现有的忽略列表 |
| | | redis_queue.redis_client.delete(self.ignore_list_key) |
| | | |
| | | # 批量添加好友w_id到忽略列表 |
| | | # 批量添加好友w_id到忽略列表(不清空现有列表) |
| | | redis_queue.redis_client.sadd(self.ignore_list_key, *friends) |
| | | |
| | | |
| | | logger.info(f"已将 {len(friends)} 个好友添加到忽略列表") |
| | | return True |
| | | |
| | |
| | | logger.error(f"添加好友到忽略列表异常: error={str(e)}") |
| | | return False |
| | | |
| | | def is_friend_ignored(self, w_id: str) -> bool: |
| | | def is_friend_ignored(self, w_id: str, group_id: Optional[str] = None) -> bool: |
| | | """ |
| | | 检查指定w_id是否应该被忽略 |
| | | |
| | | 逻辑: |
| | | 1. 如果好友忽略功能未启用,返回False(不忽略) |
| | | 2. 如果w_id在白名单中,返回False(不忽略) |
| | | 3. 如果w_id在忽略列表中,返回True(忽略) |
| | | 4. 如果w_id不在忽略列表中,返回False(不忽略) |
| | | 3. 如果w_id在忽略列表中,但所在群组为测试群组,返回False(不忽略) |
| | | 4. 如果w_id在忽略列表中,返回True(忽略) |
| | | 5. 如果w_id不在忽略列表中,返回False(不忽略) |
| | | |
| | | Args: |
| | | w_id: 用户w_id |
| | | group_id: 群组ID(可选),用于检查是否为测试群组 |
| | | |
| | | Returns: |
| | | 如果应该被忽略返回True,否则返回False |
| | |
| | | |
| | | # 检查是否在白名单中(通过昵称) |
| | | whitelist_wids = self._get_whitelist_wids() |
| | | if w_id in whitelist_wids: |
| | | |
| | | if any(w_id in wids for wids in whitelist_wids): |
| | | logger.info(f"w_id在白名单中,不忽略消息: w_id={w_id}") |
| | | return False |
| | | |
| | | # 检查是否在忽略列表中 |
| | | is_in_ignore_list = redis_queue.redis_client.sismember(self.ignore_list_key, w_id) |
| | | is_in_ignore_list = redis_queue.redis_client.sismember( |
| | | self.ignore_list_key, w_id |
| | | ) |
| | | |
| | | if is_in_ignore_list: |
| | | # 如果在忽略列表中,检查是否在测试群组中 |
| | | if group_id and silence_service.is_test_group(group_id): |
| | | logger.info( |
| | | f"测试群组中的好友消息不被忽略: w_id={w_id}, group_id={group_id}" |
| | | ) |
| | | return False |
| | | |
| | | logger.info(f"w_id在忽略列表中,忽略消息: w_id={w_id}") |
| | | |
| | | return is_in_ignore_list |
| | |
| | | "in_ignore_list": False, |
| | | "final_ignored": False, |
| | | "reason": "", |
| | | "whitelist_nicknames": settings.friend_ignore_whitelist |
| | | "whitelist_nicknames": settings.friend_ignore_whitelist, |
| | | } |
| | | |
| | | if not settings.friend_ignore_enabled: |
| | |
| | | info["reason"] = "在白名单中,不会被忽略" |
| | | return info |
| | | |
| | | info["in_ignore_list"] = redis_queue.redis_client.sismember(self.ignore_list_key, w_id) |
| | | info["in_ignore_list"] = redis_queue.redis_client.sismember( |
| | | self.ignore_list_key, w_id |
| | | ) |
| | | |
| | | if info["in_ignore_list"]: |
| | | info["final_ignored"] = True |
| | |
| | | |
| | | except Exception as e: |
| | | logger.error(f"获取忽略状态信息异常: w_id={w_id}, error={str(e)}") |
| | | return { |
| | | "w_id": w_id, |
| | | "error": str(e), |
| | | "final_ignored": False |
| | | } |
| | | return {"w_id": w_id, "error": str(e), "final_ignored": False} |
| | | |
| | | |
| | | # 全局好友忽略服务实例 |