xf
This commit is contained in:
37
app/utils.py
37
app/utils.py
@@ -307,28 +307,39 @@ def get_allowed_domains_with_time():
|
|||||||
|
|
||||||
def get_all_emails(count=50):
|
def get_all_emails(count=50):
|
||||||
"""
|
"""
|
||||||
获取所有邮件,直接从 email:* 键获取
|
获取所有邮件
|
||||||
:param count: 返回的邮件数量限制
|
:param count: 返回的邮件数量限制
|
||||||
:return: 邮件列表
|
:return: 邮件列表
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
# 直接获取所有邮件键
|
# 1. 获取所有时间索引键,因为时间索引包含了所有邮件
|
||||||
email_keys = redis_client.keys('email:*')
|
time_keys = redis_client.keys('time:*')
|
||||||
|
time_keys.sort(reverse=True) # 按时间倒序排序
|
||||||
|
time_keys = time_keys[:count] # 限制数量
|
||||||
|
|
||||||
emails = []
|
emails = []
|
||||||
|
for time_key in time_keys:
|
||||||
# 获取每个邮件的数据
|
# 2. 从时间索引获取邮件键
|
||||||
for email_key in email_keys:
|
email_key = redis_client.get(time_key)
|
||||||
|
if not email_key:
|
||||||
|
continue
|
||||||
|
|
||||||
|
# 3. 获取邮件数据
|
||||||
email_data = redis_client.hgetall(email_key)
|
email_data = redis_client.hgetall(email_key)
|
||||||
if email_data:
|
if email_data:
|
||||||
# 转换字节数据为字符串
|
# 4. 转换字节数据为字符串
|
||||||
email = {k.decode(): v.decode() for k, v in email_data.items()}
|
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.append(email)
|
||||||
|
|
||||||
# 按时间戳排序
|
return emails
|
||||||
emails.sort(key=lambda x: x.get('timestamp', ''), reverse=True)
|
|
||||||
# 限制返回数量
|
|
||||||
return emails[:count]
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error getting all emails: {str(e)}")
|
logger.error(f"Error getting all emails: {str(e)}")
|
||||||
return []
|
return []
|
||||||
Reference in New Issue
Block a user