diff --git a/app/utils.py b/app/utils.py index 0fff1d8..9eefb24 100644 --- a/app/utils.py +++ b/app/utils.py @@ -307,28 +307,39 @@ def get_allowed_domains_with_time(): def get_all_emails(count=50): """ - 获取所有邮件,直接从 email:* 键获取 + 获取所有邮件 :param count: 返回的邮件数量限制 :return: 邮件列表 """ try: - # 直接获取所有邮件键 - email_keys = redis_client.keys('email:*') + # 1. 获取所有时间索引键,因为时间索引包含了所有邮件 + time_keys = redis_client.keys('time:*') + time_keys.sort(reverse=True) # 按时间倒序排序 + time_keys = time_keys[:count] # 限制数量 + emails = [] - - # 获取每个邮件的数据 - for email_key in email_keys: + for time_key in time_keys: + # 2. 从时间索引获取邮件键 + email_key = redis_client.get(time_key) + if not email_key: + continue + + # 3. 获取邮件数据 email_data = redis_client.hgetall(email_key) if email_data: - # 转换字节数据为字符串 + # 4. 转换字节数据为字符串 email = {k.decode(): v.decode() for k, v in email_data.items()} + # 5. 解析 JSON 字符串字段 + for field in ['recipients', 'attachments', 'headers', 'peer']: + if field in email: + try: + email[field] = json.loads(email[field]) + except: + pass emails.append(email) - - # 按时间戳排序 - emails.sort(key=lambda x: x.get('timestamp', ''), reverse=True) - # 限制返回数量 - return emails[:count] - + + return emails + except Exception as e: logger.error(f"Error getting all emails: {str(e)}") return [] \ No newline at end of file