yj
2025-08-27 307236190c98c13395b114df990eec50a9160251
更新
3个文件已修改
74 ■■■■■ 已修改文件
app/api/friend_ignore.py 30 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/models/contact.py 12 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/services/friend_ignore_service.py 32 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/api/friend_ignore.py
@@ -17,16 +17,19 @@
class AddFriendsRequest(BaseModel):
    """添加好友到忽略列表请求模型"""
    friends: List[str]
class RemoveFriendRequest(BaseModel):
    """从忽略列表移除好友请求模型"""
    w_id: str
class IgnoreListResponse(BaseModel):
    """忽略列表响应模型"""
    success: bool
    message: str
    data: Set[str] = None
@@ -46,10 +49,7 @@
        count = friend_ignore_service.get_ignore_list_count()
        
        return IgnoreListResponse(
            success=True,
            message="获取忽略列表成功",
            data=ignore_list,
            count=count
            success=True, message="获取忽略列表成功", data=ignore_list, count=count
        )
    except Exception as e:
        logger.error(f"获取忽略列表失败: {str(e)}")
@@ -75,7 +75,7 @@
            return IgnoreListResponse(
                success=True,
                message=f"成功添加 {len(request.friends)} 个好友到忽略列表",
                count=count
                count=count,
            )
        else:
            raise HTTPException(status_code=400, detail="添加好友到忽略列表失败")
@@ -104,7 +104,7 @@
            return IgnoreListResponse(
                success=True,
                message=f"成功从忽略列表中移除好友: {request.w_id}",
                count=count
                count=count,
            )
        else:
            raise HTTPException(status_code=400, detail="从忽略列表移除好友失败")
@@ -126,11 +126,7 @@
        success = friend_ignore_service.clear_ignore_list()
        
        if success:
            return IgnoreListResponse(
                success=True,
                message="成功清空忽略列表",
                count=0
            )
            return IgnoreListResponse(success=True, message="成功清空忽略列表", count=0)
        else:
            raise HTTPException(status_code=400, detail="清空忽略列表失败")
            
@@ -156,9 +152,7 @@
        if success:
            count = friend_ignore_service.get_ignore_list_count()
            return IgnoreListResponse(
                success=True,
                message="联系人同步完成,忽略列表已重建",
                count=count
                success=True, message="联系人同步完成,忽略列表已重建", count=count
            )
        else:
            raise HTTPException(status_code=400, detail="联系人同步失败")
@@ -185,7 +179,7 @@
        return {
            "success": True,
            "data": status_info,
            "message": f"w_id {w_id} 状态检查完成"
            "message": f"w_id {w_id} 状态检查完成",
        }
    except Exception as e:
@@ -208,7 +202,7 @@
            "success": True,
            "data": whitelist,
            "count": len(whitelist),
            "message": "获取白名单成功"
            "message": "获取白名单成功",
        }
    except Exception as e:
@@ -231,9 +225,9 @@
                "ignore_enabled": settings.friend_ignore_enabled,
                "whitelist": settings.friend_ignore_whitelist,
                "whitelist_count": len(settings.friend_ignore_whitelist),
                "ignore_list_count": friend_ignore_service.get_ignore_list_count()
                "ignore_list_count": friend_ignore_service.get_ignore_list_count(),
            },
            "message": "获取配置信息成功"
            "message": "获取配置信息成功",
        }
    except Exception as e:
app/models/contact.py
@@ -1,6 +1,7 @@
"""
联系人信息模型
"""
from sqlalchemy import Column, String, Integer, DateTime, Text
from sqlalchemy.sql import func
from .database import Base
@@ -8,10 +9,13 @@
class Contact(Base):
    """联系人信息表"""
    __tablename__ = "contacts"
    
    id = Column(Integer, primary_key=True, index=True, autoincrement=True)
    wc_id = Column(String(100), unique=True, index=True, nullable=False, comment="微信ID/群ID")
    wc_id = Column(
        String(100), unique=True, index=True, nullable=False, comment="微信ID/群ID"
    )
    user_name = Column(String(100), nullable=True, comment="微信用户名")
    nick_name = Column(String(100), nullable=True, comment="昵称")
    remark = Column(String(100), nullable=True, comment="备注")
@@ -23,9 +27,11 @@
    small_head = Column(String(500), nullable=True, comment="小头像URL")
    label_list = Column(Text, nullable=True, comment="标签列表")
    v1 = Column(String(200), nullable=True, comment="v1数据")
    work_wc_id = Column(String(100), nullable=True, comment="企业微信id")
    created_at = Column(DateTime, default=func.now(), comment="创建时间")
    updated_at = Column(DateTime, default=func.now(), onupdate=func.now(), comment="更新时间")
    updated_at = Column(
        DateTime, default=func.now(), onupdate=func.now(), comment="更新时间"
    )
    
    def __repr__(self):
        return f"<Contact(wc_id='{self.wc_id}', nick_name='{self.nick_name}')>"
app/services/friend_ignore_service.py
@@ -30,9 +30,14 @@
        """
        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
@@ -111,17 +116,22 @@
            # 检查是否在白名单中(通过昵称)
            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}")
                    logger.info(
                        f"测试群组中的好友消息不被忽略: w_id={w_id}, group_id={group_id}"
                    )
                    return False
                
                logger.info(f"w_id在忽略列表中,忽略消息: w_id={w_id}")
@@ -233,7 +243,7 @@
                "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:
@@ -244,7 +254,9 @@
                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
@@ -256,11 +268,7 @@
        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}
# 全局好友忽略服务实例