xf
This commit is contained in:
33
app/utils.py
33
app/utils.py
@@ -307,27 +307,38 @@ def get_allowed_domains_with_time():
|
||||
|
||||
def get_all_emails(count=50):
|
||||
"""
|
||||
获取所有邮件,直接从 email:* 键获取
|
||||
获取所有邮件
|
||||
:param count: 返回的邮件数量限制
|
||||
:return: 邮件列表
|
||||
"""
|
||||
try:
|
||||
# 直接获取所有邮件键
|
||||
email_keys = redis_client.keys('email:*')
|
||||
emails = []
|
||||
# 1. 获取所有时间索引键,因为时间索引包含了所有邮件
|
||||
time_keys = redis_client.keys('time:*')
|
||||
time_keys.sort(reverse=True) # 按时间倒序排序
|
||||
time_keys = time_keys[:count] # 限制数量
|
||||
|
||||
# 获取每个邮件的数据
|
||||
for email_key in email_keys:
|
||||
emails = []
|
||||
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)}")
|
||||
|
||||
Reference in New Issue
Block a user