thread fix

This commit is contained in:
debu-2 2026-10-06 04:42:47 +05:30
parent d58fb8841b
commit a58ba11566
11 changed files with 177 additions and 30 deletions

View file

@ -27,6 +27,7 @@ from app.services.mail_sender import deliver_email
from app.services.message_ids import canonical_message_id, message_id_fields
from app.services.mime_parser import parse_mime
from app.services.raw_mime_storage import get_raw_mime_storage
from app.services.reply_extractor import build_reply_display, reply_fields_for_message
from app.services.reply_recipients import reply_recipients
from app.services.thread_service import (
create_thread,
@ -98,7 +99,12 @@ def summarize(message: dict, entry: dict) -> MessageSummary:
sender=MailAddress(**sender),
recipients=[MailAddress(**item) for item in recipients],
subject=message.get("subject", ""),
snippet=message.get("snippet", message.get("body_text", "")[:240]),
snippet=message.get("snippet")
or re.sub(
r"\s+",
" ",
message.get("new_reply_content") or message.get("body_text", ""),
).strip()[:240],
status=message.get("status", "received"),
created_at=message.get("received_at") or message["created_at"],
is_read=entry.get("is_read", True),
@ -200,6 +206,10 @@ async def create_email(
}
if not subject.strip():
subject = reply_plan.subject
reply_display = build_reply_display(
body_text=payload.body_text,
body_html=payload.body_html,
)
document = {
"direction": "outbound",
"message_scope": MESSAGE_SCOPE_EXTERNAL,
@ -212,7 +222,10 @@ async def create_email(
"cc": [address(value) for value in cc],
"bcc": [address(value) for value in bcc],
"subject": subject,
"snippet": re.sub(r"\s+", " ", payload.body_text).strip()[:240],
"snippet": reply_display["snippet"],
"new_reply_content": reply_display["new_reply_content"],
"quoted_content": reply_display["quoted_content"],
"has_quoted_text": reply_display["has_quoted_text"],
"body_text": payload.body_text,
"body_html": payload.body_html,
"attachments": [],
@ -371,6 +384,11 @@ async def get_email(
body_text = parsed["body_text"]
body_html = parsed["body_html"]
summary = summarize(document, entry)
reply_fields = reply_fields_for_message(
document,
body_text=body_text,
body_html=body_html,
)
return MessageRead(
**summary.model_dump(),
cc=document.get("cc", []),
@ -378,6 +396,7 @@ async def get_email(
reply_to=document.get("reply_to", []),
body_text=body_text,
body_html=body_html,
**reply_fields,
message_id=document.get("message_id"),
in_reply_to=document.get("in_reply_to"),
references=document.get("references", []),

View file

@ -14,6 +14,7 @@ from app.routers.emails import decode_cursor, encode_cursor, summarize
from app.schemas import MessageRead, ThreadPage, ThreadRead, ThreadSummary
from app.services.mailbox_scope import apply_external_scope
from app.services.mime_parser import parse_mime
from app.services.reply_extractor import reply_fields_for_message
from app.services.raw_mime_storage import get_raw_mime_storage
router = APIRouter(prefix="/threads", tags=["threads"])
@ -241,6 +242,11 @@ async def message_read(
body_text = parsed["body_text"]
body_html = parsed["body_html"]
summary = summarize(message, entry)
reply_fields = reply_fields_for_message(
message,
body_text=body_text,
body_html=body_html,
)
return MessageRead(
**summary.model_dump(),
cc=message.get("cc", []),
@ -248,6 +254,7 @@ async def message_read(
reply_to=message.get("reply_to", []),
body_text=body_text,
body_html=body_html,
**reply_fields,
message_id=message.get("message_id"),
in_reply_to=message.get("in_reply_to"),
references=message.get("references", []),